summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/renderer.cpp164
-rw-r--r--src/renderer.h10
2 files changed, 112 insertions, 62 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index 81e6b9c..14eefea 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -124,9 +124,47 @@ void Renderer::renderSprite(const Sprite& sprite) {
124} 124}
125 125
126void Renderer::render(Game& game) { 126void Renderer::render(Game& game) {
127 auto& effects = game.getSystem<EffectSystem>();
128 auto& menus = game.getSystem<MenuSystem>(); 127 auto& menus = game.getSystem<MenuSystem>();
129 128
129 texture_ptr canvas(
130 SDL_CreateTexture(
131 ren_.get(),
132 SDL_PIXELFORMAT_RGBA8888,
133 SDL_TEXTUREACCESS_TARGET,
134 CANVAS_WIDTH,
135 CANVAS_HEIGHT));
136
137 if (menus.getPauseAnimationProgress() < 1.0) {
138 renderGame(game);
139
140 SDL_SetRenderTarget(ren_.get(), canvas.get());
141 SDL_RenderCopy(ren_.get(), cameraTex_.get(), nullptr, nullptr);
142 }
143
144 if (menus.getPauseAnimationProgress() > 0.0) {
145 renderMenu(game);
146
147 SDL_SetRenderTarget(ren_.get(), canvas.get());
148 if (menus.getPauseAnimationProgress() == 1.0) {
149 SDL_RenderCopy(ren_.get(), menuTex_.get(), nullptr, nullptr);
150 } else {
151 int barHeight = CANVAS_HEIGHT / 2 * menus.getPauseAnimationProgress();
152 SDL_Rect topHalf { 0, 0, CANVAS_WIDTH, barHeight };
153 SDL_Rect bottomHalf { 0, CANVAS_HEIGHT - barHeight, CANVAS_WIDTH, barHeight };
154
155 SDL_RenderCopy(ren_.get(), menuTex_.get(), &topHalf, &topHalf);
156 SDL_RenderCopy(ren_.get(), menuTex_.get(), &bottomHalf, &bottomHalf);
157 }
158 }
159
160 SDL_SetRenderTarget(ren_.get(), nullptr);
161 SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr);
162 SDL_RenderPresent(ren_.get());
163}
164
165void Renderer::renderGame(Game& game) {
166 auto& effects = game.getSystem<EffectSystem>();
167
130 if (cachedMapName_ != game.getMap().getName()) { 168 if (cachedMapName_ != game.getMap().getName()) {
131 cachedMapName_ = game.getMap().getName(); 169 cachedMapName_ = game.getMap().getName();
132 renLowerLayer_ = renderMapLayer(game.getMap(), false); 170 renLowerLayer_ = renderMapLayer(game.getMap(), false);
@@ -247,23 +285,25 @@ void Renderer::render(Game& game) {
247 game.getSystem<CameraSystem>().getFieldOfView().h() 285 game.getSystem<CameraSystem>().getFieldOfView().h()
248 }; 286 };
249 287
250 texture_ptr cameraTex( 288 if (!cameraTex_) {
251 SDL_CreateTexture( 289 cameraTex_.reset(
252 ren_.get(), 290 SDL_CreateTexture(
253 SDL_PIXELFORMAT_RGBA8888, 291 ren_.get(),
254 SDL_TEXTUREACCESS_TARGET, 292 SDL_PIXELFORMAT_RGBA8888,
255 CANVAS_WIDTH, 293 SDL_TEXTUREACCESS_TARGET,
256 CANVAS_HEIGHT)); 294 CANVAS_WIDTH,
295 CANVAS_HEIGHT));
257 296
258 if (!cameraTex) { 297 if (!cameraTex_) {
259 throw sdl_error(); 298 throw sdl_error();
299 }
260 } 300 }
261 301
262 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 302 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
263 SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); 303 SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr);
264 304
265 if (game.getSystem<MessageSystem>().getCutsceneBarsProgress() > 0) { 305 if (game.getSystem<MessageSystem>().getCutsceneBarsProgress() > 0) {
266 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 306 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
267 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); 307 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
268 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); 308 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
269 309
@@ -284,7 +324,7 @@ void Renderer::render(Game& game) {
284 324
285 { 325 {
286 SDL_Rect destRect { 0, 111, 57, 13 }; 326 SDL_Rect destRect { 0, 111, 57, 13 };
287 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 327 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
288 SDL_RenderCopy(ren_.get(), textures_.at(speakerHeaderTex_).get(), nullptr, &destRect); 328 SDL_RenderCopy(ren_.get(), textures_.at(speakerHeaderTex_).get(), nullptr, &destRect);
289 } 329 }
290 330
@@ -299,7 +339,7 @@ void Renderer::render(Game& game) {
299 MESSAGE_TEXT_WIDTH, 339 MESSAGE_TEXT_WIDTH,
300 game.getFont().getCharacterHeight() }; 340 game.getFont().getCharacterHeight() };
301 341
302 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 342 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
303 SDL_RenderCopy(ren_.get(), speakerHeaderLine_.renderedTex.get(), nullptr, &destRect); 343 SDL_RenderCopy(ren_.get(), speakerHeaderLine_.renderedTex.get(), nullptr, &destRect);
304 } 344 }
305 } 345 }
@@ -323,7 +363,7 @@ void Renderer::render(Game& game) {
323 srcRect.w, 363 srcRect.w,
324 srcRect.h }; 364 srcRect.h };
325 365
326 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 366 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
327 SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect); 367 SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect);
328 } 368 }
329 369
@@ -333,7 +373,7 @@ void Renderer::render(Game& game) {
333 SDL_Rect srcRect { charLoc.x(), charLoc.y(), charSize.w(), charSize.h() }; 373 SDL_Rect srcRect { charLoc.x(), charLoc.y(), charSize.w(), charSize.h() };
334 SDL_Rect destRect { 13, 127 + 16 * lineIndex, charSize.w(), charSize.h() }; 374 SDL_Rect destRect { 13, 127 + 16 * lineIndex, charSize.w(), charSize.h() };
335 375
336 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 376 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
337 SDL_RenderCopy(ren_.get(), textures_.at(game.getFont().getTextureId()).get(), &srcRect, &destRect); 377 SDL_RenderCopy(ren_.get(), textures_.at(game.getFont().getTextureId()).get(), &srcRect, &destRect);
338 } 378 }
339 } 379 }
@@ -358,7 +398,7 @@ void Renderer::render(Game& game) {
358 8, 398 8,
359 8 }; 399 8 };
360 400
361 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 401 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
362 SDL_RenderCopy(ren_.get(), textures_.at(choiceArrowTex_).get(), nullptr, &destRect); 402 SDL_RenderCopy(ren_.get(), textures_.at(choiceArrowTex_).get(), nullptr, &destRect);
363 } else { 403 } else {
364 if (advMsgArrowTex_ == -1) { 404 if (advMsgArrowTex_ == -1) {
@@ -371,7 +411,7 @@ void Renderer::render(Game& game) {
371 8, 411 8,
372 8 }; 412 8 };
373 413
374 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 414 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
375 SDL_RenderCopy(ren_.get(), textures_.at(advMsgArrowTex_).get(), nullptr, &destRect); 415 SDL_RenderCopy(ren_.get(), textures_.at(advMsgArrowTex_).get(), nullptr, &destRect);
376 } 416 }
377 } 417 }
@@ -379,7 +419,7 @@ void Renderer::render(Game& game) {
379 } 419 }
380 420
381 if (effects.isScreenFaded()) { 421 if (effects.isScreenFaded()) {
382 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 422 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
383 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); 423 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND);
384 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, effects.getScreenFadeProgress() * 255); 424 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, effects.getScreenFadeProgress() * 255);
385 SDL_RenderFillRect(ren_.get(), nullptr); 425 SDL_RenderFillRect(ren_.get(), nullptr);
@@ -387,7 +427,7 @@ void Renderer::render(Game& game) {
387 427
388 if (effects.isCircleTransitionActive()) { 428 if (effects.isCircleTransitionActive()) {
389 if (effects.getCircleTransitionProgress() == 1.0) { 429 if (effects.getCircleTransitionProgress() == 1.0) {
390 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 430 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
391 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); 431 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
392 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); 432 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
393 SDL_RenderClear(ren_.get()); 433 SDL_RenderClear(ren_.get());
@@ -427,13 +467,13 @@ void Renderer::render(Game& game) {
427 copyFromCamera.h = CANVAS_HEIGHT; 467 copyFromCamera.h = CANVAS_HEIGHT;
428 } 468 }
429 469
430 SDL_RenderCopy(ren_.get(), cameraTex.get(), &copyFromCamera, &copyFromCamera); 470 SDL_RenderCopy(ren_.get(), cameraTex_.get(), &copyFromCamera, &copyFromCamera);
431 471
432 int pinholeTexId = loadImageFromFile("../res/pinhole.png"); 472 int pinholeTexId = loadImageFromFile("../res/pinhole.png");
433 SDL_SetTextureBlendMode(textures_[pinholeTexId].get(), SDL_BLENDMODE_MOD); 473 SDL_SetTextureBlendMode(textures_[pinholeTexId].get(), SDL_BLENDMODE_MOD);
434 SDL_RenderCopy(ren_.get(), textures_[pinholeTexId].get(), nullptr, &circleSize); 474 SDL_RenderCopy(ren_.get(), textures_[pinholeTexId].get(), nullptr, &circleSize);
435 475
436 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 476 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
437 SDL_SetTextureBlendMode(circleEffectTex.get(), SDL_BLENDMODE_NONE); 477 SDL_SetTextureBlendMode(circleEffectTex.get(), SDL_BLENDMODE_NONE);
438 SDL_RenderCopy(ren_.get(), circleEffectTex.get(), nullptr, nullptr); 478 SDL_RenderCopy(ren_.get(), circleEffectTex.get(), nullptr, nullptr);
439 } 479 }
@@ -442,7 +482,7 @@ void Renderer::render(Game& game) {
442 if (game.getSystem<InputSystem>().isDebugConsoleOpen()) { 482 if (game.getSystem<InputSystem>().isDebugConsoleOpen()) {
443 // Not sure why I'm supposed to copy the cached texture to the screen 483 // Not sure why I'm supposed to copy the cached texture to the screen
444 // BEFORE rendering it, but we get flickering if we don't, so. 484 // BEFORE rendering it, but we get flickering if we don't, so.
445 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 485 SDL_SetRenderTarget(ren_.get(), cameraTex_.get());
446 SDL_RenderCopy(ren_.get(), debugConsoleTex_.get(), nullptr, &debugDestRect_); 486 SDL_RenderCopy(ren_.get(), debugConsoleTex_.get(), nullptr, &debugDestRect_);
447 487
448 if (!debugConsoleTex_ || cachedDebugText_ != game.getSystem<InputSystem>().getDebugText()) { 488 if (!debugConsoleTex_ || cachedDebugText_ != game.getSystem<InputSystem>().getDebugText()) {
@@ -494,54 +534,54 @@ void Renderer::render(Game& game) {
494 debugDestRect_ = SDL_Rect { 0, 0, CANVAS_WIDTH, height }; 534 debugDestRect_ = SDL_Rect { 0, 0, CANVAS_WIDTH, height };
495 } 535 }
496 } 536 }
537}
497 538
498 if (menus.isMenuOpen()) { 539void Renderer::renderMenu(Game& game) {
499 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 540 auto& menus = game.getSystem<MenuSystem>();
500 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); 541
501 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); 542 if (!menuTex_) {
502 SDL_RenderFillRect(ren_.get(), nullptr); 543 menuTex_.reset(SDL_CreateTexture(
544 ren_.get(),
545 SDL_PIXELFORMAT_RGBA8888,
546 SDL_TEXTUREACCESS_TARGET,
547 CANVAS_WIDTH,
548 CANVAS_HEIGHT));
549
550 if (!menuTex_) {
551 throw sdl_error();
552 }
553 }
503 554
504 const int lineHeight = 16; 555 SDL_SetRenderTarget(ren_.get(), menuTex_.get());
505 int totalHeight = menus.getMenu().size() * lineHeight; 556 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
557 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
558 SDL_RenderFillRect(ren_.get(), nullptr);
506 559
507 int index = 0; 560 const int lineHeight = 16;
508 for (const MenuItem& menuItem : menus.getMenu()) { 561 int totalHeight = menus.getMenu().size() * lineHeight;
509 switch (menuItem.type) {
510 case MenuType::Command: {
511 MessageCache output;
512 renderMessageLine(output, menuItem.text, game);
513 562
514 SDL_Rect dest { 563 int index = 0;
515 (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, 564 for (const MenuItem& menuItem : menus.getMenu()) {
516 (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index, 565 switch (menuItem.type) {
517 MESSAGE_TEXT_WIDTH, 566 case MenuType::Command: {
518 game.getFont().getCharacterHeight() 567 MessageCache output;
519 }; 568 renderMessageLine(output, menuItem.text, game);
569
570 SDL_Rect dest {
571 (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2,
572 (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index,
573 MESSAGE_TEXT_WIDTH,
574 game.getFont().getCharacterHeight()
575 };
520 576
521 SDL_SetRenderTarget(ren_.get(), cameraTex.get()); 577 SDL_SetRenderTarget(ren_.get(), menuTex_.get());
522 SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest); 578 SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest);
523 579
524 break; 580 break;
525 }
526 } 581 }
527 index++;
528 } 582 }
529 } else if (menus.getPauseAnimationProgress() > 0.0) { 583 index++;
530 SDL_SetRenderTarget(ren_.get(), cameraTex.get());
531 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
532 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255);
533
534 int barHeight = CANVAS_HEIGHT / 2 * menus.getPauseAnimationProgress();
535 SDL_Rect topHalf { 0, 0, CANVAS_WIDTH, barHeight };
536 SDL_Rect bottomHalf { 0, CANVAS_HEIGHT - barHeight, CANVAS_WIDTH, barHeight };
537
538 SDL_RenderFillRect(ren_.get(), &topHalf);
539 SDL_RenderFillRect(ren_.get(), &bottomHalf);
540 } 584 }
541
542 SDL_SetRenderTarget(ren_.get(), nullptr);
543 SDL_RenderCopy(ren_.get(), cameraTex.get(), nullptr, nullptr);
544 SDL_RenderPresent(ren_.get());
545} 585}
546 586
547int Renderer::loadImageFromFile(std::string filename) { 587int Renderer::loadImageFromFile(std::string filename) {
diff --git a/src/renderer.h b/src/renderer.h index 4c6ccc4..75bd9fe 100644 --- a/src/renderer.h +++ b/src/renderer.h
@@ -134,6 +134,16 @@ private:
134 std::vector<texture_ptr> textures_; 134 std::vector<texture_ptr> textures_;
135 std::map<std::string, int> filenameToTexId_; 135 std::map<std::string, int> filenameToTexId_;
136 136
137 // Menu rendering
138 void renderMenu(Game& game);
139
140 texture_ptr menuTex_;
141
142 // Gameplay rendering
143 void renderGame(Game& game);
144
145 texture_ptr cameraTex_;
146
137 // Sprite rendering 147 // Sprite rendering
138 void renderSprite(const Sprite& sprite); 148 void renderSprite(const Sprite& sprite);
139 149