Переглянути джерело

Cross-platform project installed

Pabloader 4 роки тому
джерело
коміт
a83a7434cf

+ 4
- 0
.gitignore Переглянути файл

@@ -0,0 +1,4 @@
1
+awoorwa
2
+awoorwae
3
+*.exe
4
+*.o

+ 20
- 0
.vscode/c_cpp_properties.json Переглянути файл

@@ -0,0 +1,20 @@
1
+{
2
+    "configurations": [
3
+        {
4
+            "name": "Win32",
5
+            "includePath": [
6
+                "${workspaceFolder}/**"
7
+            ],
8
+            "defines": [
9
+                "_DEBUG",
10
+                "UNICODE",
11
+                "_UNICODE"
12
+            ],
13
+            "compilerPath": "C:/MinGW/bin/gcc.exe",
14
+            "cStandard": "c11",
15
+            "cppStandard": "c++17",
16
+            "intelliSenseMode": "gcc-x64"
17
+        }
18
+    ],
19
+    "version": 4
20
+}

+ 15
- 7
Makefile Переглянути файл

@@ -1,19 +1,27 @@
1
+# To compile this you will need MinGW64 with MSYS installed in PATH
2
+
1 3
 CC=gcc
2 4
 CPP=g++
3
-CFLAGS=-Wall -Wno-misleading-indentation -Werror -g
4
-CPPFLAGS=-Wall -Wno-misleading-indentation -Werror --std=c++1z -g 
5
-LDFLAGS=
6
-INCLUDES=
7
-LIBRARIES=-lX11 -lGL -lpthread -lpng
5
+CFLAGS=-m64 -Wall -Wno-misleading-indentation -Werror -g
6
+CPPFLAGS=-m64 -Wall -Wno-misleading-indentation -Werror --std=c++1z -g
7
+LDFLAGS=-m64 
8
+INCLUDES=-I./include
9
+ifeq ($(OS),Windows_NT)     # is Windows_NT on XP, 2000, 7, Vista, 10...
10
+	LIBRARIES=-luser32 -lgdi32 -lopengl32 -lgdiplus -lwinmm
11
+	EXECUTABLE=awoorwae.exe
12
+	LDFLAGS+=--machine=windows
13
+else
14
+	LIBRARIES=-lX11 -lGL -lpthread -lpng -lasound
15
+	EXECUTABLE=awoorwae
16
+endif
8 17
 SOURCES=$(wildcard src/*.c)
9 18
 SOURCES_CPP=$(wildcard src/*.cpp)
10 19
 OBJECTS=$(SOURCES:.c=.o) $(SOURCES_CPP:.cpp=.o)
11
-EXECUTABLE=awoorwa
12 20
 
13 21
 all: $(SOURCES) $(SOURCES_CPP) $(EXECUTABLE)
14 22
 
15 23
 clean: 
16
-	rm -rf $(OBJECTS) $(EXECUTABLE)
24
+	$(RM) $(OBJECTS) $(EXECUTABLE)
17 25
 	
18 26
 $(EXECUTABLE): $(OBJECTS) 
19 27
 	$(CPP) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)

+ 124
- 0
include/olcPGEX_Graphics2D.h Переглянути файл

@@ -0,0 +1,124 @@
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
+			inline Transform2D();
89
+
90
+		public:
91
+			// Set this transformation to unity
92
+			inline void Reset();
93
+			// Append a rotation of fTheta radians to this transform
94
+			inline void Rotate(float fTheta);
95
+			// Append a translation (ox, oy) to this transform
96
+			inline void Translate(float ox, float oy);
97
+			// Append a scaling operation (sx, sy) to this transform
98
+			inline void Scale(float sx, float sy);
99
+			// Append a shear operation (sx, sy) to this transform
100
+			inline void Shear(float sx, float sy);
101
+
102
+			inline void Perspective(float ox, float oy);
103
+			// Calculate the Forward Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
104
+			inline 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
+			inline void Backward(float in_x, float in_y, float &out_x, float &out_y);
107
+			// Regenerate the Inverse Transformation
108
+			inline void Invert();
109
+
110
+		private:
111
+			inline 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
+		inline static void DrawSprite(olc::Sprite *sprite, olc::GFX2D::Transform2D &transform);
121
+	};
122
+}
123
+
124
+#endif

+ 215
- 0
include/olcPGEX_Sound.h Переглянути файл

@@ -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 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

+ 593
- 0
include/olcPixelGameEngine.h Переглянути файл

@@ -0,0 +1,593 @@
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
+	#include <png.h>
210
+	typedef int(glSwapInterval_t) (Display *dpy, GLXDrawable drawable, int interval);
211
+#endif
212
+
213
+
214
+// Standard includes
215
+#include <cmath>
216
+#include <cstdint>
217
+#include <string>
218
+#include <iostream>
219
+#include <streambuf>
220
+#include <chrono>
221
+#include <vector>
222
+#include <list>
223
+#include <thread>
224
+#include <atomic>
225
+#include <condition_variable>
226
+#include <fstream>
227
+#include <map>
228
+#include <functional>
229
+#include <algorithm>
230
+
231
+#undef min
232
+#undef max
233
+
234
+namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
235
+{
236
+	struct Pixel
237
+	{
238
+		union
239
+		{
240
+			uint32_t n = 0xFF000000;
241
+			struct
242
+			{
243
+				uint8_t r;	uint8_t g;	uint8_t b;	uint8_t a;
244
+			};
245
+		};
246
+
247
+		Pixel();
248
+		Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
249
+		Pixel(uint32_t p);
250
+		enum Mode { NORMAL, MASK, ALPHA, CUSTOM };
251
+	};
252
+
253
+	// Some constants for symbolic naming of Pixels
254
+	static const Pixel
255
+		WHITE(255, 255, 255),
256
+		GREY(192, 192, 192), DARK_GREY(128, 128, 128), VERY_DARK_GREY(64, 64, 64),
257
+		RED(255, 0, 0), DARK_RED(128, 0, 0), VERY_DARK_RED(64, 0, 0),
258
+		YELLOW(255, 255, 0), DARK_YELLOW(128, 128, 0), VERY_DARK_YELLOW(64, 64, 0),
259
+		GREEN(0, 255, 0), DARK_GREEN(0, 128, 0), VERY_DARK_GREEN(0, 64, 0),
260
+		CYAN(0, 255, 255), DARK_CYAN(0, 128, 128), VERY_DARK_CYAN(0, 64, 64),
261
+		BLUE(0, 0, 255), DARK_BLUE(0, 0, 128), VERY_DARK_BLUE(0, 0, 64),
262
+		MAGENTA(255, 0, 255), DARK_MAGENTA(128, 0, 128), VERY_DARK_MAGENTA(64, 0, 64),
263
+		BLACK(0, 0, 0),
264
+		BLANK(0, 0, 0, 0);
265
+
266
+	enum rcode
267
+	{
268
+		FAIL = 0,
269
+		OK = 1,
270
+		NO_FILE = -1,
271
+	};
272
+
273
+	//==================================================================================
274
+
275
+	template <class T>
276
+	struct v2d_generic
277
+	{
278
+		T x = 0;
279
+		T y = 0;
280
+
281
+		inline v2d_generic() : x(0), y(0)                        {                                                      }
282
+		inline v2d_generic(T _x, T _y) : x(_x), y(_y)            {                                                      }
283
+		inline v2d_generic(const v2d_generic& v) : x(v.x), y(v.y){                                                      }
284
+		inline T mag()                                           { return sqrt(x * x + y * y);                          }
285
+		inline v2d_generic  norm()                               { T r = 1 / mag(); return v2d_generic(x*r, y*r);       }
286
+		inline v2d_generic  perp()                               { return v2d_generic(-y, x);                           }
287
+		inline T dot(const v2d_generic& rhs)                     { return this->x * rhs.x + this->y * rhs.y;            }
288
+		inline T cross(const v2d_generic& rhs)                   { return this->x * rhs.y - this->y * rhs.x;            }
289
+		inline v2d_generic  operator +  (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y);}
290
+		inline v2d_generic  operator -  (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y);}
291
+		inline v2d_generic  operator *  (const T& rhs)           const { return v2d_generic(this->x * rhs, this->y * rhs);    }
292
+		inline v2d_generic  operator /  (const T& rhs)           const { return v2d_generic(this->x / rhs, this->y / rhs);    }
293
+		inline v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this;    }
294
+		inline v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this;    }
295
+		inline v2d_generic& operator *= (const T& rhs)           { this->x *= rhs; this->y *= rhs; return *this;        }
296
+		inline v2d_generic& operator /= (const T& rhs)           { this->x /= rhs; this->y /= rhs; return *this;        }
297
+		inline T& operator [] (std::size_t i)                    { return *((T*)this + i);	   /* <-- D'oh :( */        }
298
+	};
299
+
300
+	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); }
301
+	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); }
302
+	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); }
303
+	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); }
304
+	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); }
305
+	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); }
306
+
307
+	typedef v2d_generic<int> vi2d;
308
+	typedef v2d_generic<float> vf2d;
309
+	typedef v2d_generic<double> vd2d;
310
+
311
+	//=============================================================
312
+
313
+	struct HWButton
314
+	{
315
+		bool bPressed = false;	// Set once during the frame the event occurs
316
+		bool bReleased = false;	// Set once during the frame the event occurs
317
+		bool bHeld = false;		// Set true for all frames between pressed and released events
318
+	};
319
+
320
+	//=============================================================
321
+
322
+
323
+	class ResourcePack
324
+	{
325
+	public:
326
+		ResourcePack();
327
+		~ResourcePack();
328
+		struct sEntry : public std::streambuf {
329
+			uint32_t nID, nFileOffset, nFileSize; uint8_t* data; void _config() { this->setg((char*)data, (char*)data, (char*)(data + nFileSize)); }
330
+		};
331
+
332
+	public:
333
+		olc::rcode AddToPack(std::string sFile);
334
+
335
+	public:
336
+		olc::rcode SavePack(std::string sFile);
337
+		olc::rcode LoadPack(std::string sFile);
338
+		olc::rcode ClearPack();
339
+
340
+	public:
341
+		olc::ResourcePack::sEntry GetStreamBuffer(std::string sFile);
342
+
343
+	private:
344
+
345
+		std::map<std::string, sEntry> mapFiles;
346
+	};
347
+
348
+	//=============================================================
349
+
350
+	// A bitmap-like structure that stores a 2D array of Pixels
351
+	class Sprite
352
+	{
353
+	public:
354
+		Sprite();
355
+		Sprite(std::string sImageFile);
356
+		Sprite(std::string sImageFile, olc::ResourcePack *pack);
357
+		Sprite(int32_t w, int32_t h);
358
+		~Sprite();
359
+
360
+	public:
361
+		olc::rcode LoadFromFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
362
+		olc::rcode LoadFromPGESprFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
363
+		olc::rcode SaveToPGESprFile(std::string sImageFile);
364
+
365
+	public:
366
+		int32_t width = 0;
367
+		int32_t height = 0;
368
+		enum Mode { NORMAL, PERIODIC };
369
+
370
+	public:
371
+		void SetSampleMode(olc::Sprite::Mode mode = olc::Sprite::Mode::NORMAL);
372
+		Pixel GetPixel(int32_t x, int32_t y);
373
+		bool  SetPixel(int32_t x, int32_t y, Pixel p);
374
+
375
+		Pixel Sample(float x, float y);
376
+		Pixel SampleBL(float u, float v);
377
+		Pixel* GetData();
378
+
379
+	private:
380
+		Pixel *pColData = nullptr;
381
+		Mode modeSample = Mode::NORMAL;
382
+
383
+#ifdef OLC_DBG_OVERDRAW
384
+	public:
385
+		static int nOverdrawCount;
386
+#endif
387
+
388
+	};
389
+
390
+	//=============================================================
391
+
392
+	enum Key
393
+	{
394
+		NONE,
395
+		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,
396
+		K0, K1, K2, K3, K4, K5, K6, K7, K8, K9,
397
+		F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
398
+		UP, DOWN, LEFT, RIGHT,
399
+		SPACE, TAB, SHIFT, CTRL, INS, DEL, HOME, END, PGUP, PGDN,
400
+		BACK, ESCAPE, RETURN, ENTER, PAUSE, SCROLL,
401
+		NP0, NP1, NP2, NP3, NP4, NP5, NP6, NP7, NP8, NP9,
402
+		NP_MUL, NP_DIV, NP_ADD, NP_SUB, NP_DECIMAL,
403
+	};
404
+
405
+
406
+	//=============================================================
407
+
408
+	class PixelGameEngine
409
+	{
410
+	public:
411
+		PixelGameEngine();
412
+
413
+	public:
414
+		olc::rcode	Construct(uint32_t screen_w, uint32_t screen_h, uint32_t pixel_w, uint32_t pixel_h, bool full_screen = false);
415
+		olc::rcode	Start();
416
+
417
+	public: // Override Interfaces
418
+		// Called once on application startup, use to load your resources
419
+		virtual bool OnUserCreate();
420
+		// Called every frame, and provides you with a time per frame value
421
+		virtual bool OnUserUpdate(float fElapsedTime);
422
+		// Called once on application termination, so you can be a clean coder
423
+		virtual bool OnUserDestroy();
424
+
425
+	public: // Hardware Interfaces
426
+		// Returns true if window is currently in focus
427
+		bool IsFocused();
428
+		// Get the state of a specific keyboard button
429
+		HWButton GetKey(Key k);
430
+		// Get the state of a specific mouse button
431
+		HWButton GetMouse(uint32_t b);
432
+		// Get Mouse X coordinate in "pixel" space
433
+		int32_t GetMouseX();
434
+		// Get Mouse Y coordinate in "pixel" space
435
+		int32_t GetMouseY();
436
+		// Get Mouse Wheel Delta
437
+		int32_t GetMouseWheel();
438
+
439
+	public: // Utility
440
+		// Returns the width of the screen in "pixels"
441
+		int32_t ScreenWidth();
442
+		// Returns the height of the screen in "pixels"
443
+		int32_t ScreenHeight();
444
+		// Returns the width of the currently selected drawing target in "pixels"
445
+		int32_t GetDrawTargetWidth();
446
+		// Returns the height of the currently selected drawing target in "pixels"
447
+		int32_t GetDrawTargetHeight();
448
+		// Returns the currently active draw target
449
+		Sprite* GetDrawTarget();
450
+
451
+	public: // Draw Routines
452
+		// Specify which Sprite should be the target of drawing functions, use nullptr
453
+		// to specify the primary screen
454
+		void SetDrawTarget(Sprite *target);
455
+		// Change the pixel mode for different optimisations
456
+		// olc::Pixel::NORMAL = No transparency
457
+		// olc::Pixel::MASK   = Transparent if alpha is < 255
458
+		// olc::Pixel::ALPHA  = Full transparency
459
+		void SetPixelMode(Pixel::Mode m);
460
+		Pixel::Mode GetPixelMode();
461
+		// Use a custom blend function
462
+		void SetPixelMode(std::function<olc::Pixel(const int x, const int y, const olc::Pixel& pSource, const olc::Pixel& pDest)> pixelMode);
463
+		// Change the blend factor form between 0.0f to 1.0f;
464
+		void SetPixelBlend(float fBlend);
465
+		// Offset texels by sub-pixel amount (advanced, do not use)
466
+		void SetSubPixelOffset(float ox, float oy);
467
+
468
+		// Draws a single Pixel
469
+		virtual bool Draw(int32_t x, int32_t y, Pixel p = olc::WHITE);
470
+		// Draws a line from (x1,y1) to (x2,y2)
471
+		void DrawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, Pixel p = olc::WHITE, uint32_t pattern = 0xFFFFFFFF);
472
+		// Draws a circle located at (x,y) with radius
473
+		void DrawCircle(int32_t x, int32_t y, int32_t radius, Pixel p = olc::WHITE, uint8_t mask = 0xFF);
474
+		// Fills a circle located at (x,y) with radius
475
+		void FillCircle(int32_t x, int32_t y, int32_t radius, Pixel p = olc::WHITE);
476
+		// Draws a rectangle at (x,y) to (x+w,y+h)
477
+		void DrawRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p = olc::WHITE);
478
+		// Fills a rectangle at (x,y) to (x+w,y+h)
479
+		void FillRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p = olc::WHITE);
480
+		// Draws a triangle between points (x1,y1), (x2,y2) and (x3,y3)
481
+		void DrawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
482
+		// Flat fills a triangle between points (x1,y1), (x2,y2) and (x3,y3)
483
+		void FillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
484
+		// Draws an entire sprite at location (x,y)
485
+		void DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale = 1);
486
+		// Draws an area of a sprite at location (x,y), where the
487
+		// selected area is (ox,oy) to (ox+w,oy+h)
488
+		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);
489
+		// Draws a single line of text
490
+		void DrawString(int32_t x, int32_t y, std::string sText, Pixel col = olc::WHITE, uint32_t scale = 1);
491
+		// Clears entire draw target to Pixel
492
+		void Clear(Pixel p);
493
+
494
+	public: // Branding
495
+		std::string sAppName;
496
+
497
+	private: // Inner mysterious workings
498
+		Sprite		*pDefaultDrawTarget = nullptr;
499
+		Sprite		*pDrawTarget = nullptr;
500
+		Pixel::Mode	nPixelMode = Pixel::NORMAL;
501
+		float		fBlendFactor = 1.0f;
502
+		uint32_t	nScreenWidth = 256;
503
+		uint32_t	nScreenHeight = 240;
504
+		uint32_t	nPixelWidth = 4;
505
+		uint32_t	nPixelHeight = 4;
506
+		int32_t		nMousePosX = 0;
507
+		int32_t		nMousePosY = 0;
508
+		int32_t		nMouseWheelDelta = 0;
509
+		int32_t		nMousePosXcache = 0;
510
+		int32_t		nMousePosYcache = 0;
511
+		int32_t		nMouseWheelDeltaCache = 0;
512
+		int32_t		nWindowWidth = 0;
513
+		int32_t		nWindowHeight = 0;
514
+		int32_t		nViewX = 0;
515
+		int32_t		nViewY = 0;
516
+		int32_t		nViewW = 0;
517
+		int32_t		nViewH = 0;
518
+		bool		bFullScreen = false;
519
+		float		fPixelX = 1.0f;
520
+		float		fPixelY = 1.0f;
521
+		float		fSubPixelOffsetX = 0.0f;
522
+		float		fSubPixelOffsetY = 0.0f;
523
+		bool		bHasInputFocus = false;
524
+		bool		bHasMouseFocus = false;
525
+		float		fFrameTimer = 1.0f;
526
+		int			nFrameCount = 0;
527
+		Sprite		*fontSprite = nullptr;
528
+		std::function<olc::Pixel(const int x, const int y, const olc::Pixel&, const olc::Pixel&)> funcPixelMode;
529
+
530
+		static std::map<uint16_t, uint8_t> mapKeys;
531
+		bool		pKeyNewState[256]{ 0 };
532
+		bool		pKeyOldState[256]{ 0 };
533
+		HWButton	pKeyboardState[256];
534
+
535
+		bool		pMouseNewState[5]{ 0 };
536
+		bool		pMouseOldState[5]{ 0 };
537
+		HWButton	pMouseState[5];
538
+
539
+#ifdef _WIN32
540
+		HDC			glDeviceContext = nullptr;
541
+		HGLRC		glRenderContext = nullptr;
542
+#else
543
+		GLXContext	glDeviceContext = nullptr;
544
+		GLXContext	glRenderContext = nullptr;
545
+#endif
546
+		GLuint		glBuffer;
547
+
548
+		void		EngineThread();
549
+
550
+		// If anything sets this flag to false, the engine
551
+		// "should" shut down gracefully
552
+		static std::atomic<bool> bAtomActive;
553
+
554
+		// Common initialisation functions
555
+		void olc_UpdateMouse(int32_t x, int32_t y);
556
+		void olc_UpdateMouseWheel(int32_t delta);
557
+		void olc_UpdateWindowSize(int32_t x, int32_t y);
558
+		void olc_UpdateViewport();
559
+		bool olc_OpenGLCreate();
560
+		void olc_ConstructFontSheet();
561
+
562
+
563
+#ifdef _WIN32
564
+		// Windows specific window handling
565
+		HWND olc_hWnd = nullptr;
566
+		HWND olc_WindowCreate();
567
+		std::wstring wsAppName;
568
+		static LRESULT CALLBACK olc_WindowEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
569
+#else
570
+		// Non-Windows specific window handling
571
+		Display*				olc_Display = nullptr;
572
+		Window					olc_WindowRoot;
573
+		Window					olc_Window;
574
+		XVisualInfo*            olc_VisualInfo;
575
+		Colormap                olc_ColourMap;
576
+		XSetWindowAttributes    olc_SetWindowAttribs;
577
+		Display*				olc_WindowCreate();
578
+#endif
579
+
580
+	};
581
+
582
+
583
+	class PGEX
584
+	{
585
+		friend class olc::PixelGameEngine;
586
+	protected:
587
+		static PixelGameEngine* pge;
588
+	};
589
+
590
+	//=============================================================
591
+}
592
+
593
+#endif // OLC_PGE_DEF

