PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Game/Game.cpp

https://bitbucket.org/beulard/water
C++ | 383 lines | 259 code | 93 blank | 31 comment | 50 complexity | cdac63a563bed1c909af72ef2f994ad5 MD5 | raw file
  1. #include "Game.hpp"
  2. #include <GL/glew.h>
  3. #include <GL/glfw.h>
  4. #include <iostream>
  5. #include "UIMainMenu.hpp"
  6. #include "Quad.hpp"
  7. #include "Triangle.hpp"
  8. #include "Tile.hpp"
  9. FILE* Log;
  10. bool Game::Success = 0;
  11. bool Game::Failure = 1;
  12. const float Game::FrameTime = 1.f / 60.f;
  13. const float Game::SimTime = 1.f / 100.f;
  14. Game::Game(){
  15. running = false;
  16. Version.Major = 0;
  17. Version.Minor = 3;
  18. Version.Revision = 6;
  19. }
  20. Game::~Game(){
  21. }
  22. bool Game::Init() {
  23. // Open the log file in append mode
  24. Log = fopen( "log", "w" );
  25. if( !Log ) {
  26. #ifdef WATER_DEBUG
  27. std::cout << "Could not open the log file." << std::endl;
  28. #endif
  29. return Game::Failure;
  30. }
  31. #ifdef WATER_DEBUG
  32. Append( "[:::::::::: Water Debug Output ::::::::::]\n\n" );
  33. #else
  34. Append( "[:::::::::: Water Release Output :::::::::]" );
  35. #endif
  36. // Load the config file and read the values for OpenGL context initialization
  37. wJSON root( "data/config.json" );
  38. u32 width = root.GetItem( "width" ).Int();
  39. u32 height = root.GetItem( "height" ).Int();
  40. bool fullscreen = root.GetItem( "fullscreen" ).Bool();
  41. u32 antialiasing = root.GetItem( "antialiasing" ).Int();
  42. int glfwMode = fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW;
  43. windowSize = vec2i( width, height );
  44. if( glfwInit() == GL_TRUE ) {
  45. Info("GLFW initialized correctly.");
  46. glfwOpenWindowHint( GLFW_WINDOW_NO_RESIZE, GL_TRUE );
  47. glfwOpenWindowHint( GLFW_OPENGL_VERSION_MAJOR, 2 );
  48. glfwOpenWindowHint( GLFW_OPENGL_VERSION_MINOR, 1 );
  49. glfwOpenWindowHint( GLFW_FSAA_SAMPLES, antialiasing );
  50. if( glfwOpenWindow( width, height, 8, 8, 8, 8, 8, 0, glfwMode ) ) {
  51. Info( "Window opened correctly." );
  52. if( fullscreen )
  53. glfwEnable( GLFW_MOUSE_CURSOR );
  54. handle = glfwGetWindowHandle();
  55. glfwSetWindowTitle( "Water" );
  56. glfwSwapInterval( 0 );
  57. if( glewInit() == GLEW_OK ) {
  58. Info( "GLEW initialized correctly." );
  59. if( !FT_Init_FreeType( &ft ) ) {
  60. Info( "FreeType initialized correctly." );
  61. running = true;
  62. Info( "Everything initialized correctly.");
  63. Info( "Using OpenGL version %i.%i", glfwGetWindowParam( GLFW_OPENGL_VERSION_MAJOR ), glfwGetWindowParam( GLFW_OPENGL_VERSION_MINOR ) );
  64. Info( "Using GLFW version %i.%i.%i", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION );
  65. Info( "Using GLEW version %s.%s.%s", glewGetString( GLEW_VERSION_MAJOR ), glewGetString( GLEW_VERSION_MINOR ), glewGetString( GLEW_VERSION_MICRO ) );
  66. Info( "Using FreeType2\n" );
  67. glClearColor( 0, 0, 0, 0 );
  68. glEnable( GL_BLEND );
  69. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  70. glEnableClientState( GL_VERTEX_ARRAY );
  71. projMatrix[0] = 2.f / windowSize.x;
  72. projMatrix[1] = 0.f;
  73. projMatrix[2] = 0.f;
  74. projMatrix[3] = 0.f;
  75. projMatrix[4] = 2.f / windowSize.y;
  76. projMatrix[5] = 0.f;
  77. projMatrix[6] = -1;
  78. projMatrix[7] = -1;
  79. projMatrix[8] = 0;
  80. state = GS_PreLoading;
  81. // The data files here can not be added or removed by the user
  82. // They are the mandatory resources the game needs to launch and load other data
  83. dataLoader.Add( "uisprite", DT_Shader );
  84. dataLoader.Add( "uicolored", DT_Shader );
  85. dataLoader.Add( "uitext", DT_Shader );
  86. dataLoader.Add( "sprite", DT_Shader );
  87. dataLoader.Add( "colored", DT_Shader );
  88. dataLoader.Add( "text", DT_Shader );
  89. dataLoader.Add( "loading", DT_Texture );
  90. dataLoader.Add( "loading_bar_outline", DT_Sprite );
  91. dataLoader.Add( "loading_bar_inside", DT_Sprite );
  92. dataLoader.Add( "dejavusans", DT_Font );
  93. // Load the basic data to be able to render the loading screen
  94. dataLoader.LoadSilent();
  95. // Set the shaders required to render the loading screen
  96. Renderer::UISpriteShader = &dataLoader.GetShaderProgram( "uisprite" );
  97. Renderer::UIColoredShader = &dataLoader.GetShaderProgram( "uicolored" );
  98. Renderer::UITextShader = &dataLoader.GetShaderProgram( "uitext" );
  99. Renderer::worldSpriteShader = &dataLoader.GetShaderProgram( "sprite" );
  100. Renderer::worldColoredShader = &dataLoader.GetShaderProgram( "colored" );
  101. Renderer::worldTextShader = &dataLoader.GetShaderProgram( "text" );
  102. inputMgr.Init();
  103. renderer.Init();
  104. // We first need to load game data, so let's initialize the loading screen
  105. state = GS_Loading;
  106. // We will now add every directory to the loading list but remove data files we have already loaded for the loading screen
  107. // Shaders can not be added/removed by users for now so we don't add the 'shaders' folder
  108. dataLoader.AddDir( "textures", DT_Texture );
  109. dataLoader.Remove( "loading", DT_Texture );
  110. dataLoader.AddDir( "fonts", DT_Font );
  111. dataLoader.Remove( "dejavusans", DT_Font );
  112. dataLoader.AddDir( "sprites", DT_Sprite );
  113. dataLoader.Remove( "loading_bar_outline", DT_Sprite );
  114. dataLoader.Remove( "loading_bar_inside", DT_Sprite );
  115. dataLoader.AddDir( "anims", DT_Anim );
  116. dataLoader.AddDir( "tiles", DT_Tile );
  117. dataLoader.AddDir( "biomes", DT_Biome );
  118. dataLoader.AddDir( "entities", DT_Entity );
  119. // Display a loading screen while game data is being read
  120. dataLoader.LoadWithScreen();
  121. camera.SetPosition( vec2i( 0, 0 ) );
  122. masterTimer.Start();
  123. return Game::Success;
  124. }
  125. else
  126. Error( "Couldn't initialize FreeType." );
  127. }
  128. else
  129. Error( "Couldn't initialize GLEW." );
  130. }
  131. else
  132. Error( "Couldn't open the window." );
  133. }
  134. else
  135. Error( "Couldn't initialize GLFW." );
  136. return Game::Failure;
  137. }
  138. bool Game::Run() {
  139. //mouse.SetCursor( CS_Normal );
  140. UIMainMenu mainMenu;
  141. // The main menu has no direct parent, so we set it to the root widget so that
  142. // it can handle user input (mouse clicks)
  143. mainMenu.AddToRoot();
  144. Random::SetSeed( 23123 );
  145. //world.map.Generate();
  146. lastState = GS_Loading;
  147. state = GS_MainMenu;
  148. soup = dataLoader.GetEntity( "soup" );
  149. world.SetPlayerBox( soup.GetBox() );
  150. camera.SetCenter( soup.GetPosition() );
  151. //soup.SetPosition( vec2i( 100, 100 ) );
  152. Font& dvs12 = dataLoader.GetFont( "dejavusans", 12 );
  153. Font& dvs16 = dataLoader.GetFont( "dejavusans", 16 );
  154. Font& dvs124 = dataLoader.GetFont( "dejavusans", 124 );
  155. //Tile hero2 = dataLoader.GetTile( "water" );
  156. //world.AddBox( hero2.GetBox() );
  157. //hero2.Move( vec2i( 100, 0 ) );
  158. //Tile hero3 = dataLoader.GetTile( "earth" );
  159. //world.AddBox( hero3.GetBox() );
  160. Text t2( "WATER", &dvs124, Color( 120, 255, 39, 150 ) );
  161. t2.SetRenderMode( RM_UIText );
  162. t2.Move( vec2i( 810, 0 ) );
  163. Timer frameTimer;
  164. frameTimer.Start();
  165. float frameTime = 0.f;
  166. UIText frameTimeText;
  167. frameTimeText.SetFont( &dvs16);
  168. Timer timer1sec( 1.f );
  169. timer1sec.SetLoop( true );
  170. timer1sec.Start();
  171. frameTimeText.SetString( "FPS" );
  172. //frameTimeText.SetPosition( vec2i( 0, 10 ) );
  173. frameTimeText.Draw();
  174. while( running ) {
  175. if( timer1sec.HasEnded() )
  176. frameTimeText.SetString( IntToString( (int)frameTime ) );
  177. double startTime = masterTimer.GetCurrentTime();
  178. inputMgr.Update();
  179. if( inputMgr.IsWindowOpened() == false )
  180. running = false;
  181. masterTimer.Update();
  182. if( inputMgr.GetState( MC_Left ) == IT_None )
  183. rootWidget.OnHover();
  184. if( inputMgr.GetState( MC_Left ) == IT_Press )
  185. rootWidget.OnPress();
  186. if( inputMgr.GetState( MC_Left ) == IT_Release )
  187. rootWidget.OnRelease();
  188. //mouse.Update();
  189. renderer.Clear();
  190. if( state != lastState ) {
  191. if( state == GS_MainMenu ) {
  192. mainMenu.Activate();
  193. }
  194. if( lastState == GS_MainMenu ) {
  195. mainMenu.Sleep();
  196. }
  197. if( lastState == GS_Game ) {
  198. //Soup.StopDrawing();
  199. }
  200. if( state == GS_Game ) {
  201. if( !soup.IsDrawn() )
  202. soup.Draw();
  203. }
  204. lastState = state;
  205. }
  206. if( state == GS_MainMenu ) {
  207. }
  208. else if( state == GS_Game ) {
  209. world.Update();
  210. masterAnim.Update();
  211. if( inputMgr.GetState( KC_Escape ) == IT_Press )
  212. state = GS_MainMenu;
  213. if( inputMgr.Shift() )
  214. soup.SetSpeed( 20.f );
  215. else
  216. soup.SetSpeed( 10.f );
  217. if( inputMgr.GetState( KC_D ) == IT_Press )
  218. soup.GetBox().MoveRight();
  219. if( inputMgr.GetState( KC_W ) == IT_Press )
  220. soup.GetBox().Jump();
  221. if( inputMgr.GetState( KC_S ) == IT_Press )
  222. soup.GetBox().velocity += vec2f( 0, 2 );
  223. if( inputMgr.GetState( KC_A ) == IT_Press )
  224. soup.GetBox().MoveLeft();
  225. if( inputMgr.GetState( KC_R ) == IT_Release ) {
  226. soup.SetPosition( vec2i( 0, 0 ) );
  227. soup.GetBox().velocity.y = 0;
  228. }
  229. if( inputMgr.GetState( MC_Left ) == IT_Release ) {
  230. }
  231. if( inputMgr.GetState( MC_Right ) == IT_Release ) {
  232. }
  233. camera.SetCenter( soup.GetBox().center );
  234. renderer.Clear();
  235. //renderer.Draw( t2 );
  236. soup.Update();
  237. //renderer.Draw( hero2 );
  238. //renderer.Draw( hero3 );
  239. //world.map.Draw();
  240. world.map.Update();
  241. //renderer.Draw( Soup );
  242. }
  243. renderer.Render();
  244. double endTime = masterTimer.GetCurrentTime();
  245. frameTime = 1 / ( endTime - startTime );
  246. }
  247. return Game::Success;
  248. }
  249. void Game::Clean() {
  250. // For linux : free the cursor and delete the X display
  251. mouse.Free();
  252. glfwTerminate();
  253. Append( "\n" );
  254. Info( "Started cleaning..." );
  255. world.Clean();
  256. rootWidget.Clean();
  257. dataLoader.Clean();
  258. masterTimer.Clean();
  259. masterAnim.Clean();
  260. renderer.Clean();
  261. Info( "Done cleaning." );
  262. }
  263. void Game::Exit() {
  264. Clean();
  265. Append( "\n\n\n[:::::::::::: End of Output ::::::::::::]\n\n\n" );
  266. // Close the log file and exit
  267. fclose( Log );
  268. exit(1);
  269. }
  270. const vec2i& Game::GetWindowSize() const {
  271. return windowSize;
  272. }
  273. const vec2i& Game::GetCameraPosition() const {
  274. return camera.GetPosition();
  275. }
  276. const vec2i& Game::GetCameraGLPosition() const {
  277. return camera.GetGLPosition();
  278. }
  279. // Set the values for our pointers declared in Common.hpp
  280. const vec2<int>* Global::winSz = &Global::Water.GetWindowSize();
  281. Renderer* const Global::renderer = &Global::Water.renderer;
  282. DataLoader* Global::dataLoader = &Global::Water.dataLoader;
  283. Mouse* Global::mouse = &Global::Water.mouse;
  284. MasterTimer* Global::masterTimer = &Global::Water.masterTimer;
  285. MasterAnim* Global::masterAnim = &Global::Water.masterAnim;
  286. RootWidget* Global::rootWidget = &Global::Water.rootWidget;
  287. Camera* Global::camera = &Global::Water.camera;
  288. InputManager* Global::inputMgr = &Global::Water.inputMgr;
  289. World* Global::world = &Global::Water.world;
  290. float* Global::projMatrix = Global::Water.projMatrix;