Path: chuka.playstation.co.uk!news From: "Andrew Murray" Newsgroups: scee.yaroze.freetalk.english Subject: Re: Main Game Loop Technique Date: Wed, 16 Apr 2003 17:48:53 +0100 Organization: PlayStation Net Yaroze (SCEE) Lines: 119 Message-ID: References: NNTP-Posting-Host: vpn102.tay.ac.uk X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 Function pointers are actaully faster that using case statements, both from a "having to type it in" perspective and performance wise. I have my program set up like so: int (*DoState)(Graphics *gfx); // ... stuff ... void main(void) { // set initial program state DoState = DoTitle; // ... stuff ... while(DoState != NULL) { if(!(DoState(&gfx)) DoState = NULL; } ResetGraph(0); } Then in another C file... int DoTitle(Graphics *gfx) { // watch for start being pressed if(padStatus.press & PadStart) DoState = DoGame; } Then in yout testing for program termination, set DoState to NULL if the user has requested quit. DoState is declared in main.c and extern'ed in main.h so that the appropriate C files has access to it. Easy peasy and nice and managable, plus it allows nice looking code, my opinion is that switch statements are ugly, although in some situations their use is unavoidable. Andy. "Peter Armstrong" wrote in message news:agsiuk$d9k29@www.netyaroze-europe.com... > I've used pointers to functions for that sort of thing. I'd have an array of > function addresses and use the return value from the function call as the > array index. > > As an example: > > // functions to call > int (*functions[5])(void) = {NULL, TitleScrn, NewGame, > PlayerSelect,GameOptions}; > . > . > . > . > void CallFunction(void) > { > int index = 1; // init index > int (*FuncCall)(void); > > while(index) > { > FuncCall = functions[index]; // get function address > > index = FuncCall(); // call function > } > } > > This method may well be less efficient than switch/case statements, I just > prefer to keep the in-function code compact, plus since I started using > function pointers I can't seem to stop. ;) > > Peter > > "Rikki Prince" wrote in message > news:agrf9f$d9k24@www.netyaroze-europe.com... > > Before I would do the following: > > main loop { > > Prepare screen drawing stuff > > if(TITLE_SCREEN) { > > GsSort Title Screen stuff > > } > > elseif(GAME_SCREEN) { > > GsSort Game stuff > > } > > etc > > > > if(ONE_PLAYER) { > > Proccess for 1 player > > } > > elseif(TWO_PLAYER) { > > Process for 2 player > > } > > etc > > > > } end of loop > > > > However, I'm wondering whether it's better to get rid of these checks > > every loop by checking what screen it is first, then going into a > > special loop for that screen, and doing similar for the number of > > players. Do those checks cause much of a performance hit - is it worth > > worrying about? Or is it better to keep the amount of code down (due > > to the 2MB limit), rather than duplicating it? I suppose I could use > > pointers to functions, so the main loop would be the same code, but > > somewhere else changes which function is pointed to, which does the > > specific proccessing. Could this be a good method? > > > > Cheers, > > Rikki > > > > > >