Browse Source

Add packer to AwoorwaeOLC

Pabloader 5 years ago
parent
commit
2e45ce013a

+ 1
- 0
.gitignore View File

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

+ 8
- 0
.vscode/settings.json View File

@@ -0,0 +1,8 @@
1
+{
2
+    "files.associations": {
3
+        "*.py": "python",
4
+        "cstdint": "cpp",
5
+        "algorithm": "cpp",
6
+        "fstream": "cpp"
7
+    }
8
+}

+ 1
- 1
LICENCE.md View File

@@ -1,6 +1,6 @@
1 1
 # License (OLC-3)
2 2
 
3
-Copyright 2018-2019 OneLoneCoder.com, Pabloader
3
+Copyright 2018-2019 OneLoneCoder.com
4 4
 
5 5
 Redistribution and use in source and binary forms, with or without 
6 6
 modification, are permitted provided that the following conditions 

+ 9
- 11
Makefile View File

@@ -1,28 +1,29 @@
1 1
 # To compile this you will need MinGW with MSYS installed in PATH
2 2
 
3
-CFLAGS=-Wall -Wno-misleading-indentation -Werror -g
4
-CPPFLAGS=-Wall -Wno-misleading-indentation -Werror --std=c++1z -g
5
-LDFLAGS= 
6
-INCLUDES=-I./include
7
-ifeq ($(OS),Windows_NT)     # is Windows_NT on XP, 2000, 7, Vista, 10...
3
+CPPFLAGS=-Wall -Wno-misleading-indentation -Werror --std=c++1z -g -flto
4
+LDFLAGS=
5
+INCLUDES=-Ipge/include
6
+EXECUTABLE=awoorwae
7
+ifeq ($(OS),Windows_NT)
8 8
 	LIBRARIES=-luser32 -lgdi32 -lopengl32 -lgdiplus -lwinmm
9
-	EXECUTABLE=awoorwae.exe
9
+	EXECUTABLE+=.exe
10 10
 	LDFLAGS+=--machine=windows
11 11
 	CPP=i686-w64-mingw32-g++
12 12
 	CC=i686-w64-mingw32-gcc
13 13
 else
14 14
 	LIBRARIES=-lX11 -lGL -lpthread -lpng -lasound
15
-	EXECUTABLE=awoorwae
16 15
 	CC=gcc
17 16
 	CPP=g++
18 17
 endif