+ 0
- 2
src/.gitignore Переглянути файл

@@ -1,2 +0,0 @@
1
-awoorwa
2
-*.o

+ 2
- 0
src/main.cpp Переглянути файл

@@ -1,5 +1,7 @@
1 1
 #define OLC_PGE_APPLICATION
2 2
 #include "olcPixelGameEngine.h"
3
+#include "olcPGEX_Graphics2D.h"
4
+#include "olcPGEX_Sound.h"
3 5
 
4 6
 class Example : public olc::PixelGameEngine
5 7
 {

src/olcPGEX_Graphics2D.h → src/olcPGEX_Graphics2D.cpp Переглянути файл

@@ -1,130 +1,4 @@
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
-
71
-
72
-#ifndef OLC_PGEX_GFX2D
73
-#define OLC_PGEX_GFX2D
74
-
75
-#include <algorithm>
76
-#undef min
77
-#undef max
78
-
79
-namespace olc
80
-{
81
-	// Container class for Advanced 2D Drawing functions
82
-	class GFX2D : public olc::PGEX
83
-	{
84
-		// A representation of an affine transform, used to rotate, scale, offset & shear space
85
-	public:
86
-		class Transform2D
87
-		{
88
-		public:
89
-			inline Transform2D();
90
-
91
-		public:
92
-			// Set this transformation to unity
93
-			inline void Reset();
94
-			// Append a rotation of fTheta radians to this transform
95
-			inline void Rotate(float fTheta);
96
-			// Append a translation (ox, oy) to this transform
97
-			inline void Translate(float ox, float oy);
98
-			// Append a scaling operation (sx, sy) to this transform
99
-			inline void Scale(float sx, float sy);
100
-			// Append a shear operation (sx, sy) to this transform
101
-			inline void Shear(float sx, float sy);
102
-
103
-			inline void Perspective(float ox, float oy);
104
-			// Calculate the Forward Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
105
-			inline void Forward(float in_x, float in_y, float &out_x, float &out_y);
106
-			// Calculate the Inverse Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
107
-			inline void Backward(float in_x, float in_y, float &out_x, float &out_y);
108
-			// Regenerate the Inverse Transformation
109
-			inline void Invert();
110
-
111
-		private:
112
-			inline void Multiply();
113
-			float matrix[4][3][3];
114
-			int nTargetMatrix;
115
-			int nSourceMatrix;
116
-			bool bDirty;
117
-		};
118
-
119
-	public:
120
-		// Draws a sprite with the transform applied
121
-		inline static void DrawSprite(olc::Sprite *sprite, olc::GFX2D::Transform2D &transform);
122
-	};
123
-}
124
-
125
-
126
-#ifdef OLC_PGE_GRAPHICS2D
127
-#undef OLC_PGE_GRAPHICS2D
1
+#include "olcPGEX_Graphics2D.h"
128 2
 
129 3
 namespace olc
130 4
 {
@@ -308,6 +182,3 @@ namespace olc
308 182
 		}				
309 183
 	}
