Browse Source

Upload source

Pabloader 5 years ago
commit
df2e263fa2

+ 5
- 0
.gitignore View File

@@ -0,0 +1,5 @@
1
+*.exe
2
+*.o
3
+*.pgp
4
+*.pgs
5
+*.res

+ 31
- 0
LICENCE.md View File

@@ -0,0 +1,31 @@
1
+# License (OLC-3)
2
+
3
+Copyright 2018-2019 OneLoneCoder.com
4
+
5
+Redistribution and use in source and binary forms, with or without 
6
+modification, are permitted provided that the following conditions 
7
+are met:
8
+
9
+1. Redistributions or derivations of source code must retain the above 
10
+   copyright notice, this list of conditions and the following disclaimer.
11
+
12
+2. Redistributions or derivative works in binary form must reproduce 
13
+   the above copyright notice. This list of conditions and the following 
14
+   disclaimer must be reproduced in the documentation and/or other 
15
+   materials provided with the distribution.
16
+
17
+3. Neither the name of the copyright holder nor the names of its 
18
+   contributors may be used to endorse or promote products derived 
19
+   from this software without specific prior written permission.
20
+    
21
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

+ 32
- 0
Makefile View File

@@ -0,0 +1,32 @@
1
+CPPFLAGS=-Wall -Wno-misleading-indentation -Werror --std=c++1z
2
+LDFLAGS=
3
+INCLUDES=-I./pge/include
4
+ifeq ($(OS),Windows_NT)
5
+	LIBRARIES=-luser32 -lgdi32 -lgdiplus
6
+	EXECUTABLE=packer.exe
7
+	CPP=i686-w64-mingw32-g++
8
+	CC=i686-w64-mingw32-gcc
9
+else
10
+	EXECUTABLE=packer
11
+	LIBRARIES=-lpng
12
+	CC=gcc
13
+	CPP=g++
14
+endif
15
+ifeq ($(DEBUG),1)
16
+	CPPFLAGS+=-g -Og
17
+else
18
+	CPPFLAGS+=-flto -O3
19
+endif
20
+SOURCES=$(wildcard src/*.cpp) $(wildcard pge/src/*.cpp)
21
+OBJECTS=$(SOURCES:.cpp=.o)
22
+
23
+all: $(SOURCES) $(EXECUTABLE)
24
+
25
+clean: 
26
+	$(RM) $(OBJECTS) $(EXECUTABLE)
27
+	
28
+$(EXECUTABLE): $(OBJECTS) 
29
+	$(CPP) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)
30
+
31
+.cpp.o:
32
+	$(CPP) $(INCLUDES) -c $(CPPFLAGS) $< -o $@

+ 20
- 0
README.md View File

@@ -0,0 +1,20 @@
1
+# How to compile on Windows
2
+
3
+To compile on windows you need MinGW-32 and MSYS (or Cygwin or Git-Bash) installed in PATH
4
+
5
+## Download links: 
6
+- [MinGW](http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/installer/mingw-w64-install.exe/download)
7
+- [MSYS](https://osdn.net/projects/mingw/downloads/68260/mingw-get-setup.exe/)
8
+
9
+**WARNING!** _MinGW version from second link is too old, do not install MinGW from mingw-get, install only MSYS_
10
+
11
+# How to compile on linux
12
+
13
+For Debian/Ubuntu: 
14
+
15
+    sudo apt install libpng-dev
16
+
17
+
18
+# Make it!
19
+
20
+Just run `make` command to build

+ 125
- 0
pge/include/olcPGEX_Graphics2D.h View File

@@ -0,0 +1,125 @@
1
+/*
2
+	olcPGEX_Graphics2D.h
3
+
4
+	+-------------------------------------------------------------+
5
+	|         OneLoneCoder Pixel Game Engine Extension            |
6
+	|                Advanced 2D Rendering - v0.4                 |
7
+	+-------------------------------------------------------------+
8
+
9
+	What is this?
10
+	~~~~~~~~~~~~~
11
+	This is an extension to the olcPixelGameEngine, which provides
12
+	advanced olc::Sprite manipulation and drawing routines. To use
13
+	it, simply include this header file.
14
+
15
+	License (OLC-3)
16
+	~~~~~~~~~~~~~~~
17
+
18
+	Copyright 2018 - 2019 OneLoneCoder.com
19
+
20
+	Redistribution and use in source and binary forms, with or without
21
+	modification, are permitted provided that the following conditions
22
+	are met:
23
+
24
+	1. Redistributions or derivations of source code must retain the above
25
+	copyright notice, this list of conditions and the following disclaimer.
26
+
27
+	2. Redistributions or derivative works in binary form must reproduce
28
+	the above copyright notice. This list of conditions and the following
29
+	disclaimer must be reproduced in the documentation and/or other
30
+	materials provided with the distribution.
31
+
32
+	3. Neither the name of the copyright holder nor the names of its
33
+	contributors may be used to endorse or promote products derived
34
+	from this software without specific prior written permission.
35
+
36
+	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37
+	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38
+	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39
+	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40
+	HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41
+	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42
+	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43
+	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44
+	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45
+	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46
+	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
+
48
+	Links
49
+	~~~~~
50
+	YouTube:	https://www.youtube.com/javidx9
51
+	Discord:	https://discord.gg/WhwHUMV
52
+	Twitter:	https://www.twitter.com/javidx9
53
+	Twitch:		https://www.twitch.tv/javidx9
54
+	GitHub:		https://www.github.com/onelonecoder
55
+	Homepage:	https://www.onelonecoder.com
56
+
57
+	Author
58
+	~~~~~~
59
+	David Barr, aka javidx9, �OneLoneCoder 2019
60
+*/
61
+
62
+/*
63
+	Matrices stored as [Column][Row] (i.e. x, y)
64
+
65
+	|C0R0 C1R0 C2R0|   | x |   | x'|
66
+	|C0R1 C1R1 C2R1| * | y | = | y'|
67
+	|C0R2 C1R2 C2R2|   |1.0|   | - |
68
+*/
69
+
70
+#ifndef OLC_PGEX_GFX2D
71
+#define OLC_PGEX_GFX2D
72
+
73
+#include "olcPixelGameEngine.h"
74
+#include <algorithm>
75
+#undef min
76
+#undef max
77
+
78
+namespace olc
79
+{
80
+	// Container class for Advanced 2D Drawing functions
81
+	class GFX2D : public olc::PGEX
82
+	{
83
+		// A representation of an affine transform, used to rotate, scale, offset & shear space
84
+	public:
85
+		class Transform2D
86
+		{
87
+		public:
88
+			Transform2D();
89
+
90
+		public:
91
+			// Set this transformation to unity
92
+			void Reset();
93
+			// Append a rotation of fTheta radians to this transform
94
+			void Rotate(float fTheta);
95
+			// Append a translation (ox, oy) to this transform
96
+			void Translate(float ox, float oy);
97
+			// Append a scaling operation (sx, sy) to this transform
98
+			void Scale(float sx, float sy);
99
+			// Append a shear operation (sx, sy) to this transform
100
+			void Shear(float sx, float sy);
101
+
102
+		    void Perspective(float ox, float oy);
103
+			// Calculate the Forward Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
104
+			void Forward(float in_x, float in_y, float &out_x, float &out_y);
105
+			// Calculate the Inverse Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
106
+			void Backward(float in_x, float in_y, float &out_x, float &out_y);
107
+			// Regenerate the Inverse Transformation
108
+			void Invert();
109
+
110
+		private:
111
+			void Multiply();
112
+			float matrix[4][3][3];
113
+			int nTargetMatrix;
114
+			int nSourceMatrix;
115
+			bool bDirty;
116
+		};
117
+
118
+	public:
119
+		// Draws a sprite with the transform applied
120
+		static void DrawSprite(olc::Sprite *sprite, olc::GFX2D::Transform2D &transform, bool centered = false);
121
+		static void DrawPartialSprite(olc::Sprite *sprite, int32_t ox, int32_t oy, int32_t w, int32_t h, olc::GFX2D::Transform2D &transform, bool centered = false);
122
+	};
123
+}
124
+
125
+#endif

+ 215
- 0
pge/include/olcPGEX_Sound.h View File

@@ -0,0 +1,215 @@
1
+/*
2
+	olcPGEX_Sound.h
3
+
4
+	+-------------------------------------------------------------+
5
+	|         OneLoneCoder Pixel Game Engine Extension            |
6
+	|                       Sound - v0.3                          |
7
+	+-------------------------------------------------------------+
8
+
9
+	What is this?
10
+	~~~~~~~~~~~~~
11
+	This is an extension to the olcPixelGameEngine, which provides
12
+	sound generation and wave playing routines.
13
+
14
+	Special Thanks:
15
+	~~~~~~~~~~~~~~~	
16
+	Slavka - For entire non-windows system back end!
17
+	Gorbit99 - Testing, Bug Fixes
18
+	Cyberdroid - Testing, Bug Fixes
19
+	Dragoneye - Testing
20
+	Puol - Testing
21
+
22
+	License (OLC-3)
23
+	~~~~~~~~~~~~~~~
24
+
25
+	Copyright 2018 - 2019 OneLoneCoder.com
26
+
27
+	Redistribution and use in source and binary forms, with or without
28
+	modification, are permitted provided that the following conditions
29
+	are met:
30
+
31
+	1. Redistributions or derivations of source code must retain the above
32
+	copyright notice, this list of conditions and the following disclaimer.
33
+
34
+	2. Redistributions or derivative works in binary form must reproduce
35
+	the above copyright notice. This list of conditions and the following
36
+	disclaimer must be reproduced in the documentation and/or other
37
+	materials provided with the distribution.
38
+
39
+	3. Neither the name of the copyright holder nor the names of its
40
+	contributors may be used to endorse or promote products derived
41
+	from this software without specific prior written permission.
42
+
43
+	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44
+	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45
+	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46
+	A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47
+	HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48
+	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49
+	LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50
+	DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51
+	THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52
+	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53
+	OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
+
55
+	Links
56
+	~~~~~
57
+	YouTube:	https://www.youtube.com/javidx9
58
+	Discord:	https://discord.gg/WhwHUMV
59
+	Twitter:	https://www.twitter.com/javidx9
60
+	Twitch:		https://www.twitch.tv/javidx9
61
+	GitHub:		https://www.github.com/onelonecoder
62
+	Homepage:	https://www.onelonecoder.com
63
+	Patreon:	https://www.patreon.com/javidx9
64
+
65
+	Author
66
+	~~~~~~
67
+	David Barr, aka javidx9, �OneLoneCoder 2019
68
+*/
69
+
70
+
71
+#ifndef OLC_PGEX_SOUND_H
72
+#define OLC_PGEX_SOUND_H
73
+
74
+#include "olcPixelGameEngine.h"
75
+#include <istream>
76
+#include <cstring>
77
+#include <climits>
78
+
79
+#include <algorithm>
80
+#undef min
81
+#undef max
82
+
83
+// Choose a default sound backend
84
+#if !defined(USE_ALSA) && !defined(USE_OPENAL) && !defined(USE_WINDOWS)
85
+#ifdef __linux__
86
+#define USE_ALSA
87
+#endif
88
+
89
+#ifdef __EMSCRIPTEN__
90
+#define USE_OPENAL
91
+#endif
92
+
93
+#ifdef _WIN32
94
+#define USE_WINDOWS
95
+#endif
96
+
97
+#endif
98
+
99
+#ifdef USE_ALSA
100
+#define ALSA_PCM_NEW_HW_PARAMS_API
101
+#include <alsa/asoundlib.h>
102
+#endif
103
+
104
+#ifdef USE_OPENAL
105
+#include <AL/al.h>
106
+#include <AL/alc.h>
107
+#include <queue>
108
+#endif
109
+
110
+#pragma pack(push, 1)
111
+typedef struct {
112
+	uint16_t wFormatTag;
113
+	uint16_t nChannels;
114
+	uint32_t nSamplesPerSec;
115
+	uint32_t nAvgBytesPerSec;
116
+	uint16_t nBlockAlign;
117
+	uint16_t wBitsPerSample;
118
+	uint16_t cbSize;
119
+} OLC_WAVEFORMATEX;
120
+#pragma pack(pop)
121
+
122
+namespace olc
123
+{
124
+	// Container class for Advanced 2D Drawing functions
125
+	class SOUND : public olc::PGEX
126
+	{
127
+		// A representation of an affine transform, used to rotate, scale, offset & shear space
128
+	public:
129
+		class AudioSample
130
+		{
131
+		public:
132
+			AudioSample();
133
+			AudioSample(std::string sWavFile, olc::ResourcePack *pack = nullptr);
134
+			olc::rcode LoadFromFile(std::string sWavFile, olc::ResourcePack *pack = nullptr);
135
+
136
+		public:
137
+			OLC_WAVEFORMATEX wavHeader;
138
+			float *fSample = nullptr;
139
+			long nSamples = 0;
140
+			int nChannels = 0;
141
+			bool bSampleValid = false;
142
+		};
143
+
144
+		struct sCurrentlyPlayingSample
145
+		{
146
+			int nAudioSampleID = 0;
147
+			long nSamplePosition = 0;
148
+			bool bFinished = false;
149
+			bool bLoop = false;
150
+			bool bFlagForStop = false;
151
+		};
152
+
153
+		static std::list<sCurrentlyPlayingSample> listActiveSamples;
154
+
155
+	public:
156
+		static bool InitialiseAudio(unsigned int nSampleRate = 44100, unsigned int nChannels = 1, unsigned int nBlocks = 8, unsigned int nBlockSamples = 512);
157
+		static bool DestroyAudio();
158
+		static void SetUserSynthFunction(std::function<float(int, float, float)> func);
159
+		static void SetUserFilterFunction(std::function<float(int, float, float)> func);
160
+
161
+	public:
162
+		static int LoadAudioSample(std::string sWavFile, olc::ResourcePack *pack = nullptr);
163
+		static void PlaySample(int id, bool bLoop = false);
164
+		static void StopSample(int id);
165
+		static void StopAll();
166
+		static float GetMixerOutput(int nChannel, float fGlobalTime, float fTimeStep);
167
+
168
+
169
+	private:
170
+#ifdef USE_WINDOWS // Windows specific sound management
171
+		static void CALLBACK waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD dwParam1, DWORD dwParam2);
172
+		static unsigned int m_nSampleRate;
173
+		static unsigned int m_nChannels;
174
+		static unsigned int m_nBlockCount;
175
+		static unsigned int m_nBlockSamples;
176
+		static unsigned int m_nBlockCurrent;
177
+		static short* m_pBlockMemory;
178
+		static WAVEHDR *m_pWaveHeaders;
179
+		static HWAVEOUT m_hwDevice;
180
+		static std::atomic<unsigned int> m_nBlockFree;
181
+		static std::condition_variable m_cvBlockNotZero;
182
+		static std::mutex m_muxBlockNotZero;
183
+#endif
184
+
185
+#ifdef USE_ALSA
186
+		static snd_pcm_t *m_pPCM;
187
+		static unsigned int m_nSampleRate;
188
+		static unsigned int m_nChannels;
189
+		static unsigned int m_nBlockSamples;
190
+		static short* m_pBlockMemory;
191
+#endif
192
+
193
+#ifdef USE_OPENAL
194
+		static std::queue<ALuint> m_qAvailableBuffers;
195
+		static ALuint *m_pBuffers;
196
+		static ALuint m_nSource;
197
+		static ALCdevice *m_pDevice;
198
+		static ALCcontext *m_pContext;
199
+		static unsigned int m_nSampleRate;
200
+		static unsigned int m_nChannels;
201
+		static unsigned int m_nBlockCount;
202
+		static unsigned int m_nBlockSamples;
203
+		static short* m_pBlockMemory;
204
+#endif
205
+
206
+		static void AudioThread();
207
+		static std::thread m_AudioThread;
208
+		static std::atomic<bool> m_bAudioThreadActive;
209
+		static std::atomic<float> m_fGlobalTime;
210
+		static std::function<float(int, float, float)> funcUserSynth;
211
+		static std::function<float(int, float, float)> funcUserFilter;
212
+	};
213
+}
214
+
215
+#endif // OLC_PGEX_SOUND

