Subject: Chapter 2: width/height
Posted By: mcleod Post Date: 5/17/2007 3:35:41 PM
I know I must be overlooking it, but where do the width and height variables get declared in the Pong game?  Here's a sample of the code using it:

spriteBatch.Draw(sprite.texture,
                    new Rectangle(
                    sprite.rect.X * width / 1024,
                    sprite.rect.Y * height / 768,
                    sprite.rect.Width * width / 1024,
                    sprite.rect.Height * height / 768),
                    sprite.sourceRect, sprite.color);
Reply By: phsarjk Reply Date: 5/17/2007 4:09:28 PM
I would think it was in LoadGraphicsContent, outside the if statement, havent checked though. It would be the logical place to put it, as LoadGraphicsContent is called everytime the graphics context is invalidated, such as when the viewport is resized, minimized or lost somehow.

You could always replace it with graphics.GraphicsDevice.Viewport.Width

/Henning

Reply By: KoRnedbeaf Reply Date: 5/18/2007 5:18:58 AM
Just right click on the variable and select "go to defintion"(got the german version so dont know if its called exactly like that) and it will show you the declaration.

Reply By: mcleod Reply Date: 5/18/2007 4:29:57 PM
I must have something defined in the wrong place because it wouldn't compile due to not finding width/height.  I replaced it with graphics.GraphicsDevice.Viewport.Width like you said and that worked fine.  

Thanks for the reply!

quote:
Originally posted by phsarjk

I would think it was in LoadGraphicsContent, outside the if statement, havent checked though. It would be the logical place to put it, as LoadGraphicsContent is called everytime the graphics context is invalidated, such as when the viewport is resized, minimized or lost somehow.

You could always replace it with graphics.GraphicsDevice.Viewport.Width

/Henning





Reply By: phsarjk Reply Date: 5/18/2007 5:59:07 PM
You're welcome.

Just be warned that i haven't checked the performance implications of accessing the GraphicsDevice.Viewport several times each frame. There just might be a reason Benjamin meant it was necessary to keep them in seperate variables.

The solution in the book should have been:

int width,height;

protected override void LoadGraphicsContent(bool loadAllContent)
{
    if (loadAllContent)
    {
        
    }
    width = graphics.GraphicsDevice.Viewport.Width;
    height = graphics.GraphicsDevice.Viewport.Height;
}

However, I think Benjamin put the width and height in the Initialize method, which im a bit sceptical about.

/Henning


Reply By: abi.exdream.com Reply Date: 5/24/2007 9:55:00 PM
Yup, width/height are just cached because they are accessed many times, especially in later games of the book. And it is not only faster this way, but also a lot easier to find out about the width/height of the render area by just typing BaseGame.Width, etc. later.

The reason it is in the Initialize method is because there was no LoadGraphicsContent in beta1, but I tried to refactor the code last month and put more stuff from the Initialize method to LoadGraphicsContent.

In more complex games you could also allow resizing and make sure that with every resize event the width/height values get updated correctly.

http://abi.exdream.com

Go to topic 60933

Return to index page 1