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 June 27th, 2007, 04:36 PM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Noob Needs Help

....again! I hate comming here for help,I hope you guys don't mind. I'm not helpless,I just need a little help lol.

I have coded all the way to the bottom of page 42. I am writing the pong game. The game compiles but I only see the background (I am supposed to see the paddles,ball and the score)

The same thing happened when I tried to render the menu graphics,I only saw the background!

At first I thought I might have named something wrong (lol,probably too easy to be true) but everything seems ok! So here is all the code I have for the PongGame.cs. It isn't well organized,because I was told that I would be refactoring the code when I was all done.

Code:
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace MichaelartsPong
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class PongGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;

        Texture2D backgroundTexture, menuTexture, gameTexture;
        int width, height;
        int leftPlayerLives = 3,
          rightPlayerLives = 3;

        SpriteBatch spriteBatch = null;

        public PongGame()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // Remember resolution
            width = graphics.GraphicsDevice.Viewport.Width;
            height = graphics.GraphicsDevice.Viewport.Height;

            base.Initialize();
        }

        public static void TestMenuSprites()
        {
            StartTest(
                delegate
                {
                    testGame.RenderSprite(testGame.menuTexture,
                        512-XnaPongLogoRect.Width/2, 150,
                        XnaPongLogoRect);
                    testGame.RenderSprite(testGame.menuTexture,
                        512-MenuSingleplayerRect.Width/2, 300,
                        MenuSingleplayerRect);
                    testGame.RenderSprite(testGame.menuTexture,
                        512-MenuMultiplayerRect.Width/2, 350,
                       MenuMultiplayerRect , Color.Orange);
                    testGame.RenderSprite(testGame.menuTexture,
                        512-MenuExitRect.Width/2, 400,
                        MenuExitRect);
                });
        } //Testmenu Sprites :O

        delegate void TestDelegate();
          class TestPongGame : PongGame  
        {
            TestDelegate testLoop;
            public TestPongGame(TestDelegate setTestLoop)
            {
                testLoop = setTestLoop;
            } //TestPongGame(setTestLoop)

            protected override void Draw(GameTime gameTime)
            {
                base.Draw(gameTime);
                testLoop();
            } //Draw (gametime)

        } //Class Ponggame. 

        static TestPongGame testGame;
        static void StartTest(TestDelegate testLoop)
        {
            using (testGame = new TestPongGame(testLoop))
            {
                testGame.Run();
            } //using
        } //StartTest (testLoop)

        public void RenderSprite(Texture2D texture, int x, int y,
            Rectangle sourceRect, Color color)
        {
            //TODO
        }   //RenderSprite(texture, rect, sourceRect)
            public void RenderSprite(Texture2D texture, int x, int y,
            Rectangle sourceRect)
        {
            //TODO
            //RenderSprite(texture, rect, sourceRect)
        }

        public static void StartGame()
        {
            using (PongGame game = new PongGame())
            {
                game.run();
            } //using
        }//StartGame()

        private void run()
        {
            throw new Exception("The method or operation is not implemented.");
        } //StartGame()
        class SpriteToRender
        {
            public Texture2D texture;
            public Rectangle rect;
            public Rectangle? sourceRect;
            public Color color;

            public SpriteToRender(Texture2D setTexture, Rectangle setRect,
                Rectangle? setSourceRect, Color setColor)
            {
                texture = setTexture;
                rect = setRect;
                sourceRect = setSourceRect;
                color = setColor;
            } // SpriteToRender(setTexture, setRect, setColor)
        } // SpriteToRender

        List<SpriteToRender> sprites = new List<SpriteToRender>();

        public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect,
            Color color)
        {
            sprites.Add(new SpriteToRender(texture, rect, sourceRect, color));
        } // RenderSprite(texture, rect, sourceRect, color)

        public void DrawSprites()
        {
            // No need to render if we got no sprites this frame
            if (sprites.Count == 0)
                return;

            // Start rendering sprites
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
                SpriteSortMode.BackToFront, SaveStateMode.None);

            // Render all sprites
            foreach (SpriteToRender sprite in sprites)
                spriteBatch.Draw(sprite.texture,
                    // Rescale to fit resolution
                    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);

            // We are done, draw everything on screen with help of the end method.
            spriteBatch.End();

            // Kill list of remembered sprites
            sprites.Clear();
        } // DrawSprites()

        public static void TestGameSprites()
        {
            StartTest(
                delegate
                {
                    //Show lives
                    testGame.ShowLives();
                    //Ball in center
                    testGame.RenderBall();
                    //render both paddles
                    testGame.RenderPaddles();
                });
        } //TestGameSprites()

        public void ShowLives()
        {
            // Support for both Windows monitors and TV screens on the Xbox 360!
            int xPos = 2;
            int yPos = 2;

            // Left players lives
            RenderSprite(menuTexture, xPos, yPos, GameLivesRect);
            for (int num = 0; num < leftPlayerLives; num++)
                RenderSprite(gameTexture,
                    xPos + GameLivesRect.Width + GameSmallBallRect.Width * num - 2,
                    yPos + 7,
                    GameSmallBallRect);

            // Right players lives
            int rightX = 1024-GameLivesRect.Width-GameSmallBallRect.Width*3-xPos-2;
            RenderSprite(menuTexture, rightX, yPos, GameLivesRect);
            for (int num = 0; num < rightPlayerLives; num++)
                RenderSprite(gameTexture,
                    rightX + GameLivesRect.Width + GameSmallBallRect.Width * num - 2,
                    yPos + 7,
                    GameSmallBallRect);
        } // ShowLives()


         public void RenderBall()
         {
             RenderSprite(gameTexture,(int)((0.05f+0.9f*ballPosition.X)*1024)-
                 GameBallRect.Width/2,
                 (int)((0.02f+0.96f*ballPosition.Y)*768)-
                 GameBallRect.Height/2,
                 GameBallRect);
         }//RenderBall()

        public void RenderPaddles()
        {
            RenderSprite(gameTexture,
            (int)(0305f*1024)-GameRedPaddleRect.Width/2,
            (int)((0.06f+0.88f*leftPaddlePosition)*768)-
            GameRedPaddleRect.Height/2,
            GameRedPaddleRect);
            RenderSprite(gameTexture,
                (int)(0.95f*1024)-GameBluePaddleRect.Width/2,
                (int)((0.06f+0.88f*rightPaddlePosition)*768)-
                GameBluePaddleRect.Height/2,
                GameBluePaddleRect);
        } //RenderPaddle(leftPaddle)


        ///<summary>
        ///Current paddle positions, 0 means top 1 means bottom
        ///</summary>
        ///
        float leftPaddlePosition = 0.5f,
            rightPaddlePosition = 0.5f;
        ///<summary>
        ///Current ball position,again from 0 to 1,0 is left and top,
        ///1 is bottom and right.
        ///</summary>
        ///
        Vector2 ballPosition = new Vector2(0.5f, 0.5f);
        ///<Summary>
        /// Ball speed vector, randomized for every new ball.
        /// Will be set to Zero if we are in menu or game is over.
        ///</Summary>

        Vector2 ballSpeedVector = new Vector2(0, 0);

        /// <summary>
        /// Load your graphics content.  If loadAllContent is true, you should
        /// load content from both ResourceManagementMode pools.  Otherwise, just
        /// load ResourceManagementMode.Manual content.
        /// </summary>
        /// <param name="loadAllContent">Which type of content to load.</param>
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                // Create sprite batch
                spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

                // Load all our content
                backgroundTexture = content.Load<Texture2D>("SpaceBackground");
                menuTexture = content.Load<Texture2D>("PongMenu");
                gameTexture = content.Load<Texture2D>("PongGame");
            } // if
            base.LoadGraphicsContent(loadAllContent);
        } // LoadGraphicsContent(loadAllContent)

        /// <summary>
        /// Unload graphic content if the device gets lost.
        /// </summary>
        /// <param name="unloadAllContent">Unload everything?</param>


        /// <summary>
        /// Unload your graphics content.  If unloadAllContent is true, you should
        /// unload content from both ResourceManagementMode pools.  Otherwise, just
        /// unload ResourceManagementMode.Manual content.  Manual content will get
        /// Disposed by the GraphicsDevice during a Reset.
        /// </summary>
        /// <param name="unloadAllContent">Which type of content to unload.</param>
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent == true)
            {
                content.Unload();
            }
        }


        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the default game to exit on Xbox 360 and Windows
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        static readonly Rectangle
            XnaPongLogoRect = new Rectangle(0, 0, 512, 110),
            MenuSingleplayerRect = new Rectangle(0, 110, 512, 38),
            MenuMultiplayerRect = new Rectangle(0, 148, 512, 38),
            MenuExitRect = new Rectangle(0, 185, 512, 38),
            GameLivesRect = new Rectangle(0, 222, 100, 34),
            GameRedWonRect = new Rectangle(151, 222, 155, 34),
            GameBlueWonRect = new Rectangle(338, 222, 165, 34),
            GameRedPaddleRect = new Rectangle(23, 0, 22, 92),
            GameBluePaddleRect = new Rectangle(0, 0, 22, 92),
            GameBallRect = new Rectangle(1, 94, 33, 33),
            GameSmallBallRect = new Rectangle(37, 108, 19, 19);


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            //Draw background texture in a separate pass, else it gets messed up with out other sprites...

            spriteBatch.Begin();
            spriteBatch.Draw(backgroundTexture,
                new Rectangle(0, 0, width, height),
                Color.LightGray);
            spriteBatch.End(); 

        }
    }
}
  #2 (permalink)  