+ 76
- 0
pge/include/olcPGE_Common.h View File

@@ -0,0 +1,76 @@
1
+#ifndef OLC_PGE_COMMON
2
+#define OLC_PGE_COMMON
3
+
4
+#ifdef _WIN32
5
+	// Link to libraries
6
+#ifndef __MINGW32__
7
+	#pragma comment(lib, "user32.lib")
8
+	#pragma comment(lib, "gdi32.lib")
9
+	#pragma comment(lib, "gdiplus.lib")
10
+#else
11
+	// In Code::Blocks, Select C++14 in your build options, and add the
12
+	// following libs to your linker: user32 gdi32 opengl32 gdiplus
13
+	#if !defined _WIN32_WINNT
14
+        #ifdef HAVE_MSMF
15
+            #define _WIN32_WINNT 0x0600 // Windows Vista
16
+        #else
17
+            #define _WIN32_WINNT 0x0500 // Windows 2000
18
+        #endif
19
+    #endif
20
+#endif
21
+	// Include WinAPI
22
+	#include <windows.h>
23
+    #include <gdiplus.h>
24
+#else
25
+#endif
26
+
27
+#include <string>
28
+#include <cmath>
29
+
30
+namespace olc {
31
+    enum rcode {
32
+        FAIL = 0,
33
+        OK = 1,
34
+        NO_FILE = -1,
35
+    };
36
+    std::wstring ConvertS2W(std::string s);
37
+
38
+    
39
+	template <class T>
40
+	struct v2d_generic
41
+	{
42
+		T x = 0;
43
+		T y = 0;
44
+
45
+		inline v2d_generic() : x(0), y(0)                        {                                                      }
46
+		inline v2d_generic(T _x, T _y) : x(_x), y(_y)            {                                                      }
47
+		inline v2d_generic(const v2d_generic& v) : x(v.x), y(v.y){                                                      }
48
+		inline T mag()                                           { return sqrt(x * x + y * y);                          }
49
+		inline v2d_generic  norm()                               { T r = 1 / mag(); return v2d_generic(x*r, y*r);       }
50
+		inline v2d_generic  perp()                               { return v2d_generic(-y, x);                           }
51
+		inline T dot(const v2d_generic& rhs)                     { return this->x * rhs.x + this->y * rhs.y;            }
52
+		inline T cross(const v2d_generic& rhs)                   { return this->x * rhs.y - this->y * rhs.x;            }
53
+		inline v2d_generic  operator +  (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y);}
54
+		inline v2d_generic  operator -  (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y);}
55
+		inline v2d_generic  operator *  (const T& rhs)           const { return v2d_generic(this->x * rhs, this->y * rhs);    }
56
+		inline v2d_generic  operator /  (const T& rhs)           const { return v2d_generic(this->x / rhs, this->y / rhs);    }
57
+		inline v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this;    }
58
+		inline v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this;    }
59
+		inline v2d_generic& operator *= (const T& rhs)           { this->x *= rhs; this->y *= rhs; return *this;        }
60
+		inline v2d_generic& operator /= (const T& rhs)           { this->x /= rhs; this->y /= rhs; return *this;        }
61
+		inline T& operator [] (std::size_t i)                    { return *((T*)this + i);	   /* <-- D'oh :( */        }
62
+	};
63
+
64
+	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); }
65
+	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); }
66
+	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); }
67
+	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); }
68
+	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); }
69
+	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); }
70
+
71
+	typedef v2d_generic<int> vi2d;
72
+	typedef v2d_generic<float> vf2d;
73
+	typedef v2d_generic<double> vd2d;
74
+}
75
+
76
+#endif

