Tutorial - C++ Game Programming #1

(1/1)

Richard Marks:
Tutorial - C++ Game Programming #1

Welcome to the first tutorial.
I'm going to be brief, and to the point. I'm going to toss code, and some explanations as I go along, and if something doesn't make sense, then contact me, and I will elaborate more on the topic. And, now, here we go.

I make a few assumptions in this tutorial.
You have your c++ development environment installed and you know how to use it.You have a basic understanding on C++ classes, methods, members, pointers, and you are just wanting to know how to build a game using the C++ knowledge you have.
To start, we will build a structure to build our game on top of.

At the end of the structure building process, your code WILL compile if you entered it all in correctly, but if you run it, it will freeze, since we will not have told it to do anything yet. ;D Making it DO something will come in tutorial #2.

Richard Marks:
Create tutorial1.cpp

This is the main source file for what will become our "game".
What I mean is that this is where the program entry point is located.
Every C/C++ program has to have a main() function and this is where it lies.
I think I rehashed that enough. So, lets get on with the code already!

Our game will be created using several classes.
One is called Game (how original) and it will be defined in the Game.h file. Let us include that sucker now.
Code:

// include the header for our game class
#include "Game.h"


Everything that will happen in the game will take place in a GameState.
Right now, we just will have a single state, called InGame.
This state is created using a class, and it is defined in the GameState_Ingame.h file. Include it now.

Code:

// include this gamestate's header
#include "GameState_Ingame.h"


Finaly we get to the main() function. Its very simple.

We will create an instance of our Game class, set the starting state,
and then call a method of our Game class called Run which starts the game.
Lastly we return control back to the operating system. That wraps it up.

Code:

// all programs need an entry point
int main(int argc, char* argv[])
{
// create an instance of our game class
Game game;

// set the first game state
game.ChangeGameState(GameState_InGame::Instance());

// run our game's main loop
game.Run();

// return to the OS
return 0;
}


Take special notice at the ChangeGameState method.
We pass what looks like a method call, but it is a little weird.
The ChangeGameState method takes a GameState pointer as a parameter as you will find out later on in the Game.h file.

This weird syntax is passing what is called a Singleton. A singleton is a special type of class that can have only a single instance.
We define the Instance() method to return a reference to that single instance of our game state. This makes it simple to manage our states.

Create Game.h

Code:

// Game.h
// NOGDUS Tutorial: C++ Game Programming #1
// Developed by Richard Marks ccpsceo@gmail.com
// (C) Copyright 2008, CCPS Solutions
// http://www.ccpssolutions.com

#ifndef GAME_H
#define GAME_H

// include the STL (standard template library) vector template
// we include here because you need to include any templates
// in the same file you will use them. unlike classes, you cannot
// forward declare a template.
#include <vector>

// our game uses gamestates to handle what happens in the game
// instead of including the headers for gamestates, we simply
// forward-declare the gamestate class so we can reference it
// as a class in this file. We include the header in the class
// implementation files (.cpp)
class GameState;

// our game class definition
class Game
{
public:
// constructor
Game();

// destructor
~Game();

// updates our game
void Update();

// renders our game
void Render();

// cleans up after our game
bool Shutdown();

// our main loop
void Run();

// kills everything
void Quit();

// our gamestate management
void ChangeGameState(GameState* newState);
void PushGameState(GameState* newState);
void PopGameState();

private:
// for our main loop
bool running_;
// our gamestate managment
std::vector<GameState*> gameStates_;
};

#endif


Don't worry, I'll explain it all later.. for now, I'm posting the code as I write it.

Create GameState.h

Code:

// GameState.h
// NOGDUS Tutorial: C++ Game Programming #1
// Developed by Richard Marks ccpsceo@gmail.com
// (C) Copyright 2008, CCPS Solutions
// http://www.ccpssolutions.com

#ifndef GAMESTATE_H
#define GAMESTATE_H

// forward declare our game engine class so we can pass it
// to our gamestate methods
class Game;

// our gamestate class is really an abstract class
// meaning we will inherit our game's different states
// from this class and implement each function that we
// specify in this class here.

class GameState
{
public:
virtual bool Initialize() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void Update() = 0;
virtual void Render() = 0;
virtual bool Shutdown() = 0;

void ChangeGameState(Game* game, GameState* newState);

virtual ~GameState(){}
protected:
GameState(){}

};

#endif


Don't get impatient..there is more to come. ;D

Richard Marks:
Create GameState_Ingame.h

Code:

