Device.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Config.h"
  23. #include "Device.h"
  24. #include "Filesystem.h"
  25. #include "InputManager.h"
  26. #include "Log.h"
  27. #include "OS.h"
  28. #include "Renderer.h"
  29. #include "Timer.h"
  30. #include "Types.h"
  31. #include <cstdlib>
  32. namespace crown
  33. {
  34. const uint16_t Device::CROWN_MAJOR = 0;
  35. const uint16_t Device::CROWN_MINOR = 1;
  36. const uint16_t Device::CROWN_MICRO = 0;
  37. //-----------------------------------------------------------------------------
  38. Device::Device() :
  39. mPreferredWindowWidth(1000),
  40. mPreferredWindowHeight(625),
  41. mPreferredWindowFullscreen(false),
  42. mPreferredRootPath(Str::EMPTY),
  43. mPreferredUserPath(Str::EMPTY),
  44. mIsInit(false),
  45. mIsRunning(false),
  46. mRenderer(NULL)
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. Device::~Device()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. bool Device::Init(int argc, char** argv)
  55. {
  56. if (ParseCommandLine(argc, argv) == false)
  57. {
  58. return false;
  59. }
  60. // Initialize
  61. Log::D("Initializing Device...");
  62. if (IsInit())
  63. {
  64. Log::E("Device is already initialized.");
  65. return false;
  66. }
  67. // Sets the root path
  68. GetFilesystem()->Init(mPreferredRootPath.c_str(), mPreferredUserPath.c_str());
  69. // Creates the main window
  70. if (!os::create_render_window(0, 0, mPreferredWindowWidth, mPreferredWindowHeight, mPreferredWindowFullscreen))
  71. {
  72. Log::E("Unuble to create the main window.");
  73. return false;
  74. }
  75. Log::D("Window created.");
  76. // Creates the renderer
  77. if (!mRenderer)
  78. {
  79. mRenderer = Renderer::CreateRenderer();
  80. }
  81. Log::D("Renderer created.");
  82. os::init_input();
  83. mIsInit = true;
  84. StartRunning();
  85. Log::I("Crown Game Engine %d.%d.%d", CROWN_MAJOR, CROWN_MINOR, CROWN_MICRO);
  86. Log::I("Crown is up and running, enjoy!");
  87. return true;
  88. }
  89. //-----------------------------------------------------------------------------
  90. void Device::Shutdown()
  91. {
  92. if (!IsInit())
  93. {
  94. Log::E("Device is not initialized.");
  95. return;
  96. }
  97. Log::I("Releasing Renderer...");
  98. if (mRenderer)
  99. {
  100. Renderer::DestroyRenderer(mRenderer);
  101. }
  102. Log::I("Releasing Render Window...");
  103. os::destroy_render_window();
  104. mIsInit = false;
  105. }
  106. //-----------------------------------------------------------------------------
  107. bool Device::IsInit()
  108. {
  109. return mIsInit;
  110. }
  111. //-----------------------------------------------------------------------------
  112. Renderer* Device::GetRenderer()
  113. {
  114. return mRenderer;
  115. }
  116. //-----------------------------------------------------------------------------
  117. void Device::StartRunning()
  118. {
  119. if (!IsInit())
  120. {
  121. return;
  122. }
  123. mIsRunning = true;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Device::StopRunning()
  127. {
  128. if (!IsInit())
  129. {
  130. return;
  131. }
  132. mIsRunning = false;
  133. }
  134. //-----------------------------------------------------------------------------
  135. bool Device::IsRunning() const
  136. {
  137. return mIsRunning;
  138. }
  139. //-----------------------------------------------------------------------------
  140. void Device::Frame()
  141. {
  142. os::event_loop();
  143. GetInputManager()->EventLoop();
  144. mRenderer->_BeginFrame();
  145. mRenderer->_EndFrame();
  146. os::swap_buffers();
  147. }
  148. //-----------------------------------------------------------------------------
  149. bool Device::ParseCommandLine(int argc, char** argv)
  150. {
  151. if (argc == 2 && Str::StrCmp(argv[1], "-help") == 0)
  152. {
  153. os::printf("Usage: %s [options]\n", argv[0]);
  154. os::printf("Options:\n\n");
  155. os::printf("All of the following options take precedence over\nenvironment variables and configuration files.\n\n");
  156. // Print32_t options
  157. os::printf(" -help\t\t\t\t\tShow this help\n");
  158. os::printf(" -root-path <path>\t\t\tUse <path> as the filesystem root path\n");
  159. os::printf(" -user-path <path>\t\t\tUse <path> as the filesystem user path\n");
  160. os::printf(" -metrics <width> <height>\t\tSet the <width> and <height> of the render window\n");
  161. os::printf(" -fullscreen\t\t\t\tStart in fullscreen\n");
  162. return false;
  163. }
  164. // Parse command line arguments
  165. int32_t i = 1;
  166. while (i < argc)
  167. {
  168. if (Str::StrCmp(argv[i], "-root-path") == 0)
  169. {
  170. if (argc < i + 2)
  171. {
  172. os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
  173. return false;
  174. }
  175. mPreferredRootPath = argv[i + 1];
  176. // Two arguments crunched
  177. i += 2;
  178. continue;
  179. }
  180. if (Str::StrCmp(argv[i], "-user-path") == 0)
  181. {
  182. if (argc < i + 2)
  183. {
  184. os::printf("%s: error: missing absolute path after `-user-path`\n", argv[0]);
  185. return false;
  186. }
  187. mPreferredUserPath = argv[i + 1];
  188. // Two arguments crunched
  189. i += 2;
  190. continue;
  191. }
  192. if (Str::StrCmp(argv[i], "-metrics") == 0)
  193. {
  194. if (argc < i + 3)
  195. {
  196. os::printf("%s: error: wrong number of arguments after `-metrics`\n", argv[0]);
  197. return false;
  198. }
  199. mPreferredWindowWidth = atoi(argv[i + 1]);
  200. mPreferredWindowHeight = atoi(argv[i + 2]);
  201. // Three arguments crunched
  202. i += 3;
  203. continue;
  204. }
  205. if (Str::StrCmp(argv[i], "-fullscreen") == 0)
  206. {
  207. mPreferredWindowFullscreen = true;
  208. // One argument crunched
  209. i++;
  210. continue;
  211. }
  212. // Next argument
  213. i++;
  214. }
  215. return true;
  216. }
  217. Device device;
  218. Device* GetDevice()
  219. {
  220. return &device;
  221. }
  222. } // namespace crown