+ 43
- 0
pge/include/olcPGE_Pixel.h View File

@@ -0,0 +1,43 @@
1
+#ifndef OLC_PGE_PIXEL
2
+#define OLC_PGE_PIXEL
3
+
4
+#include <cstdint>
5
+#include "olcPGE_Common.h"
6
+
7
+namespace olc {
8
+    struct Pixel
9
+	{
10
+		union
11
+		{
12
+			uint32_t n = 0xFF000000;
13
+			struct
14
+			{
15
+				uint8_t r;	uint8_t g;	uint8_t b;	uint8_t a;
16
+			};
17
+		};
18
+
19
+		Pixel();
20
+		Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
21
+		Pixel(uint32_t p);
22
+
23
+		static Pixel CreateFromHSV(uint8_t h, uint8_t s, uint8_t v, uint8_t alpha = 255);
24
+		enum Mode { NORMAL, MASK, ALPHA, CUSTOM };
25
+	};
26
+
27
+	// Some constants for symbolic naming of Pixels
28
+	static const Pixel
29
+		WHITE(255, 255, 255),
30
+		GREY(192, 192, 192), DARK_GREY(128, 128, 128), VERY_DARK_GREY(64, 64, 64),
31
+		RED(255, 0, 0), DARK_RED(128, 0, 0), VERY_DARK_RED(64, 0, 0),
32
+		YELLOW(255, 255, 0), DARK_YELLOW(128, 128, 0), VERY_DARK_YELLOW(64, 64, 0),
33
+		GREEN(0, 255, 0), DARK_GREEN(0, 128, 0), VERY_DARK_GREEN(0, 64, 0),
34
+		CYAN(0, 255, 255), DARK_CYAN(0, 128, 128), VERY_DARK_CYAN(0, 64, 64),
35
+		BLUE(0, 0, 255), DARK_BLUE(0, 0, 128), VERY_DARK_BLUE(0, 0, 64),
36
+		MAGENTA(255, 0, 255), DARK_MAGENTA(128, 0, 128), VERY_DARK_MAGENTA(64, 0, 64),
37
+		BLACK(0, 0, 0),
38
+		BLANK(0, 0, 0, 0);
39
+
40
+	//==================================================================================
41
+}
42
+
43
+#endif

