diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2021-03-13 13:10:47 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2021-03-13 15:34:50 -0500 |
commit | 8a7d87a312b3dc42877577e99533c96d48714368 (patch) | |
tree | d68d614ba817237e347e0982afdae17b7b54e197 /src/renderer.cpp | |
parent | fce37403bbc29521b2b5bd983291b3730f8ad7b4 (diff) | |
download | tanetane-8a7d87a312b3dc42877577e99533c96d48714368.tar.gz tanetane-8a7d87a312b3dc42877577e99533c96d48714368.tar.bz2 tanetane-8a7d87a312b3dc42877577e99533c96d48714368.zip |
Added sliders to the settings menu
#7
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r-- | src/renderer.cpp | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index f7644ca..b28e3cb 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -1,4 +1,5 @@ | |||
1 | #include "renderer.h" | 1 | #include "renderer.h" |
2 | #include <deque> | ||
2 | #include <iostream> | 3 | #include <iostream> |
3 | #include "consts.h" | 4 | #include "consts.h" |
4 | #include "game.h" | 5 | #include "game.h" |
@@ -570,11 +571,33 @@ void Renderer::renderMenu(Game& game, const Menu& menu) { | |||
570 | SDL_RenderFillRect(ren_.get(), nullptr); | 571 | SDL_RenderFillRect(ren_.get(), nullptr); |
571 | 572 | ||
572 | const int lineHeight = 16; | 573 | const int lineHeight = 16; |
574 | const int sliderBarWidth = 9; | ||
573 | int totalHeight = menu.getItems().size() * lineHeight; | 575 | int totalHeight = menu.getItems().size() * lineHeight; |
574 | std::vector<vec2i> positions; | 576 | std::vector<vec2i> positions; |
577 | std::deque<MessageCache> sliderText; | ||
578 | int maxSliderLineWidth = 0; | ||
579 | int sliderAlignX = 0; | ||
575 | 580 | ||
581 | // First, find all of the sliders so we can figure out how to align them. | ||
582 | for (const MenuItem& menuItem : menu.getItems()) { | ||
583 | if (menuItem.type == MenuType::Slider) { | ||
584 | MessageCache output; | ||
585 | renderMessageLine(output, menuItem.text, game); | ||
586 | |||
587 | int width = output.charIndexToWidth.back() + sliderBarWidth * menuItem.maxValue; | ||
588 | if (width > maxSliderLineWidth) { | ||
589 | maxSliderLineWidth = width; | ||
590 | sliderAlignX = (CANVAS_WIDTH - maxSliderLineWidth) / 2 + output.charIndexToWidth.back(); | ||
591 | } | ||
592 | |||
593 | sliderText.push_back(std::move(output)); | ||
594 | } | ||
595 | } | ||
596 | |||
597 | // Now, render all of the items. | ||
576 | int index = 0; | 598 | int index = 0; |
577 | for (const MenuItem& menuItem : menu.getItems()) { | 599 | for (const MenuItem& menuItem : menu.getItems()) { |
600 | int lineY = (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index; | ||
578 | switch (menuItem.type) { | 601 | switch (menuItem.type) { |
579 | case MenuType::Command: { | 602 | case MenuType::Command: { |
580 | MessageCache output; | 603 | MessageCache output; |
@@ -582,7 +605,24 @@ void Renderer::renderMenu(Game& game, const Menu& menu) { | |||
582 | 605 | ||
583 | SDL_Rect dest { | 606 | SDL_Rect dest { |
584 | (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, | 607 | (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, |
585 | (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index, | 608 | lineY, |
609 | MESSAGE_TEXT_WIDTH, | ||
610 | game.getFont().getCharacterHeight() | ||
611 | }; | ||
612 | |||
613 | SDL_SetRenderTarget(ren_.get(), menuTex_.get()); | ||
614 | SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest); | ||
615 | |||
616 | positions.emplace_back(dest.x, dest.y); | ||
617 | |||
618 | break; | ||
619 | } | ||
620 | case MenuType::Slider: { | ||
621 | MessageCache& output = sliderText.front(); | ||
622 | |||
623 | SDL_Rect dest { | ||
624 | sliderAlignX - output.charIndexToWidth.back(), | ||
625 | lineY, | ||
586 | MESSAGE_TEXT_WIDTH, | 626 | MESSAGE_TEXT_WIDTH, |
587 | game.getFont().getCharacterHeight() | 627 | game.getFont().getCharacterHeight() |
588 | }; | 628 | }; |
@@ -592,6 +632,19 @@ void Renderer::renderMenu(Game& game, const Menu& menu) { | |||
592 | 632 | ||
593 | positions.emplace_back(dest.x, dest.y); | 633 | positions.emplace_back(dest.x, dest.y); |
594 | 634 | ||
635 | for (int j = 0; j < menuItem.maxValue; j++) { | ||
636 | int boxTexId = menuItem.value > j ? loadImageFromFile("../res/slider_on.png") : loadImageFromFile("../res/slider_off.png"); | ||
637 | const SDL_Rect boxDest { | ||
638 | sliderAlignX + j * sliderBarWidth, | ||
639 | lineY + 2, | ||
640 | 8, | ||
641 | 8 }; | ||
642 | SDL_SetRenderTarget(ren_.get(), menuTex_.get()); | ||
643 | SDL_RenderCopy(ren_.get(), textures_.at(boxTexId).get(), nullptr, &boxDest); | ||
644 | } | ||
645 | |||
646 | sliderText.pop_front(); | ||
647 | |||
595 | break; | 648 | break; |
596 | } | 649 | } |
597 | } | 650 | } |