// GameState_Ingame.h
// NOGDUS Tutorial: C++ Game Programming #1
// Developed by Richard Marks ccpsceo@gmail.com
// (C) Copyright 2008, CCPS Solutions
// http://www.ccpssolutions.com

#ifndef GAMESTATE_INGAME_H
#define GAMESTATE_INGAME_H

// include our gamestate header
#include "GameState.h"

// our gamestate class definition for the ingame state
// is implemented as a singleton class since we do not
// need more than a single instance of each gamestate.
class GameState_InGame : public GameState
{
public:
// how we access the only instance of the class
static GameState_InGame* Instance();

// we must implement the GameState abstract methods
bool Initialize();
void Pause();
void Resume();
void Update();
void Render();
bool Shutdown();

protected:
// hide the constructor & destructor
GameState_InGame();
~GameState_InGame();
private:
// the only instance of the class
static GameState_InGame* instance_;
};

#endif


Create Game.cpp

Code:

// Game.cpp
// NOGDUS Tutorial: C++ Game Programming #1
// Developed by Richard Marks ccpsceo@gmail.com
// (C) Copyright 2008, CCPS Solutions
// http://www.ccpssolutions.com

// include our gamestate class header
#include "GameState.h"

// include our game class header
#include "Game.h"

// constructor
Game::Game()
{
running_ = true;
}

// destructor
Game::~Game()
{
}

// updates our game
void Game::Update()
{
if (!gameStates_.empty())
{
gameStates_.back()->Update();
}
}

// renders our game
void Game::Render()
{
if (!gameStates_.empty())
{
gameStates_.back()->Render();
}
}

// cleans up after our game
bool Game::Shutdown()
{
while (!gameStates_.empty())
{
gameStates_.back()->Shutdown();
gameStates_.pop_back();
}
return true;
}

// game's main loop
void Game::Run()
{
while(running_)
{
Update();
Render();
}
}

void Game::Quit()
{
running_ = false;
}

// changes the current game state
void Game::ChangeGameState(GameState *newState)
{
if (!gameStates_.empty())
{
gameStates_.back()->Shutdown();
gameStates_.pop_back();
}

gameStates_.push_back(newState);
gameStates_.back()->Initialize();
}

void Game::PushGameState(GameState *newState)
{
if (!gameStates_.empty())
{
gameStates_.back()->Pause();
}

gameStates_.push_back(newState);
gameStates_.back()->Initialize();
}

void Game::PopGameState()
{
if (!gameStates_.empty())
{
gameStates_.back()->Shutdown();
gameStates_.pop_back();
}

if (!gameStates_.empty())
{
gameStates_.back()->Resume();
}
}

Richard Marks:
Create GameState.cpp

Code:

// GameState.cpp
// NOGDUS Tutorial: C++ Game Programming #1
// Developed by Richard Marks ccpsceo@gmail.com
// (C) Copyright 2008, CCPS Solutions
// http://www.ccpssolutions.com

// include our game class header
#include "Game.h"

// include our gamestate class header
#include "GameState.h"

// changes the game's state
void GameState::ChangeGameState(Game* game, GameState* newState)
{
game->ChangeGameState(newState);
}


Create GameState_Ingame.cpp

Code:

// GameState_Ingame.cpp
// NOGDUS Tutorial: C++ Game Programming #1
// Developed by Richard Marks ccpsceo@gmail.com
// (C) Copyright 2008, CCPS Solutions
// http://www.ccpssolutions.com

// include this gamestate's header
#include "GameState_Ingame.h"

// the only instance of this class
GameState_InGame* GameState_InGame::instance_ = 0;

// constructor
GameState_InGame::GameState_InGame()
{
// initializes our singleton with the first creation
// of an instance of this class
GameState_InGame::instance_ = this;
}

// destructor
GameState_InGame::~GameState_InGame()
{
}

// returns the only instance of the class
GameState_InGame* GameState_InGame::Instance()
{
return GameState_InGame::instance_;
}

// initialize the state
bool GameState_InGame::Initialize()
{
return true;
}

void GameState_InGame::Pause()
{
}

void GameState_InGame::Resume()
{
}

// update the state
void GameState_InGame::Update()
{
}

// render the state
void GameState_InGame::Render()
{
}

// cleanup after the state
bool GameState_InGame::Shutdown()
{
return true;
}


Good news! thats all the code for the framework, for now at least.  ;D
I'll post explanations very soon.

Richard Marks:
Downloads
Whole Tutorial Source in 7z formattutorial1.cppGame.hGame.cppGameState.hGameState.cppGameState_Ingame.hGameState_Ingame.cpp

Navigation

[0] Message Index