App.cpp 8.2 KB

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