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.

master
Zed A. Shaw 2 weeks ago
parent f3e1413022
commit 0260e3d345
  1. 2
      guecs.hpp
  2. 2
      main.cpp
  3. 3
      meson.build
  4. 2
      raycaster.cpp
  5. 4
      raycaster.hpp
  6. 6
      status_ui.cpp
  7. 2
      status_ui.hpp
  8. 2
      tests/guecs.cpp
  9. 2
      tests/textures.cpp
  10. 70
      texture.cpp
  11. 31
      texture.hpp
  12. 92
      textures.cpp
  13. 38
      textures.hpp
  14. 38
      textures2.cpp
  15. 18
      textures2.hpp

@ -5,7 +5,7 @@
#include <string>
#include <memory>
#include <SFML/Graphics.hpp>
#include "textures2.hpp"
#include "textures.hpp"
#include <functional>
#include "events.hpp"
#include "constants.hpp"

@ -1,5 +1,5 @@
#include "gui_fsm.hpp"
#include "textures2.hpp"
#include "textures.hpp"
int main() {
textures::init();

@ -84,8 +84,7 @@ sources = [
'stats.cpp',
'status_ui.cpp',
'systems.cpp',
'texture.cpp',
'textures2.cpp',
'textures.cpp',
'tilemap.cpp',
'worldbuilder.cpp',
]

@ -8,7 +8,7 @@
#include <memory>
#include <numbers>
#include "components.hpp"
#include "textures2.hpp"
#include "textures.hpp"
using namespace fmt;
using std::make_unique;

@ -5,7 +5,7 @@
#include "animator.hpp"
#include "spatialmap.hpp"
#include "levelmanager.hpp"
#include "texture.hpp"
#include "textures.hpp"
using matrix::Matrix;
using RGBA = uint32_t;
@ -37,7 +37,7 @@ struct Raycaster {
int $screen_pos_y = RAY_VIEW_Y;
GameLevel $level;
Matrix $map;
std::unordered_map<DinkyECS::Entity, SpriteTexture> $sprites;
std::unordered_map<DinkyECS::Entity, textures::SpriteTexture> $sprites;
std::vector<double> $zbuffer; // width
Animator $anim;

@ -12,12 +12,12 @@ namespace gui {
{
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
$gui.layout(
"[*%(100,300)log_view]"
"[_]"
"[_]"
"[button1 | button2 | button3]"
"[button4 | button5 | button6]"
"[button7 | button8 | button9]"
"[*%(100,300)log_view]"
"[_]"
"[_]"
);
}

@ -2,7 +2,7 @@
#include "levelmanager.hpp"
#include "constants.hpp"
#include <deque>
#include "textures2.hpp"
#include "textures.hpp"
#include "guecs.hpp"
namespace gui {

@ -2,7 +2,7 @@
#include <fmt/core.h>
#include "constants.hpp"
#include "guecs.hpp"
#include "texture.hpp"
#include "textures.hpp"
using namespace guecs;

@ -1,7 +1,7 @@
#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#include <string>
#include "textures2.hpp"
#include "textures.hpp"
using namespace fmt;

@ -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…
Cancel
Save