/* * 2D Game Engine * AssetStore.cpp: * Based on pikuma.com 2D game engine in C++ and Lua course * Copyright (c) 2021 986-Studio. All rights reserved. * * Created by Manoƫl Trapier on 12/02/2021. */ #include #include #include #include #include void AssetStore::clearStore() { for(auto texture: this->textures) { SDL_DestroyTexture(texture.second); } this->textures.clear(); for(auto font: this->fonts) { TTF_CloseFont(font.second); } this->fonts.clear(); } void AssetStore::addTexture(SDL_Renderer *renderer, const std::string &assetId, const std::string &filePath) { SDL_Surface *surface = IMG_Load(filePath.c_str()); if (!surface) { Logger::Error("Error opening texture '%s' (path: '%s')", assetId.c_str(), filePath.c_str()); return; } SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); Logger::Info("New texture added to the Asset Store id=%s", assetId.c_str()); this->textures.emplace(assetId, texture); } SDL_Texture *AssetStore::getTexture(const std::string &assetId) { if (!this->textures[assetId]) { Logger::Warning("Assert '%s' requested but can't be found.", assetId.c_str()); } return this->textures[assetId]; } void AssetStore::addFont(const std::string &assetId, const std::string &filePath, uint32_t fontSize) { TTF_Font *font = TTF_OpenFont(filePath.c_str(), fontSize); if (!font) { Logger::Error("Error opening font '%s' (path: '%s') Error: %d", assetId.c_str(), filePath.c_str(), TTF_GetError); return; } this->fonts.emplace(assetId, font); } TTF_Font *AssetStore::getFont(const std::string &assetId) { return this->fonts[assetId]; }