Parcourir la source

Use resource packs

Pabloader il y a 4 ans
Parent
révision
d808b3025a
13 fichiers modifiés avec 155 ajouts et 105 suppressions
  1. 2
    2
      .gitignore
  2. 14
    6
      Makefile
  3. 31
    0
      include/awoorwae.h
  4. 33
    0
      include/entity.h
  5. 19
    0
      include/player.h
  6. 5
    2
      pge/src/olcPGE_Sprite.cpp
  7. 0
    0
      res/player.png
  8. BIN
      res/sprites.png
  9. 31
    0
      src/awoorwae.cpp
  10. 10
    0
      src/entity.cpp
  11. 1
    1
      src/main.cpp
  12. 0
    60
      src/pabloader/awoorwae.h
  13. 9
    34
      src/player.cpp

+ 2
- 2
.gitignore Voir le fichier

@@ -2,5 +2,5 @@ awoorwa
2 2
 awoorwae
3 3
 *.exe
4 4
 *.o
5
-*.pack
6
-*.spr
5
+*.pgp
6
+*.pgs

+ 14
- 6
Makefile Voir le fichier

@@ -2,12 +2,14 @@
2 2
 
3 3
 CPPFLAGS=-Wall -Wno-misleading-indentation -Werror --std=c++1z
4 4
 LDFLAGS=
5
-INCLUDES=-Ipge/include
5
+INCLUDES=-Iinclude -Ipge/include
6 6
 ifeq ($(OS),Windows_NT)
7 7
 	LIBRARIES=-luser32 -lgdi32 -lopengl32 -lgdiplus -lwinmm
8 8
 	EXECUTABLE=awoorwae.exe
9 9
 	PACKER=packer/packer.exe
10
+ifneq ($(DEBUG),1)
10 11
 	LDFLAGS+=--machine=windows
12
+endif
11 13
 	CPP=i686-w64-mingw32-g++
12 14
 	CC=i686-w64-mingw32-gcc
13 15
 	MAKE=mingw32-make
