Tutorial 01c : Using the CEventHandlerExt Class
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 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 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()) { }
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); }
Handle mouse button release events :
void MyEventHandler::EventButtonRelease(EBtnSymbol eButtonSym) { if(eButtonSym == VBS_MB1) EventCloseWindow(); }
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(); } }