Advertisement
Piratux

Untitled

Jan 31st, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.21 KB | None | 0 0
  1.  
  2. // O------------------------------------------------------------------------------O
  3. // | START IMAGE LOADER: stb_image.h, all systems, very fast |
  4. // O------------------------------------------------------------------------------O
  5. // Thanks to Sean Barrett - https://github.com/nothings/stb/blob/master/stb_image.h
  6. // MIT License - Copyright(c) 2017 Sean Barrett
  7.  
  8. // Note you need to download the above file into your project folder, and
  9. // #define OLC_IMAGE_STB
  10. // #define OLC_PGE_APPLICATION
  11. // #include "olcPixelGameEngine.h"
  12.  
  13. #if defined(OLC_IMAGE_STB)
  14. #define STB_IMAGE_IMPLEMENTATION
  15. #include "stb_image.h"
  16. namespace olc
  17. {
  18. class ImageLoader_STB : public olc::ImageLoader
  19. {
  20. public:
  21. ImageLoader_STB() : ImageLoader()
  22. {}
  23.  
  24. olc::rcode LoadImageResource(olc::Sprite* spr, const std::string& sImageFile, olc::ResourcePack* pack) override
  25. {
  26. UNUSED(pack);
  27. // clear out existing sprite
  28. if (spr->pColData != nullptr) delete[] spr->pColData;
  29. // Open file
  30. stbi_uc* bytes = nullptr;
  31. int w = 0, h = 0, cmp = 0;
  32. if (pack != nullptr)
  33. {
  34. ResourceBuffer rb = pack->GetFileBuffer(sImageFile);
  35. bytes = stbi_load_from_memory((unsigned char*)rb.vMemory.data(), rb.vMemory.size(), &w, &h, &cmp, 4);
  36. }
  37. else
  38. {
  39. // Check file exists
  40. if (!_gfs::exists(sImageFile)) return olc::rcode::NO_FILE;
  41. bytes = stbi_load(sImageFile.c_str(), &w, &h, &cmp, 4);
  42. }
  43.  
  44. if (!bytes) return olc::rcode::FAIL;
  45. spr->width = w; spr->height = h;
  46. spr->pColData = new Pixel[spr->width * spr->height];
  47. std::memcpy(spr->pColData, bytes, spr->width * spr->height * 4);
  48. delete[] bytes;
  49. return olc::rcode::OK;
  50. }
  51.  
  52. olc::rcode SaveImageResource(olc::Sprite* spr, const std::string& sImageFile) override
  53. {
  54. return olc::rcode::OK;
  55. }
  56. };
  57. }
  58. #endif
  59. // O------------------------------------------------------------------------------O
  60. // | START IMAGE LOADER: stb_image.h |
  61. // O------------------------------------------------------------------------------O
  62.  
  63.  
  64. // O------------------------------------------------------------------------------O
  65. // | START PLATFORM: MICROSOFT WINDOWS XP, VISTA, 7, 8, 10 |
  66. // O------------------------------------------------------------------------------O
  67. #if defined(OLC_PLATFORM_WINAPI)
  68.  
  69. #if defined(_WIN32) && !defined(__MINGW32__)
  70. #pragma comment(lib, "user32.lib") // Visual Studio Only
  71. #pragma comment(lib, "gdi32.lib") // For other Windows Compilers please add
  72. #pragma comment(lib, "opengl32.lib") // these libs to your linker input
  73. #endif
  74.  
  75. namespace olc
  76. {
  77. class Platform_Windows : public olc::Platform
  78. {
  79. private:
  80. HWND olc_hWnd = nullptr;
  81. std::wstring wsAppName;
  82.  
  83. std::wstring ConvertS2W(std::string s)
  84. {
  85. #ifdef __MINGW32__
  86. wchar_t* buffer = new wchar_t[s.length() + 1];
  87. mbstowcs(buffer, s.c_str(), s.length());
  88. buffer[s.length()] = L'\0';
  89. #else
  90. int count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
  91. wchar_t* buffer = new wchar_t[count];
  92. MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buffer, count);
  93. #endif
  94. std::wstring w(buffer);
  95. delete[] buffer;
  96. return w;
  97. }
  98.  
  99. public:
  100. virtual olc::rcode ApplicationStartUp() override { return olc::rcode::OK; }
  101. virtual olc::rcode ApplicationCleanUp() override { return olc::rcode::OK; }
  102. virtual olc::rcode ThreadStartUp() override { return olc::rcode::OK; }
  103.  
  104. virtual olc::rcode ThreadCleanUp() override
  105. {
  106. renderer->DestroyDevice();
  107. PostMessage(olc_hWnd, WM_DESTROY, 0, 0);
  108. return olc::OK;
  109. }
  110.  
  111. virtual olc::rcode CreateGraphics(bool bFullScreen, bool bEnableVSYNC, const olc::vi2d& vViewPos, const olc::vi2d& vViewSize) override
  112. {
  113. if (renderer->CreateDevice({ olc_hWnd }, bFullScreen, bEnableVSYNC) == olc::rcode::OK)
  114. {
  115. renderer->UpdateViewport(vViewPos, vViewSize);
  116. return olc::rcode::OK;
  117. }
  118. else
  119. return olc::rcode::FAIL;
  120. }
  121.  
  122. virtual olc::rcode CreateWindowPane(const olc::vi2d& vWindowPos, olc::vi2d& vWindowSize, bool bFullScreen) override
  123. {
  124. WNDCLASS wc;
  125. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  126. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  127. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  128. wc.hInstance = GetModuleHandle(nullptr);
  129. wc.lpfnWndProc = olc_WindowEvent;
  130. wc.cbClsExtra = 0;
  131. wc.cbWndExtra = 0;
  132. wc.lpszMenuName = nullptr;
  133. wc.hbrBackground = nullptr;
  134. wc.lpszClassName = olcT("OLC_PIXEL_GAME_ENGINE");
  135. RegisterClass(&wc);
  136.  
  137. // Define window furniture
  138. DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
  139. DWORD dwStyle = WS_CAPTION | WS_SYSMENU | WS_VISIBLE | WS_THICKFRAME;
  140.  
  141. olc::vi2d vTopLeft = vWindowPos;
  142.  
  143. // Handle Fullscreen
  144. if (bFullScreen)
  145. {
  146. dwExStyle = 0;
  147. dwStyle = WS_VISIBLE | WS_POPUP;
  148. HMONITOR hmon = MonitorFromWindow(olc_hWnd, MONITOR_DEFAULTTONEAREST);
  149. MONITORINFO mi = { sizeof(mi) };
  150. if (!GetMonitorInfo(hmon, &mi)) return olc::rcode::FAIL;
  151. vWindowSize = { mi.rcMonitor.right, mi.rcMonitor.bottom };
  152. vTopLeft.x = 0;
  153. vTopLeft.y = 0;
  154. }
  155.  
  156. // Keep client size as requested
  157. RECT rWndRect = { 0, 0, vWindowSize.x, vWindowSize.y };
  158. AdjustWindowRectEx(&rWndRect, dwStyle, FALSE, dwExStyle);
  159. int width = rWndRect.right - rWndRect.left;
  160. int height = rWndRect.bottom - rWndRect.top;
  161.  
  162. olc_hWnd = CreateWindowEx(dwExStyle, olcT("OLC_PIXEL_GAME_ENGINE"), olcT(""), dwStyle,
  163. vTopLeft.x, vTopLeft.y, width, height, NULL, NULL, GetModuleHandle(nullptr), this);
  164.  
  165. // Create Keyboard Mapping
  166. mapKeys[0x00] = Key::NONE;
  167. mapKeys[0x41] = Key::A; mapKeys[0x42] = Key::B; mapKeys[0x43] = Key::C; mapKeys[0x44] = Key::D; mapKeys[0x45] = Key::E;
  168. mapKeys[0x46] = Key::F; mapKeys[0x47] = Key::G; mapKeys[0x48] = Key::H; mapKeys[0x49] = Key::I; mapKeys[0x4A] = Key::J;
  169. mapKeys[0x4B] = Key::K; mapKeys[0x4C] = Key::L; mapKeys[0x4D] = Key::M; mapKeys[0x4E] = Key::N; mapKeys[0x4F] = Key::O;
  170. mapKeys[0x50] = Key::P; mapKeys[0x51] = Key::Q; mapKeys[0x52] = Key::R; mapKeys[0x53] = Key::S; mapKeys[0x54] = Key::T;
  171. mapKeys[0x55] = Key::U; mapKeys[0x56] = Key::V; mapKeys[0x57] = Key::W; mapKeys[0x58] = Key::X; mapKeys[0x59] = Key::Y;
  172. mapKeys[0x5A] = Key::Z;
  173.  
  174. mapKeys[VK_F1] = Key::F1; mapKeys[VK_F2] = Key::F2; mapKeys[VK_F3] = Key::F3; mapKeys[VK_F4] = Key::F4;
  175. mapKeys[VK_F5] = Key::F5; mapKeys[VK_F6] = Key::F6; mapKeys[VK_F7] = Key::F7; mapKeys[VK_F8] = Key::F8;
  176. mapKeys[VK_F9] = Key::F9; mapKeys[VK_F10] = Key::F10; mapKeys[VK_F11] = Key::F11; mapKeys[VK_F12] = Key::F12;
  177.  
  178. mapKeys[VK_DOWN] = Key::DOWN; mapKeys[VK_LEFT] = Key::LEFT; mapKeys[VK_RIGHT] = Key::RIGHT; mapKeys[VK_UP] = Key::UP;
  179. mapKeys[VK_RETURN] = Key::ENTER; //mapKeys[VK_RETURN] = Key::RETURN;
  180.  
  181. mapKeys[VK_BACK] = Key::BACK; mapKeys[VK_ESCAPE] = Key::ESCAPE; mapKeys[VK_RETURN] = Key::ENTER; mapKeys[VK_PAUSE] = Key::PAUSE;
  182. mapKeys[VK_SCROLL] = Key::SCROLL; mapKeys[VK_TAB] = Key::TAB; mapKeys[VK_DELETE] = Key::DEL; mapKeys[VK_HOME] = Key::HOME;
  183. mapKeys[VK_END] = Key::END; mapKeys[VK_PRIOR] = Key::PGUP; mapKeys[VK_NEXT] = Key::PGDN; mapKeys[VK_INSERT] = Key::INS;
  184. mapKeys[VK_SHIFT] = Key::SHIFT; mapKeys[VK_CONTROL] = Key::CTRL;
  185. mapKeys[VK_SPACE] = Key::SPACE;
  186.  
  187. mapKeys[0x30] = Key::K0; mapKeys[0x31] = Key::K1; mapKeys[0x32] = Key::K2; mapKeys[0x33] = Key::K3; mapKeys[0x34] = Key::K4;
  188. mapKeys[0x35] = Key::K5; mapKeys[0x36] = Key::K6; mapKeys[0x37] = Key::K7; mapKeys[0x38] = Key::K8; mapKeys[0x39] = Key::K9;
  189.  
  190. 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;
  191. 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;
  192. 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;
  193.  
  194. // Thanks scripticuk
  195. mapKeys[VK_OEM_1] = Key::OEM_1; // On US and UK keyboards this is the ';:' key
  196. mapKeys[VK_OEM_2] = Key::OEM_2; // On US and UK keyboards this is the '/?' key
  197. mapKeys[VK_OEM_3] = Key::OEM_3; // On US keyboard this is the '~' key
  198. mapKeys[VK_OEM_4] = Key::OEM_4; // On US and UK keyboards this is the '[{' key
  199. mapKeys[VK_OEM_5] = Key::OEM_5; // On US keyboard this is '\|' key.
  200. mapKeys[VK_OEM_6] = Key::OEM_6; // On US and UK keyboards this is the ']}' key
  201. 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
  202. mapKeys[VK_OEM_8] = Key::OEM_8; // miscellaneous characters. Varies by keyboard
  203. mapKeys[VK_OEM_PLUS] = Key::EQUALS; // the '+' key on any keyboard
  204. mapKeys[VK_OEM_COMMA] = Key::COMMA; // the comma key on any keyboard
  205. mapKeys[VK_OEM_MINUS] = Key::MINUS; // the minus key on any keyboard
  206. mapKeys[VK_OEM_PERIOD] = Key::PERIOD; // the period key on any keyboard
  207. mapKeys[VK_CAPITAL] = Key::CAPS_LOCK;
  208. return olc::OK;
  209. }
  210.  
  211. virtual olc::rcode SetWindowTitle(const std::string& s) override
  212. {
  213. #ifdef UNICODE
  214. SetWindowText(olc_hWnd, ConvertS2W(s).c_str());
  215. #else
  216. SetWindowText(olc_hWnd, s.c_str());
  217. #endif
  218. return olc::OK;
  219. }
  220.  
  221. virtual olc::rcode StartSystemEventLoop() override
  222. {
  223. MSG msg;
  224. while (GetMessage(&msg, NULL, 0, 0) > 0)
  225. {
  226. TranslateMessage(&msg);
  227. DispatchMessage(&msg);
  228. }
  229. return olc::OK;
  230. }
  231.  
  232. virtual olc::rcode HandleSystemEvent() override { return olc::rcode::FAIL; }
  233.  
  234. // Windows Event Handler - this is statically connected to the windows event system
  235. static LRESULT CALLBACK olc_WindowEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  236. {
  237. switch (uMsg)
  238. {
  239. case WM_MOUSEMOVE:
  240. {
  241. // Thanks @ForAbby (Discord)
  242. uint16_t x = lParam & 0xFFFF; uint16_t y = (lParam >> 16) & 0xFFFF;
  243. int16_t ix = *(int16_t*)&x; int16_t iy = *(int16_t*)&y;
  244. ptrPGE->olc_UpdateMouse(ix, iy);
  245. return 0;
  246. }
  247. case WM_SIZE: ptrPGE->olc_UpdateWindowSize(lParam & 0xFFFF, (lParam >> 16) & 0xFFFF); return 0;
  248. case WM_MOUSEWHEEL: ptrPGE->olc_UpdateMouseWheel(GET_WHEEL_DELTA_WPARAM(wParam)); return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement