Device.cpp 7.8 KB

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