1.

Solve : The XNA thread?

Answer»
I really, really need help with this framework because I'm doing my final on it. =P
So I'll ask questions here whenever needed!

Here's my first:

1) Does one really need to use XNA's pre-built methods such as LoadContent(), Initialize(), UnloadContent(), to simply draw SOMETHING on the screen? Sure, you need a spriteBatch, which needs a GraphicsDevice, but can't I just throw in all needed variables in one line without using these methods?

Here's something strange:

Code: [Select]public General()
{
GraphicsDeviceManager graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
sprites = new SpriteBatch(GraphicsDevice);
}

Why is my compiler complaining that GraphicsDevice is null?
woah, XNA is certainly one of those new, complex things... give me a little while to learn about it, then I'll see if I can be any help. I've heard a lot about XNA and it's about time I see what it's all about.

Also, it might help in my quest at making a version of one of my games in a .NET language. And of course I can't really help unless I learn it, right?

ATM, I don't really know what you're doing in that code segment; I assume "GraphicsDevice" is either a global static in XNA or defined elsewhere? It's certainly not being set here. (you didn't mean to use new SpriteBatch(graphics), did you?)...

OK, got XNA working.

From what I can gather, you aren't supposed to initialize the sprites and so forth in the constructor, but rather in the "loadcontent" routine.

Actually I just opened the template

This is the template it gave me; it doesn't do anything, but it doesn't crash. THis was with XNA 3.1 and Visual Studio 2008:

Code: [Select]using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace mygame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

public Game1()
{


graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

}

/// <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()
{
// TODO: Add your initialization logic here

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <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 game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here

base.Update(gameTime);
}

/// <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)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here

base.Draw(gameTime);
}
}
}

Rather hefty...

But here's the kicker- as you may have read, XNA is based on DirectX.... well, I've used DirectDraw and Direct3d 7 and 8 from VB6, and a lot of the basics here look pretty similar- they just have a helpful framework (XNA) that makes the whole process a lot easier.

For example, I wanted to actually Draw something, something fun an interactive!

the goal was simple- this is my first ever use of XNA, so I decided, to simply draw a string.

I immediate dove in- the SpriteBatch had a "DrawString" method that looked suitable. but it needed a SpriteFont! No problem, let's see if new SpriteFont() works... Nope, it doesn't have any constructors.

So I made a quick google for SpriteFont, which led me to a article about how to draw text:

http://msdn.microsoft.com/en-us/library/bb447673.aspx

This explained that I needed to add a "SpriteFont" item to my "Content" Area. Fair enough, I figured. So I did so; in the interest if using my favourite font, Consolas, I called it Consolas and changed the name appropriately as well.

Now, I assumed that the stuff in Content was being loaded by the suitably NAMED "LoadContent()" routine that the template had already created for me, so I created a class level SpriteFont variable, which I named Consolas, and then added this to the LoadContent() routine:

Code: [Select]Consolas = Content.Load<SpriteFont>("Consolas");

then, in the Draw() routine I added:
Code: [Select]spriteBatch.DrawString(Consolas, "Test string", new Vector2(50, 50), Color.Black);

Oh the excitement!

ATM I have this, which changes the background and moves the text as you move the mouse around the window:

Code: [Select]using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace mygame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont Consolas;
Color mBackColor;
int mmouseX, mmouseY;
public Game1()
{


graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";

}

/// <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()
{
// TODO: Add your initialization logic here

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
Consolas = Content.Load<SpriteFont>("Consolas");

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <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 game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here
UpdateMouseData();
base.Update(gameTime);
}

protected void UpdateMouseData()
{
MouseState current_mouse = Mouse.GetState();

// The mouse x and y positions are returned relative to the
// upper-left corner of the game window.
mmouseX = current_mouse.X;
mmouseY = current_mouse.Y;

// Change background color based on mouse position.
mBackColor = new Color((byte)(mmouseX / 3), (byte)(mmouseY / 2), 0);
}



/// <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)
{

GraphicsDevice.Clear(mBackColor);
spriteBatch.Begin();
spriteBatch.DrawString(Consolas, "Test string", new Vector2(mmouseX, mmouseY), Color.Black);
// TODO: Add your drawing code here
spriteBatch.End();
base.Draw(gameTime);
}
}
}

I have to say... I was doubtful but XNA really is awesome, and it's easy too... at least it is if you've worked with DX before YES... that's correct. If you've worked with DirectX before. =P
I've been since september working on XNA and I know just about as much as you did in one day. LOL!
Yeah. No, I've never worked with XNA. A lot of veteran programmers think XNA is a piece of cake. Well, not for me, even though it has all pre-built methods and classes in the framework.

Here's a nice starter's game you can make:

XNA 2D space invaders

Have fun making it. =P


Discussion

No Comment Found