60 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| #include "textures.hpp"
 | |
| #include <SFML/Graphics/Image.hpp>
 | |
| #include "dbc.hpp"
 | |
| #include <fmt/core.h>
 | |
| #include "config.hpp"
 | |
| #include "constants.hpp"
 | |
| #include <memory>
 | |
| 
 | |
| namespace textures {
 | |
|   using std::shared_ptr, std::make_shared;
 | |
| 
 | |
|   static TextureManager TMGR;
 | |
|   static bool initialized = false;
 | |
| 
 | |
|   void load_sprites() {
 | |
|     Config assets("assets/config.json");
 | |
| 
 | |
|     for(auto& [name, settings] : assets["sprites"].items()) {
 | |
|       auto texture = make_shared<sf::Texture>(settings["path"]);
 | |
| 
 | |
|       texture->setSmooth(assets["graphics"]["smooth_textures"]);
 | |
|       auto sprite = make_shared<sf::Sprite>(*texture);
 | |
| 
 | |
|       int width = settings["frame_width"];
 | |
|       int height = settings["frame_height"];
 | |
|       sprite->setTextureRect({{0,0}, {width, height}});
 | |
| 
 | |
|       TMGR.sprite_textures.try_emplace(name, sprite, texture);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   void init() {
 | |
|     if(!initialized) {
 | |
|       load_sprites();
 | |
|       initialized = true;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   SpriteTexture get(const std::string& name) {
 | |
|     dbc::check(initialized, "you forgot to call textures::init()");
 | |
|     dbc::check(TMGR.sprite_textures.contains(name),
 | |
|         fmt::format("!!!!! texture pack does not contain {} sprite", name));
 | |
| 
 | |
|     auto result = TMGR.sprite_textures.at(name);
 | |
| 
 | |
|     dbc::check(result.sprite != nullptr,
 | |
|         fmt::format("bad sprite from textures::get named {}", name));
 | |
|     dbc::check(result.texture != nullptr,
 | |
|         fmt::format("bad texture from textures::get named {}", name));
 | |
| 
 | |
|     return result;
 | |
|   }
 | |
| 
 | |
|   sf::Image load_image(const std::string& filename) {
 | |
|     sf::Image texture;
 | |
|     bool good = texture.loadFromFile(filename);
 | |
|     dbc::check(good, fmt::format("failed to load {}", filename));
 | |
|     return texture;
 | |
|   }
 | |
| };
 | 
