PS2 Linux Programming

 

Game Framework Template

 

Introduction

 

This tutorial introduces a basic game framework template which can be used to organise the creation of game applications under PS2 Linux. The framework is used to organise and abstract the initialisation and shut-down code and also incorporates a simple state machine template to facilitate the movement between the various game states of the application.

 

 

Framework Code

 

The heart of the code in contained in main.cpp and is listed below for clarity.

 

 

int main(void)

{

     CGame Game;

    

     if(Game.Start())

     {            

          printf("\nGame started\n");

         

          while(Game.GetRunningStatus())

          {

              Game.GameLoop();

          }

     }

    

     else printf("Startup Failed\n");

    

     printf("\nGame stopping\n\n");

    

     Game.Stop();

    

     return 0;

}

 

 

There is a single instance of the game class, CGame. Method CGame::Start() executes all of the required star-up code while CGame::Stop() performs any necessary clean-up at the end of the game execution. CGame::GameLoop() is the game loop code that is executed every frame and this contains the state machine implementation. CGame::GetRunningStatus() returns a Boolean to indicate whether the game is running (true) or has ended (false).

 

The CGame class is listed below for clarity.

 

 

enum GAMESTATE {SPLASH, MENU, LEVEL1, EXIT};

 

class CGame

{

public:

     CGame(void);

     ~CGame(void);

 

     bool Start(void);

     void GameLoop(void);

     void GameSplash(void);

     void GameMenu(void);

     void GameLevel1(void);

     void GameExit(void);

     void Stop(void);

    

     static void SignalHandler(int Signal);

 

     inline bool GetRunningStatus(void)

          {return m_bRunning;}

 

     static inline void SetRunningStatus(bool Status)

          {m_bRunning = Status;}

 

protected:

     static bool m_bRunning;

     PS2SpriteT Sprite;

     CTexture Game;

     CFont Font;

     CTexture FontTex;

     enum GAMESTATE GameState;   

};

 

 

The CGame::Start() method allocated memory, configures the signal handler, configures the control pads, initialises the screen clear colour, loads the font textures the loads the game textures. CGame::Start() returns true if all of the initialisation code is successful.

 

CGame::GameLoop() contains the state machine implementation and is shown below.

 

 

void CGame::GameLoop()

{

     pad_update(PAD_0);

     if((pad[0].buttons & PAD_START)&&(pad[0].buttons & PAD_SELECT)) SetRunningStatus(false);

 

     switch (GameState)

     {

          case SPLASH:

              GameSplash();

              break;

 

          case MENU:

              GameMenu();

              break;

 

          case LEVEL1:

              GameLevel1();

              break;

 

          case EXIT:

              GameExit();

              break;

 

          default:

              printf("Undefined Game State\n");

              SetRunningStatus(false);

              break;

     }

}

 

 

From this code extract it can be seen that CGame::GameLoop() is used to switch between the various game states based on the enumerated variable GameState. The organisation of the state machine can be adapted to meet the requirements of the application. Notice the CGame::GameLoop() also contains exit code by pressing start and select together on controller pad 0 this is very useful for debugging code where the developer does not wish to always have to work through a menu system.

 

 

The Example Code

 

 

A simple sprite application is used to illustrate the game framework. When the application starts, the user is presented with a splash screen which renders for 5 seconds. The second screen is the Menu screen which can be used to set various options within the game. Further game states could be created to implement the menu selections. When the user enters the game, all that happens is a 2D sprite mover around the screen, there is no game play or interaction of any kind. The game is terminated by pressing select on the control pad 0. This takes the application to the END splash screen which remains on screen for 5 seconds before the application is finally terminated.

 

 

Conclusions

 

A simple class and state machine based game framework for PS2 Linux has been introduced in this tutorial. The framework can be adapted to control any game application and can be used for both 2D and 3D games applications.

 

 

Dr Henry S Fortuna

University of Abertay Dundee

h.s.fortuna@abertay.ac.uk