Archive for the ‘Graphics’ Category

Displaying a Mouse Pointer in XNA

Sunday, March 2nd, 2008

XNA doesn’t automatically show you a mouse pointer within the game window. Solving this is easy. First, you need a position for your mouse. You’ll also need a 2d texture to display as your mouse pointer. Declare this within your game class:

Texture2D m_mouseTexture;

Vector2 m_mousePos;

Be sure to initialize it to zero. For simplicity, I also generate a simple red square texture to use as the mouse pointer. I’m doing all of this in the Initialize() function. In my (very short) experience with XNA, you’d want to initialize textures in the LoadContent() function, but this is not loading from a file, so it’s safe to do it all in the Initialize() function like this:

m_mousePos = Vector2.Zero;

 

m_mouseTexture = new Texture2D(graphics.GraphicsDevice, (int)POINTSIZE, (int)POINTSIZE, 1, TextureUsage.None, SurfaceFormat.Color);

 

Color[] texture = new Color[POINTSIZE * POINTSIZE];

for (int i = 0; i < texture.Length; ++i)

{

    texture[i] = Color.Red;

}

m_mouseTexture.SetData(texture);

Now, in the Update() function, update your mouse position:

MouseState mouseState = Mouse.GetState();

m_mousePos.X = mouseState.X;

m_mousePos.Y = mouseState.Y;

And finally, in the Draw() function, draw your sprite in a spritebatch:

spriteBatch.Begin();

spriteBatch.Draw(m_mouseTexture, m_mousePos, null, Color.White, 0f,

        Vector2.Zero, 1.0f, SpriteEffects.None, 0f);

spriteBatch.End();

Grab the sample project for this here.

Conway’s Game of Life

Saturday, March 1st, 2008

I thought I should talk a little about the project I’m focusing on right now. I’m still spinning up on XNA, and I was reading an article somewhere recently that mentioned Conway’s Game of Life. I was never taught about this in school for some reason. What a wonderful little thing!

Anyways, I thought it would be fun to implement Conway’s Game of Life in XNA to give myself something tangible (in the software sense… what does that really mean, anyways?) to work on. I’ve already got a basic architecture done, but I want to write down some of the guidelines I’m trying to stick to. Just basic stuff, like:

What am I trying to learn?
- I want to learn about what it will take to implement a menu system in XNA.
- I want to learn about mouse interaction with an object (the game board) on the screen.

Which Game of Life algorithm should I use?
- a few are mentioned on the wiki page, notably hashlife. Since I’m not trying to learn about Game of Life specifics, I’m going to start with brute force with two 2d arrays (one for the current interation, and one for the next). It will give me a good quick start on my goal, and also give me a good baseline if I decide to optimize the algorithm.

What operations should I allow?
- a mouse click should turn a cell on or off. Hold and drag to fill or unfill cells under the mouse pointer
- spacebar should step to the next iteration.
- enter should run through iterations at the chosen speed (we’ll need a menu for that).

What should the menu contain?
- Options
– run speed
– window size
– colors?
- I’ll leave any other nifty stuff for later.

Things that would be very interesting to implement:
- the ability to move and zoom the camera around the grid with the mouse or keyboard
- some logic to move and zoom the camera automatically, and follow the most activity in the grid. One nasty corner case for this is any gliders that come off of the high activity areas. how do i know when to ignore them?

More to come later when I have more of this implemented.

OpenGL SuperBible: Chapter 1

Friday, February 29th, 2008

I read through the introduction and chapter 1 of the OpenGL SuperBible. It was 100% review, but it’s well written, so I just went with it. I remember enjoying reading my OpenGL book in college, and I really enjoy the author of this book as well. There’s something about graphics programmers, I guess. They’re not 100% engineer. They know how to put graphics together, and I guess that bleeds over into other less technical tasks, like arranging words on a page. Come to think of it, my OpenGL teacher was pretty entertaining, too. He looked like the neighbor dad from Honey, I Shrunk the Kids, and that amused me.