<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:>
- Clean up code, separate it into smaller parts.
- Create a Game State structure
- Use the new tileset!(see picture above)
- Create a folder to store images, move all references to the images to the new folder.
<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, CREATEPLAYER = 4, 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 break; } case MENUSTART: //The first menu (new game, load...) { mygame.MenuStart(); break; } case CREATEPLAYER: //Player creation process { mygame.CreatePlayer(); //Player Creation Process break; } case MAINLOOP: //Game main loop { mygame.MainLoop();//The main game loop break; } case MENUGAME: //In game menu (save game...) { mygame.MenuGame(); break; } case OUTRO: //Outro screen { mygame.OutroScreen();//The main game loop break; }
case SAVE: //Save game { mygame.SaveGame(); break; } case LOAD: //Load Game { mygame.LoadGame(); break; }
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 #include // I use it to work with text streams using namespace std; //Class Game, i have moved everything class Game { public: 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 Game(); virtual ~Game(); void SplashScreen(); void OutroScreen(); void MenuStart(); void MenuGame(); void CreatePlayer(); void MainLoop(); void SaveGame(); void LoadGame(); protected: private: }; #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 Hero.SetX(110); Hero.SetY(310); Hero.SetSpeed(10); //Set default values for the players Hero.SetName("Conan"); Hero.Defaults(); Hero.CalculateVitMag(); Hero.MaximumAttributes(); //Ser camera starting offset Camera.x=1; Camera.y=1; Camera.w = myengine.ScreenX; Camera.h = myengine.ScreenY; }
In the next posts I will show all the other methods.
1 comment:
If somebody wants expert take on the main topic of blogging next I advise him/her to go to this site, continue the fussy job. mu online
Post a Comment