diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/game.h | 9 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/renderer.cpp | 65 | ||||
-rw-r--r-- | src/renderer.h | 6 |
4 files changed, 80 insertions, 2 deletions
diff --git a/src/game.h b/src/game.h index 274ef2e..e5976b1 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <set> | 4 | #include <set> |
5 | #include <range/v3/all.hpp> | 5 | #include <range/v3/all.hpp> |
6 | #include <vector> | 6 | #include <vector> |
7 | #include <tileson.hpp> | ||
7 | #include "sprite.h" | 8 | #include "sprite.h" |
8 | 9 | ||
9 | struct Input { | 10 | struct Input { |
@@ -54,10 +55,18 @@ public: | |||
54 | } | 55 | } |
55 | } | 56 | } |
56 | 57 | ||
58 | void loadMapFromFile(std::string_view filename) { | ||
59 | tson::Tileson t; | ||
60 | map_ = t.parse(fs::path(filename)); | ||
61 | } | ||
62 | |||
63 | tson::Map* getMap() const { return map_.get(); } | ||
64 | |||
57 | private: | 65 | private: |
58 | 66 | ||
59 | std::vector<Sprite> sprites_; | 67 | std::vector<Sprite> sprites_; |
60 | std::set<std::tuple<int, int>> spritesByY_; | 68 | std::set<std::tuple<int, int>> spritesByY_; |
69 | std::unique_ptr<tson::Map> map_; | ||
61 | }; | 70 | }; |
62 | 71 | ||
63 | #endif /* end of include guard: GAME_H_E6F1396E */ | 72 | #endif /* end of include guard: GAME_H_E6F1396E */ |
diff --git a/src/main.cpp b/src/main.cpp index ea8d8f4..df0bfff 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -8,6 +8,8 @@ void loop(Renderer& renderer) { | |||
8 | Game game; | 8 | Game game; |
9 | Input keystate; | 9 | Input keystate; |
10 | 10 | ||
11 | game.loadMapFromFile("../res/map1.json"); | ||
12 | |||
11 | int lucasSprite = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); | 13 | int lucasSprite = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); |
12 | int kumaSprite = game.addSprite(Sprite("../res/kuma_anim.txt", renderer)); | 14 | int kumaSprite = game.addSprite(Sprite("../res/kuma_anim.txt", renderer)); |
13 | int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer)); | 15 | int dusterSprite = game.addSprite(Sprite("../res/duster_anim.txt", renderer)); |
diff --git a/src/renderer.cpp b/src/renderer.cpp index 931e699..45b9045 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -29,7 +29,56 @@ Renderer::Renderer() { | |||
29 | } | 29 | } |
30 | } | 30 | } |
31 | 31 | ||
32 | texture_ptr Renderer::renderMapLayer(const tson::Layer& layer) { | ||
33 | texture_ptr canvas( | ||
34 | SDL_CreateTexture( | ||
35 | ren_.get(), | ||
36 | SDL_PIXELFORMAT_RGBA8888, | ||
37 | SDL_TEXTUREACCESS_TARGET, | ||
38 | layer.getMap()->getSize().x * layer.getMap()->getTileSize().x, | ||
39 | layer.getMap()->getSize().y * layer.getMap()->getTileSize().y)); | ||
40 | |||
41 | SDL_SetTextureBlendMode(canvas.get(), SDL_BLENDMODE_BLEND); | ||
42 | |||
43 | SDL_SetRenderTarget(ren_.get(), canvas.get()); | ||
44 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); | ||
45 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 0); | ||
46 | SDL_RenderClear(ren_.get()); | ||
47 | |||
48 | for (auto& [pos, tileObject] : layer.getTileObjects()) { | ||
49 | int x = tileObject.getPosition().x; | ||
50 | int y = tileObject.getPosition().y; | ||
51 | tson::Rect tileDestRect = tileObject.getDrawingRect(); | ||
52 | SDL_Rect srcRect { tileDestRect.x, tileDestRect.y, tileDestRect.width, tileDestRect.height }; | ||
53 | SDL_Rect destRect { | ||
54 | x, y, tileObject.getTile()->getTileSize().x, tileObject.getTile()->getTileSize().y }; | ||
55 | |||
56 | SDL_RendererFlip flip = SDL_FLIP_NONE; | ||
57 | if (tileObject.getTile()->hasFlipFlags(tson::TileFlipFlags::Horizontally | tson::TileFlipFlags::Vertically)) { | ||
58 | flip = static_cast<SDL_RendererFlip>(SDL_FLIP_HORIZONTAL | SDL_FLIP_VERTICAL); | ||
59 | } else if (tileObject.getTile()->hasFlipFlags(tson::TileFlipFlags::Horizontally)) { | ||
60 | flip = SDL_FLIP_HORIZONTAL; | ||
61 | } else if (tileObject.getTile()->hasFlipFlags(tson::TileFlipFlags::Vertically)) { | ||
62 | flip = SDL_FLIP_VERTICAL; | ||
63 | } | ||
64 | SDL_RenderCopyEx(ren_.get(), textures_[tilesetTexId_].get(), &srcRect, &destRect, 0, nullptr, flip); | ||
65 | } | ||
66 | |||
67 | return canvas; | ||
68 | } | ||
69 | |||
32 | void Renderer::render(Game& game) { | 70 | void Renderer::render(Game& game) { |
71 | if (tilesetTexId_ == -1) { | ||
72 | tilesetTexId_ = loadImageFromFile(game.getMap()->getTileset("fromRom")->getImage().c_str()); | ||
73 | } | ||
74 | |||
75 | if (!renLay1_) { | ||
76 | renLay1_ = renderMapLayer(*game.getMap()->getLayer("Layer 1")); | ||
77 | } | ||
78 | if (!renLay0_) { | ||
79 | renLay0_ = renderMapLayer(*game.getMap()->getLayer("Layer 0")); | ||
80 | } | ||
81 | |||
33 | texture_ptr canvas( | 82 | texture_ptr canvas( |
34 | SDL_CreateTexture( | 83 | SDL_CreateTexture( |
35 | ren_.get(), | 84 | ren_.get(), |
@@ -38,16 +87,22 @@ void Renderer::render(Game& game) { | |||
38 | CANVAS_WIDTH, | 87 | CANVAS_WIDTH, |
39 | CANVAS_HEIGHT)); | 88 | CANVAS_HEIGHT)); |
40 | 89 | ||
41 | if (!canvas) | 90 | if (!canvas) { |
42 | { | ||
43 | throw sdl_error(); | 91 | throw sdl_error(); |
44 | } | 92 | } |
45 | 93 | ||
94 | |||
95 | |||
46 | SDL_SetRenderTarget(ren_.get(), canvas.get()); | 96 | SDL_SetRenderTarget(ren_.get(), canvas.get()); |
47 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); | 97 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); |
48 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); | 98 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); |
49 | SDL_RenderClear(ren_.get()); | 99 | SDL_RenderClear(ren_.get()); |
50 | 100 | ||
101 | { | ||
102 | SDL_Rect dest { -80, -80, game.getMap()->getSize().x * game.getMap()->getTileSize().x, game.getMap()->getSize().y * game.getMap()->getTileSize().y }; | ||
103 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, &dest); | ||
104 | } | ||
105 | |||
51 | for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { | 106 | for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { |
52 | const SpriteFrame& frame = sprite.getFrame(); | 107 | const SpriteFrame& frame = sprite.getFrame(); |
53 | const SDL_Rect& src = frame.srcRect; | 108 | const SDL_Rect& src = frame.srcRect; |
@@ -55,6 +110,11 @@ void Renderer::render(Game& game) { | |||
55 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); | 110 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); |
56 | } | 111 | } |
57 | 112 | ||
113 | { | ||
114 | SDL_Rect dest { -80, -80, game.getMap()->getSize().x * game.getMap()->getTileSize().x, game.getMap()->getSize().y * game.getMap()->getTileSize().y }; | ||
115 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, &dest); | ||
116 | } | ||
117 | |||
58 | SDL_SetRenderTarget(ren_.get(), nullptr); | 118 | SDL_SetRenderTarget(ren_.get(), nullptr); |
59 | SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); | 119 | SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); |
60 | SDL_RenderPresent(ren_.get()); | 120 | SDL_RenderPresent(ren_.get()); |
@@ -68,6 +128,7 @@ int Renderer::loadImageFromFile(std::string_view filename) { | |||
68 | } | 128 | } |
69 | 129 | ||
70 | texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); | 130 | texture_ptr tex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); |
131 | //SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); | ||
71 | 132 | ||
72 | int texId = textures_.size(); | 133 | int texId = textures_.size(); |
73 | textures_.push_back(std::move(tex)); | 134 | textures_.push_back(std::move(tex)); |
diff --git a/src/renderer.h b/src/renderer.h index abedb62..9573d8c 100644 --- a/src/renderer.h +++ b/src/renderer.h | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <memory> | 7 | #include <memory> |
8 | #include <string_view> | 8 | #include <string_view> |
9 | #include <vector> | 9 | #include <vector> |
10 | #include <tileson.hpp> | ||
10 | 11 | ||
11 | class Game; | 12 | class Game; |
12 | 13 | ||
@@ -121,12 +122,17 @@ public: | |||
121 | 122 | ||
122 | private: | 123 | private: |
123 | 124 | ||
125 | texture_ptr renderMapLayer(const tson::Layer& layer); | ||
126 | |||
124 | sdl_wrapper sdl_; | 127 | sdl_wrapper sdl_; |
125 | img_wrapper img_; | 128 | img_wrapper img_; |
126 | window_ptr win_; | 129 | window_ptr win_; |
127 | renderer_ptr ren_; | 130 | renderer_ptr ren_; |
128 | 131 | ||
129 | std::vector<texture_ptr> textures_; | 132 | std::vector<texture_ptr> textures_; |
133 | int tilesetTexId_ = -1; | ||
134 | texture_ptr renLay0_; | ||
135 | texture_ptr renLay1_; | ||
130 | }; | 136 | }; |
131 | 137 | ||
132 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ | 138 | #endif /* end of include guard: RENDERER_H_6A58EC30 */ |