19
-SOURCES=$(wildcard src/*.cpp) $(wildcard src/**/*.cpp)
18
+SOURCES=$(wildcard src/*.cpp) $(wildcard src/**/*.cpp) $(wildcard pge/src/*.cpp)
20 19
 OBJECTS=$(SOURCES:.cpp=.o)
21 20
 
22 21
 all: $(SOURCES) $(EXECUTABLE)
22
+	$(MAKE) -C packer all
23 23
 
24 24
 clean: 
25 25
 	$(RM) $(OBJECTS) $(EXECUTABLE)
26
+	$(MAKE) -C packer clean
26 27
 	
27 28
 $(EXECUTABLE): $(OBJECTS) 
28 29
 	$(CPP) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)
@@ -30,8 +31,5 @@ $(EXECUTABLE): $(OBJECTS)
30 31
 .cpp.o:
31 32
 	$(CPP) $(INCLUDES) -c $(CPPFLAGS) $< -o $@
32 33
 
33
-debug: $(EXECUTABLE)
34
-	gdb $(EXECUTABLE)
35
-
36 34
 run: $(EXECUTABLE)
37 35
 	./$(EXECUTABLE)

include/olcPGEX_Graphics2D.h → pge/include/olcPGEX_Graphics2D.h View File


include/olcPGEX_Sound.h → pge/include/olcPGEX_Sound.h View File


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

@@ -0,0 +1,12 @@
1
+#ifndef OLC_PGE_COMMON
2
+#define OLC_PGE_COMMON
3
+
4
+namespace olc {
5
+    enum rcode {
6
+        FAIL = 0,
7
+        OK = 1,
8
+        NO_FILE = -1,
9
+    };
10
+}
11
+
12
+#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

include/olcPixelGameEngine.h → pge/include/olcPixelGameEngine.h View File

@@ -227,6 +227,8 @@
227 227
 #include <map>
228 228
 #include <functional>
229 229
 #include <algorithm>
230
+#include "olcPGE_Common.h"
231
+#include "olcPGE_ResourcePack.h"
230 232
 
231 233
 #undef min
232 234
 #undef max
@@ -265,13 +267,6 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
265 267
 		BLACK(0, 0, 0),
266 268
 		BLANK(0, 0, 0, 0);
267 269
 
268
-	enum rcode
269
-	{
270
-		FAIL = 0,
271
-		OK = 1,
272
-		NO_FILE = -1,
273
-	};
274
-
275 270
 	//==================================================================================
276 271
 
277 272
 	template <class T>
@@ -321,34 +316,6 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
321 316
 
322 317
 	//=============================================================
323 318
 
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 319
 	// A bitmap-like structure that stores a 2D array of Pixels
353 320
 	class Sprite
354 321
 	{

src/olcPGEX_Graphics2D.cpp → pge/src/olcPGEX_Graphics2D.cpp View File


src/olcPGEX_Sound.cpp → pge/src/olcPGEX_Sound.cpp View File


+ 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
+}

src/olcPixelGameEngine.cpp → pge/src/olcPixelGameEngine.cpp View File

@@ -351,144 +351,6 @@ namespace olc
351 351
 
352 352
 	//==========================================================
353 353
 
354
-	ResourcePack::ResourcePack()
355
-	{
356
-
357
-	}
358
-
359
-	ResourcePack::~ResourcePack()
360
-	{
361
-		ClearPack();
362
-	}
363
-
364
-	olc::rcode ResourcePack::AddToPack(std::string sFile)
365
-	{
366
-		std::ifstream ifs(sFile, std::ifstream::binary);
367
-		if (!ifs.is_open()) return olc::FAIL;
368
-
369
-		// Get File Size
370
-		std::streampos p = 0;
371
-		p = ifs.tellg();
372
-		ifs.seekg(0, std::ios::end);
373
-		p = ifs.tellg() - p;
374
-		ifs.seekg(0, std::ios::beg);
375
-
376
-		// Create entry
377
-		sEntry e;
378
-		e.data = nullptr;
379
-		e.nFileSize = (uint32_t)p;
380
-
381
-		// Read file into memory
382
-		e.data = new uint8_t[(uint32_t)e.nFileSize];
383
-		ifs.read((char*)e.data, e.nFileSize);
384
-		ifs.close();
385
-
386
-		// Add To Map
387
-		mapFiles[sFile] = e;
388
-		return olc::OK;
389
-	}
390
-
391
-	olc::rcode ResourcePack::SavePack(std::string sFile)
392
-	{
393
-		std::ofstream ofs(sFile, std::ofstream::binary);
394
-		if (!ofs.is_open()) return olc::FAIL;
395
-
396
-		// 1) Write Map
397
-		size_t nMapSize = mapFiles.size();
398
-		ofs.write((char*)&nMapSize, sizeof(size_t));
399
-		for (auto &e : mapFiles)
400
-		{
401
-			size_t nPathSize = e.first.size();
402
-			ofs.write((char*)&nPathSize, sizeof(size_t));
403
-			ofs.write(e.first.c_str(), nPathSize);
404
-			ofs.write((char*)&e.second.nID, sizeof(uint32_t));
405
-			ofs.write((char*)&e.second.nFileSize, sizeof(uint32_t));
406
-			ofs.write((char*)&e.second.nFileOffset, sizeof(uint32_t));
407
-		}
408
-
409
-		// 2) Write Data
410
-		std::streampos offset = ofs.tellp();
411
-		for (auto &e : mapFiles)
412
-		{
413
-			e.second.nFileOffset = (uint32_t)offset;
414
-			ofs.write((char*)e.second.data, e.second.nFileSize);
415
-			offset += e.second.nFileSize;
416
-		}
417
-
418
-		// 3) Rewrite Map (it has been updated with offsets now)
419
-		ofs.seekp(std::ios::beg);
420
-		ofs.write((char*)&nMapSize, sizeof(size_t));
421
-		for (auto &e : mapFiles)
422
-		{
423
-			size_t nPathSize = e.first.size();
424
-			ofs.write((char*)&nPathSize, sizeof(size_t));
425
-			ofs.write(e.first.c_str(), nPathSize);
426
-			ofs.write((char*)&e.second.nID, sizeof(uint32_t));
427
-			ofs.write((char*)&e.second.nFileSize, sizeof(uint32_t));
428
-			ofs.write((char*)&e.second.nFileOffset, sizeof(uint32_t));
429
-		}
430
-		ofs.close();
431
-
432
-		return olc::OK;
433
-	}
434
-
435
-	olc::rcode ResourcePack::LoadPack(std::string sFile)
436
-	{
437
-		std::ifstream ifs(sFile, std::ifstream::binary);
438
-		if (!ifs.is_open()) return olc::FAIL;
439
-
440
-		// 1) Read Map
441
-		size_t nMapEntries;
442
-		ifs.read((char*)&nMapEntries, sizeof(size_t));
443
-		for (size_t i = 0; i < nMapEntries; i++)
444
-		{
445
-			size_t nFilePathSize = 0;
446
-			ifs.read((char*)&nFilePathSize, sizeof(size_t));
447
-
448
-			std::string sFileName(nFilePathSize, ' ');
449
-			for (size_t j = 0; j < nFilePathSize; j++)
450
-				sFileName[j] = ifs.get();
451
-
452
-			sEntry e;
453
-			e.data = nullptr;
454
-			ifs.read((char*)&e.nID, sizeof(uint32_t));
455
-			ifs.read((char*)&e.nFileSize, sizeof(uint32_t));
456
-			ifs.read((char*)&e.nFileOffset, sizeof(uint32_t));
457
-			mapFiles[sFileName] = e;
458
-		}
459
-
460
-		// 2) Read Data
461
-		for (auto &e : mapFiles)
462
-		{
463
-			e.second.data = new uint8_t[(uint32_t)e.second.nFileSize];
464
-			ifs.seekg(e.second.nFileOffset);
465
-			ifs.read((char*)e.second.data, e.second.nFileSize);
466
-			e.second._config();
467
-		}
468
-
469
-		ifs.close();
470
-		return olc::OK;
471
-	}
472
-
473
-	olc::ResourcePack::sEntry ResourcePack::GetStreamBuffer(std::string sFile)
474
-	{
475
-		return mapFiles[sFile];
476
-	}
477
-
478
-	olc::rcode ResourcePack::ClearPack()
479
-	{
480
-		for (auto &e : mapFiles)
481
-		{
482
-			if (e.second.data != nullptr)
483
-				delete[] e.second.data;
484
-		}
485
-
486
-		mapFiles.clear();
487
-		return olc::OK;
488
-	}
489
-
490
-	//==========================================================
491
-
492 354
 	PixelGameEngine::PixelGameEngine()
493 355
 	{
494 356
 		sAppName = "Undefined";
@@ -1175,21 +1037,23 @@ namespace olc
1175 1037
 		Display* d = XOpenDisplay(NULL);
1176 1038
 		int defaultScreen = DefaultScreen(d);
1177 1039
 		Screen *s = ScreenOfDisplay(d, defaultScreen);
1178
-		int minResolution = std::min(WidthOfScreen(s), HeightOfScreen(s));
1040
+		int minWidth = WidthOfScreen(s);
1041
+		int minHeight = HeightOfScreen(s);
1179 1042
 		int screensCount = ScreenCount(d);
1180 1043
 		for (int i=0; i < screensCount; i++) {
1181 1044
 			s = ScreenOfDisplay(d, i);
1182
-			if (WidthOfScreen(s) < minResolution) {
1183
-				minResolution = WidthOfScreen(s);
1045
+			if (WidthOfScreen(s) < minWidth) {
1046
+				minWidth = WidthOfScreen(s);
1184 1047
 			}
1185
-			if (HeightOfScreen(s) < minResolution) {
1186
-				minResolution = HeightOfScreen(s);
1048
+			if (HeightOfScreen(s) < minHeight) {
1049
+				minHeight = HeightOfScreen(s);
1187 1050
 			}
1188 1051
 		}
1189 1052
 
1190
-		uint32_t maxScreenSize = std::max(width, height);
1053
+		uint32_t pixelWidth = minWidth / width;
1054
+		uint32_t pixelHeight = minHeight / height;
1191 1055
 
1192
-		return minResolution / maxScreenSize;		
1056
+		return std::min(pixelWidth, pixelHeight);		
1193 1057
 #endif
1194 1058
 	}
1195 1059
 

Loading…
Cancel
Save