Quick Questions, Quick Answers!

For this post, I decided to spend some time answering some frequently asked questions that some of you may or may not be thinking of right now. I remember at this stage of my learning, I started to wonder about how to do certain things. So let’s dive right in!

Eh…where’s my mouse pointer?!

Yeah…this is one I had pretty quick after using XNA Game Studio. I noticed that whenever I ran my game and moved the mouse over it, POOF!, the mouse pointer is gone. Working on the team that develops XNA has the awesome benefit of me being able to walk up to one of the developers and be like “yeah…dude…wtf?”. The answer is pretty simple, putting the following code in your Initialize method will make the mouse pointer visible:

this.IsMouseVisible = true;

The default value is “false”, hence why we don’t see it by default.

I want to run my game at a different resolution, how?

Pretty soon after you start playing around with XNAGS, you will want to run your game at a different resolution than the default one. How do you change it? Here’s the code:

   1: public Game1()
   2: {
   3:     graphics = new GraphicsDeviceManager(this);
   4:     Content.RootDirectory = “Content”;
   5:
   6:     // Set the game’s resolution to 1680×1050
   7:     this.graphics.PreferredBackBufferWidth = 1680;
   8:     this.graphics.PreferredBackBufferHeight = 1050;
   9: }

Simple :) Just make sure it’s in the Constructor vs the Initialize method.

Give me fullscreen!

Alright, a follow up question is: How do I make my game run in Fullscreen mode?

   1: protected override void Initialize()
   2: {
   3:     // If we are not Fulscreen, let’s be fullscreen!
   4:     if (!graphics.IsFullScreen)
   5:         graphics.ToggleFullScreen();
   6:
   7:     base.Initialize();
   8: }

You can do it by either calling the ToggleFullScreen() or directly setting the value of IsFullScreen to true for fullscreen or false. Both work just fine. See? We want you to have options!

How do I tell if my game is “Active” or in focus?

This is one that I ran into when I was working on a simple sample. I used the mouse to control the sample (was rotating a cube or something amazing like that) and I noticed that when I moved the mouse when the game wasn’t active and the cube still rotated. Hmm..that’s odd. Why is my game handling mouse movements even when it’s not the actively focused application? My first thought was “Aha! BUG! The devs screwed up! Time to go rub it in!”. Five mins later, I come back after talking to one of the devs and it’s not a bug (sigh…fine..almost had him). This is what you want to do:

   1: protected override void Update(GameTime gameTime)
   2: {
   3:     if (this.IsActive)
   4:     {
   5:         // Add code that you only want to run when 
   6:         // the game is the active and in focus
   7:         // application. 
   8:         //
   9:         // Stuff like input handling for instance is good 
  10:         // here.
  11:     }
  12:
  13:     base.Update(gameTime);
  14: }

All you need to do is check the IsActive flag. If it’s true, your game is in focus.

My Update method is called 60 times per second, I want faster!

The Update method gets called by default 60 times a second. This makes sure that your game runs at the same pace on any device it runs on. But sometimes you may want to change that to make it run as fast as possible. This means you want it to be called as often as possible by the XNA Framework. To do that, here’s the code:

   1: protected override void Initialize()
   2: {
   3:     this.IsFixedTimeStep = false;
   4:
   5:     base.Initialize();
   6: }

and in the Update method, you can find out how long it has been since the last Update call like this:

double elapsedTime = gameTime.ElapsedGameTime.TotalMilliseconds;

That’ll do it.

How to I set the title of the game window?

Alright, this one you probably would have spent like 20 seconds to find out how to do. But hey, I just saved you 20 seconds eh? Yeah…that’s how I roll.

Setting the title of the Window in non-Fullscreen mode is useful since it’s an easy and fast way to display some short debugging info for example. Something like Frames Per Second can be displayed here pretty quickly. This is how to do it:

   1: protected override void Initialize()
   2: {
   3:     this.Window.Title = “I am an awesome game! No really!”;
   4:
   5:     base.Initialize();
   6: }

See? now you have 20 seconds extra to do whatever with.

That’s it for now! If you have any other questions, post them as comments and I’ll see that they get some sort of an answer. Have fun!

Thanks for reading! I'd love to hear your thoughts, feel free to leave a comment below. Don't forget to subscribe to my RSS Feed!

2 Responses to “Quick Questions, Quick Answers!”

  1. thp Says:

    Hi, first congratulation for your good work!

    Than I want to post that if I do:

    graphics.IsFullScreen = true;

    the application doesn’t switch in fullscreen…

    Regards

  2. Kamal Says:

    Thanks for the tutorials!

Leave a Reply

The page's WebCounter count says that you are visitor number Visitor Counter by Digits
FireStats icon Powered by FireStats