Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #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
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:22 AM.


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