Device.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "Types.h"
  30. #include "String.h"
  31. #include "Args.h"
  32. #include <cstdlib>
  33. namespace crown
  34. {
  35. const uint16_t Device::CROWN_MAJOR = 0;
  36. const uint16_t Device::CROWN_MINOR = 1;
  37. const uint16_t Device::CROWN_MICRO = 0;
  38. //-----------------------------------------------------------------------------
  39. Device::Device() :
  40. mPreferredWindowWidth(1000),
  41. mPreferredWindowHeight(625),
  42. mPreferredWindowFullscreen(false),
  43. mIsInit(false),
  44. mIsRunning(false),
  45. mRenderer(NULL)
  46. {
  47. string::strcpy(mPreferredRootPath, string::EMPTY);
  48. string::strcpy(mPreferredUserPath, string::EMPTY);
  49. }
  50. //-----------------------------------------------------------------------------
  51. Device::~Device()
  52. {
  53. }
  54. //-----------------------------------------------------------------------------
  55. bool Device::Init(int argc, char** argv)
  56. {
  57. if (ParseCommandLine(argc, argv) == false)
  58. {
  59. return false;
  60. }
  61. // Initialize
  62. Log::D("Initializing Device...");
  63. if (IsInit())
  64. {
  65. Log::E("Device is already initialized.");
  66. return false;
  67. }
  68. // Sets the root path
  69. // GetFilesystem()->Init(mPreferredRootPath.c_str(), mPreferredUserPath.c_str());
  70. // Creates the main window
  71. if (!os::create_render_window(0, 0, mPreferredWindowWidth, mPreferredWindowHeight, mPreferredWindowFullscreen))
  72. {
  73. Log::E("Unuble to create the main window.");
  74. return false;
  75. }
  76. Log::D("Window created.");
  77. // Creates the renderer
  78. if (!mRenderer)
  79. {
  80. mRenderer = Renderer::CreateRenderer();
  81. }
  82. Log::D("Renderer created.");
  83. os::init_input();
  84. mIsInit = true;
  85. StartRunning();
  86. Log::I("Crown Game Engine %d.%d.%d", CROWN_MAJOR, CROWN_MINOR, CROWN_MICRO);
  87. Log::I("Crown is up and running, enjoy!");
  88. return true;
  89. }
  90. //-----------------------------------------------------------------------------
  91. void Device::Shutdown()
  92. {
  93. if (!IsInit())
  94. {
  95. Log::E("Device is not initialized.");
  96. return;
  97. }
  98. Log::I("Releasing Renderer...");
  99. if (mRenderer)
  100. {
  101. Renderer::DestroyRenderer(mRenderer);
  102. }
  103. Log::I("Releasing Render Window...");
  104. os::destroy_render_window();
  105. mIsInit = false;
  106. }
  107. //-----------------------------------------------------------------------------
  108. bool Device::IsInit()
  109. {
  110. return mIsInit;
  111. }
  112. //-----------------------------------------------------------------------------
  113. Renderer* Device::GetRenderer()
  114. {
  115. return mRenderer;
  116. }
  117. //-----------------------------------------------------------------------------
  118. void Device::StartRunning()
  119. {
  120. if (!IsInit())
  121. {
  122. return;
  123. }
  124. mIsRunning = true;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void Device::StopRunning()
  128. {
  129. if (!IsInit())
  130. {
  131. return;
  132. }
  133. mIsRunning = false;
  134. }
  135. //-----------------------------------------------------------------------------
  136. bool Device::IsRunning() const
  137. {
  138. return mIsRunning;
  139. }
  140. //-----------------------------------------------------------------------------
  141. void Device::Frame()
  142. {
  143. os::event_loop();
  144. GetInputManager()->EventLoop();
  145. mRenderer->begin_frame();
  146. mRenderer->end_frame();
  147. os::swap_buffers();
  148. }
  149. //-----------------------------------------------------------------------------
  150. bool Device::ParseCommandLine(int argc, char** argv)
  151. {
  152. int32_t fullscreen = 0;
  153. ArgsOption options[] =
  154. {
  155. "help", AOA_NO_ARGUMENT, NULL, 'i',
  156. "root-path", AOA_REQUIRED_ARGUMENT, NULL, 'r',
  157. "user-path", AOA_REQUIRED_ARGUMENT, NULL, 'u',
  158. "width", AOA_REQUIRED_ARGUMENT, NULL, 'w',
  159. "height", AOA_REQUIRED_ARGUMENT, NULL, 'h',
  160. "fullscreen", AOA_NO_ARGUMENT, &fullscreen, 1,
  161. NULL, 0, NULL, 0
  162. };
  163. Args args(argc, argv, "", options);
  164. while (1)
  165. {
  166. int32_t ret = args.next_option();
  167. switch (ret)
  168. {
  169. case -1:
  170. {
  171. return true;
  172. }
  173. case 0:
  174. {
  175. mPreferredWindowFullscreen = fullscreen;
  176. break;
  177. }
  178. // Help
  179. case 'i':
  180. {
  181. PrintHelpMessage();
  182. return false;
  183. }
  184. // Root path
  185. case 'r':
  186. {
  187. if (args.option_argument() == NULL)
  188. {
  189. os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
  190. return false;
  191. }
  192. string::strcpy(mPreferredRootPath, args.option_argument());
  193. break;
  194. }
  195. // User path
  196. case 'u':
  197. {
  198. if (args.option_argument() == NULL)
  199. {
  200. os::printf("%s: error: missing absolute path after `--user-path`\n", argv[0]);
  201. return false;
  202. }
  203. string::strcpy(mPreferredUserPath, args.option_argument());
  204. break;
  205. }
  206. // Window width
  207. case 'w':
  208. {
  209. if (args.option_argument() == NULL)
  210. {
  211. os::printf("%s: error: missing width value after `--width`\n", argv[0]);
  212. return false;
  213. }
  214. mPreferredWindowWidth = atoi(args.option_argument());
  215. break;
  216. }
  217. // Window height
  218. case 'h':
  219. {
  220. if (args.option_argument() == NULL)
  221. {
  222. os::printf("%s: error: missing height value after `--height`\n", argv[0]);
  223. return false;
  224. }
  225. mPreferredWindowHeight = atoi(args.option_argument());
  226. break;
  227. }
  228. default:
  229. {
  230. break;
  231. }
  232. }
  233. }
  234. return true;
  235. }
  236. //-----------------------------------------------------------------------------
  237. void Device::PrintHelpMessage()
  238. {
  239. os::printf("Usage: crown [options]\n");
  240. os::printf("Options:\n\n");
  241. os::printf("All of the following options take precedence over\n");
  242. os::printf("environment variables and configuration files.\n\n");
  243. os::printf(" --help Show this help.\n");
  244. os::printf(" --root-path <path> Use <path> as the filesystem root path.\n");
  245. os::printf(" --user-path <path> Use <path> as the filesystem user path.\n");
  246. os::printf(" --width <width> Set the <width> of the render window.\n");
  247. os::printf(" --height <width> Set the <height> of the render window.\n");
  248. os::printf(" --fullscreen Start in fullscreen.\n");
  249. }
  250. Device device;
  251. Device* GetDevice()
  252. {
  253. return &device;
  254. }
  255. } // namespace crown