310 184
 }
311
-
312
-#endif
313
-#endif

src/olcPGEX_Sound.h → src/olcPGEX_Sound.cpp Переглянути файл

@@ -1,221 +1,4 @@
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 <istream>
75
-#include <cstring>
76
-#include <climits>
77
-
78
-#include <algorithm>
79
-#undef min
80
-#undef max
81
-
82
-// Choose a default sound backend
83
-#if !defined(USE_ALSA) && !defined(USE_OPENAL) && !defined(USE_WINDOWS)
84
-#ifdef __linux__
85
-#define USE_ALSA
86
-#endif
87
-
88
-#ifdef __EMSCRIPTEN__
89
-#define USE_OPENAL
90
-#endif
91
-
92
-#ifdef _WIN32
93
-#define USE_WINDOWS
94
-#endif
95
-
96
-#endif
97
-
98
-#ifdef USE_ALSA
99
-#define ALSA_PCM_NEW_HW_PARAMS_API
100
-#include <alsa/asoundlib.h>
101
-#endif
102
-
103
-#ifdef USE_OPENAL
104
-#include <AL/al.h>
105
-#include <AL/alc.h>
106
-#include <queue>
107
-#endif
108
-
109
-#pragma pack(push, 1)
110
-typedef struct {
111
-	uint16_t wFormatTag;
112
-	uint16_t nChannels;
113
-	uint32_t nSamplesPerSec;
114
-	uint32_t nAvgBytesPerSec;
115
-	uint16_t nBlockAlign;
116
-	uint16_t wBitsPerSample;
117
-	uint16_t cbSize;
118
-} OLC_WAVEFORMATEX;
119
-#pragma pack(pop)
120
-
121
-namespace olc
122
-{
123
-	// Container class for Advanced 2D Drawing functions
124
-	class SOUND : public olc::PGEX
125
-	{
126
-		// A representation of an affine transform, used to rotate, scale, offset & shear space
127
-	public:
128
-		class AudioSample
129
-		{
130
-		public:
131
-			AudioSample();
132
-			AudioSample(std::string sWavFile, olc::ResourcePack *pack = nullptr);
133
-			olc::rcode LoadFromFile(std::string sWavFile, olc::ResourcePack *pack = nullptr);
134
-
135
-		public:
136
-			OLC_WAVEFORMATEX wavHeader;
137
-			float *fSample = nullptr;
138
-			long nSamples = 0;
139
-			int nChannels = 0;
140
-			bool bSampleValid = false;
141
-		};
142
-
143
-		struct sCurrentlyPlayingSample
144
-		{
145
-			int nAudioSampleID = 0;
146
-			long nSamplePosition = 0;
147
-			bool bFinished = false;
148
-			bool bLoop = false;
149
-			bool bFlagForStop = false;
150
-		};
151
-
152
-		static std::list<sCurrentlyPlayingSample> listActiveSamples;
153
-
154
-	public:
155
-		static bool InitialiseAudio(unsigned int nSampleRate = 44100, unsigned int nChannels = 1, unsigned int nBlocks = 8, unsigned int nBlockSamples = 512);
156
-		static bool DestroyAudio();
157
-		static void SetUserSynthFunction(std::function<float(int, float, float)> func);
158
-		static void SetUserFilterFunction(std::function<float(int, float, float)> func);
159
-
160
-	public:
161
-		static int LoadAudioSample(std::string sWavFile, olc::ResourcePack *pack = nullptr);
162
-		static void PlaySample(int id, bool bLoop = false);
163
-		static void StopSample(int id);
164
-		static void StopAll();
165
-		static float GetMixerOutput(int nChannel, float fGlobalTime, float fTimeStep);
166
-
167
-
168
-	private:
169
-#ifdef USE_WINDOWS // Windows specific sound management
170
-		static void CALLBACK waveOutProc(HWAVEOUT hWaveOut, UINT uMsg, DWORD dwParam1, DWORD dwParam2);
171
-		static unsigned int m_nSampleRate;
172
-		static unsigned int m_nChannels;
173
-		static unsigned int m_nBlockCount;
174
-		static unsigned int m_nBlockSamples;
175
-		static unsigned int m_nBlockCurrent;
176
-		static short* m_pBlockMemory;
177
-		static WAVEHDR *m_pWaveHeaders;
178
-		static HWAVEOUT m_hwDevice;
179
-		static std::atomic<unsigned int> m_nBlockFree;
180
-		static std::condition_variable m_cvBlockNotZero;
181
-		static std::mutex m_muxBlockNotZero;
182
-#endif
183
-
184
-#ifdef USE_ALSA
185
-		static snd_pcm_t *m_pPCM;
186
-		static unsigned int m_nSampleRate;
187
-		static unsigned int m_nChannels;
188
-		static unsigned int m_nBlockSamples;
189
-		static short* m_pBlockMemory;
190
-#endif
191
-
192
-#ifdef USE_OPENAL
193
-		static std::queue<ALuint> m_qAvailableBuffers;
194
-		static ALuint *m_pBuffers;
195
-		static ALuint m_nSource;
196
-		static ALCdevice *m_pDevice;
197
-		static ALCcontext *m_pContext;
198
-		static unsigned int m_nSampleRate;
199
-		static unsigned int m_nChannels;
200
-		static unsigned int m_nBlockCount;
201
-		static unsigned int m_nBlockSamples;
202
-		static short* m_pBlockMemory;
203
-#endif
204
-
205
-		static void AudioThread();
206
-		static std::thread m_AudioThread;
207
-		static std::atomic<bool> m_bAudioThreadActive;
208
-		static std::atomic<float> m_fGlobalTime;
209
-		static std::function<float(int, float, float)> funcUserSynth;
210
-		static std::function<float(int, float, float)> funcUserFilter;
211
-	};
212
-}
213
-
214
-
215
-// Implementation, platform-independent
216
-
217
-#ifdef OLC_PGEX_SOUND
218
-#undef OLC_PGEX_SOUND
1
+#include "olcPGEX_Sound.h"
219 2
 