+ 40
- 0
pge/include/olcPGE_ResourcePack.h View File

@@ -0,0 +1,40 @@
1
+#ifndef OLC_PGE_RESOURCE_PACK
2
+#define OLC_PGE_RESOURCE_PACK
3
+
4
+#include <cstdint>
5
+#include <string>
6
+#include <map>
7
+#include <vector>
8
+#include <streambuf>
9
+#include <iostream>
10
+#include <fstream>
11
+#include "olcPGE_Common.h"
12
+
13
+namespace olc {
14
+	class ResourcePack
15
+	{
16
+	public:
17
+		ResourcePack();
18
+		~ResourcePack();
19
+		struct sEntry : public std::streambuf {
20
+			uint32_t nID, nFileOffset, nFileSize; uint8_t* data; void _config() { this->setg((char*)data, (char*)data, (char*)(data + nFileSize)); }
21
+		};
22
+
23
+	public:
24
+		olc::rcode AddToPack(std::string sFile);
25
+		olc::rcode RemoveFromPack(std::string sFile);
26
+
27
+	public:
28
+		olc::rcode SavePack(std::string sFile);
29
+		olc::rcode LoadPack(std::string sFile);
30
+		olc::rcode ClearPack();
31
+
32
+	public:
33
+		olc::ResourcePack::sEntry GetStreamBuffer(std::string sFile);
34
+		std::vector<std::string> GetEntries();
35
+
36
+	private:
37
+		std::map<std::string, sEntry> mapFiles;
38
+	};
39
+}
40
+#endif

+ 80
- 0
pge/include/olcPGE_Sprite.h View File

@@ -0,0 +1,80 @@
1
+#ifndef OLC_PGE_SPRITE
2
+#define OLC_PGE_SPRITE
3
+
4
+#ifdef _WIN32
5
+	// Link to libraries
6
+#ifndef __MINGW32__
7
+	#pragma comment(lib, "user32.lib")		// Visual Studio Only
8
+	#pragma comment(lib, "gdi32.lib")		// For other Windows Compilers please add
9
+	#pragma comment(lib, "gdiplus.lib")		// these libs to your linker input
10
+#else
11
+	// In Code::Blocks, Select C++14 in your build options, and add the
12
+	// following libs to your linker: user32 gdi32 opengl32 gdiplus
13
+	#if !defined _WIN32_WINNT
14
+        #ifdef HAVE_MSMF
15
+            #define _WIN32_WINNT 0x0600 // Windows Vista
16
+        #else
17
+            #define _WIN32_WINNT 0x0500 // Windows 2000
18
+        #endif
19
+    #endif
20
+#endif
21
+	// Include WinAPI
22
+	#include <windows.h>
23
+	#include <gdiplus.h>
24
+#else
25
+	#include <png.h>
26
+#endif
27
+
28
+#include <cstdint>
29
+#include <cmath>
30
+#include <string>
31
+#include <map>
32
+#include <vector>
33
+#include <streambuf>
34
+#include <iostream>
35
+#include <fstream>
36
+#include "olcPGE_Common.h"
37
+#include "olcPGE_Pixel.h"
38
+#include "olcPGE_ResourcePack.h"
39
+
40
+namespace olc {
41
+	// A bitmap-like structure that stores a 2D array of Pixels
42
+	class Sprite
43
+	{
44
+	public:
45
+		Sprite();
46
+		Sprite(std::string sImageFile);
47
+		Sprite(std::string sImageFile, olc::ResourcePack *pack);
48
+		Sprite(int32_t w, int32_t h);
49
+		~Sprite();
50
+
51
+	public:
52
+		olc::rcode LoadFromFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
53
+		olc::rcode LoadFromPGESprFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
54
+		olc::rcode SaveToPGESprFile(std::string sImageFile);
55
+
56
+	public:
57
+		int32_t width = 0;
58
+		int32_t height = 0;
59
+		enum Mode { NORMAL, PERIODIC };
60
+
61
+	public:
62
+		void SetSampleMode(olc::Sprite::Mode mode = olc::Sprite::Mode::NORMAL);
63
+		Pixel GetPixel(int32_t x, int32_t y);
64
+		bool  SetPixel(int32_t x, int32_t y, Pixel p);
65
+
66
+		Pixel Sample(float x, float y);
67
+		Pixel SampleBL(float u, float v);
68
+		Pixel* GetData();
69
+
70
+	private:
71
+		Pixel *pColData = nullptr;
72
+		Mode modeSample = Mode::NORMAL;
73
+
74
+#ifdef OLC_DBG_OVERDRAW
75
+	public:
76
+		static int nOverdrawCount;
77
+#endif
78
+	};
79
+}
80
+#endif

