Skip to main content
gamedev.net
🔒 Locked

OO basecode.

Started by DarkNebula May 11, 2004 at 7:28 PM 6 replies 500+ views
Original Post
DarkNebula
DarkNebula
How would I add a different Draw function for different states of the game using function pointers. I know it makes Draw in WINMAIN point to the right draw function when you init it, but I want something that can change it on the fly, without exiting the main loop. Any ideas on how I can do this? - DarkNebula Galatron.net - The massively multiplayer real time strategy!
Archi
Archi
Yep. I did it that way:

smth like
extern BOOL	(*drawFrame)(); 

somewhere in "main.h"
and
BOOL mainMenuDrawFrame(); 

somewhere in "mainmenu.h"


then somewhere in "mainmenu.cpp"
drawFrame = mainMenuDrawFrame; 

and/or
drawFrame = NULL; 


finally in main loop
            if (g_bAppIsActive)
{
if (drawFrame!=NULL)
drawFrame();
else
UpdateFrame();
}
Archi
Archi
Hope that would be enough. Still now I suppose that''s not right style of coding...
Crispy
Crispy
Incidentally, this is called a fuction pointer and its proper use would look something like this:


typedef BOOL (*FUNC_DrawFrame)(int arg);

BOOL Scene1(int arg)
{
}

BOOL Scene2(int arg)
{
}

int main(...)
{
FUNC_DrawFrame FrameFunc = Scene1;

int i = 0;

while(i)
{
FrameFunc = (something < something_else) ? Scene1 : Scene2;

FrameFunc(i); //call Scene1() if something is less than something else, call Scene2 otherwise, passing in an argument


i++;
}
}




Archi''s method isn''t wrong, but skips a step and is therefore prone to error.
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Archi
Archi
hehehe... that was NOT a METHOD, just a try to gather together some lines from the working code on the topic

I''m to lazy inventing something new... It''s MORE easier to dig into old sources

rodzilla
rodzilla
I''m using a Loop class that has methods like "init()", "draw()", "move(const float dt)", "handle_key_press(const int key)"... All those methods are virtual. The Loop class also has a "start()" method (non-virtual) that probably looks like your WINMAIN: an (nearly) infinite loop that calls the previously described methods.

For each scene, I create a subclass of this loop class.

To launch a scene, just create an instance and invoke the start method on it.

It might be handy to make sure you can "nest" scenes (make scene1 create and launch scene2 ; when scene2 exits, continue scene1). That just requires you stack scenes, take that into account when you calculate the frame time ("freeze" time for the invoking loop) and invoke the "init()" method whenever necessary. Useful for game menus for example ("main menu" launchs "config menu"...).


SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.
The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
DarkNebula
DarkNebula
quote:
Original post by Archi
Yep. I did it that way:

smth like
extern BOOL	(*drawFrame)();  

somewhere in "main.h"
and
BOOL mainMenuDrawFrame();  

somewhere in "mainmenu.h"


then somewhere in "mainmenu.cpp"
drawFrame = mainMenuDrawFrame;  

and/or
drawFrame = NULL;  


finally in main loop
            if (g_bAppIsActive)
{
if (drawFrame!=NULL)
drawFrame();
else
UpdateFrame();
}




Well I was thinking of an OO approch since this is an OO basecode... I think I''ll try something like rodzilla, but I need to see the code to understand what you were talking about. How does start change the function pointers or the point to another virtual?

- DarkNebula
Galatron.net - The massively multiplayer real time strategy!
rodzilla
rodzilla
quote:
Original post by DarkNebula
Well I was thinking of an OO approch since this is an OO basecode... I think I''ll try something like rodzilla, but I need to see the code to understand what you were talking about. How does start change the function pointers or the point to another virtual?



Hmmm you can take a look at the sources; click on the sam3d link below if you don''t mind using CVS, or download the "q3view" demo in the NeHe download section (note that "nested loops" are not implemented in this code).
The "start()" function doesn''t change pointers ; it''s the loop itself and is non-virtual and implemented in the base Loop class.

It does approximately the following :

while (true) {
// process events (keyboard, mouse)
// read the code for more details.
draw(); // that''s a virtual method. Re-define in all subclasses.
move(dt); // that''s a virtual method. Re-define in all subclasses.
}

This way, if you define a "Scene1 : public Loop" class (and redefine "draw()", "move(dt)"...), and run "scene1.start()", you''ll have your newly-defined loop running: virtual methods replace pointer-to-functions in this approch (hmmm virtual methods *are* pointer-to-functions anyway).


SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.
The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)
SaM3d!, a cross-platform API for 3d based on SDL and OpenGL.The trouble is that things never get better, they just stay the same, only more so. -- (Terry Pratchett, Eric)

Topic Locked

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

Sign in to reply to this topic.