XNA Game Development – Part 3
Well expanding more on this side of it. As I have mentioned before in Part 1 and then in Part 2, about the development of an XNA game. I have enjoyed it so far and I am going to continue to keep adding to the parts to hopefully provide some insight but also give some advice to developers who are new to this side, similar to me, and hope to get the most out of their environment and time.
Games and the Console
Now, you thought perhaps that the console for games was long dead and buried. You might be right, but the console I am referring to is certainly the same thing, you have seen it, the horrible thing;
You are looking at a screen, the title says Microsoft Visual Studio.
Where would you like to move the mouse to?
Move Mouse to File Menu.
You cautiously move the mouse to the file menu of the Microsoft Visual Studio Window.
You press your right index finger onto the left mouse button. It makes a light clicking sound, more text displays below the word file as the menu opens.
Well you get the point, Click File | New Project | Select XNA Game Studio and select an XNA windows game to create.
Provide a name, anything will do since I am only showing you how the console window will work. Click OK.
Give it some time and the solution will be created. A nice base template for an XNA Windows Game. This will then open the code onto the Game1.cs file and you can begin coding your game. But I am not here, well for this post, to talk about coding a game in XNA, no, it is to just highlight the console window. There is one thing I am needing to do at this point and that is to put some code into the Game1.cs file.
Go to the method, Update. In the standard template it is just before the Draw method, which is the last one in the file.
Put the following code into the update method.
// TODO: Add your update logic here Console.WriteLine("(" + Mouse.GetState().X.ToString() + ", " + Mouse.GetState().Y.ToString() + ")");
Well, press F5.
You could be wondering where the magic is happening. And you would be right, there is no magic happening yet. Stop the application and right click the project and click Properties. This will bring up the properties window, surprisingly.
And what you can see in my poorly highlighted area, is the Output type. there are 3 options, Windows Application, the default, Console Application, mmm, way a moment, and Class Library. So you might be thinking right, select Console Application and save the properties. You might be thinking that if you select this, then all of sudden this will show you a console window in place of the game window, well you could be right, but this is what will be shown.
It is a bit difficult to see, but you can certainly from this point, only just, it is showing you, the X and Y position of the mouse, nicely formatted of course. This is the console. You will find that your wonderful Cornflower Blue screen is still displayed. The purpose of this is to output values that are difficult to debug while the game is running. It all harks back to the wonders of the game loop. Love it or hate it, it is a simple and more efficient way of handling games, since you are waiting for an event trigger, you merely look at state and change it.
But to give you a help with it, take the code that was used before, and replace it with the following.
// TODO: Add your update logic here Console.WriteLine(new Vector2(Mouse.GetState().X, Mouse.GetState().Y).ToString());
All the different that was done was to put the details into a new Vector2 instance and then use the ToString method of that, why, it formats it better.
It includes the brackets and the nice prefix of X and Y onto the values. Another question that could be raised, will this impact performance. And to that I will say absolutely. But remember this is for debugging, and often you use to work out why something isn’t working as expected, the sprite should be moving gradually in a different direction but you find that is changing immediately, things of that nature. Exactly like the Console Application you can write in any .NET language the Console.WriiteLine method will output almost any type to the Console, almost. But the last thing you want to be doing is to stream textual representations of textures or the like to the console window.
So, to help with that, you could use the following;
#if DEBUG // TODO: Add your update logic here Console.WriteLine(new Vector2(Mouse.GetState().X, Mouse.GetState().Y).ToString()); #endif
Providing you are using the #define or /define switches in the compiler properly, it will not get included with release product.