Old June 27th, 2007, 07:55 PM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Location: La Vergne, TN, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jnbutler
Default

You need to step through it in the debugger and then you will see why those sprites are not getting loaded. That is easier than trying to look through all that code.

Oh alright! I'll just tell you what is not happening.
I ran your code through the debugger and when it calls the overloaded RenderSprite() function to draw the menus on the screen, there is no code in that one. You have to implement all of the RenderSprite methods. Here is what you should have. Just copy and past these into your code right after the StartTest method:


Code:
public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect,Color color)
{
    sprites.Add(new SpriteToRender(texture, rect, sourceRect,color));
} 

public void RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect)
{
    RenderSprite(texture, rect, sourceRect, Color.White);
} 

public void RenderSprite(Texture2D texture, int x, int y, Rectangle? sourceRect,Color color)
{
    RenderSprite(texture,new Rectangle(x, y,
                 sourceRect.Value.Width,  sourceRect.Value.Height),
         sourceRect, color);
} 

public void RenderSprite(Texture2D texture, int x, int y, Rectangle? sourceRect)
{
    RenderSprite(texture,new Rectangle(x, y,
                 sourceRect.Value.Width, sourceRect.Value.Height),
         sourceRect, Color.White);
} 

public void RenderSprite(Texture2D texture, Rectangle rect, Color color)
{
    RenderSprite(texture, rect, null, color);
} 

