iMackshun

DisplayManager.cpp

Jan 16th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #include "DisplayManager.h"
  2.  
  3. /*STATIC VARS*/
  4. static InputManager sInputManager;
  5.  
  6. void DisplayManager::SetScreen(BaseScreen* targetScreen){
  7.     //Reset the input states.
  8.     sInputManager.ResetControls();
  9.     //Call the hide method of the initial screen.
  10.     if (_activeScreen){
  11.         _activeScreen->Hide();
  12.     }
  13.     //Update the pointer to the screen.
  14.     _activeScreen = targetScreen;
  15.     //Call the show method.
  16.     _activeScreen->Show();
  17. }
  18.  
  19. //Begins the Game Loop.
  20. void DisplayManager::Begin()
  21. {
  22.     //Game Loop
  23.     do{
  24.         //Query controller state.
  25.         hidScanInput();
  26.         //Obtain and process the controller states.
  27.         sInputManager.onKeyDown(hidKeysDown());
  28.         sInputManager.onKeyUp(hidKeysUp());
  29.         //Obtain the state of the circle pad and update the axes.
  30.         hidCircleRead(&sInputManager.joystickPosition);
  31.         sInputManager.UpdateJoystick();
  32.         //Call the render method of the activeScreen, only if the screen pointer is valid.
  33.         if (_activeScreen){
  34.             //Add on to the dtAccumulator.
  35.             /*physicsDtAccumulator += _deltaTime;
  36.             //Ensure that the dtAccumulator doesn't get too high.
  37.             if (physicsDtAccumulator > 0.2f){
  38.                 physicsDtAccumulator = 0.2f;
  39.             }
  40.             //Update the physics engine.
  41.             while (physicsDtAccumulator > physicsDt){
  42.                 collisionManager.Step(physicsDt);
  43.                 physicsDtAccumulator -= physicsDt;
  44.             }*/
  45.             if (!_paused){
  46.                 _activeScreen->Debug();
  47.                 _activeScreen->Render(_deltaTime);
  48.             }
  49.             else{
  50.                 _activeScreen->DuringPause(_deltaTime);
  51.             }
  52.             CalculateFPS();
  53.         }
  54.        
  55.     } // Check if the ESC key was pressed or the window was closed
  56.     while (!_terminate && !(sInputManager.LHeld && sInputManager.RHeld && sInputManager.StartHeld));
  57.    
  58.     //Exit the application.
  59.     Terminate();
  60. }
  61.  
  62. void DisplayManager::CalculateFPS(){
  63.     //Used for repetition.
  64.     static int count = 0;
  65.     //Set the previous time to the currentTime of glfw.
  66.     static u64 previousTime = osGetTime();
  67.     //Record the difference in the time.
  68.     _deltaTime = (osGetTime() - previousTime)  / 1000.0f;
  69.     //Store the frameTime inside of the array.
  70.     _frameTimes[count] = _deltaTime;
  71.  
  72.     //Set the frametime again, outside of initialization.
  73.     previousTime = osGetTime();
  74.    
  75.     if (count < 9){
  76.         count++;
  77.     }
  78.     else{
  79.         _frameTimeCountReached = true;
  80.         count = 0;
  81.     }
  82.  
  83.     //Calculate the average if enough numbers have been recorded.
  84.     if (_frameTimeCountReached){
  85.         _averageFrameTime = 0;
  86.         for (int i = 0; i < 10; i++){
  87.             _averageFrameTime += _frameTimes[i];
  88.         }
  89.         _averageFrameTime /= 10.0f;
  90.         _frameRate = 1.0f / _averageFrameTime;
  91.     }
  92. }
  93.  
  94. //Initializes the video system, allowing data to be output to the screen.
  95. void DisplayManager::VideoInit()
  96. {
  97.     //Initialize the graphics system.
  98.     gfxInitDefault();
  99.     //Initialize the console for debugging.
  100.     consoleInit(GFX_BOTTOM, NULL);
  101. }
  102.  
  103. //Initializes the Citro3D graphics library.
  104. void DisplayManager::GraphicsInit()
  105. {
  106.     //Initialize Citro3D.
  107.     C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
  108.     //Create the render target.
  109.     topRenderTarget = C3D_RenderTargetCreate(_height, _width, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
  110.     //Set the transfer attributes, defining how the data will be outputted to the screen.
  111.     u32 transferFlags = (GX_TRANSFER_FLIP_VERT(false)|GX_TRANSFER_OUT_TILED(false)|GX_TRANSFER_RAW_COPY(false)|GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8)|GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8)|GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO));
  112.     //Set the output of the render target.
  113.     C3D_RenderTargetSetOutput(topRenderTarget, GFX_TOP, GFX_LEFT, transferFlags);
  114.     // Configure attributes for use with the vertex shader
  115.     C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
  116.     AttrInfo_Init(attrInfo);
  117.     //Vertex Position.
  118.     AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 4);
  119.     //Vertex Normal
  120.     AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 3);
  121.     //Vertex Color
  122.     AttrInfo_AddLoader(attrInfo, 2, GPU_FLOAT, 4);
  123.     //Vertex UV
  124.     AttrInfo_AddLoader(attrInfo, 3, GPU_FLOAT, 2);
  125.     //Vertex Bone IDs
  126.     AttrInfo_AddLoader(attrInfo, 4, GPU_SHORT, 3);
  127.     //Vertex Bone Weights
  128.     AttrInfo_AddLoader(attrInfo, 5, GPU_FLOAT, 3);
  129.     //Handle Fragment Processing.
  130.     C3D_TexEnv* env = C3D_GetTexEnv(0);
  131.     C3D_TexEnvInit(env);
  132.     C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0);
  133.     C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
  134.     //Set the culling mode.
  135.     C3D_CullFace(GPU_CULL_NONE);
  136.     //Enable depth testing.
  137.     //C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL);
  138.     //Enable blending.
  139.     //C3D_AlphaBlend(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA);
  140.     //C3D_AlphaTest(false, GPU_ALWAYS, 0);
  141. }
  142.  
  143. //Initializes the essential components of the application.
  144. void DisplayManager::Init(){
  145.     // Initialise the Video System.
  146.     VideoInit();
  147.     //Initialize the Graphics System.
  148.     GraphicsInit();
  149.     //Set the pointer to the InputManager to that of the static variable.
  150.     inputManager = &sInputManager;
  151.     //Loads all of the shaders used by the application.
  152.     ShaderCollection::Init();
  153.     //Load all of the textures used by the application.
  154.     TextureCollection::Init();
  155.     //Initialize the Audio Manager.
  156.     AudioManager::Init();
  157. }
  158.  
  159. //Performs the necessary clean-up operations for termination.
  160. void DisplayManager::Terminate(){
  161.     //Terminate Citro3D.
  162.     C3D_Fini();
  163.     //Close the graphics.
  164.     gfxExit();
  165. }
  166.  
  167. DisplayManager::DisplayManager(int width, int height)
  168. {
  169.     _width = width;
  170.     _height = height;
  171.     Init();
  172. }
  173.  
  174.  
  175. DisplayManager::~DisplayManager()
  176. {
  177. }
Advertisement
Add Comment
Please, Sign In to add comment