Intermediate refactor to move everything over to using the textures module rather than everyone using one TexturePack thing.

master
Zed A. Shaw 2 weeks ago
parent 6c1d851e85
commit f3e1413022
  1. 2
      Makefile
  2. 4
      combat_ui.cpp
  3. 2
      combat_ui.hpp
  4. 4
      guecs.cpp
  5. 8
      guecs.hpp
  6. 8
      gui_fsm.cpp
  7. 1
      gui_fsm.hpp
  8. 2
      main.cpp
  9. 7
      main_ui.cpp
  10. 3
      main_ui.hpp
  11. 2
      meson.build
  12. 9
      overlay_ui.cpp
  13. 3
      overlay_ui.hpp
  14. 21
      raycaster.cpp
  15. 7
      raycaster.hpp
  16. 10
      status_ui.cpp
  17. 4
      status_ui.hpp
  18. 6
      tests/guecs.cpp
  19. 17
      tests/textures.cpp
  20. 14
      texture.cpp
  21. 3
      texture.hpp
  22. 38
      textures2.cpp
  23. 18
      textures2.hpp

@ -22,7 +22,7 @@ tracy_build:
meson compile -j 10 -C builddir
test: build
./builddir/runtests
./builddir/runtests "[textures]"
run: build test
powershell "cp ./builddir/zedcaster.exe ."

@ -15,7 +15,7 @@ namespace gui {
"[ >.%(100,50)label_hp | *%.(198,50)bar_hp | _ ]");
}
void CombatUI::render(TexturePack& textures) {
void CombatUI::render() {
auto& world = $gui.world();
world.set_the<Background>({$gui.$parser});
@ -39,7 +39,7 @@ namespace gui {
}
}
$gui.init(textures);
$gui.init();
}
void CombatUI::draw(sf::RenderWindow& window) {

@ -13,7 +13,7 @@ namespace gui {
CombatUI(GameLevel level);
void render(TexturePack& texture);
void render();
void draw(sf::RenderWindow& window);
void update_level(GameLevel &level) { $level = level; }
void set_damage(float percent);

@ -28,7 +28,7 @@ namespace guecs {
}
}
void UI::init(TexturePack& textures) {
void UI::init() {
if($world.has_the<Background>()) {
auto& bg = $world.get_the<Background>();
bg.init();
@ -59,7 +59,7 @@ namespace guecs {
});
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
sprite.init(cell, textures);
sprite.init(cell);
});
}

