summary refs log tree commit diff stats
path: root/src/renderer.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-04 20:45:18 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-04 20:45:18 -0500
commit871943d6a90bdb92b3cc495d4d927199611f8c6b (patch)
tree9be125438747f7370cfa56e3f3e42f8c68982852 /src/renderer.cpp
parent138e0a8f83e82c6109bfc387ac7417d4f41711b4 (diff)
downloadtanetane-871943d6a90bdb92b3cc495d4d927199611f8c6b.tar.gz
tanetane-871943d6a90bdb92b3cc495d4d927199611f8c6b.tar.bz2
tanetane-871943d6a90bdb92b3cc495d4d927199611f8c6b.zip
Added text boxes
Text now reveals itself and scrolls! Yay! It even plays speaker beeps.

TODO: the arror indicating an A press is needed. Bullets on lines that need bullets. The header that says who the speaker is, if relevant.
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r--src/renderer.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index 2c6cf5c..b22c316 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -1,4 +1,5 @@
1#include "renderer.h" 1#include "renderer.h"
2#include <iostream>
2#include "consts.h" 3#include "consts.h"
3#include "game.h" 4#include "game.h"
4#include "map.h" 5#include "map.h"
@@ -153,6 +154,33 @@ void Renderer::render(Game& game) {
153 int bottomBarHeight = 36.0 * game.getSystem<MessageSystem>().getCutsceneBarsProgress(); 154 int bottomBarHeight = 36.0 * game.getSystem<MessageSystem>().getCutsceneBarsProgress();
154 SDL_Rect bottomBar { 0, CANVAS_HEIGHT - bottomBarHeight, CANVAS_WIDTH, bottomBarHeight }; 155 SDL_Rect bottomBar { 0, CANVAS_HEIGHT - bottomBarHeight, CANVAS_WIDTH, bottomBarHeight };
155 SDL_RenderFillRect(ren_.get(), &bottomBar); 156 SDL_RenderFillRect(ren_.get(), &bottomBar);
157
158 if (game.getSystem<MessageSystem>().getCutsceneBarsProgress() == 1.0 &&
159 !game.getSystem<MessageSystem>().getLines().empty()) {
160 int lineIndex = 0;
161 for (const MessageSystem::MessageLine& line : game.getSystem<MessageSystem>().getLines()) {
162 if (messageLines_[lineIndex].line != line.text) {
163 renderMessageLine(lineIndex, line.text, game);
164 }
165
166 // 18x, 127y1, 143y2
167 if (line.charsRevealed > 0) {
168 SDL_Rect srcRect {
169 0, 0, messageLines_[lineIndex].charIndexToWidth[line.charsRevealed],
170 game.getFont().getCharacterHeight()
171 };
172 SDL_Rect destRect {
173 18,
174 127 + 16 * lineIndex,
175 srcRect.w,
176 srcRect.h };
177
178 SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect);
179 }
180
181 lineIndex++;
182 }
183 }
156 } 184 }
157 185
158 SDL_SetRenderTarget(ren_.get(), nullptr); 186 SDL_SetRenderTarget(ren_.get(), nullptr);
@@ -175,3 +203,46 @@ int Renderer::loadImageFromFile(std::string_view filename) {
175 203
176 return texId; 204 return texId;
177} 205}
206
207void Renderer::renderMessageLine(int lineIndex, const std::string& text, Game& game) {
208 MessageCache& line = messageLines_[lineIndex];
209 line.line = text;
210
211 line.renderedTex.reset(
212 SDL_CreateTexture(
213 ren_.get(),
214 SDL_PIXELFORMAT_RGBA8888,
215 SDL_TEXTUREACCESS_TARGET,
216 MESSAGE_TEXT_WIDTH,
217 game.getFont().getCharacterHeight()));
218
219 SDL_SetTextureBlendMode(line.renderedTex.get(), SDL_BLENDMODE_BLEND);
220
221 SDL_SetRenderTarget(ren_.get(), line.renderedTex.get());
222 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND);
223 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
224 SDL_RenderClear(ren_.get());
225
226 line.charIndexToWidth.clear();
227 line.charIndexToWidth.push_back(0);
228
229 int length = 0;
230
231 for (int i=0; i<line.line.size(); i++) {
232 if (line.line[i] != ' ') {
233 vec2i charLoc = game.getFont().getCharacterLocation(line.line[i]);
234 vec2i charSize = game.getFont().getCharacterSize(line.line[i]);
235 SDL_Rect srcRect { charLoc.x(), charLoc.y(), charSize.w(), charSize.h() };
236 SDL_Rect destRect { length, 0, charSize.w(), charSize.h() };
237
238 SDL_RenderCopy(ren_.get(), textures_.at(game.getFont().getTextureId()).get(), &srcRect, &destRect);
239
240 length++;
241 }
242
243 length += game.getFont().getCharacterWidth(line.line[i]);
244 line.charIndexToWidth.push_back(length);
245 }
246
247 line.charIndexToWidth.back()--;
248}