Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: how to Program object oriented???!!!  (Read 12090 times)

0 Members and 1 Guest are viewing this topic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
how to Program object oriented???!!!
« Reply #15 on: February 18, 2011, 03:07:48 pm »
A simple example of different screens/menus/surfaces:
Code: [Select]
class Surface
{
virtual ~Surface();
virtual void Run() = 0;
};

class MainMenu : public Surface
{
virtual void Run();
};

class Game : public Surface
{
virtual void Run();
};

// In your main game loop
while (inLoop)
{
surface->Run();
}

Transitions from one to the other surface can be realized through the return value of Run(). A possibility is an enum and a std::map that maps the enum to the different surfaces. (In fact, boost::ptr_map might be more appropriate, but the principle remains the same.)
Code: [Select]
enum SurfaceEnum {InGame, InMainMenu, Exit, ...};

std::map<SurfaceEnum, Surface*> map;
map[InGame]     = new Game;
map[InMainMenu] = new MainMenu;
// delete everything in the end

Then, the main loop can look like this:
Code: [Select]
SurfaceEnum next = InMainMenu;
while (next != Exit)
{
surface = map[next];
next = surface->Run();
}

That's just one of many possibilities :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything