Advertisement
Ember

main

Jan 2nd, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 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.     //Scene mScene;
  39.     //Scene::Node mNode;
  40.  
  41.     // Structures
  42.     struct WindowBuffer { Float4x4 ViewProjectionMatrix; };
  43.     struct NodeBuffer { Float4x3 WorldMatrix; };
  44.     struct InstanceBuffer { Float4x3 InstanceMatrix; };
  45.     //Buffer WindowBuffer;
  46.  
  47.     // Objects
  48.     Camera mCamera;
  49.  
  50.     // Variables
  51.     Double mFrameTime;
  52. };
  53.  
  54. // DEBUG / TEMPORARY
  55. #define isAligned(POINTER, BYTE_COUNT) (((uintptr_t)(const void *)(POINTER)) % (BYTE_COUNT) == 0)
  56. #define Aligned __declspec(align(16))
  57.  
  58. // Application initialization ---------------------------------------------------------
  59. bool Game::Initialize()
  60. {
  61.     Window::Description windowDesc;
  62.     bool result;
  63.  
  64.     // Initialize R3D
  65.     R3D::Initialize();
  66.  
  67.     // Set the frame time
  68.     mFrameTime = floor(1000 / 60); // 60 FPS target
  69.  
  70.     // Setup the window
  71.     windowDesc.Width = 960;
  72.     windowDesc.Height = 640;
  73.     windowDesc.Flags.Format = Texture::Format::RGBA8UNorm;
  74.  
  75.     // Create a window and begin initialization
  76.     result = mWindow.Create(windowDesc);
  77.     if(!result) { return false; }
  78.  
  79.     // Load some resources
  80.  
  81.     // Setup the scene
  82.     //mScene.Initialize();
  83.     //mNode = mScene.CreateNode();
  84.     //mCamera.Translate(0.0f, -64.0, 0.0f);
  85.     //mCamera.LookAt(0.0f, 0.0f, 0.0f);
  86.  
  87.     // DEBUG
  88.     //Log::Singleton->Write("Sizeof(Float2) = " + string(sizeof(Float2)) + ".");
  89.     //Log::Singleton->Write("Sizeof(Float2A) = " + string(sizeof(Float2A)) + ".");
  90.     //Log::Singleton->Write("Sizeof(Float3) = " + string(sizeof(Float3)) + ".");
  91.     //Log::Singleton->Write("Sizeof(Float3A) = " + string(sizeof(Float3A)) + ".");
  92.     Movable unaligned;
  93.     Log::Singleton->Write("Sizeof(Movable) = " + string(sizeof(unaligned)) + ".");
  94.     Log::Singleton->Write("isAligned(Movable) = " + string(isAligned(&unaligned, 16)) + ".");
  95.  
  96.     Log::Singleton->Write("Sizeof(Forward) = " + string(sizeof(unaligned.Forward)) + ".");
  97.     Log::Singleton->Write("isAligned(Forward) = " + string(isAligned(&unaligned.Forward, 16)) + ".");
  98.  
  99.     Log::Singleton->Write("Sizeof(Right) = " + string(sizeof(unaligned.Right)) + ".");
  100.     Log::Singleton->Write("isAligned(Right) = " + string(isAligned(&unaligned.Right, 16)) + ".");
  101.  
  102.     Log::Singleton->Write("Sizeof(Up) = " + string(sizeof(unaligned.Up)) + ".");
  103.     Log::Singleton->Write("isAligned(Up) = " + string(isAligned(&unaligned.Up, 16)) + ".");
  104.  
  105.     Log::Singleton->Write("Sizeof(Position) = " + string(sizeof(unaligned.Position)) + ".");
  106.     Log::Singleton->Write("isAligned(Position) = " + string(isAligned(&unaligned.Position, 16)) + ".");
  107.  
  108.     return true;
  109. }
  110.  
  111. // Application runtime ----------------------------------------------------------------
  112. void Game::Run()
  113. {
  114.     LARGE_INTEGER li;
  115.     Double frequency;
  116.     Double time;
  117.     bool done;
  118.  
  119.     // Get the high-resolution timer frequency
  120.     if(FAILED(QueryPerformanceFrequency(&li))) { return; }
  121.  
  122.     // Set the timer frequency to milliseconds
  123.     frequency = (double)li.QuadPart / 1000.0;
  124.  
  125.     // Loop until the engine wants to quit
  126.     done = false;
  127.     while(!done)
  128.     {
  129.         // Reset the timer
  130.         QueryPerformanceCounter(&li);
  131.         time = li.QuadPart;
  132.  
  133.         // Do frame processing
  134.         bool result = Frame();
  135.         if(!result) { done = true; }
  136.  
  137.         // Get the elasped time
  138.         QueryPerformanceCounter(&li);
  139.         time = li.QuadPart - time;
  140.         time /= frequency;
  141.  
  142.         // Sleep
  143.         Sleep(max(mFrameTime - time, 0));
  144.     }
  145.  
  146.     return;
  147. }
  148.  
  149. // Application frame processing -------------------------------------------------------
  150. bool Game::Frame()
  151. {
  152.     bool result;
  153.  
  154.     // Update our time
  155.     static float t = 0.0f;
  156.     static ULONGLONG timeStart = 0;
  157.     ULONGLONG timeCur = GetTickCount64();
  158.     if(timeStart == 0) { timeStart = timeCur; }
  159.     t = (timeCur - timeStart) / 1000.0f;
  160.  
  161.     // Update the engine state
  162.     result = R3D::Update();
  163.     if(!result) { return false; }
  164.  
  165.     // Render the current frame
  166.  
  167.     // <insert a comment here>
  168.     mWindow.Clear(0.1f, 0.1f, 0.1f, 1.0f);
  169.  
  170.     // Prepare the scene
  171.     Matrix worldMatrix, viewMatrix, projectionMatrix;
  172.  
  173.     //worldMatrix = Math::RotationZ(-t);
  174.     //viewMatrix = mCamera.Transform();
  175.     projectionMatrix = mWindow.ProjectionMatrix;
  176.  
  177.     // Present our back buffer to our front buffer
  178.     mWindow.Finish();
  179.  
  180.     return true;
  181. }
  182.  
  183. // Application shutdown ---------------------------------------------------------------
  184. void Game::Shutdown()
  185. {
  186.     // Shut down the engine
  187.     R3D::Shutdown();
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement