main_windows.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #if CROWN_PLATFORM_WINDOWS
  7. #include "device.h"
  8. #include "os_event_queue.h"
  9. #include "thread.h"
  10. #include <bgfx/bgfxplatform.h>
  11. #include <winsock2.h>
  12. #ifndef WIN32_LEAN_AND_MEAN
  13. #define WIN32_LEAN_AND_MEAN
  14. #endif
  15. #include <windowsx.h>
  16. #include <xinput.h>
  17. namespace crown
  18. {
  19. static KeyboardButton::Enum win_translate_key(s32 winkey)
  20. {
  21. switch (winkey)
  22. {
  23. case VK_BACK: return KeyboardButton::BACKSPACE;
  24. case VK_TAB: return KeyboardButton::TAB;
  25. case VK_SPACE: return KeyboardButton::SPACE;
  26. case VK_ESCAPE: return KeyboardButton::ESCAPE;
  27. case VK_RETURN: return KeyboardButton::ENTER;
  28. case VK_F1: return KeyboardButton::F1;
  29. case VK_F2: return KeyboardButton::F2;
  30. case VK_F3: return KeyboardButton::F3;
  31. case VK_F4: return KeyboardButton::F4;
  32. case VK_F5: return KeyboardButton::F5;
  33. case VK_F6: return KeyboardButton::F6;
  34. case VK_F7: return KeyboardButton::F7;
  35. case VK_F8: return KeyboardButton::F8;
  36. case VK_F9: return KeyboardButton::F9;
  37. case VK_F10: return KeyboardButton::F10;
  38. case VK_F11: return KeyboardButton::F11;
  39. case VK_F12: return KeyboardButton::F12;
  40. case VK_HOME: return KeyboardButton::HOME;
  41. case VK_LEFT: return KeyboardButton::LEFT;
  42. case VK_UP: return KeyboardButton::UP;
  43. case VK_RIGHT: return KeyboardButton::RIGHT;
  44. case VK_DOWN: return KeyboardButton::DOWN;
  45. case VK_PRIOR: return KeyboardButton::PAGE_UP;
  46. case VK_NEXT: return KeyboardButton::PAGE_DOWN;
  47. case VK_INSERT: return KeyboardButton::INSERT;
  48. case VK_DELETE: return KeyboardButton::DELETE;
  49. case VK_END: return KeyboardButton::END;
  50. case VK_LSHIFT: return KeyboardButton::LEFT_SHIFT;
  51. case VK_RSHIFT: return KeyboardButton::RIGHT_SHIFT;
  52. case VK_LCONTROL: return KeyboardButton::LEFT_CTRL;
  53. case VK_RCONTROL: return KeyboardButton::RIGHT_CTRL;
  54. case VK_CAPITAL: return KeyboardButton::CAPS_LOCK;
  55. case VK_LMENU: return KeyboardButton::LEFT_ALT;
  56. case VK_RMENU: return KeyboardButton::RIGHT_ALT;
  57. case VK_LWIN: return KeyboardButton::LEFT_SUPER;
  58. case VK_RWIN: return KeyboardButton::RIGHT_SUPER;
  59. case VK_NUMLOCK: return KeyboardButton::NUM_LOCK;
  60. // case VK_RETURN: return KeyboardButton::NUMPAD_ENTER;
  61. case VK_DECIMAL: return KeyboardButton::NUMPAD_DELETE;
  62. case VK_MULTIPLY: return KeyboardButton::NUMPAD_MULTIPLY;
  63. case VK_ADD: return KeyboardButton::NUMPAD_ADD;
  64. case VK_SUBTRACT: return KeyboardButton::NUMPAD_SUBTRACT;
  65. case VK_DIVIDE: return KeyboardButton::NUMPAD_DIVIDE;
  66. case VK_NUMPAD0: return KeyboardButton::NUMPAD_0;
  67. case VK_NUMPAD1: return KeyboardButton::NUMPAD_1;
  68. case VK_NUMPAD2: return KeyboardButton::NUMPAD_2;
  69. case VK_NUMPAD3: return KeyboardButton::NUMPAD_3;
  70. case VK_NUMPAD4: return KeyboardButton::NUMPAD_4;
  71. case VK_NUMPAD5: return KeyboardButton::NUMPAD_5;
  72. case VK_NUMPAD6: return KeyboardButton::NUMPAD_6;
  73. case VK_NUMPAD7: return KeyboardButton::NUMPAD_7;
  74. case VK_NUMPAD8: return KeyboardButton::NUMPAD_8;
  75. case VK_NUMPAD9: return KeyboardButton::NUMPAD_9;
  76. case '0': return KeyboardButton::NUMBER_0;
  77. case '1': return KeyboardButton::NUMBER_1;
  78. case '2': return KeyboardButton::NUMBER_2;
  79. case '3': return KeyboardButton::NUMBER_3;
  80. case '4': return KeyboardButton::NUMBER_4;
  81. case '5': return KeyboardButton::NUMBER_5;
  82. case '6': return KeyboardButton::NUMBER_6;
  83. case '7': return KeyboardButton::NUMBER_7;
  84. case '8': return KeyboardButton::NUMBER_8;
  85. case '9': return KeyboardButton::NUMBER_9;
  86. case 'A': return KeyboardButton::A;
  87. case 'B': return KeyboardButton::B;
  88. case 'C': return KeyboardButton::C;
  89. case 'D': return KeyboardButton::D;
  90. case 'E': return KeyboardButton::E;
  91. case 'F': return KeyboardButton::F;
  92. case 'G': return KeyboardButton::G;
  93. case 'H': return KeyboardButton::H;
  94. case 'I': return KeyboardButton::I;
  95. case 'J': return KeyboardButton::J;
  96. case 'K': return KeyboardButton::K;
  97. case 'L': return KeyboardButton::L;
  98. case 'M': return KeyboardButton::M;
  99. case 'N': return KeyboardButton::N;
  100. case 'O': return KeyboardButton::O;
  101. case 'P': return KeyboardButton::P;
  102. case 'Q': return KeyboardButton::Q;
  103. case 'R': return KeyboardButton::R;
  104. case 'S': return KeyboardButton::S;
  105. case 'T': return KeyboardButton::T;
  106. case 'U': return KeyboardButton::U;
  107. case 'V': return KeyboardButton::V;
  108. case 'W': return KeyboardButton::W;
  109. case 'X': return KeyboardButton::X;
  110. case 'Y': return KeyboardButton::Y;
  111. case 'Z': return KeyboardButton::Z;
  112. default: return KeyboardButton::COUNT;
  113. }
  114. }
  115. struct XinputToJoypad
  116. {
  117. WORD bit;
  118. JoypadButton::Enum button;
  119. };
  120. static XinputToJoypad s_xinput_to_joypad[] =
  121. {
  122. { XINPUT_GAMEPAD_DPAD_UP, JoypadButton::UP },
  123. { XINPUT_GAMEPAD_DPAD_DOWN, JoypadButton::DOWN },
  124. { XINPUT_GAMEPAD_DPAD_LEFT, JoypadButton::LEFT },
  125. { XINPUT_GAMEPAD_DPAD_RIGHT, JoypadButton::RIGHT },
  126. { XINPUT_GAMEPAD_START, JoypadButton::START },
  127. { XINPUT_GAMEPAD_BACK, JoypadButton::BACK },
  128. { XINPUT_GAMEPAD_LEFT_THUMB, JoypadButton::LEFT_THUMB },
  129. { XINPUT_GAMEPAD_RIGHT_THUMB, JoypadButton::RIGHT_THUMB },
  130. { XINPUT_GAMEPAD_LEFT_SHOULDER, JoypadButton::LEFT_SHOULDER },
  131. { XINPUT_GAMEPAD_RIGHT_SHOULDER, JoypadButton::RIGHT_SHOULDER },
  132. { XINPUT_GAMEPAD_A, JoypadButton::A },
  133. { XINPUT_GAMEPAD_B, JoypadButton::B },
  134. { XINPUT_GAMEPAD_X, JoypadButton::X },
  135. { XINPUT_GAMEPAD_Y, JoypadButton::Y }
  136. };
  137. struct Joypad
  138. {
  139. void init()
  140. {
  141. memset(&_state, 0, sizeof(_state));
  142. memset(&_axis, 0, sizeof(_axis));
  143. memset(&_connected, 0, sizeof(_connected));
  144. }
  145. void update(OsEventQueue& queue)
  146. {
  147. for (u8 i = 0; i < CROWN_MAX_JOYPADS; ++i)
  148. {
  149. XINPUT_STATE state;
  150. memset(&state, 0, sizeof(state));
  151. const DWORD result = XInputGetState(i, &state);
  152. const bool connected = result == ERROR_SUCCESS;
  153. if (connected != _connected[i])
  154. queue.push_joypad_event(i, connected);
  155. _connected[i] = connected;
  156. if (!connected || state.dwPacketNumber == _state[i].dwPacketNumber)
  157. continue;
  158. XINPUT_GAMEPAD& gamepad = _state[i].Gamepad;
  159. const WORD diff = state.Gamepad.wButtons ^ gamepad.wButtons;
  160. const WORD curr = state.Gamepad.wButtons;
  161. if (diff != 0)
  162. {
  163. for (u8 bb = 0; bb < CE_COUNTOF(s_xinput_to_joypad); ++bb)
  164. {
  165. WORD bit = s_xinput_to_joypad[bb].bit;
  166. if (bit & diff)
  167. {
  168. queue.push_joypad_event(i, s_xinput_to_joypad[bb].button, (curr & bit) != 0);
  169. gamepad.wButtons = curr;
  170. }
  171. }
  172. }
  173. if (state.Gamepad.sThumbLX != gamepad.sThumbLX)
  174. {
  175. SHORT value = state.Gamepad.sThumbLX;
  176. value = value > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE || value < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE
  177. ? value : 0;
  178. _axis[0].lx = value != 0
  179. ? f32(value + (value < 0 ? XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE : -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)) / f32(INT16_MAX - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
  180. : 0.0f
  181. ;
  182. queue.push_joypad_event(i, JoypadAxis::LEFT, _axis[0].lx, _axis[0].ly, _axis[0].lz);
  183. gamepad.sThumbLX = state.Gamepad.sThumbLX;
  184. }
  185. if (state.Gamepad.sThumbLY != gamepad.sThumbLY)
  186. {
  187. SHORT value = state.Gamepad.sThumbLY;
  188. value = value > XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE || value < -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE
  189. ? value : 0;
  190. _axis[0].ly = value != 0
  191. ? f32(value + (value < 0 ? XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE : -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)) / f32(INT16_MAX - XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE)
  192. : 0.0f
  193. ;
  194. queue.push_joypad_event(i, JoypadAxis::LEFT, _axis[0].lx, _axis[0].ly, _axis[0].lz);
  195. gamepad.sThumbLY = state.Gamepad.sThumbLY;
  196. }
  197. if (state.Gamepad.bLeftTrigger != gamepad.bLeftTrigger)
  198. {
  199. BYTE value = state.Gamepad.bLeftTrigger;
  200. value = value > XINPUT_GAMEPAD_TRIGGER_THRESHOLD ? value : 0;
  201. _axis[0].lz = value != 0
  202. ? f32(value + (value < 0 ? XINPUT_GAMEPAD_TRIGGER_THRESHOLD : -XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) / f32(UINT8_MAX - XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
  203. : 0.0f
  204. ;
  205. queue.push_joypad_event(i, JoypadAxis::LEFT, _axis[0].lx, _axis[0].ly, _axis[0].lz);
  206. gamepad.bLeftTrigger = state.Gamepad.bLeftTrigger;
  207. }
  208. if (state.Gamepad.sThumbRX != gamepad.sThumbRX)
  209. {
  210. SHORT value = state.Gamepad.sThumbRX;
  211. value = value > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE || value < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE
  212. ? value : 0;
  213. _axis[0].rx = value != 0
  214. ? f32(value + (value < 0 ? XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE : -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)) / f32(INT16_MAX - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
  215. : 0.0f
  216. ;
  217. queue.push_joypad_event(i, JoypadAxis::RIGHT, _axis[0].rx, _axis[0].ry, _axis[0].rz);
  218. gamepad.sThumbRX = state.Gamepad.sThumbRX;
  219. }
  220. if (state.Gamepad.sThumbRY != gamepad.sThumbRY)
  221. {
  222. SHORT value = state.Gamepad.sThumbRY;
  223. value = value > XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE || value < -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE
  224. ? value : 0;
  225. _axis[0].ry = value != 0
  226. ? f32(value + (value < 0 ? XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE : -XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)) / f32(INT16_MAX - XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE)
  227. : 0.0f
  228. ;
  229. queue.push_joypad_event(i, JoypadAxis::RIGHT, _axis[0].rx, _axis[0].ry, _axis[0].rz);
  230. gamepad.sThumbRY = state.Gamepad.sThumbRY;
  231. }
  232. if (state.Gamepad.bRightTrigger != gamepad.bRightTrigger)
  233. {
  234. BYTE value = state.Gamepad.bRightTrigger;
  235. value = value > XINPUT_GAMEPAD_TRIGGER_THRESHOLD ? value : 0;
  236. _axis[0].rz = value != 0
  237. ? f32(value + (value < 0 ? XINPUT_GAMEPAD_TRIGGER_THRESHOLD : -XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) / f32(UINT8_MAX - XINPUT_GAMEPAD_TRIGGER_THRESHOLD)
  238. : 0.0f
  239. ;
  240. queue.push_joypad_event(i, JoypadAxis::RIGHT, _axis[0].rx, _axis[0].ry, _axis[0].rz);
  241. gamepad.bRightTrigger = state.Gamepad.bRightTrigger;
  242. }
  243. }
  244. }
  245. struct Axis
  246. {
  247. f32 lx, ly, lz;
  248. f32 rx, ry, rz;
  249. };
  250. XINPUT_STATE _state[CROWN_MAX_JOYPADS];
  251. Axis _axis[CROWN_MAX_JOYPADS];
  252. bool _connected[CROWN_MAX_JOYPADS];
  253. };
  254. static bool s_exit = false;
  255. struct MainThreadArgs
  256. {
  257. DeviceOptions* opts;
  258. };
  259. s32 func(void* data)
  260. {
  261. MainThreadArgs* args = (MainThreadArgs*)data;
  262. crown::init(*args->opts);
  263. crown::shutdown();
  264. s_exit = true;
  265. return EXIT_SUCCESS;
  266. }
  267. struct WindowsDevice
  268. {
  269. WindowsDevice()
  270. : _hwnd(NULL)
  271. {
  272. }
  273. int run(DeviceOptions* opts)
  274. {
  275. MainThreadArgs mta;
  276. mta.opts = opts;
  277. HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
  278. WNDCLASSEX wnd;
  279. memset(&wnd, 0, sizeof(wnd));
  280. wnd.cbSize = sizeof(wnd);
  281. wnd.style = CS_HREDRAW | CS_VREDRAW;
  282. wnd.lpfnWndProc = window_proc;
  283. wnd.hInstance = instance;
  284. wnd.hIcon = LoadIcon(instance, IDI_APPLICATION);
  285. wnd.hCursor = LoadCursor(instance, IDC_ARROW);
  286. wnd.lpszClassName = "crown";
  287. wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION);
  288. RegisterClassExA(&wnd);
  289. _hwnd = CreateWindowA("crown"
  290. , "Crown"
  291. , WS_OVERLAPPEDWINDOW | WS_VISIBLE
  292. , opts->_window_x
  293. , opts->_window_y
  294. , opts->_window_width
  295. , opts->_window_height
  296. , 0
  297. , NULL
  298. , instance
  299. , 0
  300. );
  301. CE_ASSERT(_hwnd != NULL, "CreateWindowA: GetLastError = %d", GetLastError());
  302. Thread main_thread;
  303. main_thread.start(func, &mta);
  304. MSG msg;
  305. msg.message = WM_NULL;
  306. while (!s_exit)
  307. {
  308. _joypad.update(_queue);
  309. while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) != 0)
  310. {
  311. TranslateMessage(&msg);
  312. DispatchMessage(&msg);
  313. }
  314. }
  315. main_thread.stop();
  316. return EXIT_SUCCESS;
  317. }
  318. LRESULT pump_events(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
  319. {
  320. switch (id)
  321. {
  322. case WM_DESTROY:
  323. {
  324. break;
  325. }
  326. case WM_QUIT:
  327. case WM_CLOSE:
  328. {
  329. s_exit = true;
  330. _queue.push_exit_event(0);
  331. return 0;
  332. }
  333. case WM_SIZE:
  334. {
  335. u32 width = GET_X_LPARAM(lparam);
  336. u32 height = GET_Y_LPARAM(lparam);
  337. _queue.push_metrics_event(0, 0, width, height);
  338. break;
  339. }
  340. case WM_SYSCOMMAND:
  341. {
  342. switch (wparam)
  343. {
  344. case SC_MINIMIZE:
  345. case SC_RESTORE:
  346. {
  347. HWND parent = GetWindow(hwnd, GW_OWNER);
  348. if (NULL != parent)
  349. {
  350. PostMessage(parent, id, wparam, lparam);
  351. }
  352. }
  353. }
  354. break;
  355. }
  356. case WM_MOUSEWHEEL:
  357. {
  358. s32 mx = GET_X_LPARAM(lparam);
  359. s32 my = GET_Y_LPARAM(lparam);
  360. short delta = GET_WHEEL_DELTA_WPARAM(wparam);
  361. _queue.push_mouse_event(mx, my, (f32)(delta/WHEEL_DELTA));
  362. break;
  363. }
  364. case WM_MOUSEMOVE:
  365. {
  366. s32 mx = GET_X_LPARAM(lparam);
  367. s32 my = GET_Y_LPARAM(lparam);
  368. _queue.push_mouse_event(mx, my);
  369. break;
  370. }
  371. case WM_LBUTTONDOWN:
  372. case WM_LBUTTONUP:
  373. {
  374. s32 mx = GET_X_LPARAM(lparam);
  375. s32 my = GET_Y_LPARAM(lparam);
  376. _queue.push_mouse_event(mx, my, MouseButton::LEFT, id == WM_LBUTTONDOWN);
  377. break;
  378. }
  379. case WM_RBUTTONUP:
  380. case WM_RBUTTONDOWN:
  381. {
  382. s32 mx = GET_X_LPARAM(lparam);
  383. s32 my = GET_Y_LPARAM(lparam);
  384. _queue.push_mouse_event(mx, my, MouseButton::RIGHT, id == WM_RBUTTONDOWN);
  385. break;
  386. }
  387. case WM_MBUTTONDOWN:
  388. case WM_MBUTTONUP:
  389. {
  390. s32 mx = GET_X_LPARAM(lparam);
  391. s32 my = GET_Y_LPARAM(lparam);
  392. _queue.push_mouse_event(mx, my, MouseButton::MIDDLE, id == WM_MBUTTONDOWN);
  393. break;
  394. }
  395. case WM_KEYDOWN:
  396. case WM_SYSKEYDOWN:
  397. case WM_KEYUP:
  398. case WM_SYSKEYUP:
  399. {
  400. KeyboardButton::Enum kb = win_translate_key(wparam & 0xff);
  401. if (kb != KeyboardButton::COUNT)
  402. _queue.push_keyboard_event(kb, (id == WM_KEYDOWN || id == WM_SYSKEYDOWN));
  403. break;
  404. }
  405. default:
  406. break;
  407. }
  408. return DefWindowProc(hwnd, id, wparam, lparam);
  409. }
  410. static LRESULT CALLBACK window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam);
  411. public:
  412. HWND _hwnd;
  413. OsEventQueue _queue;
  414. Joypad _joypad;
  415. };
  416. static WindowsDevice s_wdvc;
  417. LRESULT CALLBACK WindowsDevice::window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
  418. {
  419. return s_wdvc.pump_events(hwnd, id, wparam, lparam);
  420. }
  421. struct WindowWin : public Window
  422. {
  423. HWND _hwnd;
  424. u16 _x;
  425. u16 _y;
  426. u16 _width;
  427. u16 _height;
  428. WindowWin()
  429. : _hwnd(NULL)
  430. , _x(0)
  431. , _y(0)
  432. , _width(CROWN_DEFAULT_WINDOW_WIDTH)
  433. , _height(CROWN_DEFAULT_WINDOW_HEIGHT)
  434. {
  435. _hwnd = s_wdvc._hwnd;
  436. }
  437. void open(u16 x, u16 y, u16 width, u16 height, u32 /*parent*/)
  438. {
  439. _x = x;
  440. _y = y;
  441. _width = width;
  442. _height = height;
  443. }
  444. void close()
  445. {
  446. DestroyWindow(_hwnd);
  447. }
  448. void bgfx_setup()
  449. {
  450. bgfx::winSetHwnd(_hwnd);
  451. }
  452. void show()
  453. {
  454. ShowWindow(_hwnd, SW_SHOW);
  455. }
  456. void hide()
  457. {
  458. ShowWindow(_hwnd, SW_HIDE);
  459. }
  460. void resize(u16 width, u16 height)
  461. {
  462. _width = width;
  463. _height = height;
  464. MoveWindow(_hwnd, _x, _y, width, height, FALSE);
  465. }
  466. void move(u16 x, u16 y)
  467. {
  468. _x = x;
  469. _y = y;
  470. MoveWindow(_hwnd, x, y, _width, _height, FALSE);
  471. }
  472. void minimize()
  473. {
  474. ShowWindow(_hwnd, SW_MINIMIZE);
  475. }
  476. void restore()
  477. {
  478. ShowWindow(_hwnd, SW_RESTORE);
  479. }
  480. const char* title()
  481. {
  482. static char buf[512];
  483. memset(buf, 0, sizeof(buf));
  484. GetWindowText(_hwnd, buf, sizeof(buf));
  485. return buf;
  486. }
  487. void set_title (const char* title)
  488. {
  489. SetWindowText(_hwnd, title);
  490. }
  491. void show_cursor(bool show)
  492. {
  493. ShowCursor(show);
  494. }
  495. void* handle()
  496. {
  497. return (void*)(uintptr_t)_hwnd;
  498. }
  499. };
  500. namespace window
  501. {
  502. Window* create(Allocator& a)
  503. {
  504. return CE_NEW(a, WindowWin)();
  505. }
  506. void destroy(Allocator& a, Window& w)
  507. {
  508. CE_DELETE(a, &w);
  509. }
  510. } // namespace window
  511. struct DisplayWin : public Display
  512. {
  513. void modes(Array<DisplayMode>& /*modes*/)
  514. {
  515. }
  516. void set_mode(u32 /*id*/)
  517. {
  518. }
  519. };
  520. namespace display
  521. {
  522. Display* create(Allocator& a)
  523. {
  524. return CE_NEW(a, DisplayWin)();
  525. }
  526. void destroy(Allocator& a, Display& d)
  527. {
  528. CE_DELETE(a, &d);
  529. }
  530. } // namespace display
  531. bool next_event(OsEvent& ev)
  532. {
  533. return s_wdvc._queue.pop_event(ev);
  534. }
  535. } // namespace crown
  536. int main(int argc, char** argv)
  537. {
  538. using namespace crown;
  539. memory_globals::init();
  540. WSADATA dummy;
  541. int err = WSAStartup(MAKEWORD(2, 2), &dummy);
  542. CE_ASSERT(err == 0, "WSAStartup: error = %d", err);
  543. CE_UNUSED(dummy);
  544. CE_UNUSED(err);
  545. DeviceOptions opts(argc, argv);
  546. int exitcode = opts.parse();
  547. if (exitcode == EXIT_FAILURE)
  548. return exitcode;
  549. exitcode = crown::s_ldvc.run(&opts);
  550. memory_globals::shutdown();
  551. return exitcode;
  552. }
  553. #endif // CROWN_PLATFORM_WINDOWS