Advertisement
Ember

minimum

Jan 2nd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. /*
  2. -----------------------------------------------------------------------------
  3. Filename: Main.h
  4. -----------------------------------------------------------------------------
  5.  
  6. -----------------------------------------------------------------------------
  7. */
  8.  
  9. // Declare Once
  10. #pragma once
  11. // Engine
  12. #include <R3D\R3D.hpp>
  13.  
  14. // I am going to regret this (I'm stabbing myself as I type)
  15. using namespace R3D;
  16.  
  17. // Class definition -------------------------------------------------------------------
  18. class Game
  19. {
  20. public:
  21.     // Constructors
  22.     Game() = default;
  23.     Game(const Game& other) = delete;
  24.     ~Game() = default;
  25.  
  26.     // Primary application functions
  27.     bool Initialize();
  28.     void Shutdown();
  29.     void Run();
  30.  
  31. private:
  32.     // Private methods
  33.     bool Frame();
  34.     bool Render();
  35.  
  36.     // Modules
  37.     Window mWindow;
  38.  
  39.     // Variables
  40.     Double mFrameTime;
  41. };
  42.  
  43. // Application initialization ---------------------------------------------------------
  44. bool Game::Initialize()
  45. {
  46.     Window::Description windowDesc;
  47.     bool result;
  48.  
  49.     // Initialize R3D
  50.     R3D::Initialize();
  51.  
  52.     // Set the frame time
  53.     mFrameTime = floor(1000 / 60); // 60 FPS target
  54.  
  55.     // Setup the window
  56.     windowDesc.Width = 960;
  57.     windowDesc.Height = 640;
  58.     windowDesc.Flags.Format = Texture::Format::RGBA8UNorm;
  59.  
  60.     // Create a window and begin initialization
  61.     result = mWindow.Create(windowDesc);
  62.     if(!result) { return false; }
  63.  
  64.     // Load some resources
  65.  
  66.     // Setup the scene
  67.  
  68.     return true;
  69. }
  70.  
  71. // Application runtime ----------------------------------------------------------------
  72. void Game::Run()
  73. {
  74.     LARGE_INTEGER li;
  75.     Double frequency;
  76.     Double time;
  77.     bool done;
  78.  
  79.     // Get the high-resolution timer frequency
  80.     if(FAILED(QueryPerformanceFrequency(&li))) { return; }
  81.  
  82.     // Set the timer frequency to milliseconds
  83.     frequency = (double)li.QuadPart / 1000.0;
  84.  
  85.     // Loop until the engine wants to quit
  86.     done = false;
  87.     while(!done)
  88.     {
  89.         // Reset the timer
  90.         QueryPerformanceCounter(&li);
  91.         time = li.QuadPart;
  92.  
  93.         // Do frame processing
  94.         bool result = Frame();
  95.         if(!result) { done = true; }
  96.  
  97.         // Get the elasped time
  98.         QueryPerformanceCounter(&li);
  99.         time = li.QuadPart - time;
  100.         time /= frequency;
  101.  
  102.         // Sleep
  103.         Sleep(max(mFrameTime - time, 0));
  104.     }
  105.  
  106.     return;
  107. }
  108.  
  109. // Application frame processing -------------------------------------------------------
  110. bool Game::Frame()
  111. {
  112.     bool result;
  113.  
  114.     // Update the engine state
  115.     result = R3D::Update();
  116.     if(!result) { return false; }
  117.  
  118.     // Render the current frame
  119.  
  120.     // <insert a comment here>
  121.     mWindow.Clear(0.1f, 0.1f, 0.1f, 1.0f);
  122.  
  123.     // Prepare the scene
  124.  
  125.     // Present our back buffer to our front buffer
  126.     mWindow.Finish();
  127.  
  128.     return true;
  129. }
  130.  
  131. // Application shutdown ---------------------------------------------------------------
  132. void Game::Shutdown()
  133. {
  134.     // Shut down the engine
  135.     R3D::Shutdown();
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement