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

This commit is contained in:
Zed A. Shaw 2025-02-21 03:00:56 -05:00
parent 6c1d851e85
commit f3e1413022
23 changed files with 129 additions and 64 deletions

View file

@ -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);
});
}