Skip to content
Snippets Groups Projects
Select Git revision
  • ffe77f0c6458b152b1afb4eab56e41fe2580b150
  • main default protected
  • fortran
  • usertypes
  • must-toolcoverage
  • toolcoverage
  • tools
  • must-json
  • merged
  • tools-parallel
  • coll
  • rma
  • dtypes
  • p2p
  • infrastructure-patch-3
  • infrastructure-patch2
  • devel-TJ
  • infrasructure-patch-1
  • devel-ES
  • instructionblock-lists
  • mbi
21 results

ParamMatching.py

Blame
  • RecruitingMenu.cpp 5.67 KiB
    /**
     * @authors Max Körschen
     * @author Nicolas Will
     */
    
    #include "RecruitingMenu.hpp"
    
    #include <SDL_ttf.h>
    #include <iostream>
    
    namespace advanced_wars
    {
    RecruitingMenu::RecruitingMenu()
        : m_selectedOption(0), m_unitNames({
                                   {                UnitTypeId::INFANTERY,    "Infantry"},
                                   {     UnitTypeId::MECHANIZED_INFANTERY,     "Bazooka"},
                                   {                    UnitTypeId::RECON,       "Recon"},
                                   {                      UnitTypeId::APC,         "APC"},
                                   {                UnitTypeId::ARTILLERY,   "Artillery"},
                                   {            UnitTypeId::ANTI_AIR_TANK,     "AA Tank"},
                                   {UnitTypeId::ANTI_AIR_MISSILE_LAUNCHER,   "Rocket AA"},
                                   {         UnitTypeId::ROCKET_ARTILLERY,        "MLRS"},
                                   {              UnitTypeId::MEDIUM_TANK, "Medium Tank"},
                                   {                 UnitTypeId::NEO_TANK,    "Neo Tank"},
                                   {               UnitTypeId::HEAVY_TANK,  "Heavy Tank"},
                                   {                   UnitTypeId::LANDER,      "Lander"},
                                   {                  UnitTypeId::CRUISER,     "Cruiser"},
                                   {                UnitTypeId::SUBMARINE,   "Submarine"},
                                   {               UnitTypeId::BATTLESHIP,  "Battleship"},
                                   {     UnitTypeId::TRANSPORT_HELICOPTER,     "Chinook"},
                                   {        UnitTypeId::BATTLE_HELICOPTER,  "Helicopter"},
                                   {                  UnitTypeId::FIGHTER,     "Fighter"},
                                   {                   UnitTypeId::BOMBER,      "Bomber"}
    })
    {
    }
    
    void RecruitingMenu::setOptions(const std::vector<UnitTypeId> recruitableUnits)
    {
    
        m_options = recruitableUnits;
        m_selectedOption = 0;
    }
    
    void RecruitingMenu::render(Engine& engine)
    {
        Config&      config = engine.getUnitConfig();
        Spritesheet* spritesheet = engine.getSpritesheet();
    
        if (TTF_Init() == -1)
        {
            std::cerr << "Failed to initialize TTF: " << TTF_GetError() << "\n";
            return;
        }
    
        if (m_options.empty())
        {
            // TODO handle somehow
            return;
        }
    
        std::string basePath = SDL_GetBasePath();
        std::string relativePath = "res/ARCADECLASSIC.TTF";
        std::string fullPath = basePath + relativePath;
        TTF_Font*   font = TTF_OpenFont(fullPath.c_str(), 16);
        if (font == nullptr)
        {
            std::cerr << "Failed to load font: " << TTF_GetError() << "\n";
            return;
        }
    
        SDL_Color white = {255, 255, 255, 255};
        SDL_Color yellow = {192, 255, 0, 255};
    
        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, 175, static_cast<int>(m_options.size() * spacing)};
        SDL_RenderFillRect(engine.renderer(), &box);
    
        SDL_SetRenderDrawColor(engine.renderer(), 0, 0, 0, 255);
        int i = 0;
    
        for (UnitTypeId id : m_options)
        {
            // std::pair<std::string, int> unit_option = unitNames.at(cost2UnitId.at(cost));
            if (i == m_selectedOption)
            {
                m_selectedId = id;
            }
    
            SDL_Surface* textSurface = TTF_RenderText_Solid(
                font, m_unitNames.at(id).c_str(), (i == m_selectedOption) ? yellow : white);
            if (textSurface == nullptr)
            {
                continue;
            }
    
            SDL_Texture* textTexture = SDL_CreateTextureFromSurface(engine.renderer(), textSurface);
            SDL_Rect textRect = {m_x + 10 + 16, m_y + (i * spacing), textSurface->w, textSurface->h};
            SDL_RenderCopy(engine.renderer(), textTexture, nullptr, &textRect);
    
            SDL_Texture* unitTexture = spritesheet->getUnitTextures()
                                           .at(static_cast<int>(Faction::URED))
                                           .at(static_cast<int>(id))
                                           .at(static_cast<int>(UnitState::IDLE))
                                           .first;
    
            SDL_Rect targetRect = {m_x + 5, m_y + (i * spacing), 16, 16};
    
            SDL_Rect sourceRect = {5, 0, 10, 10};
    
            SDL_RenderCopy(engine.renderer(), unitTexture, &sourceRect, &targetRect);
    
            SDL_Surface* costSurface = TTF_RenderText_Solid(
                font, std::to_string(config.getUnitCost(id)).c_str(),
                (i == m_selectedOption) ? yellow : white);
            if (textSurface == nullptr)
            {
                continue;
            }
    
            SDL_Texture* costTexture = SDL_CreateTextureFromSurface(engine.renderer(), costSurface);
    
            SDL_Rect costRect{m_x + 120, m_y + (i * spacing), costSurface->w, costSurface->h};
            SDL_RenderCopy(engine.renderer(), costTexture, nullptr, &costRect);
    
            SDL_DestroyTexture(costTexture);
            SDL_FreeSurface(costSurface);
            SDL_DestroyTexture(textTexture);
            SDL_FreeSurface(textSurface);
            i++;
        }
    
        TTF_CloseFont(font);
        TTF_Quit();
    }
    
    void RecruitingMenu::handleEvent(Engine& /*engine*/, SDL_Event& event)
    {
        if (event.type == SDL_KEYDOWN)
        {
            if (event.key.keysym.sym == SDLK_DOWN)
            {
                m_selectedOption = (m_selectedOption + 1) % m_options.size();
            }
            else if (event.key.keysym.sym == SDLK_UP)
            {
                m_selectedOption = (m_selectedOption - 1 + m_options.size()) % m_options.size();
            }
        }
    }
    
    void RecruitingMenu::update(int x, int y)
    {
        this->m_x = x;
        this->m_y = y;
    }
    
    UnitTypeId RecruitingMenu::getSelectedOption()
    {
        return m_selectedId;
    }
    
    } // namespace advanced_wars