Win32Window.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. 
  2. #include "../DFPSR/api/imageAPI.h"
  3. #include "../DFPSR/gui/BackendWindow.h"
  4. /*
  5. Link to these dependencies for MS Windows:
  6. gdi32
  7. user32
  8. kernel32
  9. comctl32
  10. */
  11. #include <tchar.h>
  12. #include <windows.h>
  13. #include <windowsx.h>
  14. class Win32Window : public dsr::BackendWindow {
  15. public:
  16. // The native windows handle
  17. HWND hwnd;
  18. // The cursors
  19. HCURSOR noCursor, defaultCursor;
  20. // Keep track of when the cursor is inside of the window,
  21. // so that we can show it again when leaving the window
  22. bool cursorIsInside = false;
  23. // Double buffering to allow drawing to a canvas while displaying the previous one
  24. // The image which can be drawn to
  25. dsr::AlignedImageRgbaU8 canvas;
  26. // Remembers the dimensions of the window from creation and resize events
  27. // This allow requesting the size of the window at any time
  28. int windowWidth = 0, windowHeight = 0;
  29. private:
  30. // Called before the application fetches events from the input queue
  31. // Closing the window, moving the mouse, pressing a key, et cetera
  32. void prefetchEvents() override;
  33. // Called to change the cursor visibility and returning true on success
  34. bool setCursorVisibility(bool visible) override;
  35. private:
  36. // Helper methods specific to calling XLib
  37. void updateTitle();
  38. private:
  39. // Canvas methods
  40. dsr::AlignedImageRgbaU8 getCanvas() override { return this->canvas; }
  41. void resizeCanvas(int width, int height) override;
  42. // Window methods
  43. void setTitle(const dsr::String &newTitle) override {
  44. this->title = newTitle;
  45. this->updateTitle();
  46. }
  47. void removeOldWindow();
  48. void createWindowed(const dsr::String& title, int width, int height);
  49. void createFullscreen();
  50. void prepareWindow();
  51. int windowState = 0; // 0=none, 1=windowed, 2=fullscreen
  52. public:
  53. // Constructors
  54. Win32Window(const Win32Window&) = delete; // Non-copyable because of pointer aliasing.
  55. Win32Window(const dsr::String& title, int width, int height);
  56. int getWidth() const override { return this->windowWidth; };
  57. int getHeight() const override { return this->windowHeight; };
  58. // Destructor
  59. ~Win32Window();
  60. // Interface
  61. void setFullScreen(bool enabled) override;
  62. bool isFullScreen() override { return this->windowState == 2; }
  63. void redraw(HWND& hwnd); // HWND is passed by argument because drawing might be called before the constructor has assigned it to this->hwnd
  64. void showCanvas() override;
  65. };
  66. static LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  67. static TCHAR windowClassName[] = _T("DfpsrWindowApplication");
  68. void Win32Window::updateTitle() {
  69. if (!SetWindowTextA(this->hwnd, this->title.toStdString().c_str())) {
  70. dsr::printText("Warning! Could not assign the window title ", dsr::string_mangleQuote(this->title), ".\n");
  71. }
  72. }
  73. bool Win32Window::setCursorVisibility(bool visible) {
  74. // Remember the cursor's visibility for anyone asking
  75. this->visibleCursor = visible;
  76. // Indicate that the feature is implemented
  77. return true;
  78. }
  79. void Win32Window::setFullScreen(bool enabled) {
  80. if (this->windowState == 1 && enabled) {
  81. // Clean up any previous window
  82. removeOldWindow();
  83. // Create the new window and graphics context
  84. this->createFullscreen();
  85. } else if (this->windowState == 2 && !enabled) {
  86. // Clean up any previous window
  87. removeOldWindow();
  88. // Create the new window and graphics context
  89. this->createWindowed(this->title, 800, 600); // TODO: Remember the dimensions from last windowed mode
  90. }
  91. }
  92. void Win32Window::removeOldWindow() {
  93. if (this->windowState != 0) {
  94. DestroyWindow(this->hwnd);
  95. }
  96. this->windowState = 0;
  97. }
  98. void Win32Window::prepareWindow() {
  99. // Reallocate the canvas
  100. this->resizeCanvas(this->windowWidth, this->windowHeight);
  101. // Show the window
  102. ShowWindow(this->hwnd, SW_NORMAL);
  103. // Repaint
  104. UpdateWindow(this->hwnd);
  105. }
  106. static bool registered = false;
  107. static void registerIfNeeded() {
  108. if (!registered) {
  109. // The Window structure
  110. WNDCLASSEX wincl;
  111. memset(&wincl, 0, sizeof(WNDCLASSEX));
  112. wincl.hInstance = NULL;
  113. wincl.lpszClassName = windowClassName;
  114. wincl.lpfnWndProc = WindowProcedure;
  115. wincl.style = 0;
  116. wincl.cbSize = sizeof(WNDCLASSEX);
  117. // Use default icon and mouse-pointer
  118. wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  119. wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  120. wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  121. wincl.lpszMenuName = NULL;
  122. wincl.cbClsExtra = 0;
  123. wincl.cbWndExtra = sizeof(LPVOID);
  124. // Use Windows's default color as the background of the window
  125. wincl.hbrBackground = (HBRUSH)COLOR_BACKGROUND; // TODO: Make black
  126. // Register the window class, and if it fails quit the program
  127. if (!RegisterClassEx (&wincl)) {
  128. dsr::throwError("Call to RegisterClassEx failed!\n");
  129. }
  130. registered = true;
  131. }
  132. }
  133. void Win32Window::createWindowed(const dsr::String& title, int width, int height) {
  134. // Request to resize the canvas and interface according to the new window
  135. this->windowWidth = width;
  136. this->windowHeight = height;
  137. this->receivedWindowResize(width, height);
  138. // Register the Window class during first creation
  139. registerIfNeeded();
  140. // The class is registered, let's create the program
  141. this->hwnd = CreateWindowEx(
  142. 0, // dwExStyle
  143. windowClassName, // lpClassName
  144. _T(""), // lpWindowName
  145. WS_OVERLAPPEDWINDOW, // dwStyle
  146. CW_USEDEFAULT, // x
  147. CW_USEDEFAULT, // y
  148. width, // nWidth
  149. height, // nHeight
  150. HWND_DESKTOP, // hWndParent
  151. NULL, // hMenu
  152. NULL, // hInstance
  153. (LPVOID)this // lpParam
  154. );
  155. this->updateTitle();
  156. this->windowState = 1;
  157. this->prepareWindow();
  158. }
  159. void Win32Window::createFullscreen() {
  160. int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  161. int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  162. // Request to resize the canvas and interface according to the new window
  163. this->windowWidth = screenWidth;
  164. this->windowHeight = screenHeight;
  165. this->receivedWindowResize(screenWidth, screenHeight);
  166. // Register the Window class during first creation
  167. registerIfNeeded();
  168. // The class is registered, let's create the program
  169. this->hwnd = CreateWindowEx(
  170. 0, // dwExStyle
  171. windowClassName, // lpClassName
  172. _T(""), // lpWindowName
  173. WS_POPUP | WS_VISIBLE, // dwStyle
  174. 0, // x
  175. 0, // y
  176. screenWidth, // nWidth
  177. screenHeight, // nHeight
  178. HWND_DESKTOP, // hWndParent
  179. NULL, // hMenu
  180. NULL, // hInstance
  181. (LPVOID)this // lpParam
  182. );
  183. this->windowState = 2;
  184. this->prepareWindow();
  185. }
  186. Win32Window::Win32Window(const dsr::String& title, int width, int height) {
  187. bool fullScreen = false;
  188. if (width < 1 || height < 1) {
  189. fullScreen = true;
  190. }
  191. // Remember the title
  192. this->title = title;
  193. // Get the default cursor
  194. this->defaultCursor = LoadCursor(0, IDC_ARROW);
  195. // Create an invisible cursor using masks padded to 32 bits for safety
  196. uint32_t cursorAndMask = 0b11111111;
  197. uint32_t cursorXorMask = 0b00000000;
  198. this->noCursor = CreateCursor(NULL, 0, 0, 1, 1, (const void*)&cursorAndMask, (const void*)&cursorXorMask);
  199. // Create a window
  200. if (fullScreen) {
  201. this->createFullscreen();
  202. } else {
  203. this->createWindowed(title, width, height);
  204. }
  205. }
  206. static dsr::DsrKey getDsrKey(WPARAM keyCode) {
  207. dsr::DsrKey result = dsr::DsrKey_Unhandled;
  208. if (keyCode == VK_ESCAPE) {
  209. result = dsr::DsrKey_Escape;
  210. } else if (keyCode == VK_F1) {
  211. result = dsr::DsrKey_F1;
  212. } else if (keyCode == VK_F2) {
  213. result = dsr::DsrKey_F2;
  214. } else if (keyCode == VK_F3) {
  215. result = dsr::DsrKey_F3;
  216. } else if (keyCode == VK_F4) {
  217. result = dsr::DsrKey_F4;
  218. } else if (keyCode == VK_F5) {
  219. result = dsr::DsrKey_F5;
  220. } else if (keyCode == VK_F6) {
  221. result = dsr::DsrKey_F6;
  222. } else if (keyCode == VK_F7) {
  223. result = dsr::DsrKey_F7;
  224. } else if (keyCode == VK_F8) {
  225. result = dsr::DsrKey_F8;
  226. } else if (keyCode == VK_F9) {
  227. result = dsr::DsrKey_F9;
  228. } else if (keyCode == VK_F10) {
  229. result = dsr::DsrKey_F10;
  230. } else if (keyCode == VK_F11) {
  231. result = dsr::DsrKey_F11;
  232. } else if (keyCode == VK_F12) {
  233. result = dsr::DsrKey_F12;
  234. } else if (keyCode == VK_PAUSE) {
  235. result = dsr::DsrKey_Pause;
  236. } else if (keyCode == VK_SPACE) {
  237. result = dsr::DsrKey_Space;
  238. } else if (keyCode == VK_TAB) {
  239. result = dsr::DsrKey_Tab;
  240. } else if (keyCode == VK_RETURN) {
  241. result = dsr::DsrKey_Return;
  242. } else if (keyCode == VK_BACK) {
  243. result = dsr::DsrKey_BackSpace;
  244. } else if (keyCode == VK_LSHIFT) {
  245. result = dsr::DsrKey_LeftShift;
  246. } else if (keyCode == VK_RSHIFT) {
  247. result = dsr::DsrKey_RightShift;
  248. } else if (keyCode == VK_LCONTROL) {
  249. result = dsr::DsrKey_LeftControl;
  250. } else if (keyCode == VK_RCONTROL) {
  251. result = dsr::DsrKey_RightControl;
  252. } else if (keyCode == VK_LMENU) {
  253. result = dsr::DsrKey_LeftAlt;
  254. } else if (keyCode == VK_RMENU) {
  255. result = dsr::DsrKey_RightAlt;
  256. } else if (keyCode == VK_DELETE) {
  257. result = dsr::DsrKey_Delete;
  258. } else if (keyCode == VK_LEFT) {
  259. result = dsr::DsrKey_LeftArrow;
  260. } else if (keyCode == VK_RIGHT) {
  261. result = dsr::DsrKey_RightArrow;
  262. } else if (keyCode == VK_UP) {
  263. result = dsr::DsrKey_UpArrow;
  264. } else if (keyCode == VK_DOWN) {
  265. result = dsr::DsrKey_DownArrow;
  266. } else if (keyCode == 0x30) {
  267. result = dsr::DsrKey_0;
  268. } else if (keyCode == 0x31) {
  269. result = dsr::DsrKey_1;
  270. } else if (keyCode == 0x32) {
  271. result = dsr::DsrKey_2;
  272. } else if (keyCode == 0x33) {
  273. result = dsr::DsrKey_3;
  274. } else if (keyCode == 0x34) {
  275. result = dsr::DsrKey_4;
  276. } else if (keyCode == 0x35) {
  277. result = dsr::DsrKey_5;
  278. } else if (keyCode == 0x36) {
  279. result = dsr::DsrKey_6;
  280. } else if (keyCode == 0x37) {
  281. result = dsr::DsrKey_7;
  282. } else if (keyCode == 0x38) {
  283. result = dsr::DsrKey_8;
  284. } else if (keyCode == 0x39) {
  285. result = dsr::DsrKey_9;
  286. } else if (keyCode == 0x41) {
  287. result = dsr::DsrKey_A;
  288. } else if (keyCode == 0x42) {
  289. result = dsr::DsrKey_B;
  290. } else if (keyCode == 0x43) {
  291. result = dsr::DsrKey_C;
  292. } else if (keyCode == 0x44) {
  293. result = dsr::DsrKey_D;
  294. } else if (keyCode == 0x45) {
  295. result = dsr::DsrKey_E;
  296. } else if (keyCode == 0x46) {
  297. result = dsr::DsrKey_F;
  298. } else if (keyCode == 0x47) {
  299. result = dsr::DsrKey_G;
  300. } else if (keyCode == 0x48) {
  301. result = dsr::DsrKey_H;
  302. } else if (keyCode == 0x49) {
  303. result = dsr::DsrKey_I;
  304. } else if (keyCode == 0x4A) {
  305. result = dsr::DsrKey_J;
  306. } else if (keyCode == 0x4B) {
  307. result = dsr::DsrKey_K;
  308. } else if (keyCode == 0x4C) {
  309. result = dsr::DsrKey_L;
  310. } else if (keyCode == 0x4D) {
  311. result = dsr::DsrKey_M;
  312. } else if (keyCode == 0x4E) {
  313. result = dsr::DsrKey_N;
  314. } else if (keyCode == 0x4F) {
  315. result = dsr::DsrKey_O;
  316. } else if (keyCode == 0x50) {
  317. result = dsr::DsrKey_P;
  318. } else if (keyCode == 0x51) {
  319. result = dsr::DsrKey_Q;
  320. } else if (keyCode == 0x52) {
  321. result = dsr::DsrKey_R;
  322. } else if (keyCode == 0x53) {
  323. result = dsr::DsrKey_S;
  324. } else if (keyCode == 0x54) {
  325. result = dsr::DsrKey_T;
  326. } else if (keyCode == 0x55) {
  327. result = dsr::DsrKey_U;
  328. } else if (keyCode == 0x56) {
  329. result = dsr::DsrKey_V;
  330. } else if (keyCode == 0x57) {
  331. result = dsr::DsrKey_W;
  332. } else if (keyCode == 0x58) {
  333. result = dsr::DsrKey_X;
  334. } else if (keyCode == 0x59) {
  335. result = dsr::DsrKey_Y;
  336. } else if (keyCode == 0x5A) {
  337. result = dsr::DsrKey_Z;
  338. } else if (keyCode == 0x2D) {
  339. result = dsr::DsrKey_Insert;
  340. } else if (keyCode == 0x24) {
  341. result = dsr::DsrKey_Home;
  342. } else if (keyCode == 0x23) {
  343. result = dsr::DsrKey_End;
  344. } else if (keyCode == 0x21) {
  345. result = dsr::DsrKey_PageUp;
  346. } else if (keyCode == 0x22) {
  347. result = dsr::DsrKey_PageDown;
  348. }
  349. return result;
  350. }
  351. // Called from DispatchMessage via prefetchEvents
  352. static LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
  353. // Get the Win32Window owning the given hwnd
  354. Win32Window *parent = nullptr;
  355. if (message == WM_CREATE) {
  356. // Cast the pointer argument into CREATESTRUCT and get the lParam given to the window on creation
  357. CREATESTRUCT *createStruct = (CREATESTRUCT*)lParam;
  358. parent = (Win32Window*)createStruct->lpCreateParams;
  359. if (parent == nullptr) {
  360. dsr::throwError("Null handle retreived from lParam (", (intptr_t)parent, ") in WM_CREATE message.\n");
  361. }
  362. SetWindowLongPtr(hwnd, GWLP_USERDATA, (intptr_t)parent);
  363. } else {
  364. // Get the parent
  365. parent = (Win32Window*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
  366. if (parent == nullptr) {
  367. // Don't try to handle global events unrelated to any window
  368. return DefWindowProc(hwnd, message, wParam, lParam);
  369. }
  370. }
  371. // Check that we're using the correct window instance (This might not be the case while toggling full-screen)
  372. // Handle the message
  373. int result = 0;
  374. switch (message) {
  375. case WM_QUIT:
  376. PostQuitMessage(wParam);
  377. break;
  378. case WM_CLOSE:
  379. parent->queueInputEvent(new dsr::WindowEvent(dsr::WindowEventType::Close, parent->windowWidth, parent->windowHeight));
  380. DestroyWindow(hwnd);
  381. break;
  382. case WM_LBUTTONDOWN:
  383. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseDown, dsr::MouseKeyEnum::Left, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  384. break;
  385. case WM_LBUTTONUP:
  386. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseUp, dsr::MouseKeyEnum::Left, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  387. break;
  388. case WM_RBUTTONDOWN:
  389. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseDown, dsr::MouseKeyEnum::Right, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  390. break;
  391. case WM_RBUTTONUP:
  392. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseUp, dsr::MouseKeyEnum::Right, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  393. break;
  394. case WM_MBUTTONDOWN:
  395. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseDown, dsr::MouseKeyEnum::Middle, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  396. break;
  397. case WM_MBUTTONUP:
  398. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseUp, dsr::MouseKeyEnum::Middle, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  399. break;
  400. case WM_MOUSEMOVE:
  401. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::MouseMove, dsr::MouseKeyEnum::NoKey, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  402. break;
  403. case WM_SETCURSOR:
  404. if (LOWORD(lParam) == HTCLIENT) {
  405. if (parent->visibleCursor) {
  406. SetCursor(parent->defaultCursor);
  407. } else {
  408. SetCursor(parent->noCursor);
  409. }
  410. }
  411. break;
  412. case WM_MOUSEWHEEL:
  413. {
  414. int delta = GET_WHEEL_DELTA_WPARAM(wParam);
  415. if (delta > 0) {
  416. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::Scroll, dsr::MouseKeyEnum::ScrollUp, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  417. } else if (delta < 0) {
  418. parent->queueInputEvent(new dsr::MouseEvent(dsr::MouseEventType::Scroll, dsr::MouseKeyEnum::ScrollDown, dsr::IVector2D(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam))));
  419. }
  420. }
  421. break;
  422. case WM_KEYDOWN: case WM_KEYUP:
  423. {
  424. dsr::DsrChar character;
  425. if (IsWindowUnicode(hwnd)) {
  426. dsr::CharacterEncoding encoding = dsr::CharacterEncoding::BOM_UTF16LE;
  427. dsr::String characterString = dsr::string_dangerous_decodeFromData((const void*)&wParam, encoding);
  428. character = characterString[0]; // Convert from UTF-16 surrogate to UTF-32 Unicode character
  429. } else {
  430. character = wParam; // Raw ansi character
  431. }
  432. dsr::DsrKey dsrKey = getDsrKey(wParam); // Portable key-code
  433. bool previouslyPressed = lParam & (1 << 30);
  434. if (message == WM_KEYDOWN) {
  435. // If not repeated
  436. if (!previouslyPressed) {
  437. // Physical key down
  438. parent->queueInputEvent(new dsr::KeyboardEvent(dsr::KeyboardEventType::KeyDown, character, dsrKey));
  439. }
  440. // Press typing with repeat
  441. parent->queueInputEvent(new dsr::KeyboardEvent(dsr::KeyboardEventType::KeyType, character, dsrKey));
  442. } else { // message == WM_KEYUP
  443. // Physical key up
  444. parent->queueInputEvent(new dsr::KeyboardEvent(dsr::KeyboardEventType::KeyUp, character, dsrKey));
  445. }
  446. }
  447. break;
  448. case WM_PAINT:
  449. parent->queueInputEvent(new dsr::WindowEvent(dsr::WindowEventType::Redraw, parent->windowWidth, parent->windowHeight));
  450. // BeginPaint and EndPaint must be called with the given hwnd to prevent having the redraw message sent again
  451. parent->redraw(hwnd);
  452. // Passing on the event to prevent flooding with more messages. This is only a temporary solution.
  453. // TODO: Avoid overwriting the result with any background color.
  454. //result = DefWindowProc(hwnd, message, wParam, lParam);
  455. break;
  456. case WM_SIZE:
  457. // If there's no size during minimization, don't try to resize the canvas
  458. if (wParam != SIZE_MINIMIZED) {
  459. int width = LOWORD(lParam);
  460. int height = HIWORD(lParam);
  461. parent->windowWidth = width;
  462. parent->windowHeight = height;
  463. parent->receivedWindowResize(width, height);
  464. }
  465. // Resize the window as requested
  466. result = DefWindowProc(hwnd, message, wParam, lParam);
  467. break;
  468. default:
  469. result = DefWindowProc(hwnd, message, wParam, lParam);
  470. }
  471. return result;
  472. }
  473. void Win32Window::prefetchEvents() {
  474. MSG messages;
  475. if (IsWindowUnicode(this->hwnd)) {
  476. while (PeekMessageW(&messages, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&messages); DispatchMessage(&messages); }
  477. } else {
  478. while (PeekMessage(&messages, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&messages); DispatchMessage(&messages); }
  479. }
  480. }
  481. // Locked because it overrides
  482. void Win32Window::resizeCanvas(int width, int height) {
  483. // Create a new canvas
  484. // Even thou Windows is using RGBA pack order for the window, the bitmap format used for drawing is using BGRA order
  485. this->canvas = dsr::image_create_RgbaU8_native(width, height, dsr::PackOrderIndex::BGRA);
  486. }
  487. Win32Window::~Win32Window() {
  488. // Destroy the invisible cursor
  489. DestroyCursor(this->noCursor);
  490. // Destroy the native window
  491. DestroyWindow(this->hwnd);
  492. }
  493. void Win32Window::redraw(HWND& hwnd) {
  494. // Let the source bitmap use a padded width to safely handle the stride
  495. // Windows require 8-byte alignment, but the image format uses 16-byte alignment.
  496. int paddedWidth = dsr::image_getStride(this->canvas) / 4;
  497. //int width = dsr::image_getWidth(this->canvas);
  498. int height = dsr::image_getHeight(this->canvas);
  499. InvalidateRect(this->hwnd, NULL, false);
  500. PAINTSTRUCT paintStruct;
  501. HDC targetContext = BeginPaint(this->hwnd, &paintStruct);
  502. BITMAPINFO bmi = {};
  503. bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
  504. bmi.bmiHeader.biWidth = paddedWidth;
  505. bmi.bmiHeader.biHeight = -height;
  506. bmi.bmiHeader.biPlanes = 1;
  507. bmi.bmiHeader.biBitCount = 32;
  508. bmi.bmiHeader.biCompression = BI_RGB;
  509. SetDIBitsToDevice(targetContext, 0, 0, paddedWidth, height, 0, 0, 0, height, dsr::image_dangerous_getData(this->canvas), &bmi, DIB_RGB_COLORS);
  510. EndPaint(this->hwnd, &paintStruct);
  511. }
  512. void Win32Window::showCanvas() {
  513. this->prefetchEvents();
  514. this->redraw(this->hwnd);
  515. }
  516. std::shared_ptr<dsr::BackendWindow> createBackendWindow(const dsr::String& title, int width, int height) {
  517. auto backend = std::make_shared<Win32Window>(title, width, height);
  518. return std::dynamic_pointer_cast<dsr::BackendWindow>(backend);
  519. }