Device.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. mInputManager(NULL),
  51. mRenderer(NULL),
  52. mGarbageBin(NULL)
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. Device::~Device()
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. bool Device::Init(int argc, char** argv)
  61. {
  62. if (ParseCommandLine(argc, argv) == false)
  63. {
  64. return false;
  65. }
  66. // Initialize
  67. Log::D("Initializing Device...");
  68. if (IsInit())
  69. {
  70. Log::E("Device is already initialized.");
  71. return false;
  72. }
  73. // Sets the root path
  74. GetFilesystem()->Init(mPreferredRootPath.c_str(), mPreferredUserPath.c_str());
  75. // Creates the main window
  76. if (!os::create_render_window(0, 0, mPreferredWindowWidth, mPreferredWindowHeight, mPreferredWindowFullscreen))
  77. {
  78. Log::E("Unuble to create the main window.");
  79. return false;
  80. }
  81. Log::D("Window created.");
  82. // Creates the garbage bin
  83. mGarbageBin = new GarbageBin();
  84. // Creates the renderer
  85. if (!mRenderer)
  86. {
  87. mRenderer = Renderer::CreateRenderer();
  88. }
  89. Log::D("Renderer created.");
  90. // // Creates the input manager
  91. // mInputManager = GetInputManager();
  92. // Mouse* mouse = NULL;
  93. // Keyboard* keyboard = NULL;
  94. // Touch* touch = NULL;
  95. // if (mInputManager)
  96. // {
  97. // keyboard = mInputManager->GetKeyboard();
  98. // mouse = mInputManager->GetMouse();
  99. // touch = mInputManager->GetTouch();
  100. // }
  101. // if (mouse)
  102. // {
  103. // mouse->SetListener(mInputManager->GetEventDispatcher());
  104. // }
  105. // if (keyboard)
  106. // {
  107. // keyboard->SetListener(mInputManager->GetEventDispatcher());
  108. // }
  109. // if (touch)
  110. // {
  111. // touch->SetListener(mInputManager->GetEventDispatcher());
  112. // }
  113. Log::D("InputManager created.");
  114. mIsInit = true;
  115. StartRunning();
  116. Log::I("Crown Game Engine %d.%d.%d", CROWN_MAJOR, CROWN_MINOR, CROWN_MICRO);
  117. Log::I("Crown is up and running, enjoy!");
  118. return true;
  119. }
  120. //-----------------------------------------------------------------------------
  121. InputManager* Device::GetInputManager()
  122. {
  123. return mInputManager;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Device::Shutdown()
  127. {
  128. if (!IsInit())
  129. {
  130. Log::E("Device is not initialized.");
  131. return;
  132. }
  133. Log::I("Releasing GarbageBin...");
  134. if (mGarbageBin)
  135. {
  136. delete mGarbageBin;
  137. }
  138. Log::I("Releasing Renderer...");
  139. if (mRenderer)
  140. {
  141. Renderer::DestroyRenderer(mRenderer);
  142. }
  143. Log::I("Releasing InputManager...");
  144. if (mInputManager)
  145. {
  146. // do nothing
  147. }
  148. Log::I("Releasing Render Window...");
  149. os::destroy_render_window();
  150. mIsInit = false;
  151. }
  152. //-----------------------------------------------------------------------------
  153. bool Device::IsInit()
  154. {
  155. return mIsInit;
  156. }
  157. //-----------------------------------------------------------------------------
  158. Renderer* Device::GetRenderer()
  159. {
  160. return mRenderer;
  161. }
  162. //-----------------------------------------------------------------------------
  163. GarbageBin* Device::GetGarbageBin()
  164. {
  165. return mGarbageBin;
  166. }
  167. //-----------------------------------------------------------------------------
  168. void Device::StartRunning()
  169. {
  170. if (!IsInit())
  171. {
  172. return;
  173. }
  174. mIsRunning = true;
  175. }
  176. //-----------------------------------------------------------------------------
  177. void Device::StopRunning()
  178. {
  179. if (!IsInit())
  180. {
  181. return;
  182. }
  183. mIsRunning = false;
  184. }
  185. //-----------------------------------------------------------------------------
  186. bool Device::IsRunning() const
  187. {
  188. return mIsRunning;
  189. }
  190. //-----------------------------------------------------------------------------
  191. void Device::Frame()
  192. {
  193. os::event_loop();
  194. mInputManager->EventLoop();
  195. mRenderer->_BeginFrame();
  196. mRenderer->_EndFrame();
  197. os::swap_buffers();
  198. mGarbageBin->Empty();
  199. }
  200. //-----------------------------------------------------------------------------
  201. bool Device::ParseCommandLine(int argc, char** argv)
  202. {
  203. if (argc == 2 && Str::StrCmp(argv[1], "-help") == 0)
  204. {
  205. os::printf("Usage: %s [options]\n", argv[0]);
  206. os::printf("Options:\n\n");
  207. os::printf("All of the following options take precedence over\nenvironment variables and configuration files.\n\n");
  208. // Print options
  209. os::printf(" -help\t\t\t\t\tShow this help\n");
  210. os::printf(" -root-path <path>\t\t\tUse <path> as the filesystem root path\n");
  211. os::printf(" -user-path <path>\t\t\tUse <path> as the filesystem user path\n");
  212. os::printf(" -metrics <width> <height>\t\tSet the <width> and <height> of the render window\n");
  213. os::printf(" -fullscreen\t\t\t\tStart in fullscreen\n");
  214. return false;
  215. }
  216. // Parse command line arguments
  217. int i = 1;
  218. while (i < argc)
  219. {
  220. if (Str::StrCmp(argv[i], "-root-path") == 0)
  221. {
  222. if (argc < i + 2)
  223. {
  224. os::printf("%s: error: missing absolute path after `-root-path`\n", argv[0]);
  225. return false;
  226. }
  227. mPreferredRootPath = argv[i + 1];
  228. // Two arguments crunched
  229. i += 2;
  230. continue;
  231. }
  232. if (Str::StrCmp(argv[i], "-user-path") == 0)
  233. {
  234. if (argc < i + 2)
  235. {
  236. os::printf("%s: error: missing absolute path after `-user-path`\n", argv[0]);
  237. return false;
  238. }
  239. mPreferredUserPath = argv[i + 1];
  240. // Two arguments crunched
  241. i += 2;
  242. continue;
  243. }
  244. if (Str::StrCmp(argv[i], "-metrics") == 0)
  245. {
  246. if (argc < i + 3)
  247. {
  248. os::printf("%s: error: wrong number of arguments after `-metrics`\n", argv[0]);
  249. return false;
  250. }
  251. mPreferredWindowWidth = atoi(argv[i + 1]);
  252. mPreferredWindowHeight = atoi(argv[i + 2]);
  253. // Three arguments crunched
  254. i += 3;
  255. continue;
  256. }
  257. if (Str::StrCmp(argv[i], "-fullscreen") == 0)
  258. {
  259. mPreferredWindowFullscreen = true;
  260. // One argument crunched
  261. i++;
  262. continue;
  263. }
  264. // Next argument
  265. i++;
  266. }
  267. return true;
  268. }
  269. Device device;
  270. Device* GetDevice()
  271. {
  272. return &device;
  273. }
  274. } // namespace crown