Wrox Programmer Forums
|
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
 
Old May 12th, 2007, 11:21 AM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 1 - Scrolling Algorithm

On page 23, the code is written for the Scrolling Algorithm to make the program function correctly. I'll admit, I'm not the sharpest one when it comes to math. Could someone explain in pseudocode how this thing works?

Much appreciated.

for (int x = 0; x <= resolutionWidth / backgroundTexture.Width; x++)
  for (int y = -1; y <= resolutionHeight / backgroundTexture.Height +
       1; y++)
    {
    Vector2 position = new Vector2(x * backgroundTexture.Width,
    y * backgroundTexture.Height +(int)scrollPosition) %
        backgroundTexture.Height);

        sprites.Draw(backgroundTexture, position, Color.White);
    }
 
Old May 12th, 2007, 05:33 PM
Wrox Author
 
Join Date: May 2007
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to abi.exdream.com
Default

Well, I thought this would really be easy, but I guess even in Chapter 1 I already assume too much ^^ Most criticism about the book was jumping to conclusions too quickly without explaining all the steps in greater detail, but I guess then not all the games would even fit into the book.

Ok, here we go:

The 2 for loops go through every single tile that is visible on the screen. Thanks to our resolution (1024x768) we can fit the CityGroundSmall.dds textures 4 times horizontally because it is 256x256 pixel in size (1024/256 = 4). Vertically it would fit 3 times (768/256 = 3), but since we allow scrolling up and down, we also render one above it and one below it to allow scrolling in both directions.
In case the resolution is different (I guess the standard is now 800x600) I also added one extra tile in both direction with the <= command, which will add another tile in case the resolution is not a multiple of 256.

This means we go through a grid of 5x6 tiles, x goes from 0 to 4, y goes from -1 to +4. The scrollPosition is helper value to scroll up and down, which gets modulated (the % command) by the height to make sure we never run out of the visible area.

To understand the code better, try to just render 2x2 tiles (use for (int x=0; x<2; x++) etc.) and see what happens when you scroll around, then increase the number of tiles until it looks correctly. That is exactly what I did when I wrote this code :)

http://abi.exdream.com
 
Old May 13th, 2007, 03:51 PM
Registered User
 
Join Date: May 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Wow, thanks! It completely makes sense now. This is really cool. I also altered the program a little here so that you can scroll in any direction. I'm enjoying the book so far, thanks again!






Similar Threads
Thread Thread Starter Forum Replies Last Post
apriori algorithm iralala J2EE 4 January 1st, 2014 06:49 PM
Can someone help me with this algorithm please! th kishan4910 VB How-To 18 February 5th, 2008 07:15 AM
algorithm angelboy Other Programming Languages 0 April 14th, 2007 08:56 AM
Algorithm kvanchi General .NET 3 January 20th, 2005 05:28 AM





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