+ 452
- 0
pge/include/olcPixelGameEngine.h View File

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

+ 33
- 0
pge/src/olcPGE_Common.cpp View File

@@ -0,0 +1,33 @@
1
+#include "olcPGE_Common.h"
2
+
3
+namespace olc {
4
+	std::wstring ConvertS2W(std::string s)
5
+	{
6
+#ifdef _WIN32
7
+		int count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
8
+		wchar_t* buffer = new wchar_t[count];
9
+		MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, buffer, count);
10
+		std::wstring w(buffer);
11
+		delete[] buffer;
12
+		return w;
13
+#else
14
+		return L"SVN FTW!";
15
+#endif
16
+	}
17
+	
18
+#ifdef _WIN32
19
+	// Thanks @MaGetzUb for this, which allows sprites to be defined
20
+	// at construction, by initialising the GDI subsystem
21
+	static class GDIPlusStartup
22
+	{
23
+	public:
24
+		GDIPlusStartup()
25
+		{
26
+			Gdiplus::GdiplusStartupInput startupInput;
27
+			ULONG_PTR	token;
28
+			Gdiplus::GdiplusStartup(&token, &startupInput, NULL);
29
+		};
30
+	} gdistartup;
31
+#endif
32
+
33
+}

+ 63
- 0
pge/src/olcPGE_Pixel.cpp View File

@@ -0,0 +1,63 @@
1
+#include "olcPGE_Pixel.h"
2
+
3
+namespace olc {
4
+    Pixel::Pixel()
5
+	{
6
+		r = 0; g = 0; b = 0; a = 255;
7
+	}
8
+
9
+	Pixel::Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
10
+	{
11
+		r = red; g = green; b = blue; a = alpha;
12
+	}
13
+
14
+	Pixel::Pixel(uint32_t p)
15
+	{
16
+		n = p;
17
+	}
18
+
19
+	Pixel Pixel::CreateFromHSV(uint8_t h, uint8_t s, uint8_t v, uint8_t alpha) {
20
+		Pixel rgb;
21
+		rgb.a = alpha;
22
+		uint8_t region, remainder, p, q, t;
23
+
24
+		if (s == 0)
25
+		{
26
+			rgb.r = v;
27
+			rgb.g = v;
28
+			rgb.b = v;
29
+			return rgb;
30
+		}
31
+
32
+		region = h / 43;
33
+		remainder = (h - (region * 43)) * 6; 
34
+
35
+		p = (v * (255 - s)) >> 8;
36
+		q = (v * (255 - ((s * remainder) >> 8))) >> 8;
37
+		t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
38
+
39
+		switch (region)
40
+		{
41
+			case 0:
42
+				rgb.r = v; rgb.g = t; rgb.b = p;
43
+				break;
44
+			case 1:
45
+				rgb.r = q; rgb.g = v; rgb.b = p;
46
+				break;
47
+			case 2:
48
+				rgb.r = p; rgb.g = v; rgb.b = t;
49
+				break;
50
+			case 3:
51
+				rgb.r = p; rgb.g = q; rgb.b = v;
52
+				break;
53
+			case 4:
54
+				rgb.r = t; rgb.g = p; rgb.b = v;
55
+				break;
56
+			default:
57
+				rgb.r = v; rgb.g = p; rgb.b = q;
58
+				break;
59
+		}
60
+
61
+		return rgb;
62
+	}
63
+}

+ 163
- 0
pge/src/olcPGE_ResourcePack.cpp View File

