Create a Game Engine: Part IV Scene Manager

We need a place where we can load our shaders, load and render our game models (just triangle for now) and also listen to notifications from GLUT methods which were presented in the previous tutorial. This place is called SceneManager and it will evolve in future tutorials.

So what we have to do is to tie up this SceneManager with Init_GLUT to listen to rendering notifications. Now, passing SceneManager to Init_GLUT as a parameter is a bad practice because it creates an ugly dependency (maybe in the future we will want other scenes and/or other object to be notified). So it’s much better if we inject an interface in Init_GLUT and SceneManager implements this interface. Note that we trade some efficiency for design here.

Basic Game Manager Architecture. Scene Manager and Listener

First we have to create the listener interface. Let’s call it IListener (IListener.h) and place it in the Init folder:

We have 3 methods: notifyBeginFrame() , notifyDisplayFrame(), notifyEndFrame() which are called in the displayCallback() method from Init_GLUT class. The main reason we are splitting displayCallback() method is to ensure that later on we will have a good flow in our rendering loop. Involving some expensive CPU processes right in the middle of the rendering loop can have a huge impact over the performance. The GPU is waiting for your draw commands and you interrupting it with some CPU stuff.

You get the idea. So that’s why it’s inportat to have a good flow and it’s better to separate CPU processing from GPU rendering process. For example we can solve physics and collision at the beginning of each frame in notifyBeginFrame() and after that we can draw everything in notifyDisplayFrame() and do other stuff in notifyEndFrame(). This is just for a single thread scenario.

As you can see we need a holder for our listener and a holder for our windowInfo to access previous width and height in case we need them in the future. Also we need a method to set the listener. They are static because our callbacks are also static.

Now finally we create the Scene_Manager class. First we create the Scene_Manager.h file in the Managers folder.

I have been working as a software engineer in computer graphics for almost 5 years. In the free time I love playing guitar, playing video games, hiking and studying computer graphics techniques.Follow me on : Google + , Twitter, Facebook