diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-03 21:29:52 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-02-03 21:29:52 -0500 |
commit | 138e0a8f83e82c6109bfc387ac7417d4f41711b4 (patch) | |
tree | af4fe2d5453d5d2090f8106a433d507bd0e75a81 | |
parent | dab96b810691c26e29fef92d88c828a311be3e9d (diff) | |
download | tanetane-138e0a8f83e82c6109bfc387ac7417d4f41711b4.tar.gz tanetane-138e0a8f83e82c6109bfc387ac7417d4f41711b4.tar.bz2 tanetane-138e0a8f83e82c6109bfc387ac7417d4f41711b4.zip |
Added cutscene bars (and resized game)
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/consts.h | 6 | ||||
-rw-r--r-- | src/input_system.cpp | 20 | ||||
-rw-r--r-- | src/main.cpp | 2 | ||||
-rw-r--r-- | src/message_system.cpp | 34 | ||||
-rw-r--r-- | src/message_system.h | 42 | ||||
-rw-r--r-- | src/renderer.cpp | 30 | ||||
-rw-r--r-- | src/system.h | 3 |
8 files changed, 128 insertions, 10 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index a87b017..36acd0b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt | |||
@@ -42,6 +42,7 @@ add_executable(tanetane | |||
42 | src/animation_system.cpp | 42 | src/animation_system.cpp |
43 | src/character_system.cpp | 43 | src/character_system.cpp |
44 | src/input_system.cpp | 44 | src/input_system.cpp |
45 | src/message_system.cpp | ||
45 | ) | 46 | ) |
46 | 47 | ||
47 | set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) | 48 | set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) |
diff --git a/src/consts.h b/src/consts.h index fcfdff2..5ef2c21 100644 --- a/src/consts.h +++ b/src/consts.h | |||
@@ -1,11 +1,11 @@ | |||
1 | #ifndef CONSTS_H_9561E49C | 1 | #ifndef CONSTS_H_9561E49C |
2 | #define CONSTS_H_9561E49C | 2 | #define CONSTS_H_9561E49C |
3 | 3 | ||
4 | const int GAME_WIDTH = 640; | 4 | const int GAME_WIDTH = 720; |
5 | const int GAME_HEIGHT = 480; | 5 | const int GAME_HEIGHT = 480; |
6 | 6 | ||
7 | const int CANVAS_WIDTH = 320; | 7 | const int CANVAS_WIDTH = 240; |
8 | const int CANVAS_HEIGHT = 240; | 8 | const int CANVAS_HEIGHT = 160; |
9 | 9 | ||
10 | const int MOVEMENT_SPEED = 2; | 10 | const int MOVEMENT_SPEED = 2; |
11 | const int PARTY_FRAME_DELAY = 10;// / MOVEMENT_SPEED; | 11 | const int PARTY_FRAME_DELAY = 10;// / MOVEMENT_SPEED; |
diff --git a/src/input_system.cpp b/src/input_system.cpp index 54a291c..7bd605c 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp | |||
@@ -1,6 +1,7 @@ | |||
1 | #include "input_system.h" | 1 | #include "input_system.h" |
2 | #include "game.h" | 2 | #include "game.h" |
3 | #include "character_system.h" | 3 | #include "character_system.h" |
4 | #include "message_system.h" | ||
4 | 5 | ||
5 | struct Input { | 6 | struct Input { |
6 | bool left = false; | 7 | bool left = false; |
@@ -16,11 +17,20 @@ void InputSystem::tick(double dt) { | |||
16 | game_.quit(); | 17 | game_.quit(); |
17 | 18 | ||
18 | return; | 19 | return; |
19 | } else if (e.type == SDL_KEYDOWN && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) { | 20 | } else if (e.type == SDL_KEYDOWN) { |
20 | for (int spriteId : game_.getSprites()) { | 21 | if (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT) { |
21 | Sprite& sprite = game_.getSprite(spriteId); | 22 | for (int spriteId : game_.getSprites()) { |
22 | if (sprite.controllable) { | 23 | Sprite& sprite = game_.getSprite(spriteId); |
23 | game_.getSystem<CharacterSystem>().beginCrouch(spriteId); | 24 | if (sprite.controllable) { |
25 | game_.getSystem<CharacterSystem>().beginCrouch(spriteId); | ||
26 | } | ||
27 | } | ||
28 | } else if (e.key.keysym.sym == SDLK_a) { | ||
29 | // TODO: Remove this, it's just for testing. | ||
30 | if (game_.getSystem<MessageSystem>().getCutsceneBarsProgress() == 1.0) { | ||
31 | game_.getSystem<MessageSystem>().hideCutsceneBars(); | ||
32 | } else { | ||
33 | game_.getSystem<MessageSystem>().displayCutsceneBars(); | ||
24 | } | 34 | } |
25 | } | 35 | } |
26 | } else if (e.type == SDL_KEYUP && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) { | 36 | } else if (e.type == SDL_KEYUP && (e.key.keysym.sym == SDLK_LSHIFT || e.key.keysym.sym == SDLK_RSHIFT)) { |
diff --git a/src/main.cpp b/src/main.cpp index e5a96e4..6674c0c 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -10,6 +10,7 @@ | |||
10 | #include "animation_system.h" | 10 | #include "animation_system.h" |
11 | #include "character_system.h" | 11 | #include "character_system.h" |
12 | #include "input_system.h" | 12 | #include "input_system.h" |
13 | #include "message_system.h" | ||
13 | 14 | ||
14 | void loop(Renderer& renderer) { | 15 | void loop(Renderer& renderer) { |
15 | Game game; | 16 | Game game; |
@@ -18,6 +19,7 @@ void loop(Renderer& renderer) { | |||
18 | game.emplaceSystem<CharacterSystem>(); | 19 | game.emplaceSystem<CharacterSystem>(); |
19 | game.emplaceSystem<AnimationSystem>(); | 20 | game.emplaceSystem<AnimationSystem>(); |
20 | game.emplaceSystem<CameraSystem>(); | 21 | game.emplaceSystem<CameraSystem>(); |
22 | game.emplaceSystem<MessageSystem>(); | ||
21 | 23 | ||
22 | auto map = std::make_unique<Map>("../res/map1.tmx", renderer); | 24 | auto map = std::make_unique<Map>("../res/map1.tmx", renderer); |
23 | game.setMap(std::move(map)); | 25 | game.setMap(std::move(map)); |
diff --git a/src/message_system.cpp b/src/message_system.cpp new file mode 100644 index 0000000..71e8a5a --- /dev/null +++ b/src/message_system.cpp | |||
@@ -0,0 +1,34 @@ | |||
1 | #include "message_system.h" | ||
2 | |||
3 | void MessageSystem::tick(double dt) { | ||
4 | if (barsState_ == BarsState::Opening || barsState_ == BarsState::Closing) { | ||
5 | accum_ += dt; | ||
6 | |||
7 | if (accum_ >= length_) { | ||
8 | if (barsState_ == BarsState::Opening) { | ||
9 | barsState_ = BarsState::Open; | ||
10 | } else { | ||
11 | barsState_ = BarsState::Closed; | ||
12 | } | ||
13 | } | ||
14 | } | ||
15 | } | ||
16 | |||
17 | void MessageSystem::displayCutsceneBars() { | ||
18 | accum_ = 0.0; | ||
19 | barsState_ = BarsState::Opening; | ||
20 | } | ||
21 | |||
22 | void MessageSystem::hideCutsceneBars() { | ||
23 | accum_ = 0.0; | ||
24 | barsState_ = BarsState::Closing; | ||
25 | } | ||
26 | |||
27 | double MessageSystem::getCutsceneBarsProgress() const { | ||
28 | switch (barsState_) { | ||
29 | case BarsState::Closed: return 0.0; | ||
30 | case BarsState::Opening: return accum_ / length_; | ||
31 | case BarsState::Open: return 1.0; | ||
32 | case BarsState::Closing: return 1.0 - (accum_ / length_); | ||
33 | } | ||
34 | } | ||
diff --git a/src/message_system.h b/src/message_system.h new file mode 100644 index 0000000..4dd0166 --- /dev/null +++ b/src/message_system.h | |||
@@ -0,0 +1,42 @@ | |||
1 | #ifndef MESSAGE_SYSTEM_H_DE10D011 | ||
2 | #define MESSAGE_SYSTEM_H_DE10D011 | ||
3 | |||
4 | #include "system.h" | ||
5 | |||
6 | class Game; | ||
7 | |||
8 | class MessageSystem : public System { | ||
9 | public: | ||
10 | |||
11 | static constexpr SystemKey Key = SystemKey::Message; | ||
12 | |||
13 | MessageSystem(Game& game) : game_(game) {} | ||
14 | |||
15 | void tick(double dt) override; | ||
16 | |||
17 | // Commands | ||
18 | |||
19 | void displayCutsceneBars(); | ||
20 | |||
21 | void hideCutsceneBars(); | ||
22 | |||
23 | // Info | ||
24 | |||
25 | double getCutsceneBarsProgress() const; | ||
26 | |||
27 | private: | ||
28 | |||
29 | enum class BarsState { | ||
30 | Closed, | ||
31 | Opening, | ||
32 | Open, | ||
33 | Closing | ||
34 | }; | ||
35 | |||
36 | Game& game_; | ||
37 | BarsState barsState_ = BarsState::Closed; | ||
38 | double accum_ = 0.0; | ||
39 | double length_ = 1000.0/8; | ||
40 | }; | ||
41 | |||
42 | #endif /* end of include guard: MESSAGE_SYSTEM_H_DE10D011 */ | ||
diff --git a/src/renderer.cpp b/src/renderer.cpp index 92083db..2c6cf5c 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -4,6 +4,7 @@ | |||
4 | #include "map.h" | 4 | #include "map.h" |
5 | #include "transform_system.h" | 5 | #include "transform_system.h" |
6 | #include "camera_system.h" | 6 | #include "camera_system.h" |
7 | #include "message_system.h" | ||
7 | 8 | ||
8 | Renderer::Renderer() { | 9 | Renderer::Renderer() { |
9 | win_ = window_ptr( | 10 | win_ = window_ptr( |
@@ -127,8 +128,35 @@ void Renderer::render(Game& game) { | |||
127 | game.getSystem<CameraSystem>().getFieldOfView().h() | 128 | game.getSystem<CameraSystem>().getFieldOfView().h() |
128 | }; | 129 | }; |
129 | 130 | ||
130 | SDL_SetRenderTarget(ren_.get(), nullptr); | 131 | texture_ptr cameraTex( |
132 | SDL_CreateTexture( | ||
133 | ren_.get(), | ||
134 | SDL_PIXELFORMAT_RGBA8888, | ||
135 | SDL_TEXTUREACCESS_TARGET, | ||
136 | CANVAS_WIDTH, | ||
137 | CANVAS_HEIGHT)); | ||
138 | |||
139 | if (!cameraTex) { | ||
140 | throw sdl_error(); | ||
141 | } | ||
142 | |||
143 | SDL_SetRenderTarget(ren_.get(), cameraTex.get()); | ||
131 | SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); | 144 | SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); |
145 | |||
146 | if (game.getSystem<MessageSystem>().getCutsceneBarsProgress() > 0) { | ||
147 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); | ||
148 | |||
149 | int topBarHeight = 16.0 * game.getSystem<MessageSystem>().getCutsceneBarsProgress(); | ||
150 | SDL_Rect topBar { 0, 0, CANVAS_WIDTH, topBarHeight }; | ||
151 | SDL_RenderFillRect(ren_.get(), &topBar); | ||
152 | |||
153 | int bottomBarHeight = 36.0 * game.getSystem<MessageSystem>().getCutsceneBarsProgress(); | ||
154 | SDL_Rect bottomBar { 0, CANVAS_HEIGHT - bottomBarHeight, CANVAS_WIDTH, bottomBarHeight }; | ||
155 | SDL_RenderFillRect(ren_.get(), &bottomBar); | ||
156 | } | ||
157 | |||
158 | SDL_SetRenderTarget(ren_.get(), nullptr); | ||
159 | SDL_RenderCopy(ren_.get(), cameraTex.get(), nullptr, nullptr); | ||
132 | SDL_RenderPresent(ren_.get()); | 160 | SDL_RenderPresent(ren_.get()); |
133 | } | 161 | } |
134 | 162 | ||
diff --git a/src/system.h b/src/system.h index fc89503..298c5f8 100644 --- a/src/system.h +++ b/src/system.h | |||
@@ -6,7 +6,8 @@ enum class SystemKey { | |||
6 | Input, | 6 | Input, |
7 | Character, | 7 | Character, |
8 | Animation, | 8 | Animation, |
9 | Camera | 9 | Camera, |
10 | Message | ||
10 | }; | 11 | }; |
11 | 12 | ||
12 | class System { | 13 | class System { |