For example, let's say that I have two classes:
class MyWorld
{
public:
MyWorld();
void setup();
void draw();
//
};
...
class OtherWorld
{
public:
OtherWorld();
void setup();
void draw();
//
};
Then, I'd like to have a "TargetWorld" class, where for example:
Then, I'd like to be able to call
targetWorld.draw();
targetWorld.setup();
And that would call the initial class's method. I was thinking about using templates or class inheritance and virtual methods, but I'm not sure if that's the right path.
int main(){
TargetWorld *tw = new MainMenuWorld();
tw->setup();
return 0;
}
Name:
Anonymous2011-11-18 18:11
>>9
Thank you! That's just what I was looking for, works great. Is this faster than having lots of switch statements to determine render, input, and drawing? Before, it looked like:
Input thread:
switch (CurrentWorld)
{
case Worlds::MainMenu:
mainMenuWorld.input(event);
break;
case Worlds::Level:
levelWorld.input(event);
break;
}
Main game loop:
//gameTime computation
{
switch (CurrentWorld)
{
case Worlds::MainMenu:
mainMenuWorld.update();
break;
case Worlds::Level:
levelWorld.update();
break;
}
}
//Drawing logic:
switch (CurrentWorld)
{
case Worlds::MainMenu:
mainMenuWorld.draw();
break;
case Worlds::Level:
levelWorld.draw();
break;
}
I assume using the virtual methods are a lot faster then having many switch statements.