Asset.hpp 1001 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include <unordered_map>
  3. #include <SFML/Graphics.hpp>
  4. #include "Log.hpp"
  5. class Asset {
  6. public:
  7. static const sf::Texture& texture(std::string str) {
  8. if(s_tex.find(str) == s_tex.end()) {
  9. if(!s_tex[str].loadFromFile(str)) {
  10. Log(ERROR, "Unable to load ", str);
  11. exit(-1);
  12. }
  13. }
  14. return s_tex[str];
  15. }
  16. static const sf::SoundBuffer& sound(std::string str) {
  17. if(s_snd.find(str) == s_snd.end()) {
  18. if(!s_snd[str].loadFromFile(str)) {
  19. Log(ERROR, "Unable to load ", str);
  20. exit(-1);
  21. }
  22. }
  23. return s_snd[str];
  24. }
  25. static const sf::Font& font(std::string str) {
  26. if(s_fnt.find(str) == s_fnt.end()) {
  27. if(!s_fnt[str].loadFromFile(str)) {
  28. Log(ERROR, "Unable to load ", str);
  29. exit(-1);
  30. }
  31. }
  32. return s_fnt[str];
  33. }
  34. private:
  35. static std::unordered_map<std::string, sf::Texture> s_tex;
  36. static std::unordered_map<std::string, sf::SoundBuffer> s_snd;
  37. static std::unordered_map<std::string, sf::Font> s_fnt;
  38. };