public void RenderSprite(Texture2D texture, Rectangle rect)
{
    RenderSprite(texture, rect, null, Color.White);
} 

public void RenderSprite(Texture2D texture)
{
    RenderSprite(texture, new Rectangle(0, 0, 1024, 768), null,
                 Color.White);
}
You have to have all of these in your PongGame.cs file.

Plus! There are some other pieces you are missing as well. Look at the downloaded example code for chapter 2 and you'll see what I'm talking about. Also run the example code through the debugger, it helps with understanding what is going on in the code.

Good Luck!
jnbutler
  #3 (permalink)  
Old June 28th, 2007, 07:05 AM
Registered User
 
Join Date: Jun 2007
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Aha! Thank you soooo much. I suppose my problem is that I'm looking through the book to find what I'm missing,when I should look at the code I downloaded!

I added that code and I still don't see anything,so I'll go look for whatelse I am missing... Thanks again!

  #4 (permalink)  
Old June 28th, 2007, 04:53 PM
Authorized User
Points: 40, Level: 1
Points: 40, Level: 1 Points: 40, Level: 1 Points: 40, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2006
Location: La Vergne, TN, USA.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jnbutler
Default

That is one of the things I do not like about this book is that you cannot follow it and type in all the code since he doesn't give you all of it in the book. You have to refer to the downloaded code exclusively. You'll figure it out though.

jnbutler
 


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
Perplexed noob mona_in ASP.NET 1.0 and 1.1 Basics 1 July 21st, 2008 09:19 AM
Help a noob wilbo BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 April 22nd, 2008 11:54 AM
noob question mattl Java Basics 1 August 27th, 2007 11:28 PM
Noob needs help. dangel75 BOOK: Beginning ASP.NET 1.0 3 March 19th, 2006 06:38 PM
Noob Question jverdin BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 April 2nd, 2004 10:01 AM



All times are GMT -4. The time now is 01:39 PM.


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