OsWindow.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "OsWindow.h"
  23. #include "GLContext.h"
  24. #include "Assert.h"
  25. #include "Keyboard.h"
  26. #include "StringUtils.h"
  27. #include "OsEvents.h"
  28. #include "Log.h"
  29. namespace crown
  30. {
  31. //-----------------------------------------------------------------------------
  32. static Key translate_key(int32_t winKey)
  33. {
  34. if ((winKey > 0x40 && winKey < 0x5B) || (winKey > 0x60 && winKey < 0x7B) || (winKey > 0x2F && winKey < 0x3A))
  35. {
  36. return (Key)winKey;
  37. }
  38. switch (winKey)
  39. {
  40. case VK_BACK: return KC_BACKSPACE;
  41. case VK_TAB: return KC_TAB;
  42. case VK_SPACE: return KC_SPACE;
  43. case VK_ESCAPE: return KC_ESCAPE;
  44. case VK_RETURN: return KC_ENTER;
  45. case VK_F1: return KC_F1;
  46. case VK_F2: return KC_F2;
  47. case VK_F3: return KC_F3;
  48. case VK_F4: return KC_F4;
  49. case VK_F5: return KC_F5;
  50. case VK_F6: return KC_F6;
  51. case VK_F7: return KC_F7;
  52. case VK_F8: return KC_F8;
  53. case VK_F9: return KC_F9;
  54. case VK_F10: return KC_F10;
  55. case VK_F11: return KC_F11;
  56. case VK_F12: return KC_F12;
  57. case VK_HOME: return KC_HOME;
  58. case VK_LEFT: return KC_LEFT;
  59. case VK_UP: return KC_UP;
  60. case VK_RIGHT: return KC_RIGHT;
  61. case VK_DOWN: return KC_DOWN;
  62. case VK_PRIOR: return KC_PAGE_UP;
  63. case VK_NEXT: return KC_PAGE_DOWN;
  64. case VK_LSHIFT: return KC_LSHIFT;
  65. case VK_RSHIFT: return KC_RSHIFT;
  66. case VK_LCONTROL: return KC_LCONTROL;
  67. case VK_RCONTROL: return KC_RCONTROL;
  68. case VK_CAPITAL: return KC_CAPS_LOCK;
  69. case VK_LMENU: return KC_LALT;
  70. case VK_RMENU: return KC_RALT;
  71. case VK_LWIN: return KC_LSUPER;
  72. case VK_RWIN: return KC_RSUPER;
  73. case VK_NUMPAD0: return KC_KP_0;
  74. case VK_NUMPAD1: return KC_KP_1;
  75. case VK_NUMPAD2: return KC_KP_2;
  76. case VK_NUMPAD3: return KC_KP_3;
  77. case VK_NUMPAD4: return KC_KP_4;
  78. case VK_NUMPAD5: return KC_KP_5;
  79. case VK_NUMPAD6: return KC_KP_6;
  80. case VK_NUMPAD7: return KC_KP_7;
  81. case VK_NUMPAD8: return KC_KP_8;
  82. case VK_NUMPAD9: return KC_KP_9;
  83. default: return KC_NOKEY;
  84. }
  85. }
  86. //-----------------------------------------------------------------------------
  87. OsWindow::OsWindow(uint32_t width, uint32_t height, uint32_t /*window*/) :
  88. m_window_handle(NULL),
  89. m_x(0),
  90. m_y(0),
  91. m_fullscreen(false)
  92. {
  93. CE_ASSERT(width != 0 || height != 0, "Width and height must differ from 0.");
  94. string::strncpy(m_window_name, "CrownWindowClass", 32);
  95. WNDCLASSEX wcex;
  96. wcex.cbSize = sizeof(WNDCLASSEX);
  97. wcex.style = CS_OWNDC;
  98. wcex.lpfnWndProc = (WNDPROC) DefWindowProc;
  99. wcex.cbClsExtra = 0;
  100. wcex.cbWndExtra = 0;
  101. wcex.hInstance = GetModuleHandle(NULL);
  102. wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  103. wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  104. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  105. wcex.lpszMenuName = NULL;
  106. wcex.lpszClassName = m_window_name;
  107. wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  108. uint32_t registered = RegisterClassEx(&wcex);
  109. CE_ASSERT(registered != 0, "Unable to register a Window Class.");
  110. m_window_handle = CreateWindowEx(0, m_window_name, "", WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX, 0, 0, width, height, NULL, NULL, GetModuleHandle(NULL), NULL);
  111. CE_ASSERT(m_window_handle != NULL, "Unable to create a Window.");
  112. //Save the WGLRenderWindow pointer to the window's user data
  113. SetWindowLongPtr(m_window_handle, GWLP_USERDATA, (LONG) this);
  114. RECT rc;
  115. rc.left = 0;
  116. rc.top = 0;
  117. rc.right = width;
  118. rc.bottom = height;
  119. int32_t style, styleEx;
  120. style = GetWindowLong(m_window_handle, GWL_STYLE);
  121. styleEx = GetWindowLong(m_window_handle, GWL_EXSTYLE);
  122. AdjustWindowRectEx(&rc, style, false, styleEx);
  123. SetWindowPos(m_window_handle, 0, 0, 0, rc.right-rc.left, rc.bottom-rc.top, SWP_NOMOVE | SWP_NOZORDER);
  124. PIXELFORMATDESCRIPTOR pfd;
  125. int32_t pixel_format;
  126. /* get the device context (DC) */
  127. HDC device_context = GetDC(m_window_handle);
  128. /* set the pixel format for the DC */
  129. ZeroMemory(&pfd, sizeof (pfd));
  130. pfd.nSize = sizeof (pfd);
  131. pfd.nVersion = 1;
  132. pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  133. pfd.iPixelType = PFD_TYPE_RGBA;
  134. pfd.cColorBits = 24;
  135. pfd.cDepthBits = 24;
  136. pfd.iLayerType = PFD_MAIN_PLANE;
  137. pixel_format = ChoosePixelFormat(device_context, &pfd);
  138. CE_ASSERT(pixel_format != 0, "Pixel format not supported.");
  139. bool pf_set = SetPixelFormat(device_context, pixel_format, &pfd);
  140. CE_ASSERT(pf_set, "Unable to set the pixel format, altough it seems to be supported.");
  141. set_win_handle_window(m_window_handle);
  142. }
  143. //-----------------------------------------------------------------------------
  144. OsWindow::~OsWindow()
  145. {
  146. if (m_window_handle)
  147. {
  148. DestroyWindow(m_window_handle);
  149. }
  150. }
  151. //-----------------------------------------------------------------------------
  152. void OsWindow::show()
  153. {
  154. ShowWindow(m_window_handle, SW_SHOW);
  155. }
  156. //-----------------------------------------------------------------------------
  157. void OsWindow::hide()
  158. {
  159. ShowWindow(m_window_handle, SW_HIDE);
  160. }
  161. //-----------------------------------------------------------------------------
  162. void OsWindow::get_size(uint32_t& width, uint32_t& height)
  163. {
  164. width = m_width;
  165. height = m_height;
  166. }
  167. //-----------------------------------------------------------------------------
  168. void OsWindow::get_position(uint32_t& x, uint32_t& y)
  169. {
  170. x = m_x;
  171. y = m_y;
  172. }
  173. //-----------------------------------------------------------------------------
  174. void OsWindow::resize(uint32_t width, uint32_t height)
  175. {
  176. SetWindowPos(m_window_handle, NULL, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER);
  177. }
  178. //-----------------------------------------------------------------------------
  179. void OsWindow::move(uint32_t x, uint32_t y)
  180. {
  181. SetWindowPos(m_window_handle, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  182. }
  183. //-----------------------------------------------------------------------------
  184. void OsWindow::show_cursor(bool show)
  185. {
  186. ShowCursor(show);
  187. }
  188. //-----------------------------------------------------------------------------
  189. void OsWindow::get_cursor_xy(int32_t& x, int32_t& y)
  190. {
  191. POINT p;
  192. if (!GetCursorPos(&p))
  193. {
  194. CE_ASSERT(false, "Unable to get cursor coordinates\n");
  195. }
  196. x = p.x;
  197. y = p.y;
  198. }
  199. //-----------------------------------------------------------------------------
  200. void OsWindow::set_cursor_xy(int32_t x, int32_t y)
  201. {
  202. SetCursorPos(x, y);
  203. }
  204. //-----------------------------------------------------------------------------
  205. void OsWindow::set_fullscreen(bool fs)
  206. {
  207. if (m_fullscreen)
  208. {
  209. memset(&m_screen_setting, 0, sizeof(m_screen_setting)); // Makes Sure Memory's Cleared
  210. m_screen_setting.dmSize = sizeof(m_screen_setting); // Size Of The Devmode Structure
  211. m_screen_setting.dmPelsWidth = m_width; // Selected Screen Width
  212. m_screen_setting.dmPelsHeight = m_height; // Selected Screen Height
  213. m_screen_setting.dmBitsPerPel = 32; // Selected Bits Per Pixel
  214. m_screen_setting.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  215. // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
  216. if (ChangeDisplaySettings(&m_screen_setting, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
  217. {
  218. m_fullscreen = false;
  219. Log::i("Fullscreen resolution not supported, switching to windowed mode.");
  220. }
  221. else
  222. {
  223. m_fullscreen = true;
  224. m_window_handle = CreateWindowEx(0, m_window_name, "", WS_POPUP, 0, 0, m_width, m_height, NULL, NULL, GetModuleHandle(NULL), NULL);
  225. }
  226. }
  227. }
  228. bool OsWindow::fullscreen()
  229. {
  230. return m_fullscreen;
  231. }
  232. //-----------------------------------------------------------------------------
  233. char* OsWindow::title()
  234. {
  235. return m_window_name;
  236. }
  237. //-----------------------------------------------------------------------------
  238. void OsWindow::set_title(const char* title)
  239. {
  240. string::strncpy(m_window_name, title, 32);
  241. SetWindowText(m_window_handle, m_window_name);
  242. }
  243. //-----------------------------------------------------------------------------
  244. void OsWindow::frame()
  245. {
  246. MSG msg;
  247. OsMouseEvent mouse_event;
  248. OsKeyboardEvent keyboard_event;
  249. // Message are removed with PM_REMOVE
  250. while (PeekMessage(&msg, m_window_handle, 0, 0, PM_REMOVE))
  251. {
  252. TranslateMessage(&msg);
  253. DispatchMessage(&msg);
  254. switch (msg.message)
  255. {
  256. // Mouse
  257. case WM_LBUTTONDOWN:
  258. case WM_LBUTTONUP:
  259. {
  260. OsEventType oset_type = msg.message == WM_LBUTTONDOWN ? OSET_BUTTON_PRESS : OSET_BUTTON_RELEASE;
  261. mouse_event.x = LOWORD(msg.lParam);
  262. mouse_event.y = HIWORD(msg.lParam);
  263. mouse_event.button = 0;
  264. os_event_buffer()->push_event(oset_type, &mouse_event, sizeof(OsMouseEvent));
  265. break;
  266. }
  267. case WM_RBUTTONDOWN:
  268. case WM_RBUTTONUP:
  269. {
  270. OsEventType oset_type = msg.message == WM_RBUTTONDOWN ? OSET_BUTTON_PRESS : OSET_BUTTON_RELEASE;
  271. mouse_event.x = LOWORD(msg.lParam);
  272. mouse_event.y = HIWORD(msg.lParam);
  273. mouse_event.button = 1;
  274. os_event_buffer()->push_event(oset_type, &mouse_event, sizeof(OsMouseEvent));
  275. break;
  276. }
  277. case WM_MBUTTONDOWN:
  278. case WM_MBUTTONUP:
  279. {
  280. OsEventType oset_type = msg.message == WM_MBUTTONDOWN ? OSET_BUTTON_PRESS : OSET_BUTTON_RELEASE;
  281. mouse_event.x = LOWORD(msg.lParam);
  282. mouse_event.y = HIWORD(msg.lParam);
  283. mouse_event.button = 2;
  284. os_event_buffer()->push_event(oset_type, &mouse_event, sizeof(OsMouseEvent));
  285. break;
  286. }
  287. case WM_MOUSEMOVE:
  288. {
  289. //mouse_event.x = LOWORD(msg.lParam);
  290. //mouse_event.y = HIWORD(msg.lParam);
  291. //os_event_buffer()->push_event(OSET_MOTION_NOTIFY, &mouse_event, sizeof(OsMouseEvent));
  292. break;
  293. }
  294. // Keyboard
  295. case WM_KEYDOWN:
  296. case WM_KEYUP:
  297. {
  298. Key kc = translate_key(msg.wParam);
  299. int32_t modifier_mask = 0;
  300. // Check if any modifier key is pressed or released
  301. if (kc == KC_LSHIFT || kc == KC_RSHIFT)
  302. {
  303. (msg.message == WM_KEYDOWN) ? modifier_mask |= MK_SHIFT : modifier_mask &= ~MK_SHIFT;
  304. }
  305. else if (kc == KC_LCONTROL || kc == KC_RCONTROL)
  306. {
  307. (msg.message == WM_KEYDOWN) ? modifier_mask |= MK_CTRL : modifier_mask &= ~MK_CTRL;
  308. }
  309. else if (kc == KC_LALT || kc == KC_RALT)
  310. {
  311. (msg.message == WM_KEYDOWN) ? modifier_mask |= MK_ALT : modifier_mask &= ~MK_ALT;
  312. }
  313. OsEventType oset_type = msg.message == WM_KEYDOWN ? OSET_KEY_PRESS : OSET_KEY_RELEASE;
  314. keyboard_event.key = ((int32_t)kc);
  315. keyboard_event.modifier = modifier_mask;
  316. os_event_buffer()->push_event(oset_type, &keyboard_event, sizeof(OsKeyboardEvent));
  317. break;
  318. }
  319. case WM_CHAR:
  320. {
  321. // MUST BE IMPLEMENTED
  322. break;
  323. }
  324. default:
  325. {
  326. break;
  327. }
  328. // Touch
  329. // TODO
  330. // Accelerometer
  331. // TODO
  332. }
  333. }
  334. }
  335. } // namespace crown