Asset.hpp 1020 B

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