MonoGame: Cannot load assets - c#

I started days ago programming c# using monogame.
Today I got an error that says "Could not load asset as a non-content file".
Here's the code, I need much help.
List textures;
public Game1()
: base()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// the below line errors
textures.Add(Content.Load<Texture2D>("Lol"));
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}

SOLVED
Create a sub-folder under the Content one.
Call it "Assets"
Right click on the image file and right click
Properties -> Copy to Output -> Copy always
Go to the main class and set the RootDirectory as "Content.RootDirectory = #"Content\Assets";"
P.s. : Everytime you add a file, repeat 3rd and 4th step

Related

C# OpenGL Window is Lagging

I'm having a problem: When I create a window in OpenGL, using C#, my window starts to lag, and I cannot use the window (I cannot close, minimize, resize, etc.). And when I hover my mouse inside the window, my cursor becomes that blue wheel, meaning it's lagging. How can I fix it?
It's worth saying that I'm using OpenGL and OpenTK to display and use the window however I want.
This is my code for generating the window:
First, this is the “GameLoop” class, everything related to the game itself is derived from this class, it contains some abstract classes that I can override, so it's really useful.
public abstract class GameLoop
{
public GameWindow window;
public Vector2Int windowSize = new Vector2Int(1280, 768);
public void Run()
{
OnInitialize();
WindowManager.CreateWindow(windowSize.x, windowSize.y, "Engine", out window);
OnLoad();
while (WindowManager.IsRunning)
{
OnUpdate();
OnRender();
}
OnCloseWindow();
}
protected abstract void OnInitialize();
protected abstract void OnLoad();
protected abstract void OnUpdate();
protected abstract void OnRender();
protected abstract void OnCloseWindow();
}
class RunGameLoop
{
static void Main()
{
GameLoop loop = new EngineWindow();
loop.Run();
}
}
Secondly, I have this method in another class that is responsible for creating the window itself:
public static void CreateWindow(int width, int height, string title, out GameWindow window)
{
window = new GameWindow(GameWindowSettings.Default, NativeWindowSettings.Default);
Vector2Int res = new Vector2Int(width, height);
//windowSize = res;
IsRunning = true;
window.CenterWindow(Vector2Int.ToVec2i(res));
window.Title = title;
}
And finally, I have the main class, where I can use OpenGL and make my project:
public class EngineWindow : GameLoop
{
protected override void OnInitialize()
{
Debug.WriteLine("On Initialize");
}
protected override void OnLoad()
{
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.Lighting);
GL.ClearColor(Color.ToColor4(BackColor));
GL.Viewport(0, 0, windowSize.x, windowSize.y);
CreateMesh(cubeMesh);
}
protected override void OnUpdate()
{
}
protected override void OnRender()
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
angle += Time.deltaTime;
SetupViewport(sceneCamera);
RenderMesh(cubeMesh);
ControllCamera(sceneCamera);
GL.CullFace(CullFaceMode.Back);
GL.Finish();
window.SwapBuffers();
}
protected override void OnCloseWindow()
{
Debug.WriteLine("Ended");
// Clean up
GL.DeleteProgram(program);
GL.DeleteBuffers(1, ref vbo);
GL.DeleteVertexArrays(1, ref vao);
}
}
But, the issue is that when the window is created, it starts up fine, but becomes extremely laggy and I can't use the window.
What might be the issue, and how can I fix it?

How can I use a wrapper in my Visual Studio project?

I want to use this wrapper in MonoGame but I get an error message if I use the code from the wrapper website.
Wrapper StatsIO
I downloaded the wrapper project and added the project to my solution in Visual Studio. After that, I created a reference from my project to the wrapper project.
Then, I copied this code to my project:
var client = new StatsIOClient("Client-ID", "Client-Secret");
await client.Statistics.CreateAsync("Stats-Id", "Username");
But something went wrong, because I get these error message:
Error CS1501: No overload for method 'CreateAsync' takes 2 arguments
(CS1501)
What is wrong with the code? How can I use the wrapper project in my Visual Studio project to send and receive leaderboard scores?
Dropbox link to my Visual Studio project: my project
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StatsIO.Objects;
using StatsIO;
namespace LeaderboardTest
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public async void NewClient()
{
var client = new StatsIOClient("Client-ID", "Client-Secret");
await client.Statistics.CreateAsync("Stats-Id", "Username");
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
NewClient();
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}

Xamarin Studio: System.InvalidOperationException

