summaryrefslogtreecommitdiffhomepage
path: root/Icons.cpp
blob: f08787f3c3429f03d8386955f04d629c5881ec45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "Icons.h"

#include <string>
#include <unordered_map>
#include <utility>

#include <raylib.h>


static const std::unordered_map<long int, std::string> FILENAMES {
    {834, "resources/frigate_16.png"},
    {324, "resources/frigate_16.png"},
    {830, "resources/frigate_16.png"},
    {25, "resources/frigate_16.png"},
    {27, "resources/battleship_16.png"},
    {540, "resources/battleCruiser_16.png"},
    {1305, "resources/destroyer_16.png"},
    {420, "resources/destroyer_16.png"},
    {1534, "resources/destroyer_16.png"},
    {541, "resources/destroyer_16.png"},
    {29, "resources/capsule_16.png"},
    {361, "resources/mobileWarpDisruptor.png"},
    {963, "resources/cruiser_16.png"},
    {894, "resources/cruiser_16.png"},
    {26, "resources/cruiser_16.png"},
    {832, "resources/cruiser_16.png"},
    {906, "resources/cruiser_16.png"},
    {358, "resources/cruiser_16.png"},
    {237, "resources/rookie_16.png"},
    {1404, "resources/engineeringComplexLarge.png"},
};


Icons::Icons() :
    m_cache {},
    m_teams {}
{
}


Icons::~Icons()
{
    reset();
}


void
Icons::reset()
{
    for (const auto& [_, texture] : m_cache)
        UnloadTexture(texture);
    m_cache.clear();
    for (const auto& texture : m_teams)
        UnloadRenderTexture(texture);
    m_teams.clear();
}


void
Icons::draw_team_icons()
{
    if (!m_teams.empty())
        return;
    m_teams.reserve(2);
    for (const auto color : {Color{204, 8, 153, 255}, Color{20, 234, 106, 255}}) {
        auto texture = LoadRenderTexture(6, 6);
        BeginTextureMode(texture);
        DrawRectangle(0, 0, 6, 6, color);
        EndTextureMode();
        m_teams.push_back(std::move(texture));
    }
}


Texture2D
Icons::find(const long int group)
{
    const auto search = FILENAMES.find(group);
    std::string filename = "resources/wreck.png";
    if (search != FILENAMES.end())
        filename = search->second;
    const auto existing = m_cache.find(filename);
    if (existing != m_cache.end()) {
        return existing->second;
    }
    else {
        auto texture = LoadTexture(filename.data());
        m_cache[filename] = texture;
        return texture;
    }
}


Texture2D
Icons::team_icon(const int team)
{
    auto& render = m_teams.at(team);
    return render.texture;
}