AssetStore.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * 2D Game Engine
  3. * AssetStore.cpp:
  4. * Based on pikuma.com 2D game engine in C++ and Lua course
  5. * Copyright (c) 2021 986-Studio. All rights reserved.
  6. *
  7. * Created by Manoël Trapier on 12/02/2021.
  8. */
  9. #include <SDL.h>
  10. #include <SDL_image.h>
  11. #include <Logger.h>
  12. #include <AssetStore.h>
  13. void AssetStore::clearStore()
  14. {
  15. for(auto texture: this->textures)
  16. {
  17. SDL_DestroyTexture(texture.second);
  18. }
  19. this->textures.clear();
  20. }
  21. void AssetStore::addTexture(SDL_Renderer *renderer, const std::string &assetId, const std::string &filePath)
  22. {
  23. SDL_Surface *surface = IMG_Load(filePath.c_str());
  24. if (!surface)
  25. {
  26. Logger::Error("Error opening texture '%s' (path: '%s')", assetId.c_str(), filePath.c_str());
  27. return;
  28. }
  29. SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
  30. SDL_FreeSurface(surface);
  31. Logger::Info("New texture added to the Asset Store id=%s", assetId.c_str());
  32. this->textures.emplace(assetId, texture);
  33. }
  34. SDL_Texture *AssetStore::getTexture(const std::string &assetId)
  35. {
  36. if (!this->textures[assetId])
  37. {
  38. Logger::Warning("Assert '%s' requested but can't be found.", assetId.c_str());
  39. }
  40. return this->textures[assetId];
  41. }