Tutorial 01c : Using the CEventHandlerExt Class


Back to Main Index Back to Tutorials and References : Main Page

Complete source file : tut01c.cc


Include the VortexGE header file as always :

#include <VMain.h>

Then, derive our specialized event handler class :

class MyEventHandler : public CEventHandlerExt {
  private:
    // Variables
    CVideoEnv  _csVideoEnv;
    C3DEnv     _cs3DEnv;
    CAudioEnv &_csAudioEnv;

  public:
    // Constructor
    MyEventHandler();
    // Reimplement only the needed event handlers
    virtual bool EventMainLoopEnter();
    virtual void EventButtonRelease(EBtnSymbol eButtonSym);
    virtual void EventUpdateFrame(bool bWindowIsActive);
};
The class is derived it from CEventHandlerExt because we want FPS control. If FPS control is not needed, just derive from CEventHandler.

The function main() is now much more simple (compared with the previous tutorial which use the low level API):

int main()
{
  // Local variables
  MyEventHandler csMyEH;

  // Invoke the default main loop
  int32_t n32AvgFPS = csMyEH.DoMainLoop();
  if(n32AvgFPS < 0) return(-1);

  // Exiting
  csMyEH.HookedVideoEnv()->MessageBox("End Program", "Click \"OK\" to exit ...");
  VInfo(_N_, _F_, _L_, "Program ended, average frame rate = %.2f FPS",
        float(n32AvgFPS) / 100.0f);

  // OK !
  return(0);
}

Define the constructor :

MyEventHandler::MyEventHandler()
: CEventHandlerExt(&_csVideoEnv), _csAudioEnv(CAudioEnv::AutoDetect())
{
}
Note that the base class, CEventHandlerExt should be initialized that way. Here the audio system to be used is also auto detected. One can always use COssAudioEnv::Instance(), COssAudioEnv::Instance() or CDummyAudioEnv::Instance() directly if necessary, however, it is not recommended.

Note that the wanted FPS can be also specified in the constructor of CEventHandlerExt. For example, the above codes can be changed to something like :

MyEventHandler::MyEventHandler()
: CEventHandlerExt(&_csVideoEnv, 50.0f), _csAudioEnv(CAudioEnv::AutoDetect())
{
}
Using these codes, the FPS will be maintained at about 50.0 fps, the default parameter is 30.0 fps.

Handle main loop enter event (we will use this as the initialization point just before the main loop really runs) :

bool MyEventHandler::EventMainLoopEnter()
{
  // Initialize system
  _csAudioEnv.DoQuickInit();
  _csVideoEnv.DoQuickInit("Tutorial 1c : Using the CEventHandlerExt Class", 640, 480);
  _cs3DEnv.Initialize(&_csVideoEnv);
  _csVideoEnv.SetForegroundColor(_csVideoEnv.CreateColor(255, 192, 255));
  return(true);
}
See that DoQuickInit()s ? With those, initialization will be much easier but will not as flexible at they was (in the previous tutorials). Note that EventMainLoopEnter() must return true or the DoMainLoop() implementation which is called from main() will abort.

Handle mouse button release events :

void MyEventHandler::EventButtonRelease(EBtnSymbol eButtonSym)
{
  if(eButtonSym == VBS_MB1) EventCloseWindow();
}
Here, if we click mouse button 1, the program will exits (the default EventCloseWindow() implementation will order the default DoMainLoop() implementation to exit/end the application)

Now, this is our rendering (updating) handler (it is also simpler now) :

void MyEventHandler::EventUpdateFrame(bool bWindowIsActive)
{
  // Update everything if the window is active (has focus)
  if(bWindowIsActive) {
    // Update audio
    _csAudioEnv.UpdateBuffer();
    // Update video
    _cs3DEnv.ClearRenderBuffer(V_RGB(12, 12, 16));
    _cs3DEnv.ClearZBuffer();
    _csVideoEnv.PutRenderImageOnDisplay();
    _csVideoEnv.DrawText(10, 20, GetFPSInfoString());
    _csVideoEnv.DrawText(10, 50, "Click the left mouse button to exit ...");
    _csVideoEnv.UpdateDisplay();
  }
  // Update everything if the window is inactive
  else {
    _csVideoEnv.PutRenderImageOnDisplay();
    _csVideoEnv.UseDefaultBigFont();
    _csVideoEnv.DrawText(0, 150, "Inactive State", ETA_CenterScreen);
    _csVideoEnv.UseDefaultSmallFont();
    _csVideoEnv.UpdateDisplay();
  }
}
It is recommended to not use FPS lower than 30.0 fps if the EventUpdateFrame() member is also going to be used for updating audio frame


Back to Tutorials and References : Main Page Back to Main Index