220 3
 namespace olc
221 4
 {
@@ -432,7 +215,9 @@ namespace olc
432 215
 
433 216
 // Implementation, Windows-specific
434 217
 #ifdef USE_WINDOWS
218
+#ifndef __MINGW32__
435 219
 #pragma comment(lib, "winmm.lib")
220
+#endif
436 221
 
437 222
 namespace olc
438 223
 {
@@ -521,7 +306,6 @@ namespace olc
521 306
 		// Goofy hack to get maximum integer for a type at run-time
522 307
 		short nMaxSample = (short)pow(2, (sizeof(short) * 8) - 1) - 1;
523 308
 		float fMaxSample = (float)nMaxSample;
524
-		short nPreviousSample = 0;
525 309
 
526 310
 		while (m_bAudioThreadActive)
527 311
 		{
@@ -558,7 +342,6 @@ namespace olc
558 342
 				{
559 343
 					nNewSample = (short)(clip(GetMixerOutput(c, m_fGlobalTime, fTimeStep), 1.0) * fMaxSample);
560 344
 					m_pBlockMemory[nCurrentBlock + n + c] = nNewSample;
561
-					nPreviousSample = nNewSample;
562 345
 				}
563 346
 
564 347
 				m_fGlobalTime = m_fGlobalTime + fTimeStep;
@@ -664,7 +447,6 @@ namespace olc
664 447
 		// Goofy hack to get maximum integer for a type at run-time
665 448
 		short nMaxSample = (short)pow(2, (sizeof(short) * 8) - 1) - 1;
666 449
 		float fMaxSample = (float)nMaxSample;
667
-		short nPreviousSample = 0;
668 450
 
669 451
 		while (m_bAudioThreadActive)
670 452
 		{
@@ -685,7 +467,6 @@ namespace olc
685 467
 				{
686 468
 					nNewSample = (short)(clip(GetMixerOutput(c, m_fGlobalTime, fTimeStep), 1.0) * fMaxSample);
687 469
 					m_pBlockMemory[n + c] = nNewSample;
688
-					nPreviousSample = nNewSample;
689 470
 				}
690 471
 
691 472
 				m_fGlobalTime = m_fGlobalTime + fTimeStep;
@@ -887,6 +668,4 @@ namespace olc
887 668
 	{	}
888 669
 }
889 670
 
890
-#endif
891
-#endif
892
-#endif // OLC_PGEX_SOUND
671
+#endif

src/olcPixelGameEngine.h → src/olcPixelGameEngine.cpp Переглянути файл

@@ -1,623 +1,11 @@
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
1
+#include "olcPixelGameEngine.h"
178 2
 
179 3
 #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
-	static wglSwapInterval_t *wglSwapInterval;
205
-#else
206
-	#include <GL/gl.h>
207
-	#include <GL/glx.h>
208
-	#include <X11/X.h>
209
-	#include <X11/Xlib.h>
210
-	#include <png.h>
211
-	typedef int(glSwapInterval_t) (Display *dpy, GLXDrawable drawable, int interval);
4
+    static wglSwapInterval_t *wglSwapInterval;
5
+#else 
212 6
 	static glSwapInterval_t *glSwapIntervalEXT;
213 7
 #endif
214 8
 
215
-
216
-// Standard includes
217
-#include <cmath>
218
-#include <cstdint>
219
-#include <string>
220
-#include <iostream>
221
-#include <streambuf>
222
-#include <chrono>
223
-#include <vector>
224
-#include <list>
225
-#include <thread>
226
-#include <atomic>
227
-#include <condition_variable>
228
-#include <fstream>
229
-#include <map>
230
-#include <functional>
231
-#include <algorithm>
232
-
233
-#undef min
234
-#undef max
235
-
236
-namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
237
-{
238
-	struct Pixel
239
-	{
240
-		union
241
-		{
242
-			uint32_t n = 0xFF000000;
243
-			struct
244
-			{
245
-				uint8_t r;	uint8_t g;	uint8_t b;	uint8_t a;
246
-			};
247
-		};
248
-
249
-		Pixel();
250
-		Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
251
-		Pixel(uint32_t p);
252
-		enum Mode { NORMAL, MASK, ALPHA, CUSTOM };
253
-	};
254
-
255
-	// Some constants for symbolic naming of Pixels
256
-	static const Pixel
257
-		WHITE(255, 255, 255),
258
-		GREY(192, 192, 192), DARK_GREY(128, 128, 128), VERY_DARK_GREY(64, 64, 64),
259
-		RED(255, 0, 0), DARK_RED(128, 0, 0), VERY_DARK_RED(64, 0, 0),
260
-		YELLOW(255, 255, 0), DARK_YELLOW(128, 128, 0), VERY_DARK_YELLOW(64, 64, 0),
261
-		GREEN(0, 255, 0), DARK_GREEN(0, 128, 0), VERY_DARK_GREEN(0, 64, 0),
262
-		CYAN(0, 255, 255), DARK_CYAN(0, 128, 128), VERY_DARK_CYAN(0, 64, 64),
263
-		BLUE(0, 0, 255), DARK_BLUE(0, 0, 128), VERY_DARK_BLUE(0, 0, 64),
264
-		MAGENTA(255, 0, 255), DARK_MAGENTA(128, 0, 128), VERY_DARK_MAGENTA(64, 0, 64),
265
-		BLACK(0, 0, 0),
266
-		BLANK(0, 0, 0, 0);
267
-
268
-	enum rcode
269
-	{
270
-		FAIL = 0,
271
-		OK = 1,
272
-		NO_FILE = -1,
273
-	};
274
-
275
-	//==================================================================================
276
-
277
-	template <class T>
278
-	struct v2d_generic
279
-	{
280
-		T x = 0;
281
-		T y = 0;
282
-
283
-		inline v2d_generic() : x(0), y(0)                        {                                                      }
284
-		inline v2d_generic(T _x, T _y) : x(_x), y(_y)            {                                                      }
285
-		inline v2d_generic(const v2d_generic& v) : x(v.x), y(v.y){                                                      }
286
-		inline T mag()                                           { return sqrt(x * x + y * y);                          }
287
-		inline v2d_generic  norm()                               { T r = 1 / mag(); return v2d_generic(x*r, y*r);       }
288
-		inline v2d_generic  perp()                               { return v2d_generic(-y, x);                           }
289
-		inline T dot(const v2d_generic& rhs)                     { return this->x * rhs.x + this->y * rhs.y;            }
290
-		inline T cross(const v2d_generic& rhs)                   { return this->x * rhs.y - this->y * rhs.x;            }
291
-		inline v2d_generic  operator +  (const v2d_generic& rhs) const { return v2d_generic(this->x + rhs.x, this->y + rhs.y);}
292
-		inline v2d_generic  operator -  (const v2d_generic& rhs) const { return v2d_generic(this->x - rhs.x, this->y - rhs.y);}
293
-		inline v2d_generic  operator *  (const T& rhs)           const { return v2d_generic(this->x * rhs, this->y * rhs);    }
294
-		inline v2d_generic  operator /  (const T& rhs)           const { return v2d_generic(this->x / rhs, this->y / rhs);    }
295
-		inline v2d_generic& operator += (const v2d_generic& rhs) { this->x += rhs.x; this->y += rhs.y; return *this;    }
296
-		inline v2d_generic& operator -= (const v2d_generic& rhs) { this->x -= rhs.x; this->y -= rhs.y; return *this;    }
297
-		inline v2d_generic& operator *= (const T& rhs)           { this->x *= rhs; this->y *= rhs; return *this;        }
298
-		inline v2d_generic& operator /= (const T& rhs)           { this->x /= rhs; this->y /= rhs; return *this;        }
299
-		inline T& operator [] (std::size_t i)                    { return *((T*)this + i);	   /* <-- D'oh :( */        }
300
-	};
301
-
302
-	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); }
303
-	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); }
304
-	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); }
305
-	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); }
306
-	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); }
307
-	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); }
308
-
309
-	typedef v2d_generic<int> vi2d;
310
-	typedef v2d_generic<float> vf2d;
311
-	typedef v2d_generic<double> vd2d;
312
-
313
-	//=============================================================
314
-
315
-	struct HWButton
316
-	{
317
-		bool bPressed = false;	// Set once during the frame the event occurs
318
-		bool bReleased = false;	// Set once during the frame the event occurs
319
-		bool bHeld = false;		// Set true for all frames between pressed and released events
320
-	};
321
-
322
-	//=============================================================
323
-
324
-
325
-	class ResourcePack
326
-	{
327
-	public:
328
-		ResourcePack();
329
-		~ResourcePack();
330
-		struct sEntry : public std::streambuf {
331
-			uint32_t nID, nFileOffset, nFileSize; uint8_t* data; void _config() { this->setg((char*)data, (char*)data, (char*)(data + nFileSize)); }
332
-		};
333
-
334
-	public:
335
-		olc::rcode AddToPack(std::string sFile);
336
-
337
-	public:
338
-		olc::rcode SavePack(std::string sFile);
339
-		olc::rcode LoadPack(std::string sFile);
340
-		olc::rcode ClearPack();
341
-
342
-	public:
343
-		olc::ResourcePack::sEntry GetStreamBuffer(std::string sFile);
344
-
345
-	private:
346
-
347
-		std::map<std::string, sEntry> mapFiles;
348
-	};
349
-
350
-	//=============================================================
351
-
352
-	// A bitmap-like structure that stores a 2D array of Pixels
353
-	class Sprite
354
-	{
355
-	public:
356
-		Sprite();
357
-		Sprite(std::string sImageFile);
358
-		Sprite(std::string sImageFile, olc::ResourcePack *pack);
359
-		Sprite(int32_t w, int32_t h);
360
-		~Sprite();
361
-
362
-	public:
363
-		olc::rcode LoadFromFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
364
-		olc::rcode LoadFromPGESprFile(std::string sImageFile, olc::ResourcePack *pack = nullptr);
365
-		olc::rcode SaveToPGESprFile(std::string sImageFile);
366
-
367
-	public:
368
-		int32_t width = 0;
369
-		int32_t height = 0;
370
-		enum Mode { NORMAL, PERIODIC };
371
-
372
-	public:
373
-		void SetSampleMode(olc::Sprite::Mode mode = olc::Sprite::Mode::NORMAL);
374
-		Pixel GetPixel(int32_t x, int32_t y);
375
-		bool  SetPixel(int32_t x, int32_t y, Pixel p);
376
-
377
-		Pixel Sample(float x, float y);
378
-		Pixel SampleBL(float u, float v);
379
-		Pixel* GetData();
380
-
381
-	private:
382
-		Pixel *pColData = nullptr;
383
-		Mode modeSample = Mode::NORMAL;
384
-
385
-#ifdef OLC_DBG_OVERDRAW
386
-	public:
387
-		static int nOverdrawCount;
388
-#endif
389
-
390
-	};
391
-
392
-	//=============================================================
393
-
394
-	enum Key
395
-	{
396
-		NONE,
397
-		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,
398
-		K0, K1, K2, K3, K4, K5, K6, K7, K8, K9,
399
-		F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
400
-		UP, DOWN, LEFT, RIGHT,
401
-		SPACE, TAB, SHIFT, CTRL, INS, DEL, HOME, END, PGUP, PGDN,
402
-		BACK, ESCAPE, RETURN, ENTER, PAUSE, SCROLL,
403
-		NP0, NP1, NP2, NP3, NP4, NP5, NP6, NP7, NP8, NP9,
404
-		NP_MUL, NP_DIV, NP_ADD, NP_SUB, NP_DECIMAL,
405
-	};
406
-
407
-
408
-	//=============================================================
409
-
410
-	class PixelGameEngine
411
-	{
412
-	public:
413
-		PixelGameEngine();
414
-
415
-	public:
416
-		olc::rcode	Construct(uint32_t screen_w, uint32_t screen_h, uint32_t pixel_w, uint32_t pixel_h, bool full_screen = false);
417
-		olc::rcode	Start();
418
-
419
-	public: // Override Interfaces
420
-		// Called once on application startup, use to load your resources
421
-		virtual bool OnUserCreate();
422
-		// Called every frame, and provides you with a time per frame value
423
-		virtual bool OnUserUpdate(float fElapsedTime);
424
-		// Called once on application termination, so you can be a clean coder
425
-		virtual bool OnUserDestroy();
426
-
427
-	public: // Hardware Interfaces
428
-		// Returns true if window is currently in focus
429
-		bool IsFocused();
430
-		// Get the state of a specific keyboard button
431
-		HWButton GetKey(Key k);
432
-		// Get the state of a specific mouse button
433
-		HWButton GetMouse(uint32_t b);
434
-		// Get Mouse X coordinate in "pixel" space
435
-		int32_t GetMouseX();
436
-		// Get Mouse Y coordinate in "pixel" space
437
-		int32_t GetMouseY();
438
-		// Get Mouse Wheel Delta
439
-		int32_t GetMouseWheel();
440
-
441
-	public: // Utility
442
-		// Returns the width of the screen in "pixels"
443
-		int32_t ScreenWidth();
444
-		// Returns the height of the screen in "pixels"
445
-		int32_t ScreenHeight();
446
-		// Returns the width of the currently selected drawing target in "pixels"
447
-		int32_t GetDrawTargetWidth();
448
-		// Returns the height of the currently selected drawing target in "pixels"
449
-		int32_t GetDrawTargetHeight();
450
-		// Returns the currently active draw target
451
-		Sprite* GetDrawTarget();
452
-
453
-	public: // Draw Routines
454
-		// Specify which Sprite should be the target of drawing functions, use nullptr
455
-		// to specify the primary screen
456
-		void SetDrawTarget(Sprite *target);
457
-		// Change the pixel mode for different optimisations
458
-		// olc::Pixel::NORMAL = No transparency
459
-		// olc::Pixel::MASK   = Transparent if alpha is < 255
460
-		// olc::Pixel::ALPHA  = Full transparency
461
-		void SetPixelMode(Pixel::Mode m);
462
-		Pixel::Mode GetPixelMode();
463
-		// Use a custom blend function
464
-		void SetPixelMode(std::function<olc::Pixel(const int x, const int y, const olc::Pixel& pSource, const olc::Pixel& pDest)> pixelMode);
465
-		// Change the blend factor form between 0.0f to 1.0f;
466
-		void SetPixelBlend(float fBlend);
467
-		// Offset texels by sub-pixel amount (advanced, do not use)
468
-		void SetSubPixelOffset(float ox, float oy);
469
-
470
-		// Draws a single Pixel
471
-		virtual bool Draw(int32_t x, int32_t y, Pixel p = olc::WHITE);
472
-		// Draws a line from (x1,y1) to (x2,y2)
473
-		void DrawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, Pixel p = olc::WHITE, uint32_t pattern = 0xFFFFFFFF);
474
-		// Draws a circle located at (x,y) with radius
475
-		void DrawCircle(int32_t x, int32_t y, int32_t radius, Pixel p = olc::WHITE, uint8_t mask = 0xFF);
476
-		// Fills a circle located at (x,y) with radius
477
-		void FillCircle(int32_t x, int32_t y, int32_t radius, Pixel p = olc::WHITE);
478
-		// Draws a rectangle at (x,y) to (x+w,y+h)
479
-		void DrawRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p = olc::WHITE);
480
-		// Fills a rectangle at (x,y) to (x+w,y+h)
481
-		void FillRect(int32_t x, int32_t y, int32_t w, int32_t h, Pixel p = olc::WHITE);
482
-		// Draws a triangle between points (x1,y1), (x2,y2) and (x3,y3)
483
-		void DrawTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
484
-		// Flat fills a triangle between points (x1,y1), (x2,y2) and (x3,y3)
485
-		void FillTriangle(int32_t x1, int32_t y1, int32_t x2, int32_t y2, int32_t x3, int32_t y3, Pixel p = olc::WHITE);
486
-		// Draws an entire sprite at location (x,y)
487
-		void DrawSprite(int32_t x, int32_t y, Sprite *sprite, uint32_t scale = 1);
488
-		// Draws an area of a sprite at location (x,y), where the
489
-		// selected area is (ox,oy) to (ox+w,oy+h)
490
-		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);
491
-		// Draws a single line of text
492
-		void DrawString(int32_t x, int32_t y, std::string sText, Pixel col = olc::WHITE, uint32_t scale = 1);
493
-		// Clears entire draw target to Pixel
494
-		void Clear(Pixel p);
495
-
496
-	public: // Branding
497
-		std::string sAppName;
498
-
499
-	private: // Inner mysterious workings
500
-		Sprite		*pDefaultDrawTarget = nullptr;
501
-		Sprite		*pDrawTarget = nullptr;
502
-		Pixel::Mode	nPixelMode = Pixel::NORMAL;
503
-		float		fBlendFactor = 1.0f;
504
-		uint32_t	nScreenWidth = 256;
505
-		uint32_t	nScreenHeight = 240;
506
-		uint32_t	nPixelWidth = 4;
507
-		uint32_t	nPixelHeight = 4;
508
-		int32_t		nMousePosX = 0;
509
-		int32_t		nMousePosY = 0;
510
-		int32_t		nMouseWheelDelta = 0;
511
-		int32_t		nMousePosXcache = 0;
512
-		int32_t		nMousePosYcache = 0;
513
-		int32_t		nMouseWheelDeltaCache = 0;
514
-		int32_t		nWindowWidth = 0;
515
-		int32_t		nWindowHeight = 0;
516
-		int32_t		nViewX = 0;
517
-		int32_t		nViewY = 0;
518
-		int32_t		nViewW = 0;
519
-		int32_t		nViewH = 0;
520
-		bool		bFullScreen = false;
521
-		float		fPixelX = 1.0f;
522
-		float		fPixelY = 1.0f;
523
-		float		fSubPixelOffsetX = 0.0f;
524
-		float		fSubPixelOffsetY = 0.0f;
525
-		bool		bHasInputFocus = false;
526
-		bool		bHasMouseFocus = false;
527
-		float		fFrameTimer = 1.0f;
528
-		int			nFrameCount = 0;
529
-		Sprite		*fontSprite = nullptr;
530
-		std::function<olc::Pixel(const int x, const int y, const olc::Pixel&, const olc::Pixel&)> funcPixelMode;
531
-
532
-		static std::map<uint16_t, uint8_t> mapKeys;
533
-		bool		pKeyNewState[256]{ 0 };
534
-		bool		pKeyOldState[256]{ 0 };
535
-		HWButton	pKeyboardState[256];
536
-
537
-		bool		pMouseNewState[5]{ 0 };
538
-		bool		pMouseOldState[5]{ 0 };
539
-		HWButton	pMouseState[5];
540
-
541
-#ifdef _WIN32
542
-		HDC			glDeviceContext = nullptr;
543
-		HGLRC		glRenderContext = nullptr;
544
-#else
545
-		GLXContext	glDeviceContext = nullptr;
546
-		GLXContext	glRenderContext = nullptr;
547
-#endif
548
-		GLuint		glBuffer;
549
-
550
-		void		EngineThread();
551
-
552
-		// If anything sets this flag to false, the engine
553
-		// "should" shut down gracefully
554
-		static std::atomic<bool> bAtomActive;
555
-
556
-		// Common initialisation functions
557
-		void olc_UpdateMouse(int32_t x, int32_t y);
558
-		void olc_UpdateMouseWheel(int32_t delta);
559
-		void olc_UpdateWindowSize(int32_t x, int32_t y);
560
-		void olc_UpdateViewport();
561
-		bool olc_OpenGLCreate();
562
-		void olc_ConstructFontSheet();
563
-
564
-
565
-#ifdef _WIN32
566
-		// Windows specific window handling
567
-		HWND olc_hWnd = nullptr;
568
-		HWND olc_WindowCreate();
569
-		std::wstring wsAppName;
570
-		static LRESULT CALLBACK olc_WindowEvent(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
571
-#else
572
-		// Non-Windows specific window handling
573
-		Display*				olc_Display = nullptr;
574
-		Window					olc_WindowRoot;
575
-		Window					olc_Window;
576
-		XVisualInfo*            olc_VisualInfo;
577
-		Colormap                olc_ColourMap;
578
-		XSetWindowAttributes    olc_SetWindowAttribs;
579
-		Display*				olc_WindowCreate();
580
-#endif
581
-
582
-	};
583
-
584
-
585
-	class PGEX
586
-	{
587
-		friend class olc::PixelGameEngine;
588
-	protected:
589
-		static PixelGameEngine* pge;
590
-	};
591
-
592
-	//=============================================================
593
-}
594
-
595
-#endif // OLC_PGE_DEF
596
-
597
-
598
-
599
-
600
-/*
601
-	Object Oriented Mode
602
-	~~~~~~~~~~~~~~~~~~~~
603
-
604
-	If the olcPixelGameEngine.h is called from several sources it can cause
605
-	multiple definitions of objects. To prevent this, ONLY ONE of the pathways
606
-	to including this file must have OLC_PGE_APPLICATION defined before it. This prevents
607
-	the definitions being duplicated.
608
-
609
-	If all else fails, create a file called "olcPixelGameEngine.cpp" with the following
610
-	two lines. Then you can just #include "olcPixelGameEngine.h" as normal without worrying
611
-	about defining things. Dont forget to include that cpp file as part of your build!
612
-
613
-	#define OLC_PGE_APPLICATION
614
-	#include "olcPixelGameEngine.h"
615
-
616
-*/
617
-
618
-#ifdef OLC_PGE_APPLICATION
619
-#undef OLC_PGE_APPLICATION
620
-
621 9
 namespace olc
622 10
 {
623 11
 	Pixel::Pixel()
@@ -2312,6 +1700,4 @@ namespace olc
2312 1700
 	int olc::Sprite::nOverdrawCount = 0;
2313 1701
 #endif
2314 1702
 	//=============================================================
2315
-}
2316
-
2317
-#endif
1703
+}

Loading…
Відмінити
Зберегти