@@ -0,0 +1,163 @@
1
+#include "olcPGE_ResourcePack.h"
2
+
3
+namespace olc {
4
+	ResourcePack::ResourcePack()
5
+	{
6
+
7
+	}
8
+
9
+	ResourcePack::~ResourcePack()
10
+	{
11
+		ClearPack();
12
+	}
13
+
14
+	olc::rcode ResourcePack::AddToPack(std::string sFile)
15
+	{
16
+		std::ifstream ifs(sFile, std::ifstream::binary);
17
+		if (!ifs.is_open()) return olc::FAIL;
18
+
19
+		// Get File Size
20
+		std::streampos p = 0;
21
+		p = ifs.tellg();
22
+		ifs.seekg(0, std::ios::end);
23
+		p = ifs.tellg() - p;
24
+		ifs.seekg(0, std::ios::beg);
25
+
26
+		// Create entry
27
+		sEntry e;
28
+		e.data = nullptr;
29
+		e.nFileSize = (uint32_t)p;
30
+
31
+		// Read file into memory
32
+		e.data = new uint8_t[(uint32_t)e.nFileSize];
33
+		ifs.read((char*)e.data, e.nFileSize);
34
+		ifs.close();
35
+
36
+		// Add To Map
37
+		mapFiles[sFile] = e;
38
+		return olc::OK;
39
+	}
40
+
41
+	olc::rcode ResourcePack::RemoveFromPack(std::string sFile)
42
+	{	
43
+		if (mapFiles.count(sFile) > 0) {	
44
+			sEntry e = mapFiles.at(sFile);
45
+			if (e.data != nullptr) {
46
+				delete[] e.data;
47
+			}
48
+			mapFiles.erase(sFile);
49
+			return olc::OK;
50
+		} else {
51
+			return olc::FAIL;
52
+		}
53
+	}
54
+
55
+	olc::rcode ResourcePack::SavePack(std::string sFile)
56
+	{
57
+		std::ofstream ofs(sFile, std::ofstream::binary);
58
+		if (!ofs.is_open()) return olc::FAIL;
59
+
60
+		// 1) Write Map
61
+		size_t nMapSize = mapFiles.size();
62
+		ofs.write((char*)&nMapSize, sizeof(size_t));
63
+		for (auto &e : mapFiles)
64
+		{
65
+			size_t nPathSize = e.first.size();
66
+			ofs.write((char*)&nPathSize, sizeof(size_t));
67
+			ofs.write(e.first.c_str(), nPathSize);
68
+			ofs.write((char*)&e.second.nID, sizeof(uint32_t));
69
+			ofs.write((char*)&e.second.nFileSize, sizeof(uint32_t));
70
+			ofs.write((char*)&e.second.nFileOffset, sizeof(uint32_t));
71
+		}
72
+
73
+		// 2) Write Data
74
+		std::streampos offset = ofs.tellp();
75
+		for (auto &e : mapFiles)
76
+		{
77
+			e.second.nFileOffset = (uint32_t)offset;
78
+			ofs.write((char*)e.second.data, e.second.nFileSize);
79
+			offset += e.second.nFileSize;
80
+		}
81
+
82
+		// 3) Rewrite Map (it has been updated with offsets now)
83
+		ofs.seekp(std::ios::beg);
84
+		ofs.write((char*)&nMapSize, sizeof(size_t));
85
+		for (auto &e : mapFiles)
86
+		{
87
+			size_t nPathSize = e.first.size();
88
+			ofs.write((char*)&nPathSize, sizeof(size_t));
89
+			ofs.write(e.first.c_str(), nPathSize);
90
+			ofs.write((char*)&e.second.nID, sizeof(uint32_t));
91
+			ofs.write((char*)&e.second.nFileSize, sizeof(uint32_t));
92
+			ofs.write((char*)&e.second.nFileOffset, sizeof(uint32_t));
93
+		}
94
+		ofs.close();
95
+
96
+		return olc::OK;
97
+	}
98
+
99
+	olc::rcode ResourcePack::LoadPack(std::string sFile)
100
+	{
101
+		std::ifstream ifs(sFile, std::ifstream::binary);
102
+		if (!ifs.is_open()) return olc::FAIL;
103
+
104
+		// 1) Read Map
105
+		size_t nMapEntries;
106
+		ifs.read((char*)&nMapEntries, sizeof(size_t));
107
+		for (size_t i = 0; i < nMapEntries; i++)
108
+		{
109
+			size_t nFilePathSize = 0;
110
+			ifs.read((char*)&nFilePathSize, sizeof(size_t));
111
+
112
+			std::string sFileName(nFilePathSize, ' ');
113
+			for (size_t j = 0; j < nFilePathSize; j++)
114
+				sFileName[j] = ifs.get();
115
+
116
+			sEntry e;
117
+			e.data = nullptr;
118
+			ifs.read((char*)&e.nID, sizeof(uint32_t));
119
+			ifs.read((char*)&e.nFileSize, sizeof(uint32_t));
120
+			ifs.read((char*)&e.nFileOffset, sizeof(uint32_t));
121
+			mapFiles[sFileName] = e;
122
+		}
123
+
124
+		// 2) Read Data
125
+		for (auto &e : mapFiles)
126
+		{
127
+			e.second.data = new uint8_t[(uint32_t)e.second.nFileSize];
128
+			ifs.seekg(e.second.nFileOffset);
129
+			ifs.read((char*)e.second.data, e.second.nFileSize);
130
+			e.second._config();
131
+		}
132
+
133
+		ifs.close();
134
+		return olc::OK;
135
+	}
136
+
137
+	olc::ResourcePack::sEntry ResourcePack::GetStreamBuffer(std::string sFile)
138
+	{
139
+		return mapFiles[sFile];
140
+	}
141
+
142
+	std::vector<std::string> ResourcePack::GetEntries() 
143
+	{
144
+		std::vector<std::string> result;
145
+		result.reserve(mapFiles.size());
146
+		for (auto& e: mapFiles) {
147
+			result.push_back(e.first);
148
+		}
149
+		return result;
150
+	}
151
+
152
+	olc::rcode ResourcePack::ClearPack()
153
+	{
154
+		for (auto &e : mapFiles)
155
+		{
156
+			if (e.second.data != nullptr)
157
+				delete[] e.second.data;
158
+		}
159
+
160
+		mapFiles.clear();
161
+		return olc::OK;
162
+	}
163
+}

+ 271
- 0
pge/src/olcPGE_Sprite.cpp View File

