简体   繁体   中英

Why do I have to declare GraphicsDeviceManager in Game1 class (XNA)?

First of all, I am new to C# and XNA (before C++)

I've searched half the web and, I believe, entire stackoverflow, but I didn't find the answer. In my project I wanted to create a GraphicsManager: its job will be to load/unload resources for open world game, provide more abstract rendering tools (load tileset, pick tile from tileset etc.) and so on.

That is why it felt natural to put all the graphic related objects in the GraphicsManager.

class GraphicsManager : Manager
{
    // provided by XNA project
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public override void Initialize(Game caller)
    {
        mainObj = caller;
        graphics = new GraphicsDeviceManager(mainObj);
    }
}

// constructor of the Game class
public Game()
{     
    Graphics.GraphicsManager.Instance.Initialize(this);
    Content.RootDirectory = "Content";
}

After some time I discovered that I need to create the GraphicsDeviceManager before Initialize(), because it will force it to call LoadContent() - I can understand that: the GraphicsDeviceManager's constructor performs some operations on the provided Game object to change its Initialize() method (or something similiar).

That is why I changed my code, so it runs GraphicManager's Initialize() in Game's constructor. Unfortunately this way it won't call LoadContent(). Why?

PS. It is enough just to declare any GraphicsDeviceManager object in the class, no need to instantiate - and then it will work. It makes no sense to me :/

@Scott W I've deleted the Game constructor, renamed the class (to Game1 ) and created this method:

protected override void Initialize()
    {
        // TODO: Add your initialization logic here
        Graphics.GraphicsManager.Instance.Initialize(this);

        Content.RootDirectory = "Content";

        base.Initialize();
    }

But now it won't call LoadContent . Any ideas?

Strictly speaking, you don't need to create a GraphicsDeviceManager object within a Game subclass. But, upon creation, it typically needs a Game instance (or subclass instance, eg Game1 ).

Usually you don't want to specify a constructor for your Game subclass. This is likely the cause of your problems relating to LoadContent not being called, etc. XNA's Game constructor does a few things that you're not replicating in yours, nor are you calling the base constructor. Instead of calling the base constructor, I recommend doing all that you need to do within Initialize or LoadContent ; that's what they're designed for.

So take out the constructor for your Game class (I also recommend calling it something other than Game as that's what XNA's class is also called), and move initialization into your Game subclass's Initialize :

protected override void Initialize()
{
    Graphics.GraphicsManager.Instance.Initialize(this); 
    base.Initialize();
}

This is the standard way to create GraphicsDeviceManager objects within Game subclasses. Do it in the Initialize override, and do not make a constructor.

Also, I'd recommend not making a singleton for this, and simply adding a GraphicsManager object directly in your Game subclass, a basic usage of composition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM