Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "DisplayManager.h"
- /*STATIC VARS*/
- static InputManager sInputManager;
- void DisplayManager::SetScreen(BaseScreen* targetScreen){
- //Reset the input states.
- sInputManager.ResetControls();
- //Call the hide method of the initial screen.
- if (_activeScreen){
- _activeScreen->Hide();
- }
- //Update the pointer to the screen.
- _activeScreen = targetScreen;
- //Call the show method.
- _activeScreen->Show();
- }
- //Begins the Game Loop.
- void DisplayManager::Begin()
- {
- //Game Loop
- do{
- //Query controller state.
- hidScanInput();
- //Obtain and process the controller states.
- sInputManager.onKeyDown(hidKeysDown());
- sInputManager.onKeyUp(hidKeysUp());
- //Obtain the state of the circle pad and update the axes.
- hidCircleRead(&sInputManager.joystickPosition);
- sInputManager.UpdateJoystick();
- //Call the render method of the activeScreen, only if the screen pointer is valid.
- if (_activeScreen){
- //Add on to the dtAccumulator.
- /*physicsDtAccumulator += _deltaTime;
- //Ensure that the dtAccumulator doesn't get too high.
- if (physicsDtAccumulator > 0.2f){
- physicsDtAccumulator = 0.2f;
- }
- //Update the physics engine.
- while (physicsDtAccumulator > physicsDt){
- collisionManager.Step(physicsDt);
- physicsDtAccumulator -= physicsDt;
- }*/
- if (!_paused){
- _activeScreen->Debug();
- _activeScreen->Render(_deltaTime);
- }
- else{
- _activeScreen->DuringPause(_deltaTime);
- }
- CalculateFPS();
- }
- } // Check if the ESC key was pressed or the window was closed
- while (!_terminate && !(sInputManager.LHeld && sInputManager.RHeld && sInputManager.StartHeld));
- //Exit the application.
- Terminate();
- }
- void DisplayManager::CalculateFPS(){
- //Used for repetition.
- static int count = 0;
- //Set the previous time to the currentTime of glfw.
- static u64 previousTime = osGetTime();
- //Record the difference in the time.
- _deltaTime = (osGetTime() - previousTime) / 1000.0f;
- //Store the frameTime inside of the array.
- _frameTimes[count] = _deltaTime;
- //Set the frametime again, outside of initialization.
- previousTime = osGetTime();
- if (count < 9){
- count++;
- }
- else{
- _frameTimeCountReached = true;
- count = 0;
- }
- //Calculate the average if enough numbers have been recorded.
- if (_frameTimeCountReached){
- _averageFrameTime = 0;
- for (int i = 0; i < 10; i++){
- _averageFrameTime += _frameTimes[i];
- }
- _averageFrameTime /= 10.0f;
- _frameRate = 1.0f / _averageFrameTime;
- }
- }
- //Initializes the video system, allowing data to be output to the screen.
- void DisplayManager::VideoInit()
- {
- //Initialize the graphics system.
- gfxInitDefault();
- //Initialize the console for debugging.
- consoleInit(GFX_BOTTOM, NULL);
- }
- //Initializes the Citro3D graphics library.
- void DisplayManager::GraphicsInit()
- {
- //Initialize Citro3D.
- C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
- //Create the render target.
- topRenderTarget = C3D_RenderTargetCreate(_height, _width, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
- //Set the transfer attributes, defining how the data will be outputted to the screen.
- 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));
- //Set the output of the render target.
- C3D_RenderTargetSetOutput(topRenderTarget, GFX_TOP, GFX_LEFT, transferFlags);
- // Configure attributes for use with the vertex shader
- C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
- AttrInfo_Init(attrInfo);
- //Vertex Position.
- AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 4);
- //Vertex Normal
- AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 3);
- //Vertex Color
- AttrInfo_AddLoader(attrInfo, 2, GPU_FLOAT, 4);
- //Vertex UV
- AttrInfo_AddLoader(attrInfo, 3, GPU_FLOAT, 2);
- //Vertex Bone IDs
- AttrInfo_AddLoader(attrInfo, 4, GPU_SHORT, 3);
- //Vertex Bone Weights
- AttrInfo_AddLoader(attrInfo, 5, GPU_FLOAT, 3);
- //Handle Fragment Processing.
- C3D_TexEnv* env = C3D_GetTexEnv(0);
- C3D_TexEnvInit(env);
- C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0);
- C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);
- //Set the culling mode.
- C3D_CullFace(GPU_CULL_NONE);
- //Enable depth testing.
- //C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL);
- //Enable blending.
- //C3D_AlphaBlend(GPU_BLEND_ADD, GPU_BLEND_ADD, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA);
- //C3D_AlphaTest(false, GPU_ALWAYS, 0);
- }
- //Initializes the essential components of the application.
- void DisplayManager::Init(){
- // Initialise the Video System.
- VideoInit();
- //Initialize the Graphics System.
- GraphicsInit();
- //Set the pointer to the InputManager to that of the static variable.
- inputManager = &sInputManager;
- //Loads all of the shaders used by the application.
- ShaderCollection::Init();
- //Load all of the textures used by the application.
- TextureCollection::Init();
- //Initialize the Audio Manager.
- AudioManager::Init();
- }
- //Performs the necessary clean-up operations for termination.
- void DisplayManager::Terminate(){
- //Terminate Citro3D.
- C3D_Fini();
- //Close the graphics.
- gfxExit();
- }
- DisplayManager::DisplayManager(int width, int height)
- {
- _width = width;
- _height = height;
- Init();
- }
- DisplayManager::~DisplayManager()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment