diff options
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 | } |
