DrawableGameComponent - Need to create Texture on fly!

I have created and drawable component but need to recreate a texture (2d sprite) on the fly from a bitmap using Texture2D.FromFile. To do this i need to access the Graphics Device.

The graphics device (this.GraphicsDevice) exposed from the base class (DrawableGameComponent.GraphicsDevice) returns a null reference in both the Update and Draw (override) methods.

Help




Answer this question

DrawableGameComponent - Need to create Texture on fly!

  • Alfredo Di Patti

    Did you add your DrawableComponent to your game's component list

    Personally, I just make a public property in my game class (public GraphicsDevice GraphicsDevice{get{return graphics.GraphicsDevice;}}), which all my components have references to. Then, I just use game.GraphicsDevice to access the device.



  • MrZkitten

    Still not sure why its happening but found a way round it by creating the following helper function.

    private GraphicsDevice ParentGraphicsDevice
    {
    get
    {
    IGraphicsDeviceService graphicsservice = (IGraphicsDeviceService)Game.Services.GetService(typeof(IGraphicsDeviceService));
    return graphicsservice.GraphicsDevice;
    }
    }

    Would prefer to use base property but ...

  • DrawableGameComponent - Need to create Texture on fly!