Creating renderer inside a GameScreen class

Topics: User Forum
Apr 7, 2011 at 5:11 PM

I hope Im doing this right, first time.  But I searched all over because I thought this problem would have been encountered before.  But I am having an issue with creating the renderer because I have no reference to the GraphicsDeviceManager, at least not that I know of.  Basically the idea is that my class doesnt inherit from Game and instead inherits from GameScreen, its a pretty useful tool for changing game screens that I was assuming many people use, can be found on XNA's tutorial area.

My problem lies here:

myRenderer = new SpriteBatchRenderer            

{              

   GraphicsDeviceService = ?            

};

I have a SpriteBatch reference is that matters...

I guess I am just hoping someone else uses or has used the GameScreen class and would have some information on this.  I apologize if this is a stupid question.

Developer
Apr 7, 2011 at 5:21 PM

There's nothing built in if I remember correctly.

I believe that the static ScreenManager class has a GraphicsDevice property on it - but not a GraphicsDeviceService.

I would look at how that is implemented (the Game must set it somewhere) and then add a GraphicsDeviceService to the Screen manager implemented the same way. Then your GameScreens can simply use that reference.

Apr 7, 2011 at 6:49 PM

Well here is what I did, that doesnt work =P

 

I added 

GraphicsDeviceManager graphics; to the ScreenManager class

In the main class (Game1.cs) I changed screenManager = new ScreenManager(this); to screenManager = new ScreenManager(this, graphics);

 

Inside my game screen (class that inherits from GameScreen) I now have...

Inside LoadContent:

Content = new ContentManager(ScreenManager.Game.Services, "Content");
myRenderer = new SpriteBatchRenderer { GraphicsDeviceService = ScreenManager.graphics };

 

I did it inside LoadContent because inside my constructor ScreenManager is null(not sure why, still looking into it)

IF this should work... Then I am doing something else wrong, as this is my first project using these effects.  I'll try starting a new project

Coordinator
Apr 7, 2011 at 8:32 PM

Hi Atomos,

The key to this is in ScreenManager.Game.Services - this service container can be used to get the IGraphicsDeviceService instance...

myRenderer = new SpriteBatchRenderer            
{              
   GraphicsDeviceService = (IGraphicsDeviceService)ScreenManager.Game.Services.GetService(typeof(IGraphicsDeviceService))
};

I've written that off the top of my head but I think that's more or less right :)

Cheers!
Matt

Apr 7, 2011 at 8:47 PM

Yep, that'll fix it.  Another stupid thing I had done to myself... Is that I was rendering it within the spriteBatch.  Took it out of the begin/end there and now it works.  Thanks for the help.

Feb 21, 2012 at 1:44 PM
Edited Feb 22, 2012 at 4:25 AM

What world/view/projection/camera matrix with ScreenManager 2D? With the following matrix I wasn't able to see the particle effect :(

Matrix worldMatrix = Matrix.Identity;
Matrix viewMatrix = Matrix.Identity;                
Matrix projectionMatrix = Matrix.Identity;
Vector3 cameraPosition = Vector3.Zero;
spriteBatchRenderer.RenderEffect(particleEffectBasicExplosion, ref worldMatrix, ref viewMatrix, ref projectionMatrix, ref cameraPosition);

Aug 15, 2012 at 10:04 AM

Sorry to resurrect an old thread, but I've recently come across the same issue and it took me a while to resolve.

I have a pong game, and I'm trying to insert Mercury into my ball class, which does not inherit from game. I also needed access to GraphicsDevice, so to get around this I created an instance of my Game1 class, and from there I was able to access

// Creates an instance of the Game1 class		
Game1 gameInstance;		


Constructor()
{

.........
			// Merury particle effects
			spriteBatch = new SpriteBatch(gameInstance.GraphicsDevice);
			myRenderer = new SpriteBatchRenderer		// Create new renderer and set its graphics devide to "this" device
			{
				GraphicsDeviceService = (IGraphicsDeviceService)gameInstance.Services.GetService(typeof(IGraphicsDeviceService))
			};
			magicTrail = new ParticleEffect();			// The actual particle effect			

............
}