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

June 5th, 2007, 02:16 PM
|
|
Registered User
|
|
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Menu Help ch.2
Im having trouble getting the menu to show up in chapter 2, i have looked at all the forums on this site and there solutions didn't work for my problem, im sure im just missing a simple rendersprite somewhere but i can't find it, i would appreciate any help.
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 WindowsGame2
{
public class PongGame : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
ContentManager content;
Texture2D backgroundTexture, menuTexture, gameTexture;
SpriteBatch spriteBatch;
int width, height;
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);
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
public PongGame()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
}
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
protected override void Initialize()
{
width = graphics.GraphicsDevice.Viewport.Width;
height = graphics.GraphicsDevice.Viewport.Height;
// TODO: Add your initialization logic here
base.Initialize();
}
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
protected override void LoadGraphicsContent(bool loadAllContent)
{
if (loadAllContent)
{
// TODO: Load any ResourceManagementMode.Automatic content
spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
backgroundTexture = content.Load<Texture2D>("SpaceBackground");
menuTexture = content.Load<Texture2D>("PongMenu");
gameTexture = content.Load<Texture2D>("PongGame");
}
// TODO: Load any ResourceManagementMode.Manual content
}
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
protected override void UnloadGraphicsContent(bool unloadAllContent)
{
if (unloadAllContent)
{
// TODO: Unload any ResourceManagementMode.Automatic content
content.Unload();
}
// TODO: Unload any ResourceManagementMode.Manual content
}
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, width, height), Color.White);
spriteBatch.End();
// TODO: Add your drawing code here
// Draw everything on screen.
DrawSprites();
base.Draw(gameTime);
}
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
public static void StartGame()
{
using (PongGame game = new PongGame())
{
game.Run();
} // using
} // StartGame()
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
public static void TestMenuSprites()
{
StartTest(
delegate
{
testGame.RenderSprite(testGame.menuTexture,
new Rectangle(512 - XnaPongLogoRect.Width / 2, 150,
XnaPongLogoRect.Width, XnaPongLogoRect.Height),
XnaPongLogoRect);
testGame.RenderSprite(testGame.menuTexture,
new Rectangle(512 - MenuSingleplayerRect.Width / 2, 300,
MenuSingleplayerRect.Width, MenuSingleplayerRect.Height),
MenuSingleplayerRect);
testGame.RenderSprite(testGame.menuTexture,
new Rectangle(512 - MenuMultiplayerRect.Width / 2, 350,
MenuMultiplayerRect.Width, MenuMultiplayerRect.Height),
MenuMultiplayerRect, Color.Orange);
testGame.RenderSprite(testGame.menuTexture,
new Rectangle(512 - MenuExitRect.Width / 2, 400,
MenuExitRect.Width, MenuExitRect.Height),
MenuExitRect);
});
} // TestMenuSprites()
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
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 TestPongGame
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
static TestPongGame testGame;
static void StartTest(TestDelegate testLoop)
{
testGame = new TestPongGame(testLoop);
testGame.Run();
testGame.Dispose();
} // RunTest(testLoop)
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
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
}//SpriteToRender class
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 RenderSprite(Texture2D texture, Rectangle rect, Rectangle? sourceRect)
{
RenderSprite(texture, rect, sourceRect, Color.White);
} // RenderSprite(texture, rect, sourceRect)
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);
} // RenderSprite(texture, rect, sourceRect)
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);
} // RenderSprite(texture, rect, sourceRect)
public void RenderSprite(Texture2D texture, Rectangle rect, Color color)
{
RenderSprite(texture, rect, null, color);
} // RenderSprite(texture, rect, color)
public void RenderSprite(Texture2D texture, Rectangle rect)
{
RenderSprite(texture, rect, null, Color.White);
} // RenderSprite(texture, rect)
public void RenderSprite(Texture2D texture)
{
RenderSprite(texture, new Rectangle(0, 0, 1024, 768), null, Color.White);
} // RenderSprite(texture)
/// ////////////////////////////////////////////////////////////////
/// ////////////////////////////////////////////////////////////////
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
} // class PongGame
} // namespace
|
|

June 6th, 2007, 10:15 PM
|
|
Registered User
|
|
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Check your RenderSprite methods: they should be adding a sprite to the list of sprites to be rendered.
public void RenderSprite(Texture2D texture, int x, int y, Rectangle sourceRect)
{
sprites.Add(new SpriteToRender(texture,
new Rectangle(x, y, sourceRect.Width, sourceRect.Height),
sourceRect,
Color.White));
}
|
|

June 7th, 2007, 09:13 AM
|
|
Registered User
|
|
Join Date: Jun 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ok i added that, and it worked great, no ill contine struggling threw the book, thanks!
|
|
 |