@@ -0,0 +1,271 @@
1
+#include "olcPGE_Sprite.h"
2
+
3
+namespace olc {
4
+	Sprite::Sprite()
5
+	{
6
+		pColData = nullptr;
7
+		width = 0;
8
+		height = 0;
9
+	}
10
+
11
+	Sprite::Sprite(std::string sImageFile)
12
+	{
13
+		LoadFromFile(sImageFile);
14
+	}
15
+
16
+	Sprite::Sprite(std::string sImageFile, olc::ResourcePack *pack)
17
+	{
18
+		LoadFromPGESprFile(sImageFile, pack);
19
+	}
20
+
21
+	Sprite::Sprite(int32_t w, int32_t h)
22
+	{
23
+		if(pColData) delete[] pColData;
24
+		width = w;		height = h;
25
+		pColData = new Pixel[width * height];
26
+		for (int32_t i = 0; i < width*height; i++)
27
+			pColData[i] = Pixel();
28
+	}
29
+
30
+	Sprite::~Sprite()
31
+	{
32
+		if (pColData) delete pColData;
33
+	}
34
+
35
+	olc::rcode Sprite::LoadFromPGESprFile(std::string sImageFile, olc::ResourcePack *pack)
36
+	{
37
+		if (pColData) delete[] pColData;
38
+
39
+		auto ReadData = [&](std::istream &is)
40
+		{
41
+			is.read((char*)&width, sizeof(int32_t));
42
+			is.read((char*)&height, sizeof(int32_t));
43
+			pColData = new Pixel[width * height];
44
+			is.read((char*)pColData, width * height * sizeof(uint32_t));
45
+		};
46
+
47
+		// These are essentially Memory Surfaces represented by olc::Sprite
48
+		// which load very fast, but are completely uncompressed
49
+		if (pack == nullptr)
50
+		{
51
+			std::ifstream ifs;
52
+			ifs.open(sImageFile, std::ifstream::binary);
53
+			if (ifs.is_open())
54
+			{
55
+				ReadData(ifs);
56
+				return olc::OK;
57
+			}
58
+			else
59
+				return olc::FAIL;
60
+		}
61
+		else
62
+		{
63
+			auto streamBuffer = pack->GetStreamBuffer(sImageFile);
64
+			if (streamBuffer.data == nullptr) 
65
+				return olc::FAIL;
66
+				
67
+			std::istream is(&streamBuffer);
68
+			ReadData(is);
69
+			return olc::OK;
70
+		}
71
+
72
+		return olc::FAIL;
73
+	}
74
+
75
+	olc::rcode Sprite::SaveToPGESprFile(std::string sImageFile)
76
+	{
77
+		if (pColData == nullptr) return olc::FAIL;
78
+
79
+		std::ofstream ofs;
80
+		ofs.open(sImageFile, std::ifstream::binary);
81
+		if (ofs.is_open())
82
+		{
83
+			ofs.write((char*)&width, sizeof(int32_t));
84
+			ofs.write((char*)&height, sizeof(int32_t));
85
+			ofs.write((char*)pColData, width*height*sizeof(uint32_t));
86
+			ofs.close();
87
+			return olc::OK;
88
+		}
89
+
90
+		return olc::FAIL;
91
+	}
92
+
93
+	olc::rcode Sprite::LoadFromFile(std::string sImageFile, olc::ResourcePack *pack)
94
+	{
95
+#ifdef _WIN32
96
+		// Use GDI+
97
+		std::wstring wsImageFile;
98
+#ifdef __MINGW32__
99
+        wchar_t *buffer = new wchar_t[sImageFile.length() + 1];
100
+        mbstowcs(buffer, sImageFile.c_str(), sImageFile.length());
101
+        buffer[sImageFile.length()] = L'\0';
102
+        wsImageFile = buffer;
103
+        delete [] buffer;
104
+#else
105
+		wsImageFile = ConvertS2W(sImageFile);
106
+#endif
107
+        Gdiplus::Bitmap *bmp = Gdiplus::Bitmap::FromFile(wsImageFile.c_str());
108
+		if (bmp == nullptr)
109
+			return olc::NO_FILE;
110
+
111
+		width = bmp->GetWidth();
112
+		height = bmp->GetHeight();
113
+		pColData = new Pixel[width * height];
114
+
115
+		for(int x=0; x<width; x++)
116
+			for (int y = 0; y < height; y++)
117
+			{
118
+				Gdiplus::Color c;
119
+				bmp->GetPixel(x, y, &c);
120
+				SetPixel(x, y, Pixel(c.GetRed(), c.GetGreen(), c.GetBlue(), c.GetAlpha()));
121
+			}
122
+		delete bmp;
123
+		return olc::OK;
124
+#else
125
+		////////////////////////////////////////////////////////////////////////////
126
+		// Use libpng, Thanks to Guillaume Cottenceau
127
+		// https://gist.github.com/niw/5963798
128
+		png_structp png;
129
+		png_infop info;
130
+
131
+		FILE *f = fopen(sImageFile.c_str(), "rb");
132
+		if (!f) return olc::NO_FILE;
133
+
134
+		png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
135
+		if (!png) goto fail_load;
136
+
137
+		info = png_create_info_struct(png);
138
+		if (!info) goto fail_load;
139
+
140
+		if (setjmp(png_jmpbuf(png))) goto fail_load;
141
+
142
+		png_init_io(png, f);
143
+		png_read_info(png, info);
144
+
145
+		png_byte color_type;
146
+		png_byte bit_depth;
147
+		png_bytep *row_pointers;
148
+		width = png_get_image_width(png, info);
149
+		height = png_get_image_height(png, info);
150
+		color_type = png_get_color_type(png, info);
151
+		bit_depth = png_get_bit_depth(png, info);
152
+
153
+#ifdef _DEBUG
154
+		std::cout << "Loading PNG: " << sImageFile << "\n";
155
+		std::cout << "W:" << width << " H:" << height << " D:" << (int)bit_depth << "\n";
156
+#endif
157
+
158
+		if (bit_depth == 16) png_set_strip_16(png);
159
+		if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_palette_to_rgb(png);
160
+		if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)	png_set_expand_gray_1_2_4_to_8(png);
161
+		if (png_get_valid(png, info, PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png);
162
+		if (color_type == PNG_COLOR_TYPE_RGB ||
163
+			color_type == PNG_COLOR_TYPE_GRAY ||
164
+			color_type == PNG_COLOR_TYPE_PALETTE)
165
+			png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
166
+		if (color_type == PNG_COLOR_TYPE_GRAY ||
167
+			color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
168
+			png_set_gray_to_rgb(png);
169
+
170
+		png_read_update_info(png, info);
171
+		row_pointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
172
+		for (int y = 0; y < height; y++) {
173
+			row_pointers[y] = (png_byte*)malloc(png_get_rowbytes(png, info));
174
+		}
175
+		png_read_image(png, row_pointers);
176
+		////////////////////////////////////////////////////////////////////////////
177
+
178
+		// Create sprite array
179
+		pColData = new Pixel[width * height];
180
+
181
+		// Iterate through image rows, converting into sprite format
182
+		for (int y = 0; y < height; y++)
183
+		{
184
+			png_bytep row = row_pointers[y];
185
+			for (int x = 0; x < width; x++)
186
+			{
187
+				png_bytep px = &(row[x * 4]);
188
+				SetPixel(x, y, Pixel(px[0], px[1], px[2], px[3]));
189
+			}
190
+		}
191
+
192
+		fclose(f);
193
+		return olc::OK;
194
+
195
+	fail_load:
196
+		width = 0;
197
+		height = 0;
198
+		fclose(f);
199
+		pColData = nullptr;
200
+		return olc::FAIL;
201
+#endif
202
+	}
203
+
204
+	void Sprite::SetSampleMode(olc::Sprite::Mode mode)
205
+	{
206
+		modeSample = mode;
207
+	}
208
+
209
+
210
+	Pixel Sprite::GetPixel(int32_t x, int32_t y)
211
+	{
212
+		if (modeSample == olc::Sprite::Mode::NORMAL)
213
+		{
214
+			if (x >= 0 && x < width && y >= 0 && y < height)
215
+				return pColData[y*width + x];
216
+			else
217
+				return Pixel(0, 0, 0, 0);
218
+		}
219
+		else
220
+		{
221
+			return pColData[abs(y%height)*width + abs(x%width)];
222
+		}
223
+	}
224
+
225
+	bool Sprite::SetPixel(int32_t x, int32_t y, Pixel p)
226
+	{
227
+
228
+#ifdef OLC_DBG_OVERDRAW
229
+		nOverdrawCount++;
230
+#endif
231
+
232
+		if (x >= 0 && x < width && y >= 0 && y < height)
233
+		{
234
+			pColData[y*width + x] = p;
235
+			return true;
236
+		}
237
+		else
238
+			return false;
239
+	}
240
+
241
+	Pixel Sprite::Sample(float x, float y)
242
+	{
243
+		int32_t sx = std::min((int32_t)((x * (float)width)), width - 1);
244
+		int32_t sy = std::min((int32_t)((y * (float)height)), height - 1);
245
+		return GetPixel(sx, sy);
246
+	}
247
+
248
+	Pixel Sprite::SampleBL(float u, float v)
249
+	{
250
+		u = u * width - 0.5f;
251
+		v = v * height - 0.5f;
252
+		int x = (int)floor(u); // cast to int rounds toward zero, not downward
253
+		int y = (int)floor(v); // Thanks @joshinils
254
+		float u_ratio = u - x;
255
+		float v_ratio = v - y;
256
+		float u_opposite = 1 - u_ratio;
257
+		float v_opposite = 1 - v_ratio;
258
+
259
+		olc::Pixel p1 = GetPixel(std::max(x, 0), std::max(y, 0));
260
+		olc::Pixel p2 = GetPixel(std::min(x + 1, (int)width - 1), std::max(y, 0));
261
+		olc::Pixel p3 = GetPixel(std::max(x, 0), std::min(y + 1, (int)height - 1));
262
+		olc::Pixel p4 = GetPixel(std::min(x + 1, (int)width - 1), std::min(y + 1, (int)height - 1));
263
+
264
+		return olc::Pixel(
265
+			(uint8_t)((p1.r * u_opposite + p2.r * u_ratio) * v_opposite + (p3.r * u_opposite + p4.r * u_ratio) * v_ratio),
266
+			(uint8_t)((p1.g * u_opposite + p2.g * u_ratio) * v_opposite + (p3.g * u_opposite + p4.g * u_ratio) * v_ratio),
267
+			(uint8_t)((p1.b * u_opposite + p2.b * u_ratio) * v_opposite + (p3.b * u_opposite + p4.b * u_ratio) * v_ratio));
268
+	}
269
+
270
+	Pixel* Sprite::GetData() { return pColData; }
271
+}

+ 1327
- 0
pge/src/olcPixelGameEngine.cpp
File diff suppressed because it is too large
View File


+ 91
- 0
src/packer.cpp View File

@@ -0,0 +1,91 @@
1
+#include "olcPGE_Sprite.h"
2
+#include "olcPGE_ResourcePack.h"
3
+#include <iostream>
4
+#include <string>
5
+
6
+void PrintUsage() {
7
+    std::cout << "Usage: packer --list PACK_FILE"            << std::endl;
8
+    std::cout << "       packer --add PACK_FILE FILE"        << std::endl;
9
+    std::cout << "       packer --remove PACK_FILE FILE"     << std::endl;
10
+    std::cout << "       packer --convert PNG_FILE SPR_FILE" << std::endl;
11
+}
12
+
13
+void ListEntries(olc::ResourcePack& pack) {
14
+    auto entries = pack.GetEntries();
15
+    std::cout << "Total entries: " << entries.size() << std::endl;
16
+    for (auto entry: entries) {
17
+        std::cout << entry << std::endl;
18
+    }
19
+}
20
+
21
+int main(int argc, char *argv[])
22
+{
23
+    std::cout << "ResourcePack tool for PixelGameEngine (onelonecoder.com)" << std::endl;
24
+    std::string packFile;
25
+    std::string action;
26
+
27
+    if (argc < 3) {
28
+        PrintUsage();
29
+        return -1;
30
+    }
31
+
32
+    action = argv[1];
33
+    packFile = argv[2];
34
+
35
+    olc::ResourcePack pack;
36
+    if (action != "--convert") {
37
+        pack.LoadPack(packFile);
38
+    }
39
+
40
+    if (action == "--list") {
41
+        ListEntries(pack);
42
+    } else if (action == "--add" || action == "--remove") {
43
+        if (argc < 4) {
44
+            PrintUsage();
45
+            return -1;
46
+        }
47
+        for (int i = 3; i < argc; i++) {
48
+            std::string fileName = argv[i];
49
+            if (action == "--add") {
50
+                if (!pack.AddToPack(fileName)) {
51
+                    std::cerr << "Could not open file " << fileName << std::endl;
52
+                    return -3;
53
+                }
54
+                std::cout << "+ " << fileName << std::endl;
55
+            } else if (action == "--remove") {
56
+                if (!pack.RemoveFromPack(fileName)) {
57
+                    std::cerr << "Could not open file " << fileName << std::endl;
58
+                    return -3;
59
+                }
60
+                std::cout << "- " << fileName << std::endl;
61
+            }
62
+        }
63
+        std::cout << std::endl;
64
+        ListEntries(pack);
65
+    } else if (action == "--convert") {
66
+        if (argc < 4) {
67
+            PrintUsage();
68
+            return -1;
69
+        }
70
+        olc::Sprite sprite;
71
+        if (sprite.LoadFromFile(packFile) != olc::OK) {
72
+            std::cerr << "Not valid PNG file " << packFile << std::endl;
73
+            return -4;
74
+        }
75
+        std::string sprFile = argv[3];
76
+        if (!sprite.SaveToPGESprFile(sprFile)) {
77
+            std::cerr << "Could not save file " << sprFile << std::endl;
78
+            return -3;
79
+        }
80
+        return 0;
81
+    } else {
82
+        PrintUsage();
83
+        return -1;
84
+    }
85
+
86
+    if (!pack.SavePack(packFile)) {
87
+        std::cerr << "Could not save file " << packFile << std::endl;
88
+    }
89
+
90
+    return 0;
91
+}

Loading…
Cancel
Save