PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Advice on Key Presses on Smartphone

I'm getting towards the final(ish) stages of the development of my first Smartphone game and have a question regarding the handling of key presses.

In my game loop I have a number of different game states. In the game loop I have a switch statement which determines what is drawn in each state (eg. MAINMENU, GAMESTATE_A, GAMESTATE_B). This I assume is a reasonably sensible way of doing things, but any other suggestions would be welcome)

My question is this - in the KeyDown function I currently have a switch statement for all the game states and within each case I have another switch statement for the behaviour of each key that is relevant. Is this sensible or should I have a switch statement for each Key, and within each case have another switch statement for each gamestate? Will changing it round make any difference at all and are there any general rules for switch statements within switch statements?

Any help would be greatly appreciated!
I can tell you the way I've organized my code... I don't know if it's the norm or not, but it has worked out fantastically well for me...

Let's say I have an intro screen, a main menu screen and a main game screen. These are my game states.

First, I define my game class as usual, and I do it in a file named main.h. It looks something like this (abbreviated here of course)...

Code:

// First, some defines for my game states
#define gsIntro 1
#define gsMainMenu 2
#define gsMainGame 3

// Then, define my game class
class CGame : public CGapiApplication {

public:

// Property to tell what game state we're in
int game_state;

// Constructor, destructor and the usual
// GapiDraw methods (InitInstance,
// ExitInstance, etc.) are defined here

// The following methods are used later as you'll
// see. In them, the following codes are used:
// pnf=ProcessNextFrame, sd=StylusDown,
// su=StylusUp, sm=StylusMove, sc=StylusClick
// kd=KeyDown, ku=KeyUp

// Intro
void pnfIntro(CGapiSurface& backbuffer, DWORD dwFlags);
void sdIntro(POINT p);
void suIntro(POINT p);
void smIntro(POINT p);
void scIntro(POINT p);
void kdIntro(DWORD dwKey, GDKEYLIST& keylist);
void kuIntro(DWORD dwKey, GDKEYLIST& keylist);

// Main menu
void pnfMainMenu(CGapiSurface& backbuffer, DWORD dwFlags);
void sdMainMenu(POINT p);
void suMainMenu(POINT p);
void smMainMenu(POINT p);
void scMainMenu(POINT p);
void kdMainMenu(DWORD dwKey, GDKEYLIST& keylist);
void kuMainMenu(DWORD dwKey, GDKEYLIST& keylist);

// Main Game
void pnfMainGame(CGapiSurface& backbuffer, DWORD dwFlags);
void sdMainGame(POINT p);
void suMainGame(POINT p);
void smMainGame(POINT p);
void scMainGame(POINT p);
void kdMainGame(DWORD dwKey, GDKEYLIST& keylist);
void kuMainGame(DWORD dwKey, GDKEYLIST& keylist);

};

Then, in a file called processNextFrame.cpp, I have:

Code:

#include "Main.h"

HRESULT CGame::ProcessNextFrame(CGapiSurface& backbuffer, DWORD dwFlags) {

switch (game_state) {

case gsIntro: {
pnfIntro(backbuffer, dwFlags);
break; }

case gsMainMenu: {
pnfMainMenu(backbuffer, dwFlags);
break; }

case gsMainGame: {
pnfMainGame(backbuffer, dwFlags);
break; }

}

return S_OK;

}

The, in a file called inputHandlers.cpp, I have...

Code:

#include "Main.h"

HRESULT CGame::StylusDown(POINT p) {

switch (game_state) {

case gsIntro: {
sdIntro(p);
break; }

case gsMainMenu: {
sdMainMenu(p);
break; }

case gsMainGame: {
sdMainGame(p);
break; }

}

return S_OK;

}

HRESULT CGame::StylusUp(POINT p) {

switch (game_state) {

case gsIntro: {
suIntro(p);
break; }

case gsMainMenu: {
suMainMenu(p);
break; }

case gsMainGame: {
suMainGame(p);
break; }

}

return S_OK;

}

HRESULT CGame::StylusMove(POINT p) {

switch (game_state) {

case gsIntro: {
smIntro(p);
break; }

case gsMainMenu: {
smMainMenu(p);
break; }

case gsMainGame: {
smMainGame(p);
break; }

}

return S_OK;

}

HRESULT CGame::StylusDblClk(POINT p) {

switch (game_state) {

case csIntro: {
scIntro(p);
break; }

case csMainMenu: {
scMainMenu(p);
break; }

case csMainGame: {
scMainGame(p);
break; }

}

return S_OK;

}