I created a new MonoGame iOS solution in Xamarin Studio. After that, I added to following code to Game1.cs:
bool IsiOS = false;
if (Device.RuntimePlatform == Device.iOS)
IsiOS = true;
But I get an error message when I build the project.
if (Device.RuntimePlatform == Device.iOS)
System.InvalidOperationException You MUST call Xamarin.Forms.Init();
prior to using it.
What is wrong with my code? I don't know what I should add or change.
code(Game1.cs):
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Xamarin.Forms;
namespace NewTest.iOS
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
bool IsiOS = false;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
if (Device.RuntimePlatform == Device.iOS)
IsiOS = true;
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color.CornflowerBlue);
base.Draw(gameTime);
}
}
}
code(Program.cs):
using System;
using Foundation;
using UIKit;
namespace NewTest.iOS
{
[Register("AppDelegate")]
class Program : UIApplicationDelegate
{
private static Game1 game;
internal static void RunGame()
{
game = new Game1();
game.Run();
}
static void Main(string[] args)
{
UIApplication.Main(args, null, "AppDelegate");
}
public override void FinishedLaunching(UIApplication app)
{
RunGame();
}
}
}
The Device class is part of Xamarin Forms. If you are using Xamarin Forms, then you must initialize it first. That is exactly what the error message is telling you.

Xamarin Android possible ScreenOrientation.Landscape bug

my app had some strange behaviour so I recreated the app until the point where it bugged and found out that ScreenOrientation.Landscape is the culprit.
If you make a new blank app in visual studio 15 and replace the MainActivity with:
[Activity(Label = "TestLandscapeBug", MainLauncher = true, Icon = "#drawable/icon",
ScreenOrientation = ScreenOrientation.Landscape)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
System.Console.WriteLine("OnCreate");
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
}
protected override void OnDestroy()
{
System.Console.WriteLine("OnDestroy");
base.OnDestroy();
}
protected override void OnPause()
{
System.Console.WriteLine("OnPause");
base.OnPause();
}
protected override void OnRestart()
{
System.Console.WriteLine("OnRestart");
base.OnRestart();
}
protected override void OnResume()
{
System.Console.WriteLine("OnResume");
base.OnResume();
}
protected override void OnStart()
{
System.Console.WriteLine("OnStart");
base.OnStart();
}
protected override void OnStop()
{
System.Console.WriteLine("OnStop");
base.OnStop();
}
}
run the app and press the sleep button:
OnPause, OnStop, OnDestroy, OnCreate, OnStart, OnResume and OnPause are called.
if you remove ScreenOrientation = ScreenOrientation.Landscape OnPause and OnStop are called.
Is this a bug? Or am I doing something wrong?
how can I fix this or use something else which locks the screen in landscape.
It's normal for an activity to switch to portrait mode when the screen is locked. Whenever the orientation changes, OnDestroy is called followed by OnCreate. So, there's nothing to worry about as what you're witnessing is default behavior of Android.
Portrait is kind of the default orientation for the lockscreen so it makes sense that your activity also switches to that when locking.

How could I know if all my modules are loaded in prism 4?

I have a WPF Desktop application using PRISM, there are 12 modules which do not depend on each other , every time i start the shell, modules are been loaded , the point is that I would like to know which module loads at the last so I could start an action. How could I determine this ?
Override Bootstrapper.InitializeModules, call base, and then ACTION!
Expanding on erikH's answer (thank you, btw), assuming that you are deriving from the default UnityBootstrapper, here is the order in which the typically overridden methods are called:
//0
public override void Run(bool runWithDefaultConfiguration)
{
base.Run(runWithDefaultConfiguration);
//this is our last opportunity to hook into the PRISM bootstrapping sequence; at this point every very other base-overridden
//method has been executed
}
//1
protected override void ConfigureModuleCatalog()
{
base.ConfigureModuleCatalog();
ModuleCatalog moduleCatalog = (ModuleCatalog)this.ModuleCatalog;
//add modules...
}
//2
protected override void ConfigureContainer()
{
base.ConfigureContainer();
//register everything with the container...
}
//3
protected override DependencyObject CreateShell()
{
return Container.Resolve<ShellView>(); //resolve your root component
}
//4
protected override void InitializeShell()
{
base.InitializeShell();
App.Current.MainWindow = (Window)Shell;
App.Current.MainWindow.Show();
}
//5
protected override void InitializeModules()
{
base.InitializeModules();
}
Note that this applies to PRISM 4 and 5

Categories

Resources