Thought for the Dazed

I've had to give up that Distance Learning course as I was having trouble seeing the teacher.

Flickr
www.flickr.com
RobMiles' items Go to RobMiles' photostream
Twitter
C# Yellow Book

Search entire site
Monday
Mar252013

Game State Management in MonoGame on Windows 8

ApplicationLog

I’ve been trying to make sense of how Windows 8 and MonoGame interact when the application changes state. I think I’ve figured it out.

There seem to be broadly two things that can happen to your game when it is running. It can get deactivated (the user finds something better to do) or it can get “snapped” to the side of the screen. If you are going to sell the program in the marketplace it needs to handle both these situations.

protected override void OnActivated(object sender, EventArgs args)
{
    addMessage("activated");
    base.OnActivated(sender, args);
}
protected override void OnDeactivated(object sender, 
EventArgs args) { addMessage("deactivated"); base.OnDeactivated(sender, args); } protected override void OnExiting(object sender,
EventArgs args) { addMessage("exiting"); base.OnExiting(sender, args); }

To handle the activation issues you override the event methods above. I’ve just put some logging code in so that I can work out what is going on. The events are fired when you would expect them to be. When the game starts you get an “onActivated” message and when anything happens to interrupt your game (for example the user minimises it or switches to another application) you get a “onDeactivated” message. At the very end, when the user quits your game you get the “onExiting” message. The activated and deactivated message come in matched pairs. The only slight trickiness is that a game will get an activated message at when it starts and when someone returns to it, but you can use a variable to keep track of this.

When your game is deactivated it should probably pause the game (because the user will not be able to interact with it). When your game is reactivated you can either resume the game or stay in pause mode and give the player a few seconds to compose themselves before continuing. When the game is exited your game should store any persistent game state and the first time it is activated your game should load the state.

The next thing you need to do is handle changes to the application view. These occur when the user “snaps” you to the side window or when the orientation/size of the screen changes. The first thing you need to do is bind an event handler to the event which fires when the application view changes:

ApplicationViewChanged += 
(sender, args) => ViewChanged(args.ViewState);

Your event handler will have to deal with changes in orientation and also “snapping” to the edge of the screen.

private void ViewChanged(ApplicationViewState viewState)
{
    switch (viewState)
    {
        case ApplicationViewState.Filled:
            addMessage("filled");
            break;

        case ApplicationViewState.FullScreenLandscape:
            addMessage("fullLand");
            break;

        case ApplicationViewState.FullScreenPortrait:
            addMessage("fullPort");
            break;

        case ApplicationViewState.Snapped:
            addMessage("snapped");
            break;
    }
}
This is my test method. It just sends a message out for the different states.

When you get snapped your game could display a “Mini-screen” in the snapped area. Above you can see what a snapped game looks like. The cornflower blue area on the right is the snapped area. When the game is unsnapped it will get a message to indicate the orientation of the screen. My games just pause when they are snapped.

One thing to remember is that when you get snapped the size of the screen changes.You need to change the size of the screen so that things are still drawn correctly in the snap panel area. You can grab the new values of the viewport size to do this.

cheeseRectangle = new Rectangle(0, 0, 
_graphics.GraphicsDevice.Viewport.Width,
_graphics.GraphicsDevice.Viewport.Height);

The above snippet resizes a cheese drawing rectangle to fit the whole screen. I could use this to make sure that my cheese is always displayed full size (although when the program is snapped this will make for a rather long and thin display).

Having got this far you are pretty much sorted. There is just one more thing you need to know, and that is the sequence of the messages. The log sequence at the top of this post will help here. The important thing to note is that you get viewport changed messages after you have been deactivated. If you think about it, this makes sense. The user can start to drag your application screen and then either put it back, or drop it into the snap area. It is important that the application is stopped when the user begins to perform such an action. So if your program is snapped you will get the sequence:

  1. deactivated
  2. snapped
  3. activated

You never get the view changed messages in isolation. This even the case when you use the Windows + . command to snap the game from the keyboard.

Monday
Mar252013

Gaming in Academia Event

Doncaster

I got this email from TripIt today. It doesn’t seem to understand that I’m changing trains in Doncaster to go on to Birmingham for the Windows Gaming Awareness event. Not that there is anything wrong with spending time in Doncaster.

I’m doing a session tomorrow in Birmingham about XNA and MonoGame in Windows 8 and Windows Phone 8. If you want to come along too, I think there may be a few places left if you are quick.

Sunday
Mar242013

Building a Door of Mystery

IMG_6031_2_3.jpg

After a considerable amount of tweakage of designs and waiting for the printer to finish I now have my “Door of Mystery” machine complete. This is how it looks inside. I’ve added a WiFi card so that the door can upload pictures after they have been taken.

After all my careful planning the box needed a bit of surgery because I’d forgotten that power connector protrudes from the front of the board, but apart from that (and having to go and get some more bolts) it is now pretty much complete.

IMG_6067.jpg

This is the finished device. I’m going to have to slightly adjust the hole for the RFID reader in the next version, but it is quite pleasingly solid and works a treat.

You buy an RFID tag to enter the competition, show it to the “Door of Mystery” above and it will give you your entry number and take your picture. Then, I can use my special “Pick a Winner” tag to pick a random entry and even view their picture. We might even use this for bashes, where we might want to give out random prizes, pick team members or let people take it in turns to use some of the games.

Saturday
Mar232013

Ahead of the Game

image

I went up town today to buy a copy of Lego City Undercover, which I had heard was due out on Friday. It is due out on Friday. Next Friday.

Oh well.

Friday
Mar222013

Get a Scope

image

I really am a sucker for mailshots (hmm. Perhaps I shouldn’t put that in the blog). Anyhoo, I got a mailshot from Cool Components advertising the above (or actually the slightly cheaper version 2 which you don’t seem to be able to get any more). So I bought one. It is a tiny digital storage oscilloscope which costs around 80 quids or so. I reckon this is extremely good value. It has an SD card that you can use to store waveforms and the whole thing is powered by a Cortex A3 and open source, so you can even put your own firmware in there if you fancy it. It is powered from a rechargeable battery and will go up to 1 MHz which for me is fine.

If you are not sure what an oscilloscope is, it lets you “see” electrical signals. It draws a plot of the voltage in the signal as it changes over time. The original ones used a cathode ray tube that drew a single dot on the screen. The dot was moved across the screen at a regular rate by a timebase generator and the value of the voltage in the signal was used to control the height of the dot. If the signal was regular then you could take a a peek at the waveform and see what it was doing.  Newer ones are of course digital. The input voltage is converted into numbers which are then used to draw a graph on the screen.

Sometimes, when a circuit just doesn’t work you need to take a look at what is happening. A voltmeter will tell you if there is anything on the wire, but not how it is changing over time. If you have any hardware aspirations I think a little device like this is a pretty good investment, and it only costs around the same as a couple of video games.