diff options
-rw-r--r-- | src/game.cpp | 24 | ||||
-rw-r--r-- | src/game.h | 21 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/renderer.cpp | 27 | ||||
-rw-r--r-- | src/vector.h | 2 |
5 files changed, 66 insertions, 14 deletions
diff --git a/src/game.cpp b/src/game.cpp index ccca10f..ebb792e 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -17,4 +17,26 @@ void Game::moveSprite(int id, vec2i newLoc) { | |||
17 | if (changedY) { | 17 | if (changedY) { |
18 | spritesByY_.emplace(newLoc.y(), id); | 18 | spritesByY_.emplace(newLoc.y(), id); |
19 | } | 19 | } |
20 | } \ No newline at end of file | 20 | } |
21 | |||
22 | void Game::tick() { | ||
23 | if (!cameraLocked_) { | ||
24 | const Sprite& follow = getSprite(followingSprite_); | ||
25 | vec2i mapBounds = map_->getMapSize() * map_->getTileSize(); | ||
26 | |||
27 | cameraPos_ = follow.loc() - (cameraFov_ / 2); | ||
28 | |||
29 | if (cameraPos_.x() < 0) { | ||
30 | cameraPos_.x() = 0; | ||
31 | } | ||
32 | if (cameraPos_.y() < 0) { | ||
33 | cameraPos_.y() = 0; | ||
34 | } | ||
35 | if (cameraPos_.x() + cameraFov_.w() >= mapBounds.w()) { | ||
36 | cameraPos_.x() = mapBounds.w() - cameraFov_.w() - 1; | ||
37 | } | ||
38 | if (cameraPos_.y() + cameraFov_.h() >= mapBounds.h()) { | ||
39 | cameraPos_.y() = mapBounds.h() - cameraFov_.h() - 1; | ||
40 | } | ||
41 | } | ||
42 | } | ||
diff --git a/src/game.h b/src/game.h index 2c1680a..a717d53 100644 --- a/src/game.h +++ b/src/game.h | |||
@@ -7,6 +7,7 @@ | |||
7 | #include <memory> | 7 | #include <memory> |
8 | #include "sprite.h" | 8 | #include "sprite.h" |
9 | #include "map.h" | 9 | #include "map.h" |
10 | #include "consts.h" | ||
10 | 11 | ||
11 | struct Input { | 12 | struct Input { |
12 | bool left = false; | 13 | bool left = false; |
@@ -60,11 +61,31 @@ public: | |||
60 | 61 | ||
61 | const Map& getMap() const { return *map_; } | 62 | const Map& getMap() const { return *map_; } |
62 | 63 | ||
64 | // Camera | ||
65 | |||
66 | const vec2i& getCameraPosition() const { return cameraPos_; } | ||
67 | |||
68 | const vec2i& getFieldOfView() const { return cameraFov_; } | ||
69 | |||
70 | void setFollowingSprite(int spriteId) { followingSprite_ = spriteId; } | ||
71 | |||
72 | void lockCamera() { cameraLocked_ = true; } | ||
73 | |||
74 | void unlockCamera() { cameraLocked_ = false; } | ||
75 | |||
76 | void tick(); | ||
77 | |||
63 | private: | 78 | private: |
64 | 79 | ||
65 | std::vector<Sprite> sprites_; | 80 | std::vector<Sprite> sprites_; |
66 | std::set<std::tuple<int, int>> spritesByY_; | 81 | std::set<std::tuple<int, int>> spritesByY_; |
67 | std::unique_ptr<Map> map_; | 82 | std::unique_ptr<Map> map_; |
83 | |||
84 | // Camera | ||
85 | vec2i cameraPos_; | ||
86 | vec2i cameraFov_ { CANVAS_WIDTH, CANVAS_HEIGHT }; | ||
87 | int followingSprite_; | ||
88 | bool cameraLocked_ = true; | ||
68 | }; | 89 | }; |
69 | 90 | ||
70 | #endif /* end of include guard: GAME_H_E6F1396E */ | 91 | #endif /* end of include guard: GAME_H_E6F1396E */ |
diff --git a/src/main.cpp b/src/main.cpp index 587ae15..b3aee1a 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -26,6 +26,10 @@ void loop(Renderer& renderer) { | |||
26 | party.addMember(game, dusterSprite); | 26 | party.addMember(game, dusterSprite); |
27 | party.addMember(game, boneySprite); | 27 | party.addMember(game, boneySprite); |
28 | 28 | ||
29 | game.setFollowingSprite(lucasSprite); | ||
30 | game.unlockCamera(); | ||
31 | game.tick(); | ||
32 | |||
29 | renderer.render(game); | 33 | renderer.render(game); |
30 | 34 | ||
31 | Timer inputTimer(50); | 35 | Timer inputTimer(50); |
@@ -65,6 +69,8 @@ void loop(Renderer& renderer) { | |||
65 | game.tickSpriteAnim(); | 69 | game.tickSpriteAnim(); |
66 | } | 70 | } |
67 | 71 | ||
72 | game.tick(); | ||
73 | |||
68 | renderer.render(game); | 74 | renderer.render(game); |
69 | } | 75 | } |
70 | } | 76 | } |
diff --git a/src/renderer.cpp b/src/renderer.cpp index 9d20fee..03d5173 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -88,13 +88,15 @@ void Renderer::render(Game& game) { | |||
88 | renLay0_ = renderMapLayer(game.getMap(), 1); | 88 | renLay0_ = renderMapLayer(game.getMap(), 1); |
89 | } | 89 | } |
90 | 90 | ||
91 | vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); | ||
92 | |||
91 | texture_ptr canvas( | 93 | texture_ptr canvas( |
92 | SDL_CreateTexture( | 94 | SDL_CreateTexture( |
93 | ren_.get(), | 95 | ren_.get(), |
94 | SDL_PIXELFORMAT_RGBA8888, | 96 | SDL_PIXELFORMAT_RGBA8888, |
95 | SDL_TEXTUREACCESS_TARGET, | 97 | SDL_TEXTUREACCESS_TARGET, |
96 | CANVAS_WIDTH, | 98 | mapBounds.w(), |
97 | CANVAS_HEIGHT)); | 99 | mapBounds.h())); |
98 | 100 | ||
99 | if (!canvas) { | 101 | if (!canvas) { |
100 | throw sdl_error(); | 102 | throw sdl_error(); |
@@ -105,12 +107,7 @@ void Renderer::render(Game& game) { | |||
105 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); | 107 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); |
106 | SDL_RenderClear(ren_.get()); | 108 | SDL_RenderClear(ren_.get()); |
107 | 109 | ||
108 | vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); | 110 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); |
109 | |||
110 | { | ||
111 | SDL_Rect dest { -80, -80, mapBounds.w(), mapBounds.h() }; | ||
112 | SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, &dest); | ||
113 | } | ||
114 | 111 | ||
115 | for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { | 112 | for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { |
116 | const SpriteFrame& frame = sprite.getFrame(); | 113 | const SpriteFrame& frame = sprite.getFrame(); |
@@ -119,13 +116,17 @@ void Renderer::render(Game& game) { | |||
119 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); | 116 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); |
120 | } | 117 | } |
121 | 118 | ||
122 | { | 119 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); |
123 | SDL_Rect dest { -80, -80, mapBounds.w(), mapBounds.h() }; | 120 | |
124 | SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, &dest); | 121 | SDL_Rect cameraField { |
125 | } | 122 | game.getCameraPosition().x(), |
123 | game.getCameraPosition().y(), | ||
124 | game.getFieldOfView().w(), | ||
125 | game.getFieldOfView().h() | ||
126 | }; | ||
126 | 127 | ||
127 | SDL_SetRenderTarget(ren_.get(), nullptr); | 128 | SDL_SetRenderTarget(ren_.get(), nullptr); |
128 | SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); | 129 | SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); |
129 | SDL_RenderPresent(ren_.get()); | 130 | SDL_RenderPresent(ren_.get()); |
130 | } | 131 | } |
131 | 132 | ||
diff --git a/src/vector.h b/src/vector.h index d54fdbb..8019edf 100644 --- a/src/vector.h +++ b/src/vector.h | |||
@@ -1,6 +1,8 @@ | |||
1 | #ifndef VECTOR_H_5458ED71 | 1 | #ifndef VECTOR_H_5458ED71 |
2 | #define VECTOR_H_5458ED71 | 2 | #define VECTOR_H_5458ED71 |
3 | 3 | ||
4 | #include <tuple> | ||
5 | |||
4 | template <typename T> | 6 | template <typename T> |
5 | class vec2 { | 7 | class vec2 { |
6 | public: | 8 | public: |