diff options
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r-- | src/renderer.cpp | 133 |
1 files changed, 130 insertions, 3 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index b90372e..1aa5cbf 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -16,8 +16,6 @@ Renderer::Renderer() | |||
16 | GAME_HEIGHT, | 16 | GAME_HEIGHT, |
17 | SDL_WINDOW_SHOWN)); | 17 | SDL_WINDOW_SHOWN)); |
18 | 18 | ||
19 | //SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); | ||
20 | |||
21 | if (!win_) | 19 | if (!win_) |
22 | { | 20 | { |
23 | throw sdl_error(); | 21 | throw sdl_error(); |
@@ -111,6 +109,7 @@ Renderer::Renderer() | |||
111 | loadTextureFromFile("../res/runninbloods.png", tileset_); | 109 | loadTextureFromFile("../res/runninbloods.png", tileset_); |
112 | loadTextureFromFile("../res/lamp.png", lamp_); | 110 | loadTextureFromFile("../res/lamp.png", lamp_); |
113 | loadTextureFromFile("../res/read_instruction.png", readInstruction_); | 111 | loadTextureFromFile("../res/read_instruction.png", readInstruction_); |
112 | loadTextureFromFile("../res/menu.png", menuBg_); | ||
114 | 113 | ||
115 | loadTextureFromFile("../res/title0.png", titles_[0]); | 114 | loadTextureFromFile("../res/title0.png", titles_[0]); |
116 | SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); | 115 | SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); |
@@ -394,6 +393,20 @@ void Renderer::renderGame( | |||
394 | 393 | ||
395 | SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); | 394 | SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr); |
396 | 395 | ||
396 | texture_ptr overlays( | ||
397 | SDL_CreateTexture( | ||
398 | ren_.get(), | ||
399 | SDL_PIXELFORMAT_RGBA8888, | ||
400 | SDL_TEXTUREACCESS_TARGET, | ||
401 | GAME_WIDTH, | ||
402 | GAME_HEIGHT)); | ||
403 | |||
404 | SDL_SetTextureBlendMode(overlays.get(), SDL_BLENDMODE_BLEND); | ||
405 | SDL_SetRenderTarget(ren_.get(), overlays.get()); | ||
406 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); | ||
407 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | ||
408 | SDL_RenderClear(ren_.get()); | ||
409 | |||
397 | if (game.signInstructionState != SignInstructionState::Hidden) { | 410 | if (game.signInstructionState != SignInstructionState::Hidden) { |
398 | int instOpacity = 255; | 411 | int instOpacity = 255; |
399 | if (game.signInstructionState == SignInstructionState::FadingIn) { | 412 | if (game.signInstructionState == SignInstructionState::FadingIn) { |
@@ -444,7 +457,7 @@ void Renderer::renderGame( | |||
444 | srcRect.w, | 457 | srcRect.w, |
445 | srcRect.h }; | 458 | srcRect.h }; |
446 | 459 | ||
447 | SDL_SetRenderTarget(ren_.get(), nullptr); | 460 | SDL_SetRenderTarget(ren_.get(), overlays.get()); |
448 | SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect); | 461 | SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect); |
449 | //std::cout << line.charsRevealed << " (" << messageLines_[lineIndex].charIndexToWidth[line.charsRevealed] << "): " << messageLines_[lineIndex].line.substr(0,line.charsRevealed) << std::endl; | 462 | //std::cout << line.charsRevealed << " (" << messageLines_[lineIndex].charIndexToWidth[line.charsRevealed] << "): " << messageLines_[lineIndex].line.substr(0,line.charsRevealed) << std::endl; |
450 | } | 463 | } |
@@ -455,6 +468,34 @@ void Renderer::renderGame( | |||
455 | } | 468 | } |
456 | } | 469 | } |
457 | 470 | ||
471 | if (game.menu.menuState != MenuState::Closed) { | ||
472 | if (!menu_ || menuSelected_ != game.menu.cursor) { | ||
473 | renderMenu(game); | ||
474 | } | ||
475 | |||
476 | SDL_Rect rect { | ||
477 | 0, 0, GAME_WIDTH, GAME_HEIGHT | ||
478 | }; | ||
479 | |||
480 | int opacity = 128; | ||
481 | if (game.menu.menuState == MenuState::Opening) { | ||
482 | opacity = game.menu.menuDisplayProgress.getProgress(0, 128); | ||
483 | rect.y = game.menu.menuDisplayProgress.getProgress(-GAME_HEIGHT, 0); | ||
484 | } else if (game.menu.menuState == MenuState::Closing) { | ||
485 | opacity = game.menu.menuDisplayProgress.getProgress(128, 0); | ||
486 | rect.y = game.menu.menuDisplayProgress.getProgress(0, -GAME_HEIGHT); | ||
487 | } | ||
488 | |||
489 | SDL_SetRenderTarget(ren_.get(), overlays.get()); | ||
490 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); | ||
491 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, opacity); | ||
492 | SDL_RenderFillRect(ren_.get(), nullptr); | ||
493 | |||
494 | SDL_RenderCopy(ren_.get(), menu_.get(), nullptr, &rect); | ||
495 | } | ||
496 | |||
497 | SDL_SetRenderTarget(ren_.get(), nullptr); | ||
498 | SDL_RenderCopy(ren_.get(), overlays.get(), nullptr, nullptr); | ||
458 | SDL_RenderPresent(ren_.get()); | 499 | SDL_RenderPresent(ren_.get()); |
459 | } | 500 | } |
460 | 501 | ||
@@ -550,3 +591,89 @@ void Renderer::renderMessageLine(MessageCache& line, const std::string& text, co | |||
550 | SDL_RenderCopy(ren_.get(), lineTex.get(), nullptr, &rect); | 591 | SDL_RenderCopy(ren_.get(), lineTex.get(), nullptr, &rect); |
551 | } | 592 | } |
552 | 593 | ||
594 | void Renderer::renderMenu(const Game& game) { | ||
595 | int maxTextWidth = 0; | ||
596 | for (const MenuItem& item : game.menu.items) { | ||
597 | int width = 0; | ||
598 | TTF_SizeText(font_.get(), item.text.c_str(), &width, nullptr); | ||
599 | if (width > maxTextWidth) { | ||
600 | maxTextWidth = width; | ||
601 | } | ||
602 | } | ||
603 | |||
604 | menuWidth_ = maxTextWidth + MENU_PADDING * 2; | ||
605 | menuHeight_ = (TTF_FontHeight(font_.get()) + 1) * game.menu.items.size() - 1 + MENU_PADDING * 2; | ||
606 | |||
607 | menu_.reset(SDL_CreateTexture( | ||
608 | ren_.get(), | ||
609 | SDL_PIXELFORMAT_RGBA8888, | ||
610 | SDL_TEXTUREACCESS_TARGET, | ||
611 | GAME_WIDTH, | ||
612 | GAME_HEIGHT)); | ||
613 | |||
614 | SDL_SetRenderTarget(ren_.get(), menu_.get()); | ||
615 | SDL_SetTextureBlendMode(menu_.get(), SDL_BLENDMODE_BLEND); | ||
616 | SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); | ||
617 | SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); | ||
618 | SDL_RenderClear(ren_.get()); | ||
619 | |||
620 | SDL_Color bgColor {.r = 55, .g = 55, .b = 87, .a = 255}; | ||
621 | SDL_Color fgColor {.r = 146, .g = 146, .b = 146, .a = 255}; | ||
622 | SDL_Color selectedColor {.r = 255, .g = 255, .b = 255, .a = 255}; | ||
623 | |||
624 | /*{ | ||
625 | SDL_Rect drawRect { | ||
626 | (GAME_WIDTH - menuWidth_) / 2 - MENU_BORDER, | ||
627 | (GAME_HEIGHT - menuHeight_) / 2 - MENU_BORDER, | ||
628 | menuWidth_ + MENU_BORDER * 2, | ||
629 | menuHeight_ + MENU_BORDER * 2 | ||
630 | }; | ||
631 | |||
632 | SDL_SetRenderDrawColor(ren_.get(), 200, 200, 200, 255); | ||
633 | SDL_RenderFillRect(ren_.get(), &drawRect); | ||
634 | } | ||
635 | |||
636 | { | ||
637 | SDL_Rect drawRect { | ||
638 | (GAME_WIDTH - menuWidth_) / 2, | ||
639 | (GAME_HEIGHT - menuHeight_) / 2, | ||
640 | menuWidth_, | ||
641 | menuHeight_ | ||
642 | }; | ||
643 | |||
644 | SDL_SetRenderDrawColor(ren_.get(), bgColor.r, bgColor.g, bgColor.b, 255); | ||
645 | SDL_RenderFillRect(ren_.get(), &drawRect); | ||
646 | |||
647 | std::cout << "(" << drawRect.x << "," << drawRect.y << "),(" << drawRect.w << "," << drawRect.h << ")" << std::endl; | ||
648 | }*/ | ||
649 | |||
650 | SDL_RenderCopy(ren_.get(), menuBg_.get(), nullptr, nullptr); | ||
651 | |||
652 | for (int i=0; i < game.menu.items.size(); i++) { | ||
653 | const MenuItem& item = game.menu.items.at(i); | ||
654 | int width = 0; | ||
655 | TTF_SizeText(font_.get(), item.text.c_str(), &width, nullptr); | ||
656 | |||
657 | surface_ptr lineSurf = surface_ptr(TTF_RenderText_Shaded(font_.get(), item.text.c_str(), game.menu.cursor == i ? selectedColor : fgColor, bgColor)); | ||
658 | texture_ptr lineTex = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), lineSurf.get())); | ||
659 | |||
660 | SDL_Rect drawRect { | ||
661 | (GAME_WIDTH - width) / 2, | ||
662 | (GAME_HEIGHT - menuHeight_) / 2 + MENU_PADDING + (TTF_FontHeight(font_.get()) + 1) * i, | ||
663 | width, | ||
664 | TTF_FontHeight(font_.get()) | ||
665 | }; | ||
666 | SDL_RenderCopy(ren_.get(), lineTex.get(), nullptr, &drawRect); | ||
667 | } | ||
668 | |||
669 | menuSelected_ = game.menu.cursor; | ||
670 | } | ||
671 | |||
672 | void Renderer::toggleFullscreen() { | ||
673 | isFullscreen = !isFullscreen; | ||
674 | if (isFullscreen) { | ||
675 | SDL_SetWindowFullscreen(win_.get(), SDL_WINDOW_FULLSCREEN_DESKTOP); | ||
676 | } else { | ||
677 | SDL_SetWindowFullscreen(win_.get(), 0); | ||
678 | } | ||
679 | } | ||