Device.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "Types.h"
  23. #include "Config.h"
  24. #include "Device.h"
  25. #include "Filesystem.h"
  26. #include "GarbageBin.h"
  27. #include "InputManager.h"
  28. #include "Log.h"
  29. #include "Renderer.h"
  30. #include "GarbageBin.h"
  31. #include "Config.h"
  32. #include "Filesystem.h"
  33. #include "Timer.h"
  34. #include "OS.h"
  35. #include <cstdlib>
  36. namespace crown
  37. {
  38. const ushort Device::CROWN_MAJOR = 0;
  39. const ushort Device::CROWN_MINOR = 1;
  40. const ushort Device::CROWN_MICRO = 0;
  41. //-----------------------------------------------------------------------------
  42. Device::Device() :
  43. mPreferredWindowWidth(1000),
  44. mPreferredWindowHeight(625),
  45. mPreferredWindowFullscreen(false),
  46. mPreferredRootPath(Str::EMPTY),
  47. mPreferredUserPath(Str::EMPTY),
  48. mIsInit(false),
  49. mIsRunning(false),
  50. mRenderer(NULL)
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. Device::~Device()
  55. {
  56. }
  57. //-----------------------------------------------------------------------------
  58. bool Device::Init(int argc, char** argv)
  59. {
  60. if (ParseCommandLine(argc, argv) == false)
  61. {
  62. return false;
  63. }
  64. // Initialize
  65. Log::D("Initializing Device...");
  66. if (IsInit())
  67. {
  68. Log::E("Device is already initialized.");
  69. return false;
  70. }
  71. // Sets the root path
  72. GetFilesystem()->Init(mPreferredRootPath.c_str(), mPreferredUserPath.c_str());
  73. // Creates the main window
  74. if (!os::create_render_window(0, 0, mPreferredWindowWidth, mPreferredWindowHeight, mPreferredWindowFullscreen))
  75. {
  76. Log::E("Unuble to create the main window.");
  77. return false;
  78. }
  79. Log::D("Window created.");
  80. // Creates the renderer
  81. if (!mRenderer)
  82. {
  83. mRenderer = Renderer::CreateRenderer();
  84. }
  85. Log::D("Renderer created.");
  86. os::init_input();
  87. mIsInit = true;
  88. StartRunning();
  89. Log::I("Crown Game Engine %d.%d.%d", CROWN_MAJOR, CROWN_MINOR, CROWN_MICRO);
  90. Log::I("Crown is up and running, enjoy!");
  91. return true;
  92. }
  93. //-----------------------------------------------------------------------------
  94. void Device::Shutdown()
  95. {
  96. if (!IsInit())
  97. {
  98. Log::E("Device is not initialized.");
  99. return;
  100. }
  101. Log::I("Releasing Renderer...");
  102. if (mRenderer)
  103. {
  104. Renderer::DestroyRenderer(mRenderer);
  105. }
  106. Log::I("Releasing Render Window...");
  107. os::destroy_render_window();
  108. mIsInit = false;
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool Device::IsInit()
  112. {
  113. return mIsInit;
  114. }
  115. //-----------------------------------------------------------------------------
  116. Renderer* Device::GetRenderer()
  117. {
  118. return mRenderer;
  119. }
  120. //-----------------------------------------------------------------------------
  121. void Device::StartRunning()
  122. {
  123. if (!IsInit())
  124. {
  125. return;
  126. }
  127. mIsRunning = true;
  128. }
  129. //-----------------------------------------------------------------------------
  130. void Device::StopRunning()
  131. {
  132. if (!IsInit())
  133. {
  134. return;
  135. }
  136. mIsRunning = false;
  137. }
  138. //-----------------------------------------------------------------------------
  139. bool Device::IsRunning() const
  140. {
  141. return mIsRunning;
  142. }
  143. //-----------------------------------------------------------------------------
  144. void Device::Frame()
  145. {
  146. os::event_loop();
  147. GetInputManager()->EventLoop();
  148. mRenderer->_BeginFrame();
  149. mRenderer->_EndFrame();
  150. os::swap_buffers();
  151. }
  152. //-----------------------------------------------------------------------------
  153. bool Device::ParseCommandLine(int argc, char** argv)
  154. {
  155. if (argc == 2 && Str::StrCmp(argv[1], "-help") == 0)
  156. {
  157. os::printf("Usage: %s [options]\n", argv[0]);
  158. os::printf("Options:\n\n");
  159. os::printf("All of the following options take precedence over\nenvironment variables and configuration files.\n\n");
  160. // Print options
  161. os::printf(" -help\t\t\t\t\tShow this help\n");
  162. os::printf(" -root-path <path>\t\t\tUse <path> as the filesystem root path\n");
  163. os::printf(" -user-path <path>\t\t\tUse <path> as the filesystem user path\n");
  164. os::printf(" -metrics <width> <height>\t\tSet the <width> and <height> of the render window\n");
  165. os::printf(" -fullscreen\t\t\t\tStart in fullscreen\n");
  166. return false;
  167. }
  168. // Parse command line arguments
  169. int i = 1;
  170. while (i < argc)
  171. {
  172. if (Str::StrCmp(argv[i], "-root-path") == 0)
  173. {
  174. if (argc < i + 2)
  175. {
  176. os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
  177. return false;
  178. }
  179. mPreferredRootPath = argv[i + 1];
  180. // Two arguments crunched
  181. i += 2;
  182. continue;
  183. }
  184. if (Str::StrCmp(argv[i], "-user-path") == 0)
  185. {
  186. if (argc < i + 2)
  187. {
  188. os::printf("%s: error: missing absolute path after `-user-path`\n", argv[0]);
  189. return false;
  190. }
  191. mPreferredUserPath = argv[i + 1];
  192. // Two arguments crunched
  193. i += 2;
  194. continue;
  195. }
  196. if (Str::StrCmp(argv[i], "-metrics") == 0)
  197. {
  198. if (argc < i + 3)
  199. {
  200. os::printf("%s: error: wrong number of arguments after `-metrics`\n", argv[0]);
  201. return false;
  202. }
  203. mPreferredWindowWidth = atoi(argv[i + 1]);
  204. mPreferredWindowHeight = atoi(argv[i + 2]);
  205. // Three arguments crunched
  206. i += 3;
  207. continue;
  208. }
  209. if (Str::StrCmp(argv[i], "-fullscreen") == 0)
  210. {
  211. mPreferredWindowFullscreen = true;
  212. // One argument crunched
  213. i++;
  214. continue;
  215. }
  216. // Next argument
  217. i++;
  218. }
  219. return true;
  220. }
  221. Device device;
  222. Device* GetDevice()
  223. {
  224. return &device;
  225. }
  226. } // namespace crown