Skip to main content
gamedev.net
🔒 Locked

easy question

Started by wazoo69 3 replies 200+ views
Original Post
wazoo69
wazoo69
Okay this one sounds ridiculous, but what''s the best way to manage your main game loop?? Right now I''ve got a pretty ugly looking batch of switch statements, and I''m wondering what the better way is... thanks,
Abstraction
Abstraction
I kinda like to use function pointers. Figure out when the the state of your game changes and then just assing the function ptr. That does get a bit tricky sometimes. There's an article somewhere on this site about creating multiple threads for you game. You might want to consider that also.

EDIT: typo

Edited by - Abstraction on December 3, 2001 2:36:23 PM
henryx
henryx
I agree with the Abstraction that function pointers combined with a state machine give an efficient and tight game loop. An simple example is given below. In each state all you need to do is figure out what state you need to be in next time round the game loop and change (or not as the case may be) the DoState function pointer as approptiate.

henry


//main program
void main(void)
{

InitGfx(); //Initialise PSX graphics
InitPads(); //Initialise the controller pads
FntID = LoadFont(); //Load debug fonts into VRAM
LoadGFX(); //Load game graphics into VRAM

GameState = RUNNING; //set the initial game state
DoState = Intro; //set the initial DoState pointer

do //game loop
{

GetUserInput(); //Get input from controllers
DoState(); //Execute game state function

}while(GameState != FINISH);

ResetGraph(3);
}

HenryLecturer in Computer Games TechnologyUniversity of Abertay DundeeScotlandUK
DrunkenHyena
DrunkenHyena
G''day!

I define an interface called IGameState and then subclass it for the various game states that I need. Basically an OOP spin on the function pointer method. I used it in my Doodle Invaders tutorial on my site. Full source, if you''re interested.


Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena</FON

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.