App.cpp 8.2 KB

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