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

mapcreator git · master

SDL3 2.5D game and engine using assets, with map editor

sdl3 c++ game engine map-editor cmake · first commit 2025-10-31 · last commit 2026-09-07 (1 month ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-spieleentwicklung/map_creator

C++ 99.3%
git clone https://git.christianimmanuel.de/sdl-graphics/mapcreator.gitwget https://git.christianimmanuel.de/sdl-graphics/mapcreator/archive/mapcreator.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;
    }
}