Advertisement
Piratux

Untitled

Jan 31st, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. #endif
  2. // O------------------------------------------------------------------------------O
  3. // | START IMAGE LOADER: stb_image.h |
  4. // O------------------------------------------------------------------------------O
  5.  
  6.  
  7. // O------------------------------------------------------------------------------O
  8. // | START PLATFORM: MICROSOFT WINDOWS XP, VISTA, 7, 8, 10 |
  9. // O------------------------------------------------------------------------------O
  10. #if defined(OLC_PLATFORM_WINAPI)
  11.  
  12. #if defined(_WIN32) && !defined(__MINGW32__)
  13. #pragma comment(lib, "user32.lib") // Visual Studio Only
  14. #pragma comment(lib, "gdi32.lib") // For other Windows Compilers please add
  15. #pragma comment(lib, "opengl32.lib") // these libs to your linker input
  16. #endif
  17.  
  18. namespace olc
  19. {
  20. class Platform_Windows : public olc::Platform
  21. {
  22. private:
  23. HWND olc_hWnd = nullptr;
  24. std::wstring wsAppName;
  25.  
  26. std::wstring ConvertS2W(std::string s)
  27. {
  28. #ifdef __MINGW32__
  29. wchar_t* buffer = new wchar_t[s.length() + 1];
  30. mbstowcs(buffer, s.c_str(), s.length());
  31. buffer[s.length()] = L'\0';
  32. #else
  33. int count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
  34. wchar_t* buffer = new wchar_t[count];
  35. MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buffer, count);
  36. #endif
  37. std::wstring w(buffer);
  38. delete[] buffer;
  39. return w;
  40. }
  41.  
  42. public:
  43. virtual olc::rcode ApplicationStartUp() override { return olc::rcode::OK; }
  44. virtual olc::rcode ApplicationCleanUp() override { return olc::rcode::OK; }
  45. virtual olc::rcode ThreadStartUp() override { return olc::rcode::OK; }
  46.  
  47. virtual olc::rcode ThreadCleanUp() override
  48. {
  49. renderer->DestroyDevice();
  50. PostMessage(olc_hWnd, WM_DESTROY, 0, 0);
  51. return olc::OK;
  52. }
  53.  
  54. virtual olc::rcode CreateGraphics(bool bFullScreen, bool bEnableVSYNC, const olc::vi2d& vViewPos, const olc::vi2d& vViewSize) override
  55. {
  56. if (renderer->CreateDevice({ olc_hWnd }, bFullScreen, bEnableVSYNC) == olc::rcode::OK)
  57. {
  58. renderer->UpdateViewport(vViewPos, vViewSize);
  59. return olc::rcode::OK;
  60. }
  61. else
  62. return olc::rcode::FAIL;
  63. }
  64.  
  65. virtual olc::rcode CreateWindowPane(const olc::vi2d& vWindowPos, olc::vi2d& vWindowSize, bool bFullScreen) override
  66. {
  67. WNDCLASS wc;
  68. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  69. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  70. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  71. wc.hInstance = GetModuleHandle(nullptr);
  72. wc.lpfnWndProc = olc_WindowEvent;
  73. wc.cbClsExtra = 0;
  74. wc.cbWndExtra = 0;
  75. wc.lpszMenuName = nullptr;
  76. wc.hbrBackground = nullptr;
  77. wc.lpszClassName = olcT("OLC_PIXEL_GAME_ENGINE");
  78. RegisterClass(&wc);
  79.  
  80. // Define window furniture
  81. DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  82. DWORD dwStyle = WS_CAPTION | WS_SYSMENU | WS_VISIBLE | WS_THICKFRAME;
  83.  
  84. olc::vi2d vTopLeft = vWindowPos;
  85.  
  86. // Handle Fullscreen
  87. if (bFullScreen)
  88. {
  89. dwExStyle = 0;
  90. dwStyle = WS_VISIBLE | WS_POPUP;
  91. HMONITOR hmon = MonitorFromWindow(olc_hWnd, MONITOR_DEFAULTTONEAREST);
  92. MONITORINFO mi = { sizeof(mi) };
  93. if (!GetMonitorInfo(hmon, &mi)) return olc::rcode::FAIL;
  94. vWindowSize = { mi.rcMonitor.right, mi.rcMonitor.bottom };
  95. vTopLeft.x = 0;
  96. vTopLeft.y = 0;
  97. }
  98.  
  99. // Keep client size as requested
  100. RECT rWndRect = { 0, 0, vWindowSize.x, vWindowSize.y };
  101. AdjustWindowRectEx(&rWndRect, dwStyle, FALSE, dwExStyle);
  102. int width = rWndRect.right - rWndRect.left;
  103. int height = rWndRect.bottom - rWndRect.top;
  104.  
  105. olc_hWnd = CreateWindowEx(dwExStyle, olcT("OLC_PIXEL_GAME_ENGINE"), olcT(""), dwStyle,
  106. vTopLeft.x, vTopLeft.y, width, height, NULL, NULL, GetModuleHandle(nullptr), this);
  107.  
  108. // Create Keyboard Mapping
  109. mapKeys[0x00] = Key::NONE;
  110. mapKeys[0x41] = Key::A; mapKeys[0x42] = Key::B; mapKeys[0x43] = Key::C; mapKeys[0x44] = Key::D; mapKeys[0x45] = Key::E;
  111. mapKeys[0x46] = Key::F; mapKeys[0x47] = Key::G; mapKeys[0x48] = Key::H; mapKeys[0x49] = Key::I; mapKeys[0x4A] = Key::J;
  112. mapKeys[0x4B] = Key::K; mapKeys[0x4C] = Key::L; mapKeys[0x4D] = Key::M; mapKeys[0x4E] = Key::N; mapKeys[0x4F] = Key::O;
  113. mapKeys[0x50] = Key::P; mapKeys[0x51] = Key::Q; mapKeys[0x52] = Key::R; mapKeys[0x53] = Key::S; mapKeys[0x54] = Key::T;
  114. mapKeys[0x55] = Key::U; mapKeys[0x56] = Key::V; mapKeys[0x57] = Key::W; mapKeys[0x58] = Key::X; mapKeys[0x59] = Key::Y;
  115. mapKeys[0x5A] = Key::Z;
  116.  
  117. mapKeys[VK_F1] = Key::F1; mapKeys[VK_F2] = Key::F2; mapKeys[VK_F3] = Key::F3; mapKeys[VK_F4] = Key::F4;
  118. mapKeys[VK_F5] = Key::F5; mapKeys[VK_F6] = Key::F6; mapKeys[VK_F7] = Key::F7; mapKeys[VK_F8] = Key::F8;
  119. mapKeys[VK_F9] = Key::F9; mapKeys[VK_F10] = Key::F10; mapKeys[VK_F11] = Key::F11; mapKeys[VK_F12] = Key::F12;
  120.  
  121. mapKeys[VK_DOWN] = Key::DOWN; mapKeys[VK_LEFT] = Key::LEFT; mapKeys[VK_RIGHT] = Key::RIGHT; mapKeys[VK_UP] = Key::UP;
  122. mapKeys[VK_RETURN] = Key::ENTER; //mapKeys[VK_RETURN] = Key::RETURN;
  123.  
  124. mapKeys[VK_BACK] = Key::BACK; mapKeys[VK_ESCAPE] = Key::ESCAPE; mapKeys[VK_RETURN] = Key::ENTER; mapKeys[VK_PAUSE] = Key::PAUSE;
  125. mapKeys[VK_SCROLL] = Key::SCROLL; mapKeys[VK_TAB] = Key::TAB; mapKeys[VK_DELETE] = Key::DEL; mapKeys[VK_HOME] = Key::HOME;
  126. mapKeys[VK_END] = Key::END; mapKeys[VK_PRIOR] = Key::PGUP; mapKeys[VK_NEXT] = Key::PGDN; mapKeys[VK_INSERT] = Key::INS;
  127. mapKeys[VK_SHIFT] = Key::SHIFT; mapKeys[VK_CONTROL] = Key::CTRL;
  128. mapKeys[VK_SPACE] = Key::SPACE;
  129.  
  130. mapKeys[0x30] = Key::K0; mapKeys[0x31] = Key::K1; mapKeys[0x32] = Key::K2; mapKeys[0x33] = Key::K3; mapKeys[0x34] = Key::K4;
  131. mapKeys[0x35] = Key::K5; mapKeys[0x36] = Key::K6; mapKeys[0x37] = Key::K7; mapKeys[0x38] = Key::K8; mapKeys[0x39] = Key::K9;
  132.  
  133. mapKeys[VK_NUMPAD0] = Key::NP0; mapKeys[VK_NUMPAD1] = Key::NP1; mapKeys[VK_NUMPAD2] = Key::NP2; mapKeys[VK_NUMPAD3] = Key::NP3; mapKeys[VK_NUMPAD4] = Key::NP4;
  134. mapKeys[VK_NUMPAD5] = Key::NP5; mapKeys[VK_NUMPAD6] = Key::NP6; mapKeys[VK_NUMPAD7] = Key::NP7; mapKeys[VK_NUMPAD8] = Key::NP8; mapKeys[VK_NUMPAD9] = Key::NP9;
  135. mapKeys[VK_MULTIPLY] = Key::NP_MUL; mapKeys[VK_ADD] = Key::NP_ADD; mapKeys[VK_DIVIDE] = Key::NP_DIV; mapKeys[VK_SUBTRACT] = Key::NP_SUB; mapKeys[VK_DECIMAL] = Key::NP_DECIMAL;
  136.  
  137. // Thanks scripticuk
  138. mapKeys[VK_OEM_1] = Key::OEM_1; // On US and UK keyboards this is the ';:' key
  139. mapKeys[VK_OEM_2] = Key::OEM_2; // On US and UK keyboards this is the '/?' key
  140. mapKeys[VK_OEM_3] = Key::OEM_3; // On US keyboard this is the '~' key
  141. mapKeys[VK_OEM_4] = Key::OEM_4; // On US and UK keyboards this is the '[{' key
  142. mapKeys[VK_OEM_5] = Key::OEM_5; // On US keyboard this is '\|' key.
  143. mapKeys[VK_OEM_6] = Key::OEM_6; // On US and UK keyboards this is the ']}' key
  144. mapKeys[VK_OEM_7] = Key::OEM_7; // On US keyboard this is the single/double quote key. On UK, this is the single quote/@ symbol key
  145. mapKeys[VK_OEM_8] = Key::OEM_8; // miscellaneous characters. Varies by keyboard
  146. mapKeys[VK_OEM_PLUS] = Key::EQUALS; // the '+' key on any keyboard
  147. mapKeys[VK_OEM_COMMA] = Key::COMMA; // the comma key on any keyboard
  148. mapKeys[VK_OEM_MINUS] = Key::MINUS; // the minus key on any keyboard
  149. mapKeys[VK_OEM_PERIOD] = Key::PERIOD; // the period key on any keyboard
  150. mapKeys[VK_CAPITAL] = Key::CAPS_LOCK;
  151. return olc::OK;
  152. }
  153.  
  154. virtual olc::rcode SetWindowTitle(const std::string& s) override
  155. {
  156. #ifdef UNICODE
  157. SetWindowText(olc_hWnd, ConvertS2W(s).c_str());
  158. #else
  159. SetWindowText(olc_hWnd, s.c_str());
  160. #endif
  161. return olc::OK;
  162. }
  163.  
  164. virtual olc::rcode StartSystemEventLoop() override
  165. {
  166. MSG msg;
  167. while (GetMessage(&msg, NULL, 0, 0) > 0)
  168. {
  169. TranslateMessage(&msg);
  170. DispatchMessage(&msg);
  171. }
  172. return olc::OK;
  173. }
  174.  
  175. virtual olc::rcode HandleSystemEvent() override { return olc::rcode::FAIL; }
  176.  
  177. // Windows Event Handler - this is statically connected to the windows event system
  178. static LRESULT CALLBACK olc_WindowEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  179. {
  180. switch (uMsg)
  181. {
  182. case WM_MOUSEMOVE:
  183. {
  184. // Thanks @ForAbby (Discord)
  185. uint16_t x = lParam & 0xFFFF; uint16_t y = (lParam >> 16) & 0xFFFF;
  186. int16_t ix = *(int16_t*)&x; int16_t iy = *(int16_t*)&y;
  187. ptrPGE->olc_UpdateMouse(ix, iy);
  188. return 0;
  189. }
  190. case WM_SIZE: ptrPGE->olc_UpdateWindowSize(lParam & 0xFFFF, (lParam >> 16) & 0xFFFF); return 0;
  191. case WM_MOUSEWHEEL: ptrPGE->olc_UpdateMouseWheel(GET_WHEEL_DELTA_WPARAM(wParam)); return 0;
  192. case WM_MOUSELEAVE: ptrPGE->olc_UpdateMouseFocus(false); return 0;
  193. case WM_SETFOCUS: ptrPGE->olc_UpdateKeyFocus(true); return 0;
  194. case WM_KILLFOCUS: ptrPGE->olc_UpdateKeyFocus(false); return 0;
  195. case WM_KEYDOWN: ptrPGE->olc_UpdateKeyState(mapKeys[wParam], true); return 0;
  196. case WM_KEYUP: ptrPGE->olc_UpdateKeyState(mapKeys[wParam], false); return 0;
  197. case WM_SYSKEYDOWN: ptrPGE->olc_UpdateKeyState(mapKeys[wParam], true); return 0;
  198. case WM_SYSKEYUP: ptrPGE->olc_UpdateKeyState(mapKeys[wParam], false); return 0;
  199. case WM_LBUTTONDOWN:ptrPGE->olc_UpdateMouseState(0, true); return 0;
  200. case WM_LBUTTONUP: ptrPGE->olc_UpdateMouseState(0, false); return 0;
  201. case WM_RBUTTONDOWN:ptrPGE->olc_UpdateMouseState(1, true); return 0;
  202. case WM_RBUTTONUP: ptrPGE->olc_UpdateMouseState(1, false); return 0;
  203. case WM_MBUTTONDOWN:ptrPGE->olc_UpdateMouseState(2, true); return 0;
  204. case WM_MBUTTONUP: ptrPGE->olc_UpdateMouseState(2, false); return 0;
  205. case WM_CLOSE: ptrPGE->olc_Terminate(); return 0;
  206. case WM_DESTROY: PostQuitMessage(0); DestroyWindow(hWnd); return 0;
  207. }
  208. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  209. }
  210. };
  211. }
  212. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement