Thursday, August 18, 2011

Code::Blocks(14) RPG 4 - More classes , Game states and a few tweaks more

<In this post I'm going to create the basic structure of the game, until now I had almost all the code included in the "rpg.cpp" file, but it was starting to get too big. I knew i needed to do a few things:>
  1. Clean up code, separate it into smaller parts.
  2. Create a Game State structure
  3. Use the new tileset!(see picture above)
  4. Create a folder to store images, move all references to the images to the new folder.
<Before starting with the updates to the code I'm going to explain what the game states are and what i expect to do with them>

<The best way to understand it is to look at this diagram:>
 <We have many "Screens" that represent different game states, in every game state the game acts in a different way (different input, output and logic), what we need is to have a way to store where the game is, so we can launch the code that will handle that particular state>

<At the moment that's the project structure:>

<To create the game states and to divide the code into smaller parts, we are going to change the project structure to:>

<Now the "main" function include all the code,  how i made the changes? First I created a file "gamestates.h" that is basically an enum to know in what state the game is:>

<File "gamestates.h">

enum GameState { //Enumerates the game states
    INTRO = 1,
    MENUSTART = 2,
    LOAD = 3,
    MAINLOOP = 5,
    MENUGAME = 6,
    SAVE = 7,
    OUTRO = 8 ,
    END = 99};

<File rpg.cpp has been totally rewritten:>

#include  //SDL library
#include  //+SDL_image library (I use it for loading .png files)

#include "game.h" //Core game class

int main ( int argc, char** argv ) //when you use a SDL c++ app you have to initialize main that way

Game mygame; //Create an Instance of Game, Loads default values

mygame.myGameState = INTRO; //We start with the intro screen

bool done = false;
while (!done) //Loop infinite times until done

switch (mygame.myGameState)
case INTRO: //Intro screen
    mygame.SplashScreen(); //Shows an intro Splash Screen

case MENUSTART: //The first menu (new game, load...)

case CREATEPLAYER: //Player creation process
    mygame.CreatePlayer(); //Player Creation Process

case MAINLOOP: //Game main loop
    mygame.MainLoop();//The main game loop

case MENUGAME: //In game menu (save game...)

case OUTRO: //Outro screen
    mygame.OutroScreen();//The main game loop

case SAVE: //Save game

case LOAD: //Load Game

case END: //End game
    done = true;
    break; //exit switch , end game

} //switch
} //while

return 0; //Everything is ok

}//End main

<Now it just contains a game class instance and a simple switch to select the game state and execute the right code (clean and neat), the game class is declared here:>

<File "game.h":>

#ifndef GAME_H
#define GAME_H

#include  //SDL library
#include  //+SDL_image library (I use it for loading .png files)

#include "engine.h" // The simple engine class
#include "cplayer.h" //The player class
#include "cmap.h" // The Map class
#include "gamestates.h" //The Game states Enum

#include  // I use it to work with text streams

using namespace std;

//Class Game, i have moved everything
class Game

GameState myGameState ; //used to know what we should do at any moment
Engine myengine; //Create a object from the type "Engine"
std::stringstream text; //Variable used to display text
Uint8 x,y; //variables used to travel across the map array
cmap mymap; // Our map class
cplayer Hero; //Our player class
//cmenu mymenu; //Our Menu Class
Uint8 *keys;
SDL_Rect Camera;

 char* MenuTittle; //The menu tittle
 vector MenuItems; //The menu items

        virtual ~Game();
        void SplashScreen();
        void OutroScreen();
        void MenuStart();
        void MenuGame();
        void CreatePlayer();
        void MainLoop();
        void SaveGame();
        void LoadGame();


#endif // GAME_H

<Notice I moved all the variables as properties into the new class, and functions now are methods, notice too we have achieved two objectives: clean the code and split it into smaller parts, it is easier to understand and implement too, next objective is to code game.cpp >

The first function, the class initializer is this:
file "game.cpp"

Game::Game()//Initialize Game class instance
myengine.SetVga(800,600,32,true); //Start graphics mode
myengine.LoadTileset("images/tileset3.png"); //Load the image tileset to a SDLSurface

//Set starting position and speed for the player

//Set default values for the players

//Ser camera starting offset
Camera.w = myengine.ScreenX;
Camera.h = myengine.ScreenY;

In the next posts I will show all the other methods.

No comments: