App.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include <GL/glew.h>
  2. #include <sstream>
  3. #include <SDL.h>
  4. #include "App.h"
  5. #include "Scene.h"
  6. #include "MainRenderer.h"
  7. bool App::isCreated = false;
  8. //======================================================================================================================
  9. // parseCommandLineArgs =
  10. //======================================================================================================================
  11. void App::parseCommandLineArgs( int argc, char* argv[] )
  12. {
  13. for( int i=1; i<argc; i++ )
  14. {
  15. char* arg = argv[ i ];
  16. if( strcmp( arg, "--terminal-coloring" ) == 0 )
  17. {
  18. terminalColoringEnabled = true;
  19. }
  20. else if( strcmp( arg, "--no-terminal-coloring" ) == 0 )
  21. {
  22. terminalColoringEnabled = false;
  23. }
  24. else
  25. {
  26. FATAL( "Incorrect command line argument \"" << arg << "\"" );
  27. }
  28. }
  29. }
  30. //======================================================================================================================
  31. // Constructor =
  32. //======================================================================================================================
  33. App::App():
  34. windowW( 1280 ),
  35. windowH( 720 ),
  36. terminalColoringEnabled( true ),
  37. fullScreenFlag( false )
  38. {
  39. }
  40. //======================================================================================================================
  41. // init =
  42. //======================================================================================================================
  43. void App::init( int argc, char* argv[] )
  44. {
  45. parseCommandLineArgs( argc, argv );
  46. printAppInfo();
  47. if( isCreated )
  48. FATAL( "You cannot init a second App instance" )
  49. isCreated = true;
  50. scene = new Scene;
  51. mainRenderer = new MainRenderer;
  52. activeCam = NULL;
  53. /*windowW = 1440;
  54. windowH = 900;*/
  55. timerTick = 1000/40; // in ms. 1000/Hz
  56. time = 0;
  57. }
  58. //======================================================================================================================
  59. // initWindow =
  60. //======================================================================================================================
  61. void App::initWindow()
  62. {
  63. INFO( "SDL window initializing..." );
  64. if( SDL_Init(SDL_INIT_VIDEO) < 0 )
  65. FATAL( "Failed to init SDL_VIDEO" );
  66. // print driver name
  67. const char* driverName = SDL_GetCurrentVideoDriver();
  68. if( driverName != NULL )
  69. {
  70. INFO( "Video driver name: " << driverName );
  71. }
  72. else
  73. {
  74. ERROR( "Failed to obtain the video driver name" );
  75. }
  76. // set GL attribs
  77. SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
  78. SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 1 );
  79. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 8 ); // WARNING: Set this low only in deferred shading
  80. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  81. SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
  82. // OpenWindow
  83. windowId = SDL_CreateWindow( "AnKi Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, windowW, windowH,
  84. SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN );
  85. if( !windowId )
  86. FATAL( "Cannot create main window" );
  87. glContext = SDL_GL_CreateContext( windowId );
  88. // the icon
  89. iconImage = SDL_LoadBMP("gfx/icon.bmp");
  90. if( iconImage == NULL )
  91. {
  92. ERROR( "Cannot load window icon" );
  93. }
  94. else
  95. {
  96. Uint32 colorkey = SDL_MapRGB( iconImage->format, 255, 0, 255 );
  97. SDL_SetColorKey( iconImage, SDL_SRCCOLORKEY, colorkey );
  98. //SDL_WM_SetIcon( iconImage, NULL );
  99. SDL_SetWindowIcon( windowId, iconImage );
  100. }
  101. INFO( "SDL window initialization ends" );
  102. }
  103. //======================================================================================================================
  104. // togleFullScreen =
  105. //======================================================================================================================
  106. void App::togleFullScreen()
  107. {
  108. //SDL_WM_ToggleFullScreen( mainSurf );
  109. SDL_SetWindowFullscreen( windowId, fullScreenFlag );
  110. fullScreenFlag = !fullScreenFlag;
  111. }
  112. //======================================================================================================================
  113. // swapBuffers =
  114. //======================================================================================================================
  115. void App::swapBuffers()
  116. {
  117. //SDL_GL_SwapBuffers();
  118. SDL_GL_SwapWindow( windowId );
  119. }
  120. //======================================================================================================================
  121. // quitApp =
  122. //======================================================================================================================
  123. void App::quitApp( int code )
  124. {
  125. SDL_FreeSurface( iconImage );
  126. SDL_GL_DeleteContext( glContext );
  127. SDL_DestroyWindow( windowId );
  128. SDL_Quit();
  129. exit(code);
  130. }
  131. //======================================================================================================================
  132. // waitForNextFrame =
  133. //======================================================================================================================
  134. void App::waitForNextFrame()
  135. {
  136. uint now = SDL_GetTicks();
  137. if( now - time < timerTick )
  138. {
  139. // the new time after the SDL_Delay will be...
  140. time += timerTick;
  141. // sleep a little
  142. SDL_Delay( time - now);
  143. }
  144. else
  145. time = now;
  146. }
  147. //======================================================================================================================
  148. // printAppInfo =
  149. //======================================================================================================================
  150. #if !defined( REVISION )
  151. #define REVISION "unknown"
  152. #endif
  153. void App::printAppInfo()
  154. {
  155. stringstream msg;
  156. msg << "App info: debugging ";
  157. #if defined( DEBUG )
  158. msg << "on, ";
  159. #else
  160. msg << "off, ";
  161. #endif
  162. msg << "platform ";
  163. #if defined( PLATFORM_LINUX )
  164. msg << "Linux, ";
  165. #elif defined( PLATFORM_WIN )
  166. msg << "Windows, ";
  167. #else
  168. #error "See file"
  169. #endif
  170. msg << "GLEW " << glewGetString(GLEW_VERSION) << ", ";
  171. const SDL_version* v = SDL_Linked_Version();
  172. msg << "SDL " << int(v->major) << '.' << int(v->minor) << '.' << int(v->patch) << ", ";
  173. msg << "build date " __DATE__ << ", ";
  174. msg << "rev " << REVISION;
  175. INFO( msg.str() )
  176. }
  177. //======================================================================================================================
  178. // getDesktopWidth =
  179. //======================================================================================================================
  180. uint App::getDesktopWidth() const
  181. {
  182. SDL_DisplayMode mode;
  183. SDL_GetDesktopDisplayMode( &mode );
  184. return mode.w;
  185. }
  186. //======================================================================================================================
  187. // getDesktopHeight =
  188. //======================================================================================================================
  189. uint App::getDesktopHeight() const
  190. {
  191. SDL_DisplayMode mode;
  192. SDL_GetDesktopDisplayMode( &mode );
  193. return mode.h;
  194. }
  195. //======================================================================================================================
  196. // getTicks =
  197. //======================================================================================================================
  198. uint App::getTicks()
  199. {
  200. return SDL_GetTicks();
  201. }