Browse Source

Use resource packs

Pabloader 5 years ago
parent
commit
d808b3025a
13 changed files with 155 additions and 105 deletions
  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 View File

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

+ 14
- 6
Makefile View File

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

+ 31
- 0
include/awoorwae.h View File

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 View File

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 View File

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 View File

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

res/awoorwa.png → res/player.png View File


BIN
res/sprites.png View File


+ 31
- 0
src/awoorwae.cpp View File

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 View File

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 View File

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

+ 0
- 60
src/pabloader/awoorwae.h View File

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 View File

1
+#include "player.h"
1
 #include "awoorwae.h"
2
 #include "awoorwae.h"
2
-#include <cmath>
3
-#include <string>
3
+#include <iostream>
4
 
4
 
5
 namespace pabloader {
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
 Player::Player(Awoorwae* game_)
6
 Player::Player(Awoorwae* game_)
35
     : Entity(game_)
7
     : Entity(game_)
36
 {
8
 {
38
 
10
 
39
 olc::rcode Player::Load()
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
     if (result) {
15
     if (result) {
43
         w = img.width / 4;
16
         w = img.width / 4;
44
         h = img.height / 4;
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
     return result;
23
     return result;
49
 }
24
 }
50
 
25
 
132
 
107
 
133
 void Player::Draw()
108
 void Player::Draw()
134
 {
109
 {
135
-    float xOff = xv < 0 ? -1: 0;
110
+    int xOff = xv < 0 ? -1 : 0;
136
     if (std::abs(xv) < 1) {
111
     if (std::abs(xv) < 1) {
137
         olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, h + tile * h, w, h, transform, true);
112
         olc::GFX2D::DrawPartialSprite(&img, skin * w + xOff, h + tile * h, w, h, transform, true);
138
     } else {
113
     } else {

Loading…
Cancel
Save