Skip to content
Snippets Groups Projects
Commit 4abb8ad5 authored by Max Cherris's avatar Max Cherris
Browse files

Add correct sprite rendering for all unit types

parent 143fe0c1
No related branches found
No related tags found
2 merge requests!24Ui interaction/building,!21Ui components
......@@ -450,7 +450,18 @@ void Level::handleMenuActiveEvents(Engine& engine, SDL_Event& event)
m_recruitingMenu.update(
(tilePos.first * 16 + 15) * RENDERING_SCALE,
(tilePos.second * 16 + 15) * RENDERING_SCALE);
m_recruitingMenu.setOptions();
m_recruitingMenu.setOptions({
UnitId::INFANTERY,
UnitId::MECHANIZED_INFANTERY,
UnitId::RECON,
UnitId::APC,
UnitId::ARTILLERY,
UnitId::ANTI_AIR_TANK,
UnitId::ANTI_AIR_MISSILE_LAUNCHER,
UnitId::ROCKET_ARTILLERY,
UnitId::MEDIUM_TANK,
UnitId::NEO_TANK,
UnitId::HEAVY_TANK});
std::cout << "no training here" << std::endl;
}
}
......
#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;
......@@ -120,8 +120,28 @@ 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;
......
#pragma once
#include "../Scene.hpp"
#include "../Unit.hpp"
namespace advanced_wars
{
......@@ -10,9 +11,11 @@ namespace advanced_wars
private:
size_t m_selectedOption;
std::vector<std::string> m_options;
std::vector<std::pair<std::string, int>> m_options;
int m_x;
int m_y;
const std::unordered_map <UnitId ,std::pair <std::string, int>> unitNames;
std::unordered_map<int, UnitId> cost2UnitId;
void handleEvent(Engine& engine, SDL_Event& event);
......@@ -22,8 +25,6 @@ namespace advanced_wars
std::string getSelectedOption();
std::string unitIdToString(UnitId id);
public:
void update(int x, int y);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment