Tutorial 01b : Adding a Frame Limiter Mechanism
Complete source file : tut01b.cc
This application is a slight modification from the previous one :
#include <VMain.h> static CVideoEnv csVideoEnv; static C3DEnv cs3DEnv; static CAudioEnv &csAudioEnv = CAudioEnv::AutoDetect(); int main() { // Local variables CFPSControl csFPSCtrl; // For FPS control char acBuff[PATH_MAX]; // Processing string buffer
Then initialize the video and audio environments like in the previous tutorial :
// Initialize video csVideoEnv.CreateMainWindow(640, 480, "Tutorial 1b : Initializing the System with FPS Limiter"); csVideoEnv.CreateRenderImage(640, 480); csVideoEnv.CreateRenderBuffer(640, 480); cs3DEnv.Initialize(&csVideoEnv); csVideoEnv.SetForegroundColor(csVideoEnv.CreateColor(255, 192, 255)); // Initialize audio csAudioEnv.QueryDeviceName(acBuff, sizeof(acBuff)); csAudioEnv.OpenDevice(acBuff); csAudioEnv.InitializeDevice(); csAudioEnv.ActivateDevice();
The main loop now looks like this :
csVideoEnv.AutoRepeatOff(); csFPSCtrl.SaveStartTicks(); while(1) { // Frame rate control if(csFPSCtrl.PumpEventAndDelay(csVideoEnv)) continue; if(csFPSCtrl.AdjustDelay()) sprintf(acBuff, "%05.1f fps, %06d delays", csFPSCtrl.GetFPS(), csFPSCtrl.GetDelayFactor()); // Update everything (the application is active) if(csVideoEnv.IsActive()) { // Check event if(csVideoEnv.CheckEvent()) { // Keyboard event if(csVideoEnv.EventKeyPress()) VPrint("KeyPress : %05u (%05lu))\n", csVideoEnv.GetKeySym(), csVideoEnv.GetRawKeyCode()); else if(csVideoEnv.EventKeyRelease()) VPrint("KeyRelease : %05u (%05lu)\n", csVideoEnv.GetKeySym(), csVideoEnv.GetRawKeyCode()); // Mouse event else if(csVideoEnv.EventButtonPress()) VPrint("BtnPress : %d\n", csVideoEnv.GetButtonSym()); else if(csVideoEnv.EventButtonRelease()) { VPrint("BtnRelease : %d\n", csVideoEnv.GetButtonSym()); if(csVideoEnv.GetButtonSym() == VBS_MB1) break; } // Other event else if(csVideoEnv.EventCloseWindow()) break; } // Don't update till the delay counter reach zero if(csFPSCtrl.GetDelayCounter()) continue; // Update display cs3DEnv.ClearRenderBuffer(V_RGB(12, 12, 16)); cs3DEnv.ClearZBuffer(); csVideoEnv.PutRenderImageOnDisplay(); csVideoEnv.DrawText(10, 20, acBuff); csVideoEnv.DrawText(10, 50, "Click the left mouse button to exit ..."); csVideoEnv.UpdateDisplay(); // Update sound csAudioEnv.UpdateBuffer(); } // Update everything (the application is inactive) else { while(csVideoEnv.IsInactive()) { // Update display (only of the application window is visible) if(csVideoEnv.IsVisible()) { csVideoEnv.PutRenderImageOnDisplay(); csVideoEnv.UseDefaultBigFont(); csVideoEnv.DrawText(0, 150, "Inactive State", ETA_CenterScreen); csVideoEnv.UseDefaultSmallFont(); csVideoEnv.UpdateDisplay(); } // Pump event csVideoEnv.DispatchEvent(); usleep(100000); } } } csFPSCtrl.SaveEndTicks();
And finally, the end of main() can be rewritten like :
// Deactivate audio csAudioEnv.DeactivateDevice(); // Calculate & display the avegare frame rate sprintf(acBuff, "Average frame rate = %.2f fps\n\nClick \"OK\" to exit ...", csFPSCtrl.GetAvgFPS()); csVideoEnv.MessageBox("End Program", acBuff); VInfo(_N_, _F_, _L_, "Program ended, average frame rate = %.2f FPS", csFPSCtrl.GetAvgFPS()); // OK ! return(0); }