3
\$\begingroup\$

I tried to make a collision box visible (for testing) with spriteBatch.Draw(with a blank texture) in my TileSprite class. I needed a GraphicsDevice for this so these are my variables:

Texture2D blankTexture;
GraphicsDevice GraphicsDevice;

I used this code as my LoadContent():

public void LoadContent()
{
    blankTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
    Color[] color = new Color[1];
    color[0] = Color.White;
    blankTexture.SetData(color);
} 

I have one TileSprite constructor in my TileSprite class which looks like this (NOTE: this constructor is partially taken from another abstract class, named Sprite):

public TileSprite(GraphicsDevice gd, Texture2D textureImage, Vector2 position, Point frameSize, Point currentFrame)
    : base (gd, textureImage, position, frameSize, currentFrame)
{
    GraphicsDevice = gd;
    bCollisionRect = new Rectangle((int)position.X, (int)position.Y, frameSize.X, frameSize.Y);
}

And then I use this code in my Draw method to draw the box:

spriteBatch.Draw(blankTexture, bCollisionRect, Color.White);

And I have another line of code in the Game1 : Game class (variables like textureImage are written as variables in the beginning of the Game1 class):

tileSprite = new TileSprite(this.GraphicsDevice, textureImage, position, frameSize, currentFrame);

I tested this and I saw that my GraphicsDevice was returning null on a new TileSprite that used a GraphicsDevice from DrawableGameComponent.

\$\endgroup\$
  • \$\begingroup\$ OK, are you saying that TileSprite is a DrawableGameComponent? Honestly, you shouldn't be using DrawableGameComponents, they are really poorly designed. \$\endgroup\$ – jgallant Nov 8 '15 at 14:50
  • \$\begingroup\$ What should I use then? I followed a couple of monogame tutorials and they made a sort of little project and I worked further on it. So all my methods in the SpriteManager : DrawableGameComponent class are from DrawableGameComponent \$\endgroup\$ – Jelle Nov 8 '15 at 14:59
  • \$\begingroup\$ DrawableGameComponent was supposed to be more fleshed out, by was pretty much abandoned by XNA after 1.0. Read more about it here: gamedev.stackexchange.com/questions/9204/… \$\endgroup\$ – jgallant Nov 8 '15 at 15:28
  • \$\begingroup\$ Thank you for the link! But do you know why GraphicsDevice is returning null? \$\endgroup\$ – Jelle Nov 8 '15 at 15:40
  • \$\begingroup\$ I would need to see more code, specifically how you are initializing the SpriteManager. I would personally seriously remove the DrawableGameComponent stuff. \$\endgroup\$ – jgallant Nov 8 '15 at 16:02
3
\$\begingroup\$

Typically, you would use the GraphicsDevice object that is created in your Game class. You should be able to create a second Graphics Device, however, I think there is a access issue when you attempt to create a second GraphicsDevice using the same adapter.

When you create a new XNA Framework game using XNA Game Studio Express, the game derives from Game and contains a member variable that is a GraphicsDeviceManager. This GraphicsDeviceManager automatically creates the best device that is available for the application and exposes it as the GraphicsDeviceManager.GraphicsDevice property.

So what you are going to want to do, is pass the Game.GraphicsDevice object to this class in order to use the same one that is already initialized.

As requested in comments, an example:

public class TileSprite
{
    GraphicsDevice GraphicsDevice;
    Texture2D BlankTexture;

    public TileSprite(GraphicsDevice gd)
    {
        GraphicsDevice = gd;
    }

    public void LoadContent()
    {       
        BlankTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
        Color[] color = new Color[1];
        color[0] = Color.White;
        BlankTexture.SetData(color);        
    } 
}

Then in your Game class:

TileSprite tileSprite = new TileSprite(this.GraphicsDevice);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks. You are really helpfull! But one question though. I'm new to programming and how do I pass the GraphicsDevice to the TileSprite class? \$\endgroup\$ – Jelle Nov 5 '15 at 21:20
  • \$\begingroup\$ You could add a variable to your TileSprite constructor of type GraphicsDevice, and then pass it along that way. \$\endgroup\$ – jgallant Nov 5 '15 at 21:23
  • \$\begingroup\$ Could you give me a little example? I already have a variable of type GraphicsDevice in my class constructor, but I'm still a little bit confused on how to pass it. \$\endgroup\$ – Jelle Nov 5 '15 at 21:36
  • \$\begingroup\$ @Jelle I added a small code example, as requested. \$\endgroup\$ – jgallant Nov 5 '15 at 22:05
  • \$\begingroup\$ I implemented your code and I can run my program. I tested if GraphicsDevice was null and it wasn't. But when I wanted to draw my boxes (my previous question), then I get a NullException (probably from my GraphicsDevice). I will update my question above. \$\endgroup\$ – Jelle Nov 5 '15 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.