Quellcode durchsuchen

Automatic screen size

Pabloader vor 5 Jahren
Ursprung
Commit
6a2b52b59a
7 geänderte Dateien mit 226 neuen und 40 gelöschten Zeilen
  1. 27
    0
      .vscode/launch.json
  2. 3
    7
      Makefile
  3. 5
    0
      include/olcPixelGameEngine.h
  4. 5
    33
      src/main.cpp
  5. 77
    0
      src/olcPixelGameEngine.cpp
  6. 59
    0
      src/pabloader/awoorwae.cpp
  7. 50
    0
      src/pabloader/awoorwae.h

+ 27
- 0
.vscode/launch.json Datei anzeigen

@@ -0,0 +1,27 @@
1
+{
2
+    // Используйте IntelliSense, чтобы узнать о возможных атрибутах.
3
+    // Наведите указатель мыши, чтобы просмотреть описания существующих атрибутов.
4
+    // Для получения дополнительной информации посетите: https://go.microsoft.com/fwlink/?linkid=830387
5
+    "version": "0.2.0",
6
+    "configurations": [
7
+        {
8
+            "name": "(gdb) Launch",
9
+            "type": "cppdbg",
10
+            "request": "launch",
11
+            "program": "${workspaceFolder}/awoorwae",
12
+            "args": [],
13
+            "stopAtEntry": false,
14
+            "cwd": "${workspaceFolder}",
15
+            "environment": [],
16
+            "externalConsole": false,
17
+            "MIMode": "gdb",
18
+            "setupCommands": [
19
+                {
20
+                    "description": "Enable pretty-printing for gdb",
21
+                    "text": "-enable-pretty-printing",
22
+                    "ignoreFailures": true
23
+                }
24
+            ]
25
+        }
26
+    ]
27
+}

+ 3
- 7
Makefile Datei anzeigen

@@ -16,11 +16,10 @@ else
16 16
 	CC=gcc
17 17
 	CPP=g++
18 18
 endif
19
-SOURCES=$(wildcard src/*.c)
20
-SOURCES_CPP=$(wildcard src/*.cpp)
21
-OBJECTS=$(SOURCES:.c=.o) $(SOURCES_CPP:.cpp=.o)
19
+SOURCES=$(wildcard src/*.cpp) $(wildcard src/**/*.cpp)
20
+OBJECTS=$(SOURCES:.cpp=.o)
22 21
 
23
-all: $(SOURCES) $(SOURCES_CPP) $(EXECUTABLE)
22
+all: $(SOURCES) $(EXECUTABLE)
24 23
 
25 24
 clean: 
26 25
 	$(RM) $(OBJECTS) $(EXECUTABLE)
@@ -28,9 +27,6 @@ clean:
28 27
 $(EXECUTABLE): $(OBJECTS) 
29 28
 	$(CPP) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)
30 29
 
31
-.c.o:
32
-	$(CC) $(INCLUDES) -c $(CFLAGS) $< -o $@
33
-
34 30
 .cpp.o:
35 31
 	$(CPP) $(INCLUDES) -c $(CPPFLAGS) $< -o $@
36 32
 

+ 5
- 0
include/olcPixelGameEngine.h Datei anzeigen

@@ -247,6 +247,8 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
247 247
 		Pixel();
248 248
 		Pixel(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255);
249 249
 		Pixel(uint32_t p);
250
+
251
+		static Pixel CreateFromHSV(uint8_t h, uint8_t s, uint8_t v, uint8_t alpha = 255);
250 252
 		enum Mode { NORMAL, MASK, ALPHA, CUSTOM };
251 253
 	};
252 254
 
@@ -412,6 +414,8 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
412 414
 
413 415
 	public:
414 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
+		// Selects maximum pixel size to fit in the display resolution
418
+		olc::rcode	ConstructAuto(uint32_t screen_w, uint32_t screen_h, bool full_screen = false);
415 419
 		olc::rcode	Start();
416 420
 
417 421
 	public: // Override Interfaces
@@ -558,6 +562,7 @@ namespace olc // All OneLoneCoder stuff will now exist in the "olc" namespace
558 562
 		void olc_UpdateViewport();
559 563
 		bool olc_OpenGLCreate();
560 564
 		void olc_ConstructFontSheet();
565
+		uint32_t olc_GetMaximumPixelSize(uint32_t w, uint32_t h);
561 566
 
562 567
 
