Skip to content
Snippets Groups Projects

Ui components

3 files
+ 71
44
Compare changes
  • Side-by-side
  • Inline

Files

+ 54
39
#include "Recruitingmenu.hpp"
#include <iostream>
#include <SDL_ttf.h>
#include "../Unit.hpp"
namespace advanced_wars
{
RecruitingMenu::RecruitingMenu() : m_selectedOption(0) {
RecruitingMenu::RecruitingMenu() : m_selectedOption(0), unitNames({
{UnitId::INFANTERY, {"Infantry", 100}},
{UnitId::MECHANIZED_INFANTERY, {"Bazooka", 200}},
{UnitId::RECON, {"Recon", 300}},
{UnitId::APC, {"APC", 400}},
{UnitId::ARTILLERY, {"Artillery", 500}},
{UnitId::ANTI_AIR_TANK, {"AA Tank", 600}},
{UnitId::ANTI_AIR_MISSILE_LAUNCHER, {"Rocket AA", 700}},
{UnitId::ROCKET_ARTILLERY, {"MLRS", 800}},
{UnitId::MEDIUM_TANK, {"Medium Tank", 900}},
{UnitId::NEO_TANK, {"Neo Tank", 1000}},
{UnitId::HEAVY_TANK, {"Heavy Tank", 1100}},
{UnitId::LANDER, {"Lander", 1200}},
{UnitId::CRUISER, {"Cruiser", 1300}},
{UnitId::SUBMARINE, {"Submarine", 1400}},
{UnitId::BATTLESHIP, {"Battleship", 1500}},
{UnitId::TRANSPORT_HELICOPTER, {"Chinook", 1600}},
{UnitId::BATTLE_HELICOPTER, {"Helicopter", 1700}},
{UnitId::FIGHTER, {"Fighter", 1800}},
{UnitId::BOMBER, {"Bomber", 1900}}
}) {
}
void RecruitingMenu::setOptions(const std::vector<UnitId> recruitableUnits) {
std::vector<std::string> options;
std::vector<std::pair<std::string, int>> options;
for (UnitId id : recruitableUnits) {
options.push_back(unitIdToString(id));
options.push_back(unitNames.at(id));
cost2UnitId.insert(std::make_pair(unitNames.at(id).second, id));
}
m_options = options;
m_selectedOption = 0;
}
std::string RecruitingMenu::unitIdToString(UnitId id) {
static const std::unordered_map<UnitId, std::string> unitNames = {
{UnitId::INFANTERY, "Infantry"},
{UnitId::MECHANIZED_INFANTERY, "Mechanized Infantry"},
{UnitId::RECON, "Recon"},
{UnitId::APC, "APC"},
{UnitId::ARTILLERY, "Artillery"},
{UnitId::ANTI_AIR_TANK, "Anti-Air Tank"},
{UnitId::ANTI_AIR_MISSILE_LAUNCHER, "Missile Launcher"},
{UnitId::ROCKET_ARTILLERY, "Rocket Artillery"},
{UnitId::MEDIUM_TANK, "Medium Tank"},
{UnitId::NEO_TANK, "Neo Tank"},
{UnitId::HEAVY_TANK, "Heavy Tank"},
{UnitId::LANDER, "Lander"},
{UnitId::CRUISER, "Cruiser"},
{UnitId::SUBMARINE, "Submarine"},
{UnitId::BATTLESHIP, "Battleship"},
{UnitId::TRANSPORT_HELICOPTER, "Transport Helicopter"},
{UnitId::BATTLE_HELICOPTER, "Battle Helicopter"},
{UnitId::FIGHTER, "Fighter"},
{UnitId::BOMBER, "Bomber"}
};
}
void RecruitingMenu::render(Engine& engine)
{
@@ -78,15 +75,18 @@ namespace advanced_wars
int spacing = 20; // Abstand zwischen den Optionen
// box around options
SDL_SetRenderDrawColor(engine.renderer(), 0, 0, 255, 255);
SDL_Rect box = {m_x, m_y - 3, 125, static_cast<int>(m_options.size() * spacing)};
SDL_Rect box = {m_x, m_y - 3, 150, static_cast<int>(m_options.size() * spacing)};
SDL_RenderFillRect(engine.renderer(), &box);
SDL_SetRenderDrawColor(engine.renderer(), 0, 0, 0, 255);
int i = 0;
for (size_t i = 0; i < m_options.size(); ++i)
for (auto& [render_name, cost] : m_options)
{
//std::pair<std::string, int> unit_option = unitNames.at(cost2UnitId.at(cost));
SDL_Surface* textSurface = TTF_RenderText_Solid(
font, m_options[i].c_str(), (i == m_selectedOption) ? yellow : white);
font, render_name.c_str(), (i == m_selectedOption) ? yellow : white);
if (!textSurface)
{
continue;
@@ -100,7 +100,7 @@ namespace advanced_wars
SDL_Texture* unit_texture = spritesheet->getUnitTextures()
.at(static_cast<int>(UnitFaction::URED))
.at(static_cast<int>(UnitId::INFANTERY))
.at(static_cast<int>(cost2UnitId.at(cost)))
.at(static_cast<int>(UnitState::IDLE))
.first;
@@ -119,9 +119,29 @@ namespace advanced_wars
};
SDL_RenderCopy(engine.renderer(), unit_texture, &src_rect, &trgt_rect);
SDL_Surface* costSurface = TTF_RenderText_Solid(
font, std::to_string(cost).c_str(), (i == m_selectedOption) ? yellow : white);
if (!textSurface)
{
continue;
}
SDL_Texture* costTexture = SDL_CreateTextureFromSurface(engine.renderer(), costSurface);
SDL_Rect cost_rect {
m_x + 120 ,
m_y + static_cast<int>(i * spacing),
costSurface->w,
costSurface->h
};
SDL_RenderCopy(engine.renderer(), costTexture, nullptr, &cost_rect);
SDL_DestroyTexture(costTexture);
SDL_FreeSurface(costSurface);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
i++;
}
TTF_CloseFont(font);
@@ -143,11 +163,6 @@ void RecruitingMenu::handleEvent(Engine& engine, SDL_Event& event)
}
}
std::string RecruitingMenu::getSelectedOption()
{
return m_options[m_selectedOption];
}
void RecruitingMenu::update(int x, int y)
{
this->m_x = x;
Loading