HRESULT CGame::KeyDown(DWORD dwKey, GDKEYLIST& keylist) {

switch (game_state) {

case csIntro: {
kdIntro(dwKey, keylist);
break; }

case csMainMenu: {
kdMainMenu(dwKey, keylist);
break; }

case csMainGame: {
kdMainGame(dwKey, keylist);
break; }

}

return S_OK;

}

HRESULT CGame::KeyUp(DWORD dwKey, GDKEYLIST& keylist) {

switch (game_state) {

case csIntro: {
kuIntro(dwKey, keylist);
break; }

case csMainMenu: {
kuMainMenu(dwKey, keylist);
break; }

case csMainGame: {
kuMainGame(dwKey, keylist);
break; }

}

return S_OK;

}

Finally, I have a separate .cpp file for each of the game states (intro, main menu and main game) that would look something like this (I'll just show the main menu for example, the others are the same in structure)...

Code:

#include "main.h"

namespace ns_MainMenu {

// Define any global variables here

} // End of namespace

void CGame::pnfMainMenu(CGapiSurface& backbuffer, DWORD dwFlags) {

using namespace ns_MainMenu;

// Do all your ProcessNextFrame stuff here

}

void CGame::sdMainMenu(POINT p) {

using namespace ns_MainMenu;

// Handle all StylusDown events here

}

void CGame::suMainMenu(POINT p) {

using namespace ns_MainMenu;

// Handle ann StylusUs events here

}

void CGame::smMainMenu(POINT p) {

using namespace ns_MainMenu;

// Handle ann StylusMove events here

}

void CGame::scMainMenu(POINT p) {

using namespace ns_MainMenu;

// Handle all StylusDblClick events here

}

void CGame::kdMainMenu(DWORD dwKey, GDKEYLIST& keylist) {

using namespace ns_MainMenu;

// HAndle all KeyDown events here

}

void CGame::kuMainMenu(DWORD dwKey, GDKEYLIST& keylist) {

using namespace ns_MainMenu;

// Handle all KeyUp events here

}

And that's how I've done things. My current project has right now about 25 different game states (i.e., different screens in essence) and this is how I've organized things. I actually have more in there because each screen can essentially have a number of sub-states, but this is the general structure I've used very successfully. I don't know if I'm doing anything special, but it works. :)
InexorableTash · 08 Aug 2003, 15:46 UTC
Game Programming Gems I has a design pattern for this called Layers. Like most good design patterns, it's pretty obvious when you hear it, and I'd stumbled across it before reading GPG.

Use a class called Layer that looks like this:

Code:

class Layer
{
public:
HRESULT CreateSurfaces(...);
HRESULT ProcessNextFrame(...);
HRESULT KeyDown(...);
...
}


Then have your game states be subclasses, like:

Code:

class GameStateALayer : public Layer { ... }
class GameStateBLayer : public Layer { ... }


(Alternately, use an interface/implementation strategy rather than class/subclass strategy. Whatever you prefer.)

Change your main app class to have a pointer to the current layer (e.g. Layer* m_pActiveLayer) , and have the main functions like KeyDown and ProcessNextFrame simply delegate to the active layer. That keeps all of the code for every state nicely isolated.

When you have a state change, simply reassign m_pActiveLayer, and the next "tick" or event will be delegated appropriately.

Note that if you have a small number of layers it's probably easiest to have them created statically, and initialize surfaces all at once rather than delegating to just the active layer, like this:

Code:

MyApp
{
GameStateALayer m_layerStateA;
GameStateBLayer m_layerStateB;
Layer* m_pActiveLayer;

MyApp() : m_pActiveLayer( &m_layerStateA ) { ... }
HRESULT CreateSurfaces( ... )
{
m_layerStateA.CreateSurfaces( ... );
m_layerStateB.CreateSurfaces( ... );
}
HRESULT ProcessNextFrame( ... )
{
if( m_pActiveLayer == NULL )
return E_FAIL;
else
return m_pActiveLayer->ProcessNextFrame( ... );
}
}


If you try and have layers created dynamically then you have to be much more intelligent and careful about the order in which CreateSurfaces gets called and do careful ref-counting when you switch active layers in response to an event. Multithreading can be loads of fun in that case. :)

You can take Layers one step further and allow them to, well, layer - each layer has a parent and a child, and delegates method calls appropriately. For example, your game could have an animated background that keeps animating even during transitions between levels - in that case, a lower level is animating but not consuming events. I use them for dialogs that pop up on top of the game action, but keep rendering the state of the layer below (non-active layers in my framework know not to "tick" the clock).

You can even implement things like an on-screen keypad as a layer - it would render and consume events with the highest priority, but the rest of the screen would still animate and respond to events.