GameHelper.cpp
Go to the documentation of this file.00001 //*** GameHelper.cpp *** 00002 00003 #include "GameHelper.h" 00004 00005 #include "Platform_OS.h" 00006 #include "Screen.h" 00007 #include "ColorHelper.h" 00008 #include "Bitmap.h" 00009 00010 #include "AudioFormat_OGG.h" 00011 #include "AudioFormat_YM.h" 00012 #include "AudioFormat_MOD.h" 00013 #include "ImageFormat_TGA.h" 00014 #include "ImageFormat_GIF.h" 00015 #include "ImageFormat_JPG.h" 00016 #include "ImageFormat_PNG.h" 00017 00018 00019 //*** Constructor *** 00020 00021 GameHelper::GameHelper(const char* applicationName) 00022 { 00023 // Set the window/application title 00024 if (Platform::GetPlatform_OS()) 00025 { 00026 Platform::GetPlatform_OS()->SetApplicationName(applicationName); 00027 } 00028 00029 // Register the sound formats we will be using - better register all of them, as we want this game to be easy to 00030 // mod, and we don't know which formats the modders will want to use 00031 AudioFormat_OGG::Register(); 00032 AudioFormat_YM::Register(); 00033 AudioFormat_MOD::Register(); 00034 00035 // Register the image formats we will be using - better register all of them, as we want this game to be easy to 00036 // mod, and we don't know which formats the modders will want to use 00037 ImageFormat_TGA::Register(); 00038 ImageFormat_GIF::Register(); 00039 ImageFormat_JPG::Register(); 00040 ImageFormat_PNG::Register(); 00041 } 00042 00043 00044 //*** Destructor *** 00045 00046 GameHelper::~GameHelper() 00047 { 00048 gameStateManager_.ExitCurrentState(); 00049 } 00050 00051 00052 //*** UpdateGame *** 00053 00054 bool GameHelper::UpdateGame(float deltaTime, Screen& screen) 00055 { 00056 // Update systems 00057 inputManager_.Update(); 00058 musicManager_.Update(deltaTime); 00059 audio_.Update(); 00060 gameStateManager_.Update(deltaTime); 00061 spriteControllerManager_.Update(deltaTime); 00062 spriteSystem_.Update(deltaTime); 00063 spriteSystem_.Render(screen.GetBackBuffer()); 00064 00065 // Toggle between fullscreen and windowed mode on Alt+Enter 00066 if (inputManager_.IsKeyDown(KEY_MENU) && inputManager_.WasKeyPressed(KEY_RETURN)) 00067 { 00068 screen.SetFullscreen(!screen.GetFullscreen()); 00069 inputManager_.RestoreCursor(); 00070 } 00071 00072 // Handle screen fade when transitioning between game states 00073 unsigned short screenModulate=0xffff; 00074 if (gameStateManager_.IsTransitioningIn()) 00075 { 00076 float c=gameStateManager_.GetTransitionValue(); 00077 screenModulate=FLOATTORGB16(c,c,c); 00078 } 00079 else if (gameStateManager_.IsTransitioningOut()) 00080 { 00081 float c=1-gameStateManager_.GetTransitionValue(); 00082 screenModulate=FLOATTORGB16(c,c,c); 00083 } 00084 if (screenModulate==0) 00085 { 00086 screen.GetBackBuffer().Clear(); 00087 } 00088 00089 // Surround screen with a one pixel black border - makes it look a bit nicer when it is scaled up 00090 int w=screen.GetBackBuffer().GetWidth()-1; 00091 int h=screen.GetBackBuffer().GetHeight()-1; 00092 screen.GetBackBuffer().Fill(0,0,w,0,0x0); 00093 screen.GetBackBuffer().Fill(0,h,w,h,0x0); 00094 screen.GetBackBuffer().Fill(0,0,0,h,0x0); 00095 screen.GetBackBuffer().Fill(w,0,w,h,0x0); 00096 00097 // Show the screen buffer, modifying its content for the state transition fading 00098 screen.Present(screenModulate); 00099 00100 // Give some time to the OS. If we didn't, things like moving the window around would feel quite sluggish. 00101 if (Platform::GetPlatform_OS()) 00102 { 00103 Platform::GetPlatform_OS()->OsYield(); 00104 } 00105 00106 // Check if exit was requested on OS level (the main window was closed or Alt+F4 pressed) 00107 if (Platform::GetPlatform_OS() && Platform::GetPlatform_OS()->ExitRequested()) 00108 { 00109 // If so, tell game to exit 00110 gameStateManager_.ExitApplication(); 00111 } 00112 00113 return !gameStateManager_.IsExitFlagSet(); 00114 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