@@ -27,12 +29,15 @@ SOURCES=$(wildcard src/*.cpp) $(wildcard src/**/*.cpp) $(wildcard pge/src/*.cpp)
27 29
 IMAGES=$(wildcard res/*.png)
28 30
 
29 31
 OBJECTS=$(SOURCES:.cpp=.o)
30
-SPRITES=$(IMAGES:.png=.spr)
32
+SPRITES=$(IMAGES:.png=.pgs)
33
+RESOURCES=$(SPRITES) $(wildcard res/*.wav)
34
+
35
+PACK=awoorwae.pgp
31 36
 
32
-all: $(SOURCES) $(SPRITES) $(EXECUTABLE)
37
+all: $(SOURCES) $(PACK) $(EXECUTABLE)
33 38
 
34 39
 clean: 
35
-	$(RM) $(OBJECTS) $(SPRITES) $(EXECUTABLE)
40
+	$(RM) $(OBJECTS) $(SPRITES) $(PACK) $(EXECUTABLE)
36 41
 	
37 42
 $(EXECUTABLE): $(OBJECTS)
38 43
 	$(CPP) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)
@@ -40,10 +45,13 @@ $(EXECUTABLE): $(OBJECTS)
40 45
 .cpp.o:
41 46
 	$(CPP) $(INCLUDES) -c $(CPPFLAGS) $< -o $@
42 47
 
43
-%.spr: %.png $(PACKER)
48
+%.pgs: %.png $(PACKER)
44 49
 	$(PACKER) --convert $< $@
45 50
 
46
-run: $(SPRITES) $(EXECUTABLE)
51
+$(PACK): $(RESOURCES) $(PACKER)
52
+	$(PACKER) --add $(PACK) $(RESOURCES)
53
+
54
+run: $(PACK) $(EXECUTABLE)
47 55
 	./$(EXECUTABLE)
48 56
 
49 57
 $(PACKER):

+ 31
- 0
include/awoorwae.h Voir le fichier

@@ -0,0 +1,31 @@
1
+#ifndef AWOORWAE_H
2
+#define AWOORWAE_H
3
+
4
+#include "olcPixelGameEngine.h"
5
+#include "player.h"
6
+
7
+namespace pabloader {
8
+class Awoorwae : public olc::PixelGameEngine {
9
+private:
10
+    Player* player;
11
+
12
+public:
13
+    Awoorwae()
14
+    {
15
+        sAppName = "Awoorwa";
16
+        player = new Player(this);
17
+    }
18
+
19
+    ~Awoorwae()
20
+    {
21
+        delete player;
22
+    }
23
+
24
+public:
25
+    bool OnUserCreate() override;
26
+    bool OnUserUpdate(float fElapsedTime) override;
27
+    olc::ResourcePack pack;
28
+};
29
+}
30
+
31
+#endif

+ 33
- 0
include/entity.h Voir le fichier

@@ -0,0 +1,33 @@
1
+#ifndef ENTITY_H
2
+#define ENTITY_H
3
+
4
+#include "olcPGEX_Graphics2D.h"
5
+#include "olcPixelGameEngine.h"
6
+
7
+namespace pabloader {    
8
+class Awoorwae;
9
+
10
+class Entity {
11
+protected:
12
+    Awoorwae* game;
13
+    olc::Sprite img;
14
+    olc::GFX2D::Transform2D transform;
15
+    float x, y;
16
+    float xv, yv;
17
+    int w, h;
18
+    int tile = 0;
19
+    int skin = 0;
20
+    int tileCounter = 0;
21
+    bool onGround = false;
22
+
23
+public:
24
+    Entity(Awoorwae* game_);
25
+    virtual ~Entity(){};
26
+
27
+    virtual olc::rcode Load() = 0;
28
+    virtual void Update(float dt) = 0;
29
+    virtual void Draw() = 0;
30
+};
31
+}
32
+
33
+#endif

+ 19
- 0
include/player.h Voir le fichier

@@ -0,0 +1,19 @@
1
+#ifndef PLAYER_H
2
+#define PLAYER_H
3
+
4
+#include "entity.h"
5
+#include "olcPGE_Common.h"
6
+
7
+namespace pabloader {
8
+
9
+class Player : public Entity {
10
+public:
11
+    Player(Awoorwae* game);
12
+    olc::rcode Load() override;
13
+    void Update(float dt) override;
14
+    void Draw() override;
15
+};
16
+
17
+}
18
+
19
+#endif

+ 5
- 2
pge/src/olcPGE_Sprite.cpp Voir le fichier

@@ -61,13 +61,16 @@ namespace olc {
61 61
 		else
62 62
 		{
63 63
 			auto streamBuffer = pack->GetStreamBuffer(sImageFile);
64
+			if (streamBuffer.data == nullptr) 
65
+				return olc::FAIL;
66
+				
64 67
 			std::istream is(&streamBuffer);
65 68
 			ReadData(is);
69
+			return olc::OK;
66 70
 		}
67 71
 
68
-
69 72
 		return olc::FAIL;
70
-		}
73
+	}
71 74
 
72 75
 	olc::rcode Sprite::SaveToPGESprFile(std::string sImageFile)
73 76
 	{

res/awoorwa.png → res/player.png Voir le fichier


BIN
res/sprites.png Voir le fichier


+ 31
- 0
src/awoorwae.cpp Voir le fichier

@@ -0,0 +1,31 @@
1
+#include "awoorwae.h"
2
+#include <cmath>
3
+#include <string>
4
+#include <iostream>
5
+
6
+namespace pabloader {
7
+
8
+bool Awoorwae::OnUserCreate()
9
+{
10
+    if (!pack.LoadPack("awoorwae.pgp")) {
11
+        std::cerr << "Cannot load resource pack res/awoorwae.pgp" << std::endl;
12
+        return false;
13
+    }
14
+    if (!player->Load())
15
+        return false;
16
+
17
+    SetPixelMode(olc::Pixel::MASK);
18
+
19
+    pack.ClearPack();
20
+    return true;
21
+}
22
+
23
+bool Awoorwae::OnUserUpdate(float fElapsedTime)
24
+{
25
+    Clear(olc::VERY_DARK_CYAN);
26
+
27
+    player->Update(fElapsedTime);
28
+    player->Draw();
29
+    return true;
30
+}
31
+}

+ 10
- 0
src/entity.cpp Voir le fichier

@@ -0,0 +1,10 @@
1
+#include "entity.h"
2
+
3
+namespace pabloader {
4
+Entity::Entity(Awoorwae* game_)
5
+    : game(game_)
6
+{
7
+    xv = 0;
8
+    yv = 0;
9
+}
10
+}

+ 1
- 1
src/main.cpp Voir le fichier

@@ -1,4 +1,4 @@
1
-#include "pabloader/awoorwae.h"
1
+#include "awoorwae.h"
2 2
 
3 3
 int main()
4 4
 {

+ 0
- 60
src/pabloader/awoorwae.h Voir le fichier

@@ -1,60 +0,0 @@
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
-protected:
12
-    Awoorwae* game;
13
-    olc::Sprite img;
14
-    olc::GFX2D::Transform2D transform;
15
-    float x, y;
16
-    float xv, yv;
17
-    int w, h;
18
-    int tile = 0;
19
-    int skin = 0;
20
-    int tileCounter = 0;
21
-    bool onGround = false;
22
-
23
-public:
24
-    Entity(Awoorwae* game_);
25
-    virtual ~Entity(){};
26
-
27
-    virtual olc::rcode Load() = 0;
28
-    virtual void Update(float dt) = 0;
29
-    virtual void Draw() = 0;
30
-};
31
-
32
-class Player : public Entity {
33
-public:
34
-    Player(Awoorwae* game);
35
-    olc::rcode Load() override;
36
-    void Update(float dt) override;
37
-    void Draw() override;
38
-};
39
-
40
-class Awoorwae : public olc::PixelGameEngine {
41
-private:
42
-    Player* player;
43
-
44
-public:
45
-    Awoorwae()
46
-    {
47
-        sAppName = "Awoorwa";
48
-        player = new Player(this);
49
-    }
50
-
51
-    ~Awoorwae()
52
-    {
53
-        delete player;
54
-    }
55
-
56
-public:
57
-    bool OnUserCreate() override;
58
-    bool OnUserUpdate(float fElapsedTime) override;
59
-};
60
-}

src/pabloader/awoorwae.cpp → src/player.cpp Voir le fichier

@@ -1,36 +1,8 @@
1
+#include "player.h"
1 2
 #include "awoorwae.h"
2
-#include <cmath>
3
-#include <string>
3
+#include <iostream>
4 4
 
5 5
 namespace pabloader {
6
-
7
-bool Awoorwae::OnUserCreate()
8
-{
9
-    if (!player->Load()) {
10
-        std::cerr << "Could not load awoorwa.spr" << std::endl;
11
-        return false;
12
-    }
13
-
14
-    SetPixelMode(olc::Pixel::MASK);
15
-
16
-    return true;
17
-}
18
-
19
-bool Awoorwae::OnUserUpdate(float fElapsedTime)
20
-{
21
-    Clear(olc::VERY_DARK_CYAN);
22
-
23
-    player->Update(fElapsedTime);
24
-    player->Draw();
25
-    return true;
26
-}
27
-
28
-Entity::Entity(Awoorwae* game_)
29
-    : game(game_)
30
-{
31
-    xv = 0;
32
-    yv = 0;}
33
-
34 6
 Player::Player(Awoorwae* game_)
35 7
     : Entity(game_)
36 8
 {
@@ -38,13 +10,16 @@ Player::Player(Awoorwae* game_)
38 10
 
39 11
 olc::rcode Player::Load()
40 12
 {
41
-    olc::rcode result = img.LoadFromPGESprFile("res/awoorwa.spr");
13
+    const std::string fileName = "res/player.pgs";
14
+    olc::rcode result = img.LoadFromPGESprFile(fileName, &game->pack);
42 15
     if (result) {
43 16
         w = img.width / 4;
44 17
         h = img.height / 4;
18
+        x = (game->ScreenWidth() - w) / 2;
19
+        y = game->ScreenHeight() / 2;
20
+    } else {
21
+        std::cerr << "File " << fileName << " is not found" << std::endl;
45 22
     }
46
-    x = (game->ScreenWidth() - w) / 2;
47
-    y = game->ScreenHeight() / 2;
48 23
     return result;
49 24
 }
50 25
 
@@ -132,7 +107,7 @@ void Player::Update(float dt)
132 107
 
133 108
 void Player::Draw()
134 109
 {
135
-    float xOff = xv < 0 ? -1: 0;
110
+    int xOff = xv < 0 ? -1 : 0;
136 111
     if (std::abs(xv) < 1) {
137 112
         olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, h + tile * h, w, h, transform, true);
138 113
     } else {

Loading…
Annuler
Enregistrer