563 568
 #ifdef _WIN32

+ 5
- 33
src/main.cpp Datei anzeigen

@@ -1,38 +1,10 @@
1
-#include "olcPixelGameEngine.h"
2
-#include "olcPGEX_Graphics2D.h"
3
-#include "olcPGEX_Sound.h"
4
-
5
-class Example : public olc::PixelGameEngine
6
-{
7
-public:
8
-	Example()
9
-	{
10
-		sAppName = "Awoorwa";
11
-	}
12
-
13
-public:
14
-	bool OnUserCreate() override
15
-	{
16
-		// Called once at the start, so create things here
17
-		return true;
18
-	}
19
-
20
-	bool OnUserUpdate(float fElapsedTime) override
21
-	{
22
-		// called once per frame
23
-		for (int x = 0; x < ScreenWidth(); x++)
24
-			for (int y = 0; y < ScreenHeight(); y++)
25
-				Draw(x, y, olc::Pixel(rand() % 255, rand() % 255, rand()% 255));	
26
-		return true;
27
-	}
28
-};
29
-
1
+#include "pabloader/awoorwae.h"
30 2
 
31 3
 int main()
32 4
 {
33
-	Example demo;
34
-	if (demo.Construct(256, 240, 2, 1))
35
-		demo.Start();
5
+    pabloader::Awoorwae game;
6
+    if (game.ConstructAuto(320, 240))
7
+        game.Start();
36 8
 
37
-	return 0;
9
+    return 0;
38 10
 }

+ 77
- 0
src/olcPixelGameEngine.cpp Datei anzeigen

@@ -23,6 +23,51 @@ namespace olc
23 23
 		n = p;
24 24
 	}
25 25
 
26
+	Pixel Pixel::CreateFromHSV(uint8_t h, uint8_t s, uint8_t v, uint8_t alpha) {
27
+		Pixel rgb;
28
+		rgb.a = alpha;
29
+		uint8_t region, remainder, p, q, t;
30
+
31
+		if (s == 0)
32
+		{
33
+			rgb.r = v;
34
+			rgb.g = v;
35
+			rgb.b = v;
36
+			return rgb;
37
+		}
38
+
39
+		region = h / 43;
40
+		remainder = (h - (region * 43)) * 6; 
41
+
42
+		p = (v * (255 - s)) >> 8;
43
+		q = (v * (255 - ((s * remainder) >> 8))) >> 8;
44
+		t = (v * (255 - ((s * (255 - remainder)) >> 8))) >> 8;
45
+
46
+		switch (region)
47
+		{
48
+			case 0:
49
+				rgb.r = v; rgb.g = t; rgb.b = p;
50
+				break;
51
+			case 1:
52
+				rgb.r = q; rgb.g = v; rgb.b = p;
53
+				break;
54
+			case 2:
55
+				rgb.r = p; rgb.g = v; rgb.b = t;
56
+				break;
57
+			case 3:
58
+				rgb.r = p; rgb.g = q; rgb.b = v;
59
+				break;
60
+			case 4:
61
+				rgb.r = t; rgb.g = p; rgb.b = v;
62
+				break;
63
+			default:
64
+				rgb.r = v; rgb.g = p; rgb.b = q;
65
+				break;
66
+		}
67
+
68
+		return rgb;
69
+	}
70
+
26 71
 	//==========================================================
27 72
 
28 73
 	std::wstring ConvertS2W(std::string s)
@@ -480,6 +525,12 @@ namespace olc
480 525
 		return olc::OK;
481 526
 	}
482 527
 
528
+	olc::rcode PixelGameEngine::ConstructAuto(uint32_t screen_w, uint32_t screen_h, bool fullscreen)
529
+	{
530
+		uint32_t pixelSize = olc_GetMaximumPixelSize(screen_w, screen_h);
531
+		return Construct(screen_w, screen_h, pixelSize, pixelSize, fullscreen);
532
+	}
533
+
483 534
 	olc::rcode PixelGameEngine::Start()
484 535
 	{
485 536
 		// Construct the window
@@ -1116,6 +1167,32 @@ namespace olc
1116 1167
 			nMousePosYcache = 0;
1117 1168
 	}
1118 1169
 
