App.cpp 8.1 KB

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