main.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <windowsx.h>
  24. #define WM_USER_SET_WINDOW_SIZE (WM_USER+0)
  25. #define WM_USER_TOGGLE_WINDOW_FRAME (WM_USER+1)
  26. #define WM_USER_MOUSE_LOCK (WM_USER+2)
  27. #include "Crown.h"
  28. #include "OsTypes.h"
  29. #include "EventQueue.h"
  30. #include "BundleCompiler.h"
  31. #include "OS.h"
  32. #define ENTRY_DEFAULT_WIDTH 1024
  33. #define ENTRY_DEFAULT_HEIGHT 768
  34. namespace crown
  35. {
  36. extern void set_win_handle_window(HWND hwnd);
  37. //-----------------------------------------------------------------------------
  38. void init()
  39. {
  40. memory::init();
  41. os::init_os();
  42. }
  43. //-----------------------------------------------------------------------------
  44. void shutdown()
  45. {
  46. memory::shutdown();
  47. }
  48. //-----------------------------------------------------------------------------
  49. static KeyboardButton::Enum win_translate_key(int32_t winKey)
  50. {
  51. if ((winKey > 0x40 && winKey < 0x5B) || (winKey > 0x60 && winKey < 0x7B) || (winKey > 0x2F && winKey < 0x3A))
  52. {
  53. return (KeyboardButton::Enum)winKey;
  54. }
  55. switch (winKey)
  56. {
  57. case VK_BACK: return KeyboardButton::BACKSPACE;
  58. case VK_TAB: return KeyboardButton::TAB;
  59. case VK_SPACE: return KeyboardButton::SPACE;
  60. case VK_ESCAPE: return KeyboardButton::ESCAPE;
  61. case VK_RETURN: return KeyboardButton::ENTER;
  62. case VK_F1: return KeyboardButton::F1;
  63. case VK_F2: return KeyboardButton::F2;
  64. case VK_F3: return KeyboardButton::F3;
  65. case VK_F4: return KeyboardButton::F4;
  66. case VK_F5: return KeyboardButton::F5;
  67. case VK_F6: return KeyboardButton::F6;
  68. case VK_F7: return KeyboardButton::F7;
  69. case VK_F8: return KeyboardButton::F8;
  70. case VK_F9: return KeyboardButton::F9;
  71. case VK_F10: return KeyboardButton::F10;
  72. case VK_F11: return KeyboardButton::F11;
  73. case VK_F12: return KeyboardButton::F12;
  74. case VK_HOME: return KeyboardButton::HOME;
  75. case VK_LEFT: return KeyboardButton::LEFT;
  76. case VK_UP: return KeyboardButton::UP;
  77. case VK_RIGHT: return KeyboardButton::RIGHT;
  78. case VK_DOWN: return KeyboardButton::DOWN;
  79. case VK_PRIOR: return KeyboardButton::PAGE_UP;
  80. case VK_NEXT: return KeyboardButton::PAGE_DOWN;
  81. case VK_LSHIFT: return KeyboardButton::LSHIFT;
  82. case VK_RSHIFT: return KeyboardButton::RSHIFT;
  83. case VK_LCONTROL: return KeyboardButton::LCONTROL;
  84. case VK_RCONTROL: return KeyboardButton::RCONTROL;
  85. case VK_CAPITAL: return KeyboardButton::CAPS_LOCK;
  86. case VK_LMENU: return KeyboardButton::LALT;
  87. case VK_RMENU: return KeyboardButton::RALT;
  88. case VK_LWIN: return KeyboardButton::LSUPER;
  89. case VK_RWIN: return KeyboardButton::RSUPER;
  90. case VK_NUMPAD0: return KeyboardButton::KP_0;
  91. case VK_NUMPAD1: return KeyboardButton::KP_1;
  92. case VK_NUMPAD2: return KeyboardButton::KP_2;
  93. case VK_NUMPAD3: return KeyboardButton::KP_3;
  94. case VK_NUMPAD4: return KeyboardButton::KP_4;
  95. case VK_NUMPAD5: return KeyboardButton::KP_5;
  96. case VK_NUMPAD6: return KeyboardButton::KP_6;
  97. case VK_NUMPAD7: return KeyboardButton::KP_7;
  98. case VK_NUMPAD8: return KeyboardButton::KP_8;
  99. case VK_NUMPAD9: return KeyboardButton::KP_9;
  100. default: return KeyboardButton::NONE;
  101. }
  102. }
  103. //-----------------------------------------------------------------------------
  104. class CE_EXPORT WindowsDevice : public Device
  105. {
  106. public:
  107. WindowsDevice()
  108. : m_hwnd(0)
  109. , m_rect()
  110. , m_style(0)
  111. , m_width(0)
  112. , m_height(0)
  113. , m_old_width(0)
  114. , m_old_height(0)
  115. , m_frame_width(0)
  116. , m_frame_height(0)
  117. , m_aspect_ratio(0.0f)
  118. , m_x(0)
  119. , m_y(0)
  120. , m_frame(true)
  121. , m_mouse_lock(false)
  122. , m_started(false)
  123. , m_exit(false)
  124. {
  125. }
  126. //-----------------------------------------------------------------------------
  127. void init(int argc, char** argv)
  128. {
  129. parse_command_line(argc, argv);
  130. check_preferred_settings();
  131. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  132. if (m_compile == 1)
  133. {
  134. m_bundle_compiler = CE_NEW(default_allocator(), BundleCompiler);
  135. if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir))
  136. {
  137. CE_DELETE(default_allocator(), m_bundle_compiler);
  138. Log::e("Exiting.");
  139. exit(EXIT_FAILURE);
  140. }
  141. if (!m_continue)
  142. {
  143. CE_DELETE(default_allocator(), m_bundle_compiler);
  144. exit(EXIT_SUCCESS);
  145. }
  146. }
  147. #endif
  148. }
  149. //-----------------------------------------------------------------------------
  150. void shutdown()
  151. {
  152. #if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
  153. CE_DELETE(default_allocator(), m_bundle_compiler);
  154. #endif
  155. }
  156. //-----------------------------------------------------------------------------
  157. int32_t run(int argc, char** argv)
  158. {
  159. init(argc, argv);
  160. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  161. WNDCLASSEX wnd;
  162. memset(&wnd, 0, sizeof(wnd));
  163. wnd.cbSize = sizeof(wnd);
  164. wnd.lpfnWndProc = DefWindowProc;
  165. wnd.hInstance = instance;
  166. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  167. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  168. wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  169. wnd.lpszClassName = "crown_letterbox";
  170. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  171. RegisterClassExA(&wnd);
  172. memset(&wnd, 0, sizeof(wnd));
  173. wnd.cbSize = sizeof(wnd);
  174. wnd.style = CS_HREDRAW | CS_VREDRAW;
  175. wnd.lpfnWndProc = window_proc;
  176. // wnd.lpfnWndProc = DefWindowProc;
  177. wnd.hInstance = instance;
  178. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  179. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  180. wnd.lpszClassName = "crown";
  181. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  182. RegisterClassExA(&wnd);
  183. HWND hwnd = CreateWindowA("crown_letterbox", "CROWN", WS_POPUP|WS_SYSMENU, -32000, -32000, 0,
  184. 0, NULL, NULL, instance, 0);
  185. m_hwnd = CreateWindowA("crown", "CROWN", WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0, 0, ENTRY_DEFAULT_WIDTH,
  186. ENTRY_DEFAULT_HEIGHT, 0, NULL, instance, 0);
  187. set_win_handle_window(m_hwnd);
  188. adjust(ENTRY_DEFAULT_WIDTH, ENTRY_DEFAULT_HEIGHT, true);
  189. m_width = ENTRY_DEFAULT_WIDTH;
  190. m_height = ENTRY_DEFAULT_HEIGHT;
  191. m_old_width = ENTRY_DEFAULT_WIDTH;
  192. m_old_height = ENTRY_DEFAULT_HEIGHT;
  193. m_argc = argc;
  194. m_argv = argv;
  195. OsThread thread("game-loop");
  196. thread.start(WindowsDevice::main_loop, this);
  197. m_started = true;
  198. // m_eventQueue.postSizeEvent(m_width, m_height);
  199. MSG msg;
  200. msg.message = WM_NULL;
  201. while (!m_exit)
  202. {
  203. WaitMessage();
  204. while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) != 0)
  205. {
  206. TranslateMessage(&msg);
  207. DispatchMessage(&msg);
  208. }
  209. }
  210. thread.stop();
  211. shutdown();
  212. DestroyWindow(m_hwnd);
  213. DestroyWindow(hwnd);
  214. return 0;
  215. }
  216. //-----------------------------------------------------------------------------
  217. int32_t loop()
  218. {
  219. Device::init();
  220. while(!process_events() && is_running())
  221. {
  222. Device::frame();
  223. m_keyboard->update();
  224. m_mouse->update();
  225. }
  226. Device::shutdown();
  227. m_exit = true;
  228. return 0;
  229. }
  230. //-----------------------------------------------------------------------------
  231. void adjust(uint32_t width, uint32_t height, bool window_frame)
  232. {
  233. m_width = width;
  234. m_height = height;
  235. m_aspect_ratio = float(width) / float(height);
  236. ShowWindow(m_hwnd, SW_SHOWNORMAL);
  237. RECT rect;
  238. RECT newrect = {0, 0, (LONG)width, (LONG)height};
  239. DWORD style = WS_POPUP|WS_SYSMENU;
  240. if (m_frame)
  241. {
  242. GetWindowRect(m_hwnd, &m_rect);
  243. m_style = GetWindowLong(m_hwnd, GWL_STYLE);
  244. }
  245. if (window_frame)
  246. {
  247. rect = m_rect;
  248. style = m_style;
  249. }
  250. else
  251. {
  252. HMONITOR monitor = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTONEAREST);
  253. MONITORINFO mi;
  254. mi.cbSize = sizeof(mi);
  255. GetMonitorInfo(monitor, &mi);
  256. newrect = mi.rcMonitor;
  257. rect = mi.rcMonitor;
  258. }
  259. SetWindowLong(m_hwnd, GWL_STYLE, style);
  260. uint32_t prewidth = newrect.right - newrect.left;
  261. uint32_t preheight = newrect.bottom - newrect.top;
  262. AdjustWindowRect(&newrect, style, FALSE);
  263. m_frame_width = (newrect.right - newrect.left) - prewidth;
  264. m_frame_height = (newrect.bottom - newrect.top) - preheight;
  265. UpdateWindow(m_hwnd);
  266. if (rect.left < 0 || rect.top < 0)
  267. {
  268. rect.left = 0;
  269. rect.top = 0;
  270. }
  271. int32_t left_t = rect.left;
  272. int32_t top_t = rect.top;
  273. int32_t width_t = (newrect.right-newrect.left);
  274. int32_t height_t = (newrect.bottom-newrect.top);
  275. if (!window_frame)
  276. {
  277. float aspect_ratio = 1.0f / m_aspect_ratio;
  278. width_t = math::max(uint32_t(ENTRY_DEFAULT_WIDTH / 4), width);
  279. height_t = uint32_t(float(width) * aspect_ratio);
  280. left_t = newrect.left+(newrect.right-newrect.left-width) / 2;
  281. top_t = newrect.top+(newrect.bottom-newrect.top-height) / 2;
  282. }
  283. HWND parent = GetWindow(m_hwnd, GW_OWNER);
  284. if (NULL != parent)
  285. {
  286. if (window_frame)
  287. {
  288. SetWindowPos(parent, HWND_TOP, -32000, -32000, 0, 0, SWP_SHOWWINDOW);
  289. }
  290. else
  291. {
  292. SetWindowPos(parent, HWND_TOP, newrect.left, newrect.top, newrect.right-newrect.left, newrect.bottom-newrect.top, SWP_SHOWWINDOW);
  293. }
  294. }
  295. SetWindowPos(m_hwnd, HWND_TOP, left_t, top_t, width_t, height_t, SWP_SHOWWINDOW);
  296. ShowWindow(m_hwnd, SW_RESTORE);
  297. m_frame = window_frame;
  298. }
  299. //-----------------------------------------------------------------------------
  300. void set_mouse_pos(int32_t x, int32_t y)
  301. {
  302. POINT pt = {x, y};
  303. ClientToScreen(m_hwnd, &pt);
  304. SetCursorPos(pt.x, pt.y);
  305. }
  306. //-----------------------------------------------------------------------------
  307. void set_mouse_lock(bool lock)
  308. {
  309. if (lock != m_mouse_lock)
  310. {
  311. if (lock)
  312. {
  313. m_x = m_width / 2;
  314. m_y = m_height / 2;
  315. ShowCursor(false);
  316. set_mouse_pos(m_x, m_y);
  317. }
  318. else
  319. {
  320. set_mouse_pos(m_x, m_y);
  321. ShowCursor(true);
  322. }
  323. m_mouse_lock = lock;
  324. }
  325. }
  326. //-----------------------------------------------------------------------------
  327. static int32_t main_loop(void* data)
  328. {
  329. return ((WindowsDevice*)data)->loop();
  330. }
  331. //-----------------------------------------------------------------------------
  332. bool process_events()
  333. {
  334. uint32_t type = 0;
  335. do
  336. {
  337. type = m_queue.event_type();
  338. if (type != 0)
  339. {
  340. switch (type)
  341. {
  342. case OsEvent::MOUSE:
  343. {
  344. OsMouseEvent ev;
  345. m_queue.get_next_event(&ev);
  346. switch (ev.type)
  347. {
  348. case OsMouseEvent::BUTTON: m_mouse->set_button_state(ev.x, ev.y, ev.button, ev.pressed); break;
  349. case OsMouseEvent::MOVE: m_mouse->set_position(ev.x, ev.y); break;
  350. default: CE_FATAL("Oops, unknown mouse event type"); break;
  351. }
  352. break;
  353. }
  354. case OsEvent::KEYBOARD:
  355. {
  356. OsKeyboardEvent ev;
  357. m_queue.get_next_event(&ev);
  358. m_keyboard->set_button_state(ev.button, ev.pressed);
  359. break;
  360. }
  361. case OsEvent::METRICS:
  362. {
  363. OsMetricsEvent ev;
  364. m_queue.get_next_event(&ev);
  365. m_mouse->set_metrics(ev.width, ev.height);
  366. break;
  367. }
  368. case OsEvent::EXIT:
  369. {
  370. return true;
  371. }
  372. default:
  373. {
  374. Log::d("Unmanaged. type: %d", type);
  375. break;
  376. }
  377. }
  378. }
  379. }
  380. while (type != 0);
  381. return false;
  382. }
  383. //-----------------------------------------------------------------------------
  384. LRESULT bump_events(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
  385. {
  386. if (m_started)
  387. {
  388. switch (id)
  389. {
  390. case WM_USER_SET_WINDOW_SIZE:
  391. {
  392. uint32_t width = GET_X_LPARAM(lparam);
  393. uint32_t height = GET_Y_LPARAM(lparam);
  394. adjust(width, height, true);
  395. break;
  396. }
  397. case WM_USER_TOGGLE_WINDOW_FRAME:
  398. {
  399. if (m_frame)
  400. {
  401. m_old_width = m_width;
  402. m_old_height = m_height;
  403. }
  404. adjust(m_old_width, m_old_height, !m_frame);
  405. break;
  406. }
  407. case WM_USER_MOUSE_LOCK:
  408. {
  409. set_mouse_lock(!!lparam);
  410. break;
  411. }
  412. case WM_DESTROY:
  413. {
  414. break;
  415. }
  416. case WM_QUIT:
  417. case WM_CLOSE:
  418. {
  419. m_exit = true;
  420. OsExitEvent event;
  421. event.code = 0;
  422. m_queue.push_event(OsEvent::EXIT, &event, sizeof(OsExitEvent));
  423. break;
  424. }
  425. case WM_SIZING:
  426. {
  427. RECT& rect = *(RECT*)lparam;
  428. uint32_t width = rect.right - rect.left - m_frame_width;
  429. uint32_t height = rect.bottom - rect.top - m_frame_height;
  430. //Recalculate size according to aspect ratio
  431. switch (wparam)
  432. {
  433. case WMSZ_LEFT:
  434. case WMSZ_RIGHT:
  435. {
  436. float aspect_ratio = 1.0f / m_aspect_ratio;
  437. width = math::max((uint32_t)ENTRY_DEFAULT_WIDTH / 4, width);
  438. height = uint32_t(float(width)*aspect_ratio);
  439. }
  440. break;
  441. default:
  442. {
  443. float aspect_ratio = m_aspect_ratio;
  444. height = math::max((uint32_t)ENTRY_DEFAULT_HEIGHT / 4, height);
  445. width = uint32_t(float(height)*aspect_ratio);
  446. }
  447. break;
  448. }
  449. //Recalculate position using different anchor points
  450. switch(wparam)
  451. {
  452. case WMSZ_LEFT:
  453. case WMSZ_TOPLEFT:
  454. case WMSZ_BOTTOMLEFT:
  455. rect.left = rect.right - width - m_frame_width;
  456. rect.bottom = rect.top + height + m_frame_height;
  457. break;
  458. default:
  459. rect.right = rect.left + width + m_frame_width;
  460. rect.bottom = rect.top + height + m_frame_height;
  461. break;
  462. }
  463. // m_eventQueue.postSizeEvent(m_width, m_height);
  464. return 0;
  465. }
  466. case WM_SIZE:
  467. {
  468. uint32_t width = GET_X_LPARAM(lparam);
  469. uint32_t height = GET_Y_LPARAM(lparam);
  470. m_width = width;
  471. m_height = height;
  472. // m_eventQueue.postSizeEvent(m_width, m_height);
  473. break;
  474. }
  475. case WM_SYSCOMMAND:
  476. {
  477. switch (wparam)
  478. {
  479. case SC_MINIMIZE:
  480. case SC_RESTORE:
  481. {
  482. HWND parent = GetWindow(hwnd, GW_OWNER);
  483. if (NULL != parent)
  484. {
  485. PostMessage(parent, id, wparam, lparam);
  486. }
  487. }
  488. }
  489. break;
  490. }
  491. case WM_MOUSEMOVE:
  492. {
  493. int32_t mx = GET_X_LPARAM(lparam);
  494. int32_t my = GET_Y_LPARAM(lparam);
  495. if (m_mouse_lock)
  496. {
  497. mx -= m_x;
  498. my -= m_y;
  499. if (mx == 0 && my == 0)
  500. {
  501. break;
  502. }
  503. set_mouse_pos(m_x, m_y);
  504. }
  505. OsMouseEvent event;
  506. event.type = OsMouseEvent::MOVE;
  507. event.x = mx;
  508. event.y = my;
  509. m_queue.push_event(OsEvent::MOUSE, &event, sizeof(OsMouseEvent));
  510. break;
  511. }
  512. case WM_LBUTTONDOWN:
  513. case WM_LBUTTONUP:
  514. {
  515. int32_t mx = GET_X_LPARAM(lparam);
  516. int32_t my = GET_Y_LPARAM(lparam);
  517. OsMouseEvent event;
  518. event.type = OsMouseEvent::BUTTON;
  519. event.button = MouseButton::LEFT;
  520. event.x = mx;
  521. event.y = my;
  522. event.pressed = id == WM_LBUTTONDOWN ? true : false;
  523. m_queue.push_event(OsEvent::MOUSE, &event, sizeof(OsMouseEvent));
  524. break;
  525. }
  526. case WM_RBUTTONUP:
  527. case WM_RBUTTONDOWN:
  528. {
  529. int32_t mx = GET_X_LPARAM(lparam);
  530. int32_t my = GET_Y_LPARAM(lparam);
  531. OsMouseEvent event;
  532. event.type = OsMouseEvent::BUTTON;
  533. event.button = MouseButton::RIGHT;
  534. event.x = mx;
  535. event.y = my;
  536. event.pressed = id == WM_LBUTTONDOWN ? true : false;
  537. m_queue.push_event(OsEvent::MOUSE, &event, sizeof(OsMouseEvent));
  538. break;
  539. }
  540. case WM_MBUTTONDOWN:
  541. case WM_MBUTTONUP:
  542. {
  543. int32_t mx = GET_X_LPARAM(lparam);
  544. int32_t my = GET_Y_LPARAM(lparam);
  545. OsMouseEvent event;
  546. event.type = OsMouseEvent::BUTTON;
  547. event.button = MouseButton::MIDDLE;
  548. event.x = mx;
  549. event.y = my;
  550. event.pressed = id == WM_LBUTTONDOWN ? true : false;
  551. m_queue.push_event(OsEvent::MOUSE, &event, sizeof(OsMouseEvent));
  552. break;
  553. }
  554. case WM_KEYDOWN:
  555. case WM_SYSKEYDOWN:
  556. case WM_KEYUP:
  557. case WM_SYSKEYUP:
  558. {
  559. KeyboardButton::Enum kb = win_translate_key(wparam);
  560. int32_t modifier_mask = 0;
  561. if (kb == KeyboardButton::LSHIFT || kb == KeyboardButton::RSHIFT)
  562. {
  563. (id == WM_KEYDOWN || id == WM_SYSKEYDOWN) ? modifier_mask |= ModifierButton::SHIFT : modifier_mask &= ~ModifierButton::SHIFT;
  564. }
  565. else if (kb == KeyboardButton::LCONTROL || kb == KeyboardButton::RCONTROL)
  566. {
  567. (id == WM_KEYDOWN || id == WM_SYSKEYDOWN) ? modifier_mask |= ModifierButton::CTRL : modifier_mask &= ~ModifierButton::CTRL;
  568. }
  569. else if (kb == KeyboardButton::LALT || kb == KeyboardButton::RALT)
  570. {
  571. (id == WM_KEYDOWN || id == WM_SYSKEYDOWN) ? modifier_mask |= ModifierButton::ALT : modifier_mask &= ~ModifierButton::ALT;
  572. }
  573. OsKeyboardEvent event;
  574. event.button = kb;
  575. event.modifier = modifier_mask;
  576. event.pressed = (id == WM_KEYDOWN || id == WM_SYSKEYDOWN);
  577. m_queue.push_event(OsEvent::KEYBOARD, &event, sizeof(OsKeyboardEvent));
  578. break;
  579. }
  580. default:
  581. break;
  582. }
  583. }
  584. return DefWindowProc(hwnd, id, wparam, lparam);
  585. }
  586. //-----------------------------------------------------------------------------
  587. void parse_command_line(int argc, char** argv)
  588. {
  589. static const char* help_message =
  590. "Usage: crown [options]\n"
  591. "Options:\n\n"
  592. "All of the following options take precedence over\n"
  593. "environment variables and configuration files.\n\n"
  594. " --help Show this help.\n"
  595. " --bundle-dir <path> Use <path> as the source directory for compiled resources.\n"
  596. " --width <width> Set the <width> of the main window.\n"
  597. " --height <width> Set the <height> of the main window.\n"
  598. " --fullscreen Start in fullscreen.\n"
  599. " --parent-window <handle> Set the parent window <handle> of the main window.\n"
  600. " Used only by tools.\n"
  601. "\nAvailable only in debug and development builds:\n\n"
  602. " --source-dir <path> Use <path> as the source directory for resource compilation.\n"
  603. " --compile Run the engine as resource compiler.\n"
  604. " --continue Do a full compile of the resources and continue the execution.\n";
  605. static ArgsOption options[] =
  606. {
  607. { "help", AOA_NO_ARGUMENT, NULL, 'i' },
  608. { "source-dir", AOA_REQUIRED_ARGUMENT, NULL, 's' },
  609. { "bundle-dir", AOA_REQUIRED_ARGUMENT, NULL, 'b' },
  610. { "compile", AOA_NO_ARGUMENT, &m_compile, 1 },
  611. { "continue", AOA_NO_ARGUMENT, &m_continue, 1 },
  612. { "width", AOA_REQUIRED_ARGUMENT, NULL, 'w' },
  613. { "height", AOA_REQUIRED_ARGUMENT, NULL, 'h' },
  614. { "fullscreen", AOA_NO_ARGUMENT, &m_fullscreen, 1 },
  615. { "parent-window", AOA_REQUIRED_ARGUMENT, NULL, 'p' },
  616. { NULL, 0, NULL, 0 }
  617. };
  618. Args args(argc, argv, "", options);
  619. int32_t opt;
  620. while ((opt = args.getopt()) != -1)
  621. {
  622. switch (opt)
  623. {
  624. case 0:
  625. {
  626. break;
  627. }
  628. // Source directory
  629. case 's':
  630. {
  631. string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
  632. break;
  633. }
  634. // Bundle directory
  635. case 'b':
  636. {
  637. string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
  638. break;
  639. }
  640. // Window width
  641. case 'w':
  642. {
  643. m_width = atoi(args.optarg());
  644. break;
  645. }
  646. // Window height
  647. case 'h':
  648. {
  649. m_height = atoi(args.optarg());
  650. break;
  651. }
  652. // Parent window
  653. case 'p':
  654. {
  655. m_parent_window_handle = string::parse_uint(args.optarg());
  656. break;
  657. }
  658. case 'i':
  659. case '?':
  660. default:
  661. {
  662. os::printf(help_message);
  663. exit(EXIT_FAILURE);
  664. }
  665. }
  666. }
  667. }
  668. //-----------------------------------------------------------------------------
  669. void check_preferred_settings()
  670. {
  671. if (m_compile == 1)
  672. {
  673. if (string::strcmp(m_source_dir, "") == 0)
  674. {
  675. Log::e("You have to specify the source directory when running in compile mode.");
  676. exit(EXIT_FAILURE);
  677. }
  678. if (!os::is_absolute_path(m_source_dir))
  679. {
  680. Log::e("The source directory must be absolute.");
  681. exit(EXIT_FAILURE);
  682. }
  683. }
  684. if (!os::is_absolute_path(m_bundle_dir))
  685. {
  686. Log::e("The bundle directory must be absolute.");
  687. exit(EXIT_FAILURE);
  688. }
  689. // if (m_width == 0 || m_height == 0)
  690. // {
  691. // Log::e("Window width and height must be greater than zero.");
  692. // exit(EXIT_FAILURE);
  693. // }
  694. }
  695. private:
  696. static LRESULT CALLBACK window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam);
  697. public:
  698. HWND m_hwnd;
  699. RECT m_rect;
  700. DWORD m_style;
  701. uint32_t m_width;
  702. uint32_t m_height;
  703. uint32_t m_old_width;
  704. uint32_t m_old_height;
  705. uint32_t m_frame_width;
  706. uint32_t m_frame_height;
  707. float m_aspect_ratio;
  708. int32_t m_x;
  709. int32_t m_y;
  710. bool m_frame;
  711. bool m_mouse_lock;
  712. bool m_started;
  713. bool m_exit;
  714. int32_t m_argc;
  715. char** m_argv;
  716. uint32_t m_parent_window_handle;
  717. int32_t m_fullscreen;
  718. int32_t m_compile;
  719. int32_t m_continue;
  720. EventQueue m_queue;
  721. };
  722. WindowsDevice* engine;
  723. LRESULT CALLBACK WindowsDevice::window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
  724. {
  725. return ((WindowsDevice*)engine)->bump_events(hwnd, id, wparam, lparam);
  726. }
  727. } // namespace crown
  728. int main(int argc, char** argv)
  729. {
  730. using namespace crown;
  731. init();
  732. engine = CE_NEW(default_allocator(), WindowsDevice)();
  733. set_device(engine);
  734. int32_t ret = ((WindowsDevice*)engine)->run(argc, argv);
  735. CE_DELETE(default_allocator(), engine);
  736. shutdown();
  737. return ret;
  738. }