Code organization

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Code organization

Post by 3DModelerMan »

I want to have an IGameManager class that the declaration for looks like this.

Code: Select all

class IGameManager
{
 public:
 //constructor and stuff
 void enterMainMenu();//Calls splash screen and other private functions in it.
 void enterGame(int levelNumber);//Calls the first level function and others.
private:
//Date and stuff

void splashScreen();//splash screen function
//menus etc. etc.
void levelOne();
//More level functions below this.

}
Is this a good way to organize code? Should the seperate levels be different member functions. And make it so that an IGameManager can manage the classes like, IEntity, and IPlayerCharacter? Thanks for any help :D .
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
MasterM
Posts: 128
Joined: Sat Oct 20, 2007 2:38 am
Location: netherlands antilles, Curacao

Post by MasterM »

When i orginize my stuff i make a class for menu,levels,etc,etc
But dont listen from me cause i am a rooky anyway :wink:
C++ is not the Magdalena...it takes patience...shes like the well aged prostitute, it takes years to learn her tricks! she is cruel...laughs at you when you are naked...
Life of a programmer == Const abuse
Manawenuz
Posts: 22
Joined: Wed Jun 10, 2009 12:42 am
Location: France

Post by Manawenuz »

I do agree with MasterM. To define a level, try something like XML files, or some external files like that (even Doom levels were external WAD files).
The reason why it is better is that:
-it forces you to right a more general game engine
-you can (in a more or less far future) write a level editor which won't have to modify the source code and recompile it
-your code is clearer

But anyway, you're the master of your project!!!
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Yeah

Post by 3DModelerMan »

Yeah there's alot of ways. How does microsoft do it?
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
twilight17
Posts: 362
Joined: Sun Dec 16, 2007 9:25 pm

Re: Yeah

Post by twilight17 »

3DModelerMan wrote:How does microsoft do it?
LOL! probably in the worst possible way :wink:
Post this userbar I made on other websites to show your support for Irrlicht!
Image
http://img147.imageshack.us/img147/1261 ... wernq4.png
Brainsaw
Posts: 1241
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I don't think that MS-Code is so bad. They are even trying to learn, e.g. they support GCC development (a guy in my company works on GCC and they DO get support from MS ... funny thing ... maybe the management doesn't know what the developers are doing ;) ).

back to topic:

you should try to make your game states more general, e.g. by using a finite state machine (FSM on wikipedia: http://en.wikipedia.org/wiki/Finite-state_machine ... or maybe obey to this: http://en.wikipedia.org/wiki/Flying_Spaghetti_Monster which is also a "FSM"). I do it like this:

Code: Select all


//a baseclass for all gamestates
class gameState {
  public:
    virtual void activate()=0;
    virtual void deactivate()=0;
    virtual int update()=0;
};

//a state for menu
class menuState : public gameState {
  public:
    virtual void activate() {
      //do some stuff you need when the state is activated, 
      //e.g. set an EventReceiver and load a scene
    }

    virtual void deactivate() {
     //do some stuff you need when deactivating, e.g. clear the scene
    }

    int void update() {
       //this method is called for every frame
       //returns "0" if no state change should happen, the index of
      //the new state otherwise
    }
}

class playState() : public gameState {
  //something for this state
}

int main(void) {
  [...]
  array<gameState *> aStates;
  aStates.push_back(new menuState());
  aStates.push_back(new playState());
  gameState *activeState=aStates[0];
  [...]
  while (device->run && iRet<aStates.size()) {
    int iRet=activeState->update();
    //if we get something!=0 we switch to another state, if
    //the new index is out of bounds we know the app wants to exit
    if (iRet!=0 && iRet<aStates.size()) {
      activeState->deactivate();
      activeState=aStates[iRet];
      activeState->activate();
    }
    //do the usual Irrlicht stuff here
    [...]
  }
}
I hope this helps. This way you can easily add and remove states of the game, e.g. if you want a menu item to dislay the HighScore table or you want to add an intro or an outro.
Image
Dustbin::Games on the web: https://www.dustbin-online.de/
Ulf
Posts: 281
Joined: Mon Jun 15, 2009 8:53 am
Location: Australia

Post by Ulf »

My simple idea for a game engine so far is this:

Code: Select all

// Enter the main message loop
while(_pIrrDevice->run())
{
	// Make sure the game engine isn't sleeping
	if (!_pGameEngine->Sleep())
	{
		// Check the tick count to see if a game cycle has elapsed
		iTickCount = _pTimer->getTime();
		if (iTickCount > iTickTrigger)
		{
			iTickTrigger = iTickCount + _pGameEngine->FrameDelay();
			
			//HandleKeys();
			pGameEngine->HandleJoystick();
			GameCycle();

			_pVideoDriver->beginScene(true, true, irr::video::SColor(255,100,101,140));
			// Call game paint function	
			GamePaint();
			_pVideoDriver->endScene();
		}
	}
}
The GameCycle, HandleJoystick and HandleKeys functions are game specific.

The GameCycle is game specific, it takes care of state management and could use something like Brainsaw's example game state manager.
You'd have to manage game states uniquely for every game anyway.

Obviously need to tie in an event manager with this
In the event manager collect keyboard, joystick and mouse events and call the game specific event handlers which will also include:
MouseDown(), MouseUp(), MouseMove(), MouseWheel() functions.

Calling the game specific Handle Keyboard function can just be handled automatically in the event manager as I am doing at the moment (it's commented out of the loop)

Code: Select all

//HandleKeys(); 
Same as the Mouse is handled.
Last edited by Ulf on Fri Jul 03, 2009 6:37 pm, edited 2 times in total.
I can hear birds chirping
:twisted:

I live in the Eye of Insanity.
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Thanks

Post by 3DModelerMan »

Thanks for the help. I've got it figured out now.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
Katsankat
Posts: 178
Joined: Sun Mar 12, 2006 4:15 am
Contact:

Post by Katsankat »

Some open-sourcinspiration : pass the device pointer to different functions. Each function has its own run loop (use break; to get out) and returns a value eg. return 0 means "close program", 1->"load map" 2->"display menu" etc... This method avoids checks in the render loop.
Post Reply