Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

olcPixelGameEngine.h 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. olcPixelGameEngine.h
  3. +-------------------------------------------------------------+
  4. | OneLoneCoder Pixel Game Engine v1.17 |
  5. | "Like the command prompt console one, but not..." - javidx9 |
  6. +-------------------------------------------------------------+
  7. What is this?
  8. ~~~~~~~~~~~~~
  9. The olcConsoleGameEngine has been a surprising and wonderful success for me,
  10. and I'm delighted how people have reacted so positively towards it, so thanks
  11. for that.
  12. However, there are limitations that I simply cannot avoid. Firstly, I need to
  13. maintain several different versions of it to accommodate users on Windows7,
  14. 8, 10, Linux, Mac, Visual Studio & Code::Blocks. Secondly, this year I've been
  15. pushing the console to the limits of its graphical capabilities and the effect
  16. is becoming underwhelming. The engine itself is not slow at all, but the process
  17. that Windows uses to draw the command prompt to the screen is, and worse still,
  18. it's dynamic based upon the variation of character colours and glyphs. Sadly
  19. I have no control over this, and recent videos that are extremely graphical
  20. (for a command prompt :P ) have been dipping to unacceptable framerates. As
  21. the channel has been popular with aspiring game developers, I'm concerned that
  22. the visual appeal of the command prompt is perhaps limited to us oldies, and I
  23. dont want to alienate younger learners. Finally, I'd like to demonstrate many
  24. more algorithms and image processing that exist in the graphical domain, for
  25. which the console is insufficient.
  26. For this reason, I have created olcPixelGameEngine! The look and feel to the
  27. programmer is almost identical, so all of my existing code from the videos is
  28. easily portable, and the programmer uses this file in exactly the same way. But
  29. I've decided that rather than just build a command prompt emulator, that I
  30. would at least harness some modern(ish) portable technologies.
  31. As a result, the olcPixelGameEngine supports 32-bit colour, is written in a
  32. cross-platform style, uses modern(ish) C++ conventions and most importantly,
  33. renders much much faster. I will use this version when my applications are
  34. predominantly graphics based, but use the console version when they are
  35. predominantly text based - Don't worry, loads more command prompt silliness to
  36. come yet, but evolution is important!!
  37. License (OLC-3)
  38. ~~~~~~~~~~~~~~~
  39. Copyright 2018 - 2019 OneLoneCoder.com
  40. Redistribution and use in source and binary forms, with or without modification,
  41. are permitted provided that the following conditions are met:
  42. 1. Redistributions or derivations of source code must retain the above copyright
  43. notice, this list of conditions and the following disclaimer.
  44. 2. Redistributions or derivative works in binary form must reproduce the above
  45. copyright notice. This list of conditions and the following disclaimer must be
  46. reproduced in the documentation and/or other materials provided with the distribution.
  47. 3. Neither the name of the copyright holder nor the names of its contributors may
  48. be used to endorse or promote products derived from this software without specific
  49. prior written permission.
  50. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  51. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  52. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  53. SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  54. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  55. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  56. BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  57. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  58. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. SUCH DAMAGE.
  60. Links
  61. ~~~~~
  62. YouTube: https://www.youtube.com/javidx9
  63. https://www.youtube.com/javidx9extra
  64. Discord: https://discord.gg/WhwHUMV
  65. Twitter: https://www.twitter.com/javidx9
  66. Twitch: https://www.twitch.tv/javidx9
  67. GitHub: https://www.github.com/onelonecoder
  68. Homepage: https://www.onelonecoder.com
  69. Patreon: https://www.patreon.com/javidx9
  70. Relevant Videos
  71. ~~~~~~~~~~~~~~~
  72. https://youtu.be/kRH6oJLFYxY Introducing olcPixelGameEngine
  73. Compiling in Linux
  74. ~~~~~~~~~~~~~~~~~~
  75. You will need a modern C++ compiler, so update yours!
  76. To compile use the command:
  77. g++ -o YourProgName YourSource.cpp -lX11 -lGL -lpthread -lpng
  78. On some Linux configurations, the frame rate is locked to the refresh
  79. rate of the monitor. This engine tries to unlock it but may not be
  80. able to, in which case try launching your program like this:
  81. vblank_mode=0 ./YourProgName
  82. Compiling in Code::Blocks on Windows
  83. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. Well I wont judge you, but make sure your Code::Blocks installation
  85. is really up to date - you may even consider updating your C++ toolchain
  86. to use MinGW32-W64, so google this. You will also need to enable C++14
  87. in your build options, and add to your linker the following libraries:
  88. user32 gdi32 opengl32 gdiplus
  89. Ports
  90. ~~~~~
  91. olc::PixelGameEngine has been ported and tested with varying degrees of
  92. success to: WinXP, Win7, Win8, Win10, Various Linux, Rapberry Pi,
  93. Chromebook, Playstation Portable (PSP) and Nintendo Switch. If you are
  94. interested in the details of these ports, come and visit the Discord!
  95. Thanks
  96. ~~~~~~
  97. I'd like to extend thanks to Eremiell, slavka, gurkanctn, Phantim,
  98. JackOJC, KrossX, Huhlig, Dragoneye, Appa, JustinRichardsMusic, SliceNDice
  99. Ralakus, Gorbit99, raoul, joshinils, benedani & MagetzUb for advice, ideas and
  100. testing, and I'd like to extend my appreciation to the 40K YouTube followers,
  101. 22 Patreons and 2.6K Discord server members who give me the motivation to keep
  102. going with all this :D
  103. Special thanks to those who bring gifts!
  104. GnarGnarHead.......Domina
  105. Gorbit99...........Bastion, Ori & The Blind Forest
  106. Marti Morta........Gris
  107. Special thanks to my Patreons too - I wont name you on here, but I've
  108. certainly enjoyed my tea and flapjacks :D
  109. Author
  110. ~~~~~~
  111. David Barr, aka javidx9, ©OneLoneCoder 2018, 2019
  112. */
  113. //////////////////////////////////////////////////////////////////////////////////////////
  114. /* Example Usage (main.cpp)
  115. #define OLC_PGE_APPLICATION
  116. #include "olcPixelGameEngine.h"
  117. // Override base class with your custom functionality
  118. class Example : public olc::PixelGameEngine
  119. {
  120. public:
  121. Example()
  122. {
  123. sAppName = "Example";
  124. }
  125. public:
  126. bool OnUserCreate() override
  127. {
  128. // Called once at the start, so create things here
  129. return true;
  130. }
  131. bool OnUserUpdate(float fElapsedTime) override
  132. {
  133. // called once per frame, draws random coloured pixels
  134. for (int x = 0; x < ScreenWidth(); x++)
  135. for (int y = 0; y < ScreenHeight(); y++)
  136. Draw(x, y, olc::Pixel(rand() % 255, rand() % 255, rand()% 255));
  137. return true;
  138. }
  139. };
  140. int main()
  141. {
  142. Example demo;
  143. if (demo.Construct(256, 240, 4, 4))
  144. demo.Start();
  145. return 0;
  146. }
  147. */
  148. #ifndef OLC_PGE_DEF
  149. #define OLC_PGE_DEF
  150. #ifdef _WIN32
  151. // Link to libraries
  152. #ifndef __MINGW32__
  153. #pragma comment(lib, "user32.lib") // Visual Studio Only
  154. #pragma comment(lib, "gdi32.lib") // For other Windows Compilers please add
  155. #pragma comment(lib, "opengl32.lib") // these libs to your linker input
  156. #pragma comment(lib, "gdiplus.lib")
  157. #else
  158. // In Code::Blocks, Select C++14 in your build options, and add the
  159. // following libs to your linker: user32 gdi32 opengl32 gdiplus
  160. #if !defined _WIN32_WINNT
  161. #ifdef HAVE_MSMF
  162. #define _WIN32_WINNT 0x0600 // Windows Vista
  163. #else
  164. #define _WIN32_WINNT 0x0500 // Windows 2000
  165. #endif
  166. #endif
  167. #endif
  168. // Include WinAPI
  169. #include <windows.h>
  170. #include <gdiplus.h>
  171. // OpenGL Extension
  172. #include <GL/gl.h>
  173. typedef BOOL(WINAPI wglSwapInterval_t) (int interval);
  174. #else
  175. #include <GL/gl.h>
  176. #include <GL/glx.h>
  177. #include <X11/X.h>
  178. #include <X11/Xlib.h>
  179. #include <png.h>
  180. typedef int(glSwapInterval_t) (Display *dpy, GLXDrawable drawable, int interval);
  181. #endif
  182. // Standard includes
  183. #include <cmath>
  184. #include <cstdint>
  185. #include <string>
  186. #include <iostream>
  187. #include <streambuf>
  188. #include <chrono>
  189. #include <vector>
  190. #include <list>
  191. #include <thread>
  192. #include <atomic>
  193. #include <condition_variable>
  194. #include <fstream>
  195. #include <map>
  196. #include <functional>
  197. #include <algorithm>
  198. #include "olcPGE_Common.h"
  199. #include "olcPGE_ResourcePack.h"
  200. #undef min
  201. #undef max
  202. namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
  203. {
  204. struct Pixel
  205. {
  206. union
  207. {
  208. uint32_t n = 0xFF000000;
  209. struct
  210. {
  211. uint8_t r; uint8_t g; uint8_t b; uint8_t a;
  212. };
  213. };
  214. Pixel();
  215. Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
  216. Pixel(uint32_t p);
  217. static Pixel CreateFromHSV(uint8_t h, uint8_t s, uint8_t v, uint8_t alpha = 255);
  218. enum Mode { NORMAL, MASK, ALPHA, CUSTOM };
  219. };
  220. // Some constants for symbolic naming of Pixels
  221. static const Pixel
  222. WHITE(255, 255, 255),
  223. GREY(192, 192, 192), DARK_GREY(128, 128, 128), VERY_DARK_GREY(64, 64, 64),
  224. RED(255, 0, 0), DARK_RED(128, 0, 0), VERY_DARK_RED(64, 0, 0),
  225. YELLOW(255, 255, 0), DARK_YELLOW(128, 128, 0), VERY_DARK_YELLOW(64, 64, 0),
  226. GREEN(0, 255, 0), DARK_GREEN(0, 128, 0), VERY_DARK_GREEN(0, 64, 0),
  227. CYAN(0, 255, 255), DARK_CYAN(0, 128, 128), VERY_DARK_CYAN(0, 64, 64),
  228. BLUE(0, 0, 255), DARK_BLUE(0, 0, 128), VERY_DARK_BLUE(0, 0, 64),
  229. MAGENTA(255, 0, 255), DARK_MAGENTA(128, 0, 128), VERY_DARK_MAGENTA(64, 0, 64),
  230. BLACK(0, 0, 0),
  231. BLANK(0, 0, 0, 0);
  232. //==================================================================================
  233. template <class T>
  234. struct v2d_generic
  235. {
  236. T x = 0;
  237. T y = 0;
  238. inline v2d_generic() : x(0), y(0) { }
  239. inline v2d_generic(T _x, T _y) : x(_x), y(_y) { }
  240. inline v2d_generic(const v2d_generic& v) : x(v.x), y(v.y){ }
  241. inline T mag() { return sqrt(x * x + y * y); }
  242. inline v2d_generic norm() { T r = 1 / mag(); return v2d_generic(x*r, y*r); }
  243. inline v2d_generic perp() { return v2d_generic(-y, x); }
  244. inline T dot(const v2d_generic& rhs) { return this->x * rhs.x + this->y * rhs.y; }
  245. inline T cross(const v2d_generic& rhs) { return this->x * rhs.y - this->y * rhs.x; }
  246. inline v2d_generic operator + (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y);}
  247. inline v2d_generic operator - (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y);}
  248. inline v2d_generic operator * (const T& rhs) const { return v2d_generic(this->x * rhs, this->y * rhs); }
  249. inline v2d_generic operator / (const T& rhs) const { return v2d_generic(this->x / rhs, this->y / rhs); }
  250. inline v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this; }
  251. inline v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this; }
  252. inline v2d_generic& operator *= (const T& rhs) { this->x *= rhs; this->y *= rhs; return *this; }
  253. inline v2d_generic& operator /= (const T& rhs) { this->x /= rhs; this->y /= rhs; return *this; }
  254. inline T& operator [] (std::size_t i) { return *((T*)this + i); /* <-- D'oh :( */ }
  255. };
  256. template<class T> inline v2d_generic<T> operator * (const float& lhs, const v2d_generic<T>& rhs) { return v2d_generic<T>(lhs * rhs.x, lhs * rhs.y); }
  257. template<class T> inline v2d_generic<T> operator * (const double& lhs, const v2d_generic<T>& rhs){ return v2d_generic<T>(lhs * rhs.x, lhs * rhs.y); }
  258. template<class T> inline v2d_generic<T> operator * (const int& lhs, const v2d_generic<T>& rhs) { return v2d_generic<T>(lhs * rhs.x, lhs * rhs.y); }
  259. template<class T> inline v2d_generic<T> operator / (const float& lhs, const v2d_generic<T>& rhs) { return v2d_generic<T>(lhs / rhs.x, lhs / rhs.y); }
  260. template<class T> inline v2d_generic<T> operator / (const double& lhs, const v2d_generic<T>& rhs){ return v2d_generic<T>(lhs / rhs.x, lhs / rhs.y); }
  261. template<class T> inline v2d_generic<T> operator / (const int& lhs, const v2d_generic<T>& rhs) { return v2d_generic<T>(lhs / rhs.x, lhs / rhs.y); }
  262. typedef v2d_generic<int> vi2d;
  263. typedef v2d_generic<float> vf2d;
  264. typedef v2d_generic<double> vd2d;
  265. //=============================================================
  266. struct HWButton
  267. {
  268. bool bPressed = false; // Set once during the frame the event occurs
  269. bool bReleased = false; // Set once during the frame the event occurs
  270. bool bHeld = false; // Set true for all frames between pressed and released events
  271. };
  272. //=============================================================
  273. // A bitmap-like structure that stores a 2D array of Pixels
  274. class Sprite
  275. {
  276. public:
  277. Sprite();
  278. Sprite(std::string sImageFile);
  279. Sprite(std::string sImageFile, olc::ResourcePack *pack);
  280. Sprite(int32_t w, int32_t h);
  281. ~Sprite();
  282. public:
  283. olc::rcode LoadFromFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
  284. olc::rcode LoadFromPGESprFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
  285. olc::rcode SaveToPGESprFile(std::string sImageFile);
  286. public:
  287. int32_t width = 0;
  288. int32_t height = 0;
  289. enum Mode { NORMAL, PERIODIC };
  290. public:
  291. void SetSampleMode(olc::Sprite::Mode mode = olc::Sprite::Mode::NORMAL);
  292. Pixel GetPixel(int32_t x, int32_t y);
  293. bool SetPixel(int32_t x, int32_t y, Pixel p);
  294. Pixel Sample(float x, float y);
  295. Pixel SampleBL(float u, float v);
  296. Pixel* GetData();
  297. private:
  298. Pixel *pColData = nullptr;
  299. Mode modeSample = Mode::NORMAL;
  300. #ifdef OLC_DBG_OVERDRAW
  301. public:
  302. static int nOverdrawCount;
  303. #endif
  304. };
  305. //=============================================================
  306. enum Key
  307. {
  308. NONE,
  309. A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
  310. K0, K1, K2, K3, K4, K5, K6, K7, K8, K9,
  311. F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
  312. UP, DOWN, LEFT, RIGHT,
  313. SPACE, TAB, SHIFT, CTRL, INS, DEL, HOME, END, PGUP, PGDN,
  314. BACK, ESCAPE, RETURN, ENTER, PAUSE, SCROLL,
  315. NP0, NP1, NP2, NP3, NP4, NP5, NP6, NP7, NP8, NP9,
  316. NP_MUL, NP_DIV, NP_ADD, NP_SUB, NP_DECIMAL,
  317. };
  318. //=============================================================
  319. class PixelGameEngine
  320. {
  321. public:
  322. PixelGameEngine();
  323. public:
  324. olc::rcode Construct(uint32_t screen_w, uint32_t screen_h, uint32_t pixel_w, uint32_t pixel_h, bool full_screen = false);
  325. // Selects maximum pixel size to fit in the display resolution
  326. olc::rcode ConstructAuto(uint32_t screen_w, uint32_t screen_h, bool full_screen = false);
  327. olc::rcode Start();
  328. public: // Override Interfaces
  329. // Called once on application startup, use to load your resources
  330. virtual bool OnUserCreate();
  331. // Called every frame, and provides you with a time per frame value
  332. virtual bool OnUserUpdate(float fElapsedTime);
  333. // Called once on application termination, so you can be a clean coder
  334. virtual bool OnUserDestroy();
  335. public: // Hardware Interfaces
  336. // Returns true if window is currently in focus
  337. bool IsFocused();
  338. // Get the state of a specific keyboard button
  339. HWButton GetKey(Key k);
  340. // Get the state of a specific mouse button
  341. HWButton GetMouse(uint32_t b);
  342. // Get Mouse X coordinate in "pixel" space
  343. int32_t GetMouseX();
  344. // Get Mouse Y coordinate in "pixel" space
  345. int32_t GetMouseY();
  346. // Get Mouse Wheel Delta
  347. int32_t GetMouseWheel();
  348. public: // Utility
  349. // Returns the width of the screen in "pixels"
  350. int32_t ScreenWidth();
  351. // Returns the height of the screen in "pixels"
  352. int32_t ScreenHeight();
  353. // Returns the width of the currently selected drawing target in "pixels"
  354. int32_t GetDrawTargetWidth();
  355. // Returns the height of the currently selected drawing target in "pixels"
  356. int32_t GetDrawTargetHeight();
  357. // Returns the currently active draw target
  358. Sprite* GetDrawTarget();
  359. public: // Draw Routines
  360. // Specify which Sprite should be the target of drawing functions, use nullptr
  361. // to specify the primary screen
  362. void SetDrawTarget(Sprite *target);
  363. // Change the pixel mode for different optimisations
  364. // olc::Pixel::NORMAL = No transparency
  365. // olc::Pixel::MASK = Transparent if alpha is < 255
  366. // olc::Pixel::ALPHA = Full transparency
  367. void SetPixelMode(Pixel::Mode m);
  368. Pixel::Mode GetPixelMode();
  369. // Use a custom blend function
  370. void SetPixelMode(std::function<olc::Pixel(const int x, const int y, const olc::Pixel& pSource, const olc::Pixel& pDest)> pixelMode);
  371. // Change the blend factor form between 0.0f to 1.0f;
  372. void SetPixelBlend(float fBlend);
  373. // Offset texels by sub-pixel amount (advanced, do not use)
  374. void SetSubPixelOffset(float ox, float oy);
  375. // Draws a single Pixel
  376. virtual bool Draw(int32_t x, int32_t y, Pixel p = olc::WHITE);
  377. // Draws a line from (x1,y1) to (x2,y2)
  378. void DrawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, Pixel p = olc::WHITE, uint32_t pattern = 0xFFFFFFFF);
  379. // Draws a circle located at (x,y) with radius
  380. void DrawCircle(int32_t x, int32_t y, int32_t radius, Pixel p = olc::WHITE, uint8_t mask = 0xFF);
  381. // Fills a circle located at (x,y) with radius
  382. void FillCircle(int32_t x, int32_t y, int32_t radius, Pixel p = olc::WHITE);
  383. // Draws a rectangle at (x,y) to (x+w,y+h)
  384. void DrawRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p = olc::WHITE);
  385. // Fills a rectangle at (x,y) to (x+w,y+h)
  386. void FillRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p = olc::WHITE);
  387. // Draws a triangle between points (x1,y1), (x2,y2) and (x3,y3)
  388. void DrawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
  389. // Flat fills a triangle between points (x1,y1), (x2,y2) and (x3,y3)
  390. void FillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
  391. // Draws an entire sprite at location (x,y)
  392. void DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale = 1);
  393. // Draws an area of a sprite at location (x,y), where the
  394. // selected area is (ox,oy) to (ox+w,oy+h)
  395. void DrawPartialSprite(int32_t x, int32_t y, Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, uint32_t scale = 1);
  396. // Draws a single line of text
  397. void DrawString(int32_t x, int32_t y, std::string sText, Pixel col = olc::WHITE, uint32_t scale = 1);
  398. // Clears entire draw target to Pixel
  399. void Clear(Pixel p);
  400. public: // Branding
  401. std::string sAppName;
  402. private: // Inner mysterious workings
  403. Sprite *pDefaultDrawTarget = nullptr;
  404. Sprite *pDrawTarget = nullptr;
  405. Pixel::Mode nPixelMode = Pixel::NORMAL;
  406. float fBlendFactor = 1.0f;
  407. uint32_t nScreenWidth = 256;
  408. uint32_t nScreenHeight = 240;
  409. uint32_t nPixelWidth = 4;
  410. uint32_t nPixelHeight = 4;
  411. int32_t nMousePosX = 0;
  412. int32_t nMousePosY = 0;
  413. int32_t nMouseWheelDelta = 0;
  414. int32_t nMousePosXcache = 0;
  415. int32_t nMousePosYcache = 0;
  416. int32_t nMouseWheelDeltaCache = 0;
  417. int32_t nWindowWidth = 0;
  418. int32_t nWindowHeight = 0;
  419. int32_t nViewX = 0;
  420. int32_t nViewY = 0;
  421. int32_t nViewW = 0;
  422. int32_t nViewH = 0;
  423. bool bFullScreen = false;
  424. float fPixelX = 1.0f;
  425. float fPixelY = 1.0f;
  426. float fSubPixelOffsetX = 0.0f;
  427. float fSubPixelOffsetY = 0.0f;
  428. bool bHasInputFocus = false;
  429. bool bHasMouseFocus = false;
  430. float fFrameTimer = 1.0f;
  431. int nFrameCount = 0;
  432. Sprite *fontSprite = nullptr;
  433. std::function<olc::Pixel(const int x, const int y, const olc::Pixel&, const olc::Pixel&)> funcPixelMode;
  434. static std::map<uint16_t, uint8_t> mapKeys;
  435. bool pKeyNewState[256]{ 0 };
  436. bool pKeyOldState[256]{ 0 };
  437. HWButton pKeyboardState[256];
  438. bool pMouseNewState[5]{ 0 };
  439. bool pMouseOldState[5]{ 0 };
  440. HWButton pMouseState[5];
  441. #ifdef _WIN32
  442. HDC glDeviceContext = nullptr;
  443. HGLRC glRenderContext = nullptr;
  444. #else
  445. GLXContext glDeviceContext = nullptr;
  446. GLXContext glRenderContext = nullptr;
  447. #endif
  448. GLuint glBuffer;
  449. void EngineThread();
  450. // If anything sets this flag to false, the engine
  451. // "should" shut down gracefully
  452. static std::atomic<bool> bAtomActive;
  453. // Common initialisation functions
  454. void olc_UpdateMouse(int32_t x, int32_t y);
  455. void olc_UpdateMouseWheel(int32_t delta);
  456. void olc_UpdateWindowSize(int32_t x, int32_t y);
  457. void olc_UpdateViewport();
  458. bool olc_OpenGLCreate();
  459. void olc_ConstructFontSheet();
  460. uint32_t olc_GetMaximumPixelSize(uint32_t w, uint32_t h);
  461. #ifdef _WIN32
  462. // Windows specific window handling
  463. HWND olc_hWnd = nullptr;
  464. HWND olc_WindowCreate();
  465. std::wstring wsAppName;
  466. static LRESULT CALLBACK olc_WindowEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  467. #else
  468. // Non-Windows specific window handling
  469. Display* olc_Display = nullptr;
  470. Window olc_WindowRoot;
  471. Window olc_Window;
  472. XVisualInfo* olc_VisualInfo;
  473. Colormap olc_ColourMap;
  474. XSetWindowAttributes olc_SetWindowAttribs;
  475. XSizeHints olc_SizeHints;
  476. Display* olc_WindowCreate();
  477. #endif
  478. };
  479. class PGEX
  480. {
  481. friend class olc::PixelGameEngine;
  482. protected:
  483. static PixelGameEngine* pge;
  484. };
  485. //=============================================================
  486. }
  487. #endif // OLC_PGE_DEF