App.cpp 7.4 KB

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