Nimbin[12]?SDL & Graphics / SDL_Game_CPP / src/defaults/icons.cpp

SDL_Game_CPP git · main

SDL game in C++

sdl game c++ · first commit 2026-03-24 · last commit 2026-03-24 (8 months ago) · synced 1 hour ago · upstream: codeberg.org/Studying_at_HsNR/SDL_Game_CPP

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/SDL_Game_CPP.gitwget https://git.christianimmanuel.de/sdl-graphics/SDL_Game_CPP/archive/SDL_Game_CPP.tar.gz
src/defaults/icons.cpp 2.2 KB · 80 lines raw
#include "defaults/icons.hpp"

#include <SDL3_image/SDL_image.h>

#include <iostream>

SDL_Texture* Icon::icons_texture = nullptr;

std::unordered_map<ResourceType, Icon::TilePos> Icon::resource_pos = {
    { ResourceType::WOOD, {0, 0} },
    { ResourceType::STONE,{1, 0} },
    { ResourceType::CLAY, {2, 0} },
    { ResourceType::IRON, {3, 0} }
    // the rest filled in init()
};

std::unordered_map<ItemType, Icon::TilePos> Icon::item_pos = {
    { ItemType::SWORD_WOOD,    {7, 0} },
    { ItemType::SWORD_STONE,   {7, 0} },
    { ItemType::PICKAXE_STONE, {16,0} },
    { ItemType::WOODAXE_STONE, {16,1} },
    { ItemType::BUILDING_TOOL, {15,0} },
    { ItemType::CULTIVATOR,    {15,1} }
    // the rest filled in init()
};

std::unordered_map<BuildingType, Icon::TilePos> Icon::building_pos = {
    { BuildingType::RESEARCH_WOOD_1,  {11, 0} },
    { BuildingType::CRAFTING_WOOD_1,  {12, 0} },
    { BuildingType::RESEARCH_STONE_1, {11, 1} },
    { BuildingType::CRAFTING_STONE_1, {12, 1} },
    { BuildingType::RESEARCH_IRON_1,  {11, 2} },
    { BuildingType::CRAFTING_IRON_1,  {12, 2} },
    // the rest filled in init()
};

std::unordered_map<IconType, Icon::TilePos> Icon::icon_pos = {
    { IconType::CHECKMARK,  {0, 24} },
};

bool
Icon::Init(SDL_Renderer* renderer) {
    const char* file = "Pixel_Crawler/Icons/Icons.png";
    if(!renderer || !file) {
        std::cerr << "MISSING ICONS.png\n";
        return false;
    }

    icons_texture = IMG_LoadTexture(renderer, file);
    if(!icons_texture) {
        std::cout << "Failed to create texture!\n";
        return false;
    }

    // defaults for resources
    for(int i = (int)ResourceType::NONE; i < (int)ResourceType::LAST; ++i) {
        ResourceType t = (ResourceType)i;
        if(resource_pos.find(t) == resource_pos.end()) {
            resource_pos[t] = {0, 1};
        }
    }

    // defaults for items
    for(int i = (int)ItemType::SWORD_WOOD; i <= (int)ItemType::NONE; ++i) {
        ItemType t = (ItemType)i;
        if(item_pos.find(t) == item_pos.end()) {
            item_pos[t] = {0, 1};
        }
    }

    return true;
}

void
Icon::Quit() {
    if(icons_texture) {
        SDL_DestroyTexture(icons_texture);
        icons_texture = nullptr;
    }
}