Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6
This is the forum to discuss the Wrox book Professional XNA Game Programming: For Xbox 360 and Windows by Benjamin Nitschke; ISBN: 9780470126776
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 17th, 2007, 03:35 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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);
  #2 (permalink)  
Old May 17th, 2007, 04:09 PM
Registered User
 
Join Date: May 2007
Location: , , Denmark.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

  #3 (permalink)  
Old May 18th, 2007, 05:18 AM
Registered User
 
Join Date: May 2007
Location: , , Germany.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

  #4 (permalink)  
Old May 18th, 2007, 04:29 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

  #5 (permalink)  
Old May 18th, 2007, 05:59 PM
Registered User
 
Join Date: May 2007
Location: , , Denmark.
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


  #6 (permalink)  
Old May 24th, 2007, 09:55 PM
Wrox Author
 
Join Date: May 2007
Location: Hannover, , Germany.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to abi.exdream.com
Default

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get image height and width Dj Kat Javascript How-To 10 June 4th, 2009 01:49 PM
get the height and width of an image with js crmpicco Javascript How-To 1 August 30th, 2006 11:26 PM
how can I get width/height of a picture? keyvanjan Classic ASP Basics 2 May 29th, 2006 07:29 AM
Height and Width? venkatesh_anmca Beginning VB 6 0 January 31st, 2005 05:13 AM
datalist and images(get width and height) collie VB.NET 2002/2003 Basics 1 August 4th, 2004 08:24 AM





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