p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 (http://p2p.wrox.com/forumdisplay.php?f=325)
-   -   Chapter 2: width/height (http://p2p.wrox.com/showthread.php?t=58017)

mcleod May 17th, 2007 03:35 PM

Chapter 2: width/height
 
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);

phsarjk May 17th, 2007 04:09 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


KoRnedbeaf May 18th, 2007 05:18 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.


mcleod May 18th, 2007 04:29 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:

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



phsarjk May 18th, 2007 05:59 PM

You're welcome. :D

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:
Code:

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



abi.exdream.com May 24th, 2007 09:55 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


All times are GMT -4. The time now is 05:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.