App.cpp 8.3 KB

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