Textures and sprites are now managed by a single module in textures.hpp, and even though it is a _single_ location to access all sprites it is NOT a singleton. Those are illegal.
parent
f3e1413022
commit
0260e3d345
@ -1,70 +0,0 @@ |
||||
#include "texture.hpp" |
||||
#include <SFML/Graphics/Image.hpp> |
||||
#include "dbc.hpp" |
||||
#include <fmt/core.h> |
||||
#include "config.hpp" |
||||
#include "constants.hpp" |
||||
|
||||
using std::shared_ptr, std::make_shared; |
||||
|
||||
sf::Image TextureManager::load_image(std::string filename) { |
||||
sf::Image texture; |
||||
bool good = texture.loadFromFile(filename); |
||||
dbc::check(good, fmt::format("failed to load {}", filename)); |
||||
return texture; |
||||
} |
||||
|
||||
void TextureManager::load_sprites() { |
||||
Config assets("assets/config.json"); |
||||
|
||||
for(auto& el : assets["sprites"].items()) { |
||||
string path = el.value(); |
||||
auto texture = make_shared<sf::Texture>(path); |
||||
|
||||
texture->setSmooth(false); |
||||
auto sprite = make_shared<sf::Sprite>(*texture); |
||||
|
||||
string name = el.key(); |
||||
sprite_textures[name] = {sprite, texture}; |
||||
} |
||||
|
||||
floor = load_image(assets["sprites"]["floor"]); |
||||
ceiling = load_image(assets["sprites"]["ceiling"]); |
||||
} |
||||
|
||||
void TextureManager::load_tiles() { |
||||
Config assets("assets/tiles.json"); |
||||
auto &tiles = assets.json(); |
||||
|
||||
for(auto &el : tiles.items()) { |
||||
auto &config = el.value(); |
||||
surfaces.emplace_back(load_image(config["texture"])); |
||||
|
||||
std::wstring display = assets.wstring(el.key(), "display"); |
||||
int surface_i = surfaces.size() - 1; |
||||
wchar_t tid = display[0]; |
||||
|
||||
char_to_texture[tid] = surface_i; |
||||
} |
||||
} |
||||
|
||||
const uint32_t* TextureManager::get_surface(size_t num) { |
||||
return (const uint32_t *)surfaces[num].getPixelsPtr(); |
||||
} |
||||
|
||||
matrix::Matrix TextureManager::convert_char_to_texture(matrix::Matrix &tile_ids) { |
||||
auto result = matrix::make(matrix::width(tile_ids), matrix::height(tile_ids)); |
||||
|
||||
for(matrix::each_cell it(tile_ids); it.next();) { |
||||
wchar_t tid = tile_ids[it.y][it.x]; |
||||
result[it.y][it.x] = char_to_texture.at(tid); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
SpriteTexture TextureManager::get(std::string name) { |
||||
dbc::check(sprite_textures.contains(name), |
||||
fmt::format("!!!!! texture pack does not contain {} sprite", name)); |
||||
return sprite_textures.at(name); |
||||
} |
@ -1,31 +0,0 @@ |
||||
#pragma once |
||||
|
||||
#include <cstdint> |
||||
#include <vector> |
||||
#include <string> |
||||
#include <SFML/Graphics.hpp> |
||||
#include <unordered_map> |
||||
#include <memory> |
||||
#include "matrix.hpp" |
||||
|
||||
struct SpriteTexture { |
||||
std::shared_ptr<sf::Sprite> sprite = nullptr; |
||||
std::shared_ptr<sf::Texture> texture = nullptr; |
||||
}; |
||||
|
||||
struct TextureManager { |
||||
std::vector<sf::Image> surfaces; |
||||
std::unordered_map<std::string, SpriteTexture> sprite_textures; |
||||
std::unordered_map<wchar_t, int> char_to_texture; |
||||
sf::Image floor; |
||||
sf::Image ceiling; |
||||
|
||||
void load_tiles(); |
||||
void load_sprites(); |
||||
SpriteTexture get(std::string name); |
||||
sf::Image load_image(std::string filename); |
||||
const uint32_t* get_surface(size_t num); |
||||
|
||||
// ZED: this is ugly so maybe you should like rewrite it or something
|
||||
matrix::Matrix convert_char_to_texture(matrix::Matrix &from); |
||||
}; |
@ -0,0 +1,92 @@ |
||||
#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& el : assets["sprites"].items()) { |
||||
string path = el.value(); |
||||
auto texture = make_shared<sf::Texture>(path); |
||||
|
||||
texture->setSmooth(false); |
||||
auto sprite = make_shared<sf::Sprite>(*texture); |
||||
|
||||
string name = el.key(); |
||||
TMGR.sprite_textures[name] = {sprite, texture}; |
||||
} |
||||
|
||||
TMGR.floor = load_image(assets["sprites"]["floor"]); |
||||
TMGR.ceiling = load_image(assets["sprites"]["ceiling"]); |
||||
} |
||||
|
||||
void load_tiles() { |
||||
Config assets("assets/tiles.json"); |
||||
auto &tiles = assets.json(); |
||||
|
||||
for(auto &el : tiles.items()) { |
||||
auto &config = el.value(); |
||||
TMGR.surfaces.emplace_back(load_image(config["texture"])); |
||||
|
||||
std::wstring display = assets.wstring(el.key(), "display"); |
||||
int surface_i = TMGR.surfaces.size() - 1; |
||||
wchar_t tid = display[0]; |
||||
|
||||
TMGR.char_to_texture[tid] = surface_i; |
||||
} |
||||
} |
||||
|
||||
void init() { |
||||
if(!initialized) { |
||||
load_tiles(); |
||||
load_sprites(); |
||||
initialized = true; |
||||
} |
||||
} |
||||
|
||||
SpriteTexture get(std::string name) { |
||||
dbc::check(TMGR.sprite_textures.contains(name), |
||||
fmt::format("!!!!! texture pack does not contain {} sprite", name)); |
||||
return TMGR.sprite_textures.at(name); |
||||
} |
||||
|
||||
sf::Image load_image(std::string filename) { |
||||
sf::Image texture; |
||||
bool good = texture.loadFromFile(filename); |
||||
dbc::check(good, fmt::format("failed to load {}", filename)); |
||||
return texture; |
||||
} |
||||
|
||||
const uint32_t* get_surface(size_t num) { |
||||
return (const uint32_t *)TMGR.surfaces[num].getPixelsPtr(); |
||||
} |
||||
|
||||
matrix::Matrix convert_char_to_texture(matrix::Matrix &tile_ids) { |
||||
auto result = matrix::make(matrix::width(tile_ids), matrix::height(tile_ids)); |
||||
|
||||
for(matrix::each_cell it(tile_ids); it.next();) { |
||||
wchar_t tid = tile_ids[it.y][it.x]; |
||||
result[it.y][it.x] = TMGR.char_to_texture.at(tid); |
||||
} |
||||
|
||||
return result; |
||||
} |
||||
|
||||
const uint32_t* get_floor() { |
||||
return (const uint32_t *)TMGR.floor.getPixelsPtr(); |
||||
} |
||||
|
||||
const uint32_t* get_ceiling() { |
||||
return (const uint32_t *)TMGR.ceiling.getPixelsPtr(); |
||||
} |
||||
}; |
@ -0,0 +1,38 @@ |
||||
#pragma once |
||||
#include <cstdint> |
||||
#include <vector> |
||||
#include <string> |
||||
#include <SFML/Graphics.hpp> |
||||
#include <unordered_map> |
||||
#include <memory> |
||||
#include "matrix.hpp" |
||||
|
||||
namespace textures { |
||||
|
||||
struct SpriteTexture { |
||||
std::shared_ptr<sf::Sprite> sprite = nullptr; |
||||
std::shared_ptr<sf::Texture> texture = nullptr; |
||||
}; |
||||
|
||||
struct TextureManager { |
||||
std::vector<sf::Image> surfaces; |
||||
std::unordered_map<std::string, SpriteTexture> sprite_textures; |
||||
std::unordered_map<wchar_t, int> char_to_texture; |
||||
sf::Image floor; |
||||
sf::Image ceiling; |
||||
}; |
||||
|
||||
void init(); |
||||
|
||||
SpriteTexture get(std::string name); |
||||
|
||||
sf::Image load_image(std::string filename); |
||||
|
||||
const uint32_t* get_surface(size_t num); |
||||
|
||||
matrix::Matrix convert_char_to_texture(matrix::Matrix &from); |
||||
|
||||
const uint32_t* get_floor(); |
||||
|
||||
const uint32_t* get_ceiling(); |
||||
} |
@ -1,38 +0,0 @@ |
||||
#include "textures2.hpp" |
||||
|
||||
namespace textures { |
||||
static TextureManager textures; |
||||
static bool initialized = false; |
||||
|
||||
void init() { |
||||
if(!initialized) { |
||||
textures.load_tiles(); |
||||
textures.load_sprites(); |
||||
initialized = true; |
||||
} |
||||
} |
||||
|
||||
SpriteTexture get(std::string name) { |
||||
return textures.get(name); |
||||
} |
||||
|
||||
sf::Image load_image(std::string filename) { |
||||
return textures.load_image(filename); |
||||
} |
||||
|
||||
const uint32_t* get_surface(size_t num) { |
||||
return textures.get_surface(num); |
||||
} |
||||
|
||||
matrix::Matrix convert_char_to_texture(matrix::Matrix &from) { |
||||
return textures.convert_char_to_texture(from); |
||||
} |
||||
|
||||
const uint32_t* get_floor() { |
||||
return (const uint32_t *)textures.floor.getPixelsPtr(); |
||||
} |
||||
|
||||
const uint32_t* get_ceiling() { |
||||
return (const uint32_t *)textures.ceiling.getPixelsPtr(); |
||||
} |
||||
}; |
@ -1,18 +0,0 @@ |
||||
#pragma once |
||||
#include "texture.hpp" |
||||
|
||||
namespace textures { |
||||
void init(); |
||||
|
||||
SpriteTexture get(std::string name); |
||||
|
||||
sf::Image load_image(std::string filename); |
||||
|
||||
const uint32_t* get_surface(size_t num); |
||||
|
||||
matrix::Matrix convert_char_to_texture(matrix::Matrix &from); |
||||
|
||||
const uint32_t* get_floor(); |
||||
|
||||
const uint32_t* get_ceiling(); |
||||
} |
Loading…
Reference in new issue