Tutorial 01a : Initializing the System


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

Complete source file : tut01a.cc


To start creating an application using VortexGE, first include the VortexGE header file :

#include <VMain.h>

Next, create some instances of the VortexGE main classes :

static CVideoEnv csVideoEnv;
static C3DEnv    cs3DEnv;
static CAudioEnv &csAudioEnv = CAudioEnv::AutoDetect();
Here the audio system to be used is auto detected. One can actually use CAlsaAudioEnv::Instance(), COssAudioEnv::Instance() or CDummyAudioEnv::Instance() directly, however, it is not recommended.

After that, write the main() function :

int main()
{
  uint64_t un64Frames, un64Ticks1, un64Ticks2;
  float    fFPS = 0, fOldFPS = 1;
  char     acBuff[PATH_MAX];

The first step in main() is to initialize the video environment :

  csVideoEnv.CreateMainWindow(640, 480, "Tutorial 1a : Initializing the System");
  csVideoEnv.CreateRenderImage(640, 480);
  csVideoEnv.CreateRenderBuffer(640, 480);
  cs3DEnv.Initialize(&csVideoEnv);
  csVideoEnv.SetForegroundColor(csVideoEnv.CreateColor(255, 192, 255));

Then the audio environment :

  csAudioEnv.QueryDeviceName(acBuff, sizeof(acBuff));
  csAudioEnv.OpenDevice(acBuff);
  csAudioEnv.InitializeDevice();
  csAudioEnv.ActivateDevice();

The main loop can be written like this :

  csVideoEnv.AutoRepeatOff();
  un64Frames = 0;
  un64Ticks1 = cs3DEnv.GetTicks();
  while(1) {
    // Calculate FPS
    fFPS = cs3DEnv.CalcFPS();
    if(fOldFPS != fFPS) {
      fOldFPS = fFPS;
      sprintf(acBuff, "%05.1f fps", fFPS);
    }
    un64Frames++;

    // Update everything if the application is active
    if(csVideoEnv.IsActive()) {
      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();

      // Pump event
      csVideoEnv.DispatchEvent();
      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;
      }
    }
    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);
      }
    }
  }
  un64Ticks2 = cs3DEnv.GetTicks();

Before exiting, deactivate the audio environment (this will stop all playing sound buffers) :

csAudioEnv.DeactivateDevice();

One can display the average frame rate using the CVideoEnv::MessageBox() function :

  sprintf(acBuff, "Average frame rate = %.2f fps\n\nClick \"OK\" to exit ...",
          cs3DEnv.CalcAvgFPS(un64Frames, un64Ticks1, un64Ticks2));
  csVideoEnv.MessageBox("End Program", acBuff);
  VInfo(_N_, _F_, _L_, "Program ended, average frame rate = %.2f FPS",
        cs3DEnv.CalcAvgFPS(un64Frames, un64Ticks1, un64Ticks2));

End of main() :

  // OK !
  return(0);
}


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