App.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "anki/core/App.h"
  2. #include "anki/core/Logger.h"
  3. #include "anki/util/Exception.h"
  4. #include "anki/util/Platform.h"
  5. #include "anki/util/Filesystem.h"
  6. #include <GL/glew.h>
  7. #include <sstream>
  8. #include <SDL/SDL.h>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <boost/algorithm/string.hpp>
  12. namespace anki {
  13. //==============================================================================
  14. void App::handleLoggerMessages(const Logger::Info& info)
  15. {
  16. std::ostream* out = NULL;
  17. const char* x = NULL;
  18. switch(info.type)
  19. {
  20. case Logger::LMT_NORMAL:
  21. out = &std::cout;
  22. x = "Info";
  23. break;
  24. case Logger::LMT_ERROR:
  25. out = &std::cerr;
  26. x = "Error";
  27. break;
  28. case Logger::LMT_WARNING:
  29. out = &std::cerr;
  30. x = "Warn";
  31. break;
  32. }
  33. (*out) << "(" << info.file << ":" << info.line << " "<< info.func
  34. << ") " << x << ": " << info.msg << std::flush;
  35. }
  36. //==============================================================================
  37. void App::parseCommandLineArgs(int argc, char* argv[])
  38. {
  39. for(int i = 1; i < argc; i++)
  40. {
  41. char* arg = argv[i];
  42. if(strcmp(arg, "--terminal-coloring") == 0)
  43. {
  44. terminalColoringEnabled = true;
  45. }
  46. else if(strcmp(arg, "--no-terminal-coloring") == 0)
  47. {
  48. terminalColoringEnabled = false;
  49. }
  50. else
  51. {
  52. std::cerr << "Incorrect command line argument: " << arg
  53. << std::endl;
  54. abort();
  55. }
  56. }
  57. }
  58. //==============================================================================
  59. void App::init(int argc, char* argv[])
  60. {
  61. windowW = 1280;
  62. windowH = 720;
  63. terminalColoringEnabled = true,
  64. fullScreenFlag = false;
  65. // send output to handleMessageHanlderMsgs
  66. ANKI_CONNECT(&LoggerSingleton::get(), messageRecieved,
  67. this, handleLoggerMessages);
  68. parseCommandLineArgs(argc, argv);
  69. printAppInfo();
  70. initDirs();
  71. initWindow();
  72. // other
  73. activeCam = NULL;
  74. timerTick = 1.0 / 60.0; // in sec. 1.0 / period
  75. }
  76. //==============================================================================
  77. void App::initWindow()
  78. {
  79. ANKI_LOGI("SDL window initializing...");
  80. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  81. {
  82. throw ANKI_EXCEPTION("Failed to init SDL_VIDEO");
  83. }
  84. // print driver name
  85. const char* driverName = SDL_GetCurrentVideoDriver();
  86. if(driverName != NULL)
  87. {
  88. ANKI_LOGI("Video driver name: " << driverName);
  89. }
  90. // set GL attribs
  91. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  92. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  93. // WARNING: Set this low only in deferred shading
  94. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
  95. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  96. SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  97. // OpenWindow
  98. windowId = SDL_CreateWindow("AnKi 3D Engine", SDL_WINDOWPOS_CENTERED,
  99. SDL_WINDOWPOS_CENTERED, windowW, windowH,
  100. SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  101. if(!windowId)
  102. {
  103. throw ANKI_EXCEPTION("Cannot create main window");
  104. }
  105. glContext = SDL_GL_CreateContext(windowId);
  106. // the icon
  107. iconImage = SDL_LoadBMP("gfx/icon.bmp");
  108. if(iconImage == NULL)
  109. {
  110. ANKI_LOGW("Cannot load window icon");
  111. }
  112. else
  113. {
  114. Uint32 colorkey = SDL_MapRGB(iconImage->format, 255, 0, 255);
  115. SDL_SetColorKey(iconImage, SDL_SRCCOLORKEY, colorkey);
  116. //SDL_WM_SetIcon(iconImage, NULL);
  117. SDL_SetWindowIcon(windowId, iconImage);
  118. }
  119. ANKI_LOGI("SDL window initialization ends");
  120. }
  121. //==============================================================================
  122. void App::initDirs()
  123. {
  124. settingsPath = std::string(getenv("HOME")) + "/.anki";
  125. if(!directoryExists(settingsPath.c_str()))
  126. {
  127. ANKI_LOGI("Creating settings dir: " << settingsPath);
  128. createDirectory(settingsPath.c_str());
  129. }
  130. cachePath = settingsPath + "/cache";
  131. if(directoryExists(cachePath.c_str()))
  132. {
  133. ANKI_LOGI("Deleting dir \"" << cachePath << "\"");
  134. removeDirectory(cachePath.c_str());
  135. }
  136. ANKI_LOGI("Creating cache dir \"" << cachePath << "\"");
  137. createDirectory(cachePath.c_str());
  138. }
  139. //==============================================================================
  140. void App::togleFullScreen()
  141. {
  142. //SDL_WM_ToggleFullScreen(mainSurf);
  143. SDL_SetWindowFullscreen(windowId, fullScreenFlag ? SDL_TRUE : SDL_FALSE);
  144. fullScreenFlag = !fullScreenFlag;
  145. }
  146. //==============================================================================
  147. void App::swapBuffers()
  148. {
  149. //SDL_GL_SwapBuffers();
  150. SDL_GL_SwapWindow(windowId);
  151. }
  152. //==============================================================================
  153. void App::quit(int code)
  154. {
  155. SDL_FreeSurface(iconImage);
  156. SDL_GL_DeleteContext(glContext);
  157. SDL_DestroyWindow(windowId);
  158. SDL_Quit();
  159. exit(code);
  160. }
  161. //==============================================================================
  162. #if !defined(ANKI_REVISION)
  163. # define ANKI_REVISION "unknown"
  164. #endif
  165. void App::printAppInfo()
  166. {
  167. std::stringstream msg;
  168. msg << "App info: ";
  169. #if defined(NDEBUG)
  170. msg << "Release";
  171. #else
  172. msg << "Debug";
  173. #endif
  174. msg << " build, ";
  175. msg << "platform ID " << ANKI_PLATFORM << ", ";
  176. msg << "compiler ID " << ANKI_COMPILER << ", ";
  177. msg << "GLEW " << glewGetString(GLEW_VERSION) << ", ";
  178. const SDL_version* v = SDL_Linked_Version();
  179. msg << "SDL " << int(v->major) << '.' << int(v->minor) << '.'
  180. << int(v->patch) << ", " << "build date " __DATE__ << ", "
  181. << "rev " << ANKI_REVISION;
  182. ANKI_LOGI(msg.str());
  183. }
  184. //==============================================================================
  185. uint App::getDesktopWidth() const
  186. {
  187. SDL_DisplayMode mode;
  188. /// @todo re-enable it
  189. //SDL_GetDesktopDisplayMode(&mode);
  190. return mode.w;
  191. }
  192. //==============================================================================
  193. uint App::getDesktopHeight() const
  194. {
  195. SDL_DisplayMode mode;
  196. /// @todo re-enable it
  197. //SDL_GetDesktopDisplayMode(&mode);
  198. return mode.h;
  199. }
  200. } // end namespace