1170
+	uint32_t PixelGameEngine::olc_GetMaximumPixelSize(uint32_t width, uint32_t height) 
1171
+	{
1172
+#ifdef _WIN32
1173
+		return 1; // TODO 
1174
+#else
1175
+		Display* d = XOpenDisplay(NULL);
1176
+		int defaultScreen = DefaultScreen(d);
1177
+		Screen *s = ScreenOfDisplay(d, defaultScreen);
1178
+		int minResolution = std::min(WidthOfScreen(s), HeightOfScreen(s));
1179
+		int screensCount = ScreenCount(d);
1180
+		for (int i=0; i < screensCount; i++) {
1181
+			s = ScreenOfDisplay(d, i);
1182
+			if (WidthOfScreen(s) < minResolution) {
1183
+				minResolution = WidthOfScreen(s);
1184
+			}
1185
+			if (HeightOfScreen(s) < minResolution) {
1186
+				minResolution = HeightOfScreen(s);
1187
+			}
1188
+		}
1189
+
1190
+		uint32_t minScreenSize = std::min(width, height);
1191
+
1192
+		return minResolution / minScreenSize;		
1193
+#endif
1194
+	}
1195
+
1119 1196
 	void PixelGameEngine::EngineThread()
1120 1197
 	{
1121 1198
 		// Start OpenGL, the context is owned by the game thread

+ 59
- 0
src/pabloader/awoorwae.cpp Datei anzeigen

@@ -0,0 +1,59 @@
1
+#include "awoorwae.h"
2
+#include <cmath>
3
+#include <string>
4
+
5
+namespace pabloader {
6
+
7
+bool Awoorwae::OnUserUpdate(float fElapsedTime)
8
+{
9
+    Clear(olc::BLACK);
10
+
11
+    player->Update(fElapsedTime);
12
+    player->Draw();
13
+    return true;
14
+}
15
+
16
+Entity::Entity(Awoorwae* game_, float x_, float y_)
17
+    : game(game_)
18
+    , x(x_)
19
+    , y(y_)
20
+    , xv(50)
21
+    , yv(30)
22
+    , color(0)
23
+{
24
+    xv = 10;
25
+    yv = 10;
26
+}
27
+
28
+void Entity::Update(float dt)
29
+{
30
+    x += dt * xv;
31
+    y += dt * yv;
32
+
33
+    if (x > game->ScreenWidth() - 20) {
34
+        xv *= -1;
35
+        x = game->ScreenWidth() - 20;
36
+    }
37
+
38
+    if (x < 20) {
39
+        xv *= -1.1;
40
+        x = 20;
41
+    }
42
+
43
+    if (y > game->ScreenHeight() - 20) {
44
+        yv *= -1;
45
+        y = game->ScreenHeight() - 20;
46
+    }
47
+
48
+    if (y < 20) {
49
+        yv *= -1.1;
50
+        y = 20;
51
+    }
52
+    color = fmodf(color + 256 * dt, 256);
53
+}
54
+
55
+void Entity::Draw()
56
+{
57
+    game->DrawCircle((int)x, (int)y, 20, olc::Pixel::CreateFromHSV((int)color, 255, 255));
58
+}
59
+}

+ 50
- 0
src/pabloader/awoorwae.h Datei anzeigen

@@ -0,0 +1,50 @@
1
+#include "olcPGEX_Graphics2D.h"
2
+#include "olcPGEX_Sound.h"
3
+#include "olcPixelGameEngine.h"
4
+#include <cstdint>
5
+#include <iostream>
6
+
7
+namespace pabloader {
8
+class Awoorwae;
9
+
10
+class Entity {
11
+private:
12
+    Awoorwae* game;
13
+    float x;
14
+    float y;
15
+    float xv;
16
+    float yv;
17
+    float color;
18
+
19
+public:
20
+    Entity(Awoorwae* game_, float x_, float y_);
21
+
22
+    void Update(float dt);
23
+    void Draw();
24
+};
25
+
26
+class Awoorwae : public olc::PixelGameEngine {
27
+private:
28
+    Entity* player;
29
+
30
+public:
31
+    Awoorwae()
32
+    {
33
+        sAppName = "Awoorwa";
34
+        player = new Entity(this, 128, 128);
35
+    }
36
+
37
+    ~Awoorwae()
38
+    {
39
+        delete player;
40
+    }
41
+
42
+public:
43
+    bool OnUserCreate() override
44
+    {
45
+        return true;
46
+    }
47
+
48
+    bool OnUserUpdate(float fElapsedTime) override;
49
+};
50
+}

Laden…
Abbrechen
Speichern