@ -5,7 +5,7 @@
#include <string>
#include <memory>
#include <SFML/Graphics.hpp>
#include "texture.hpp"
#include "textures2.hpp"
#include <functional>
#include "events.hpp"
#include "constants.hpp"
@ -57,8 +57,8 @@ namespace guecs {
std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr;
void init(lel::Cell &cell, TexturePack &textures) {
auto sprite_texture = textures.get(name);
void init(lel::Cell &cell) {
auto sprite_texture = textures::get(name);
texture = sprite_texture.texture;
sprite = make_shared<sf::Sprite>(*texture);
sprite->setPosition({
@ -145,7 +145,7 @@ namespace guecs {
return $world;
}
void init(TexturePack& textures);
void init();
void render(sf::RenderWindow& window);
void mouse(float x, float y);

@ -13,7 +13,7 @@ namespace gui {
FSM::FSM() :
$window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Raycaster Thing"),
$main_ui($window, $levels.current(), $textures),
$main_ui($window, $levels.current()),
$renderer($window),
$level($levels.current()),
$map_ui($level),
@ -21,8 +21,6 @@ namespace gui {
$status_ui($level),
$font{FONT_FILE_NAME}
{
$textures.load_tiles();
$textures.load_sprites();
}
void FSM::event(Event ev) {
@ -45,8 +43,8 @@ namespace gui {
$main_ui.init();
$combat_ui.render($textures);
$status_ui.render($textures);
$combat_ui.render();
$status_ui.render();
$status_ui.log("Welcome to the game!");
$renderer.init_terminal();

@ -52,7 +52,6 @@ namespace gui {
CombatUI $combat_ui;
StatusUI $status_ui;
sf::Font $font;
TexturePack $textures;
FSM();

@ -1,6 +1,8 @@
#include "gui_fsm.hpp"
#include "textures2.hpp"
int main() {
textures::init();
gui::FSM main;
main.event(gui::Event::STARTED);

@ -4,12 +4,11 @@
namespace gui {
using namespace components;
MainUI::MainUI(sf::RenderWindow& window, GameLevel level, TexturePack& textures) :
MainUI::MainUI(sf::RenderWindow& window, GameLevel level) :
$window(window),
$level(level),
$textures(textures),
$overlay_ui($level, $textures),
$rayview($textures, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)
$overlay_ui($level),
$rayview(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)
{
$window.setVerticalSyncEnabled(VSYNC);
$window.setFramerateLimit(FRAME_LIMIT);

@ -15,12 +15,11 @@ namespace gui {
Stats $stats;
sf::RenderWindow& $window;
GameLevel $level;
TexturePack& $textures;
OverlayUI $overlay_ui;
Raycaster $rayview;
CameraLOL $camera;
MainUI(sf::RenderWindow& window, GameLevel level, TexturePack &textures);
MainUI(sf::RenderWindow& window, GameLevel level);
void mouse(int x, int y);
void debug();

@ -85,6 +85,7 @@ sources = [
'status_ui.cpp',
'systems.cpp',
'texture.cpp',
'textures2.cpp',
'tilemap.cpp',
'worldbuilder.cpp',
]
@ -105,6 +106,7 @@ executable('runtests', sources + [
'tests/pathing.cpp',
'tests/spatialmap.cpp',
'tests/tilemap.cpp',
'tests/textures.cpp',
], override_options: exe_defaults,
dependencies: dependencies + [catch2])

@ -8,9 +8,8 @@ namespace gui {
using namespace guecs;
using std::string;
OverlayUI::OverlayUI(GameLevel level, TexturePack& textures) :
$level(level),
$textures(textures)
OverlayUI::OverlayUI(GameLevel level) :
$level(level)
{
$gui.position(RAY_VIEW_X, RAY_VIEW_Y, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT);
$gui.layout(
@ -28,7 +27,7 @@ namespace gui {
auto region = $gui.entity(name);
world.set<lel::Cell>(region, cell);
}
$gui.init($textures);
$gui.init();
}
void OverlayUI::draw(sf::RenderWindow& window) {
@ -39,7 +38,7 @@ namespace gui {
auto ent = $gui.entity(region);
Sprite blood{sprite_name};
auto& cell = $gui.cell_for(ent);
blood.init(cell, $textures);
blood.init(cell);
$gui.set<guecs::Sprite>(ent, blood);
}

@ -9,9 +9,8 @@ namespace gui {
public:
guecs::UI $gui;
GameLevel $level;
TexturePack& $textures;
OverlayUI(GameLevel level, TexturePack& $textures);
OverlayUI(GameLevel level);
void render();
void draw(sf::RenderWindow& window);

@ -8,6 +8,7 @@
#include <memory>
#include <numbers>
#include "components.hpp"
#include "textures2.hpp"
using namespace fmt;
using std::make_unique;
@ -33,8 +34,7 @@ inline uint32_t new_lighting(uint32_t pixel, int level) {
return conv.as_int;
}
Raycaster::Raycaster(TexturePack &textures, int width, int height) :
$textures(textures),
Raycaster::Raycaster(int width, int height) :
$view_texture(sf::Vector2u{(unsigned int)width, (unsigned int)height}),
$view_sprite($view_texture),
$width(width), $height(height),
@ -44,6 +44,8 @@ Raycaster::Raycaster(TexturePack &textures, int width, int height) :
$view_sprite.setPosition({0, 0});
$pixels = make_unique<RGBA[]>($width * $height);
$view_texture.setSmooth(false);
$floor_texture = textures::get_floor();
$ceiling_texture = textures::get_ceiling();
}
void Raycaster::set_position(int x, int y) {
@ -226,7 +228,7 @@ void Raycaster::cast_rays() {
int draw_end = line_height / 2 + $height / 2 + $pitch;
if(draw_end >= $height) draw_end = $height - 1;
auto texture = $textures.get_surface($map[map_y][map_x] - 1);
auto texture = textures::get_surface($map[map_y][map_x] - 1);
// calculate value of wall_x
double wall_x; // where exactly the wall was hit
@ -265,9 +267,6 @@ void Raycaster::cast_rays() {
void Raycaster::draw_ceiling_floor() {
constexpr static const int texture_width = TEXTURE_WIDTH;
constexpr static const int texture_height = TEXTURE_HEIGHT;
auto floor_texture = (const uint32_t *)$textures.floor.getPixelsPtr();
auto ceiling_texture = (const uint32_t *)$textures.ceiling.getPixelsPtr();
auto &lights = $level.lights->lighting();
for(int y = $height / 2 + 1; y < $height; ++y) {
@ -324,11 +323,11 @@ void Raycaster::draw_ceiling_floor() {
float light_level = matrix::inbounds(lights, map_x, map_y) ? lights[map_y][map_x] : 30;
// FLOOR
color = floor_texture[texture_width * ty + tx];
color = $floor_texture[texture_width * ty + tx];
$pixels[pixcoord(x, y)] = new_lighting(color, light_level);
// CEILING
color = ceiling_texture[texture_width * ty + tx];
color = $ceiling_texture[texture_width * ty + tx];
$pixels[pixcoord(x, $height - y - 1)] = new_lighting(color, light_level);
}
}
@ -344,7 +343,7 @@ void Raycaster::draw(sf::RenderTarget& target) {
void Raycaster::update_sprite(DinkyECS::Entity ent, components::Sprite& sprite) {
fmt::println("entity UPDATE SPRITE {} will have sprite named {}", ent, sprite.name);
auto sprite_txt = $textures.get(sprite.name);
auto sprite_txt = textures::get(sprite.name);
$sprites.insert_or_assign(ent, sprite_txt);
}
@ -353,13 +352,13 @@ void Raycaster::set_level(GameLevel level) {
auto& tiles = $level.map->tiles();
auto world = $level.world;
auto& player = world->get_the<components::Player>();
$map = $textures.convert_char_to_texture(tiles.$tile_ids);
$map = textures::convert_char_to_texture(tiles.$tile_ids);
world->query<components::Sprite>([&](const auto ent, auto& sprite) {
// player doesn't need a sprite
if(player.entity == ent) return;
fmt::println("entity {} will have sprite named {}", ent, sprite.name);
auto sprite_txt = $textures.get(sprite.name);
auto sprite_txt = textures::get(sprite.name);
$sprites.try_emplace(ent, sprite_txt);
});
}

@ -2,10 +2,10 @@
#include <SFML/Graphics.hpp>
#include <SFML/System/Clock.hpp>
#include "texture.hpp"
#include "animator.hpp"
#include "spatialmap.hpp"
#include "levelmanager.hpp"
#include "texture.hpp"
using matrix::Matrix;
using RGBA = uint32_t;
@ -14,7 +14,6 @@ struct Raycaster {
int $pitch=0;
sf::Clock $clock;
sf::Shader $brightness;
TexturePack &$textures;
double $pos_x = 0;
double $pos_y = 0;
@ -27,6 +26,8 @@ struct Raycaster {
double $plane_y = 0.66;
sf::Texture $view_texture;
sf::Sprite $view_sprite;
const uint32_t *$floor_texture = nullptr;
const uint32_t *$ceiling_texture = nullptr;
std::unique_ptr<RGBA[]> $pixels = nullptr;
@ -40,7 +41,7 @@ struct Raycaster {
std::vector<double> $zbuffer; // width
Animator $anim;
Raycaster(TexturePack &textures, int width, int height);
Raycaster(int width, int height);
void cast_rays();
void draw_ceiling_floor();

@ -12,16 +12,16 @@ namespace gui {
{
$gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT);
$gui.layout(
"[button1 | button2 | button3]"
"[button4 | button5 | button6]"
"[button7 | button8 | button9]"
"[*%(100,300)log_view]"
"[_]"
"[_]"
"[button1 | button2 | button3]"
"[button4 | button5 | button6]"
"[button7 | button8 | button9]"
);
}
void StatusUI::render(TexturePack &textures) {
void StatusUI::render() {
auto& world = $gui.world();
std::vector<std::string> fake_items{
@ -48,7 +48,7 @@ namespace gui {
}
}
$gui.init(textures);
$gui.init();
}
void StatusUI::draw(sf::RenderWindow &window) {

@ -2,7 +2,7 @@
#include "levelmanager.hpp"
#include "constants.hpp"
#include <deque>
#include "texture.hpp"
#include "textures2.hpp"
#include "guecs.hpp"
namespace gui {
@ -15,7 +15,7 @@ namespace gui {
StatusUI(GameLevel level);
void update_level(GameLevel &level) { $level = level; }
void log(std::string msg);
void render(TexturePack &textures);
void render();
void draw(sf::RenderWindow &window);
};
}

@ -8,9 +8,7 @@ using namespace guecs;
TEST_CASE("prototype one gui", "[ecs-gui]") {
guecs::UI gui;
TexturePack textures;
textures.load_sprites();
textures::init();
gui.position(0, 0, 1000, 500);
gui.layout("[test1|test2|test3][test4|_|test5]");
@ -24,7 +22,7 @@ TEST_CASE("prototype one gui", "[ecs-gui]") {
world.set<Textual>(button, {name});
}
gui.init(textures);
gui.init();
// at this point it's mostly ready but I'd need to render it to a window real quick
sf::RenderWindow window;

@ -0,0 +1,17 @@
#include <catch2/catch_test_macros.hpp>
#include <fmt/core.h>
#include <string>
#include "textures2.hpp"
using namespace fmt;
TEST_CASE("test texture management", "[textures]") {
textures::init();
auto spider = textures::get("hairy_spider");
auto image = textures::load_image("assets/hairy_spider-256.png");
auto img_ptr = textures::get_surface(0);
REQUIRE(img_ptr != nullptr);
}

@ -7,14 +7,14 @@
using std::shared_ptr, std::make_shared;
sf::Image TexturePack::load_image(std::string filename) {
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 TexturePack::load_sprites() {
void TextureManager::load_sprites() {
Config assets("assets/config.json");
for(auto& el : assets["sprites"].items()) {
@ -28,13 +28,11 @@ void TexturePack::load_sprites() {
sprite_textures[name] = {sprite, texture};
}
sword = sprite_textures["sword"];
floor = load_image(assets["sprites"]["floor"]);
ceiling = load_image(assets["sprites"]["ceiling"]);
}
void TexturePack::load_tiles() {
void TextureManager::load_tiles() {
Config assets("assets/tiles.json");
auto &tiles = assets.json();
@ -50,11 +48,11 @@ void TexturePack::load_tiles() {
}
}
const uint32_t* TexturePack::get_surface(size_t num) {
const uint32_t* TextureManager::get_surface(size_t num) {
return (const uint32_t *)surfaces[num].getPixelsPtr();
}
matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) {
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();) {
@ -65,7 +63,7 @@ matrix::Matrix TexturePack::convert_char_to_texture(matrix::Matrix &tile_ids) {
return result;
}
SpriteTexture TexturePack::get(std::string name) {
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);

@ -13,13 +13,12 @@ struct SpriteTexture {
std::shared_ptr<sf::Texture> texture = nullptr;
};
struct TexturePack {
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;
SpriteTexture sword;
void load_tiles();
void load_sprites();

@ -0,0 +1,38 @@
#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();
}
};

@ -0,0 +1,18 @@
#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