From 91ab5eb624a6c1081be7f5c6a587cd2860797038 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 1 Mar 2026 14:04:22 -0500 Subject: [PATCH] Some more rayview cleanup. --- src/graphics/raycaster.cpp | 30 +++++++++++++++++------------- src/graphics/raycaster.hpp | 19 ++++++++++--------- src/gui/main_ui.cpp | 3 +-- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/graphics/raycaster.cpp b/src/graphics/raycaster.cpp index cb76218..b5848ee 100644 --- a/src/graphics/raycaster.cpp +++ b/src/graphics/raycaster.cpp @@ -58,13 +58,15 @@ inline RGBA lighting_calc(RGBA pixel, float dist, int level) { return conv.as_int; } -Raycaster::Raycaster(int width, int height) : +Raycaster::Raycaster(int x, int y, int width, int height) : $view_texture(sf::Vector2u{(unsigned int)width, (unsigned int)height}), $view_sprite($view_texture), $width(width), $height(height), + $screen_pos_x(x), + $screen_pos_y(y), $zbuffer(width) { - $view_sprite.setPosition({0, 0}); + $view_sprite.setPosition({float($screen_pos_x), float($screen_pos_y)}); $pixels = make_unique($width * $height); $view_texture.setSmooth(false); @@ -73,12 +75,6 @@ Raycaster::Raycaster(int width, int height) : update_camera_aiming(); } -void Raycaster::set_position(int x, int y) { - $screen_pos_x = x; - $screen_pos_y = y; - $view_sprite.setPosition({(float)x, (float)y}); -} - void Raycaster::position_camera(float player_x, float player_y) { // x and y start position $pos_x = player_x; @@ -101,6 +97,15 @@ void Raycaster::apply_sprite_effect(shared_ptr effect, float width, effect->setUniform("u_resolution", u_resolution); } +std::shared_ptr Raycaster::apply_lighting_effect(components::Position& sprite_pos, matrix::Matrix &lights) { + auto effect = $brightness; + float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT; + // this boosts the brightness of anything we're aiming at + level += (aiming_at == sprite_pos.location) * AIMED_AT_BRIGHTNESS; + effect->setUniform("darkness", level); + return effect; +} + inline void step_animation(animation::Animation& anim, sf::Sprite& sprite, sf::Vector2f& position, sf::Vector2f& scale, sf::IntRect& in_texture, sf::Vector2f& origin) { anim.update(); anim.apply(sprite, in_texture); @@ -209,14 +214,12 @@ void Raycaster::sprite_casting() { shared_ptr effect = System::sprite_effect(rec.entity); - float level = lights[sprite_pos.location.y][sprite_pos.location.x] * PERCENT; - if(effect) { + // has an effect, use that instead of lighting apply_sprite_effect(effect, sprite_width, sprite_height); } else { - effect = $brightness; - level += (aiming_at == sprite_pos.location) * AIMED_AT_BRIGHTNESS; - effect->setUniform("darkness", level); + // no effect applied in the system so apply lighting + effect = apply_lighting_effect(sprite_pos, lights); } auto anim = world->get_if(rec.entity); @@ -345,6 +348,7 @@ void Raycaster::cast_rays() { } void Raycaster::draw_ceiling_floor() { + // BUG: this should come from the texture's config constexpr const int texture_width = TEXTURE_WIDTH; constexpr const int texture_height = TEXTURE_HEIGHT; diff --git a/src/graphics/raycaster.hpp b/src/graphics/raycaster.hpp index 3209326..2c5c92d 100644 --- a/src/graphics/raycaster.hpp +++ b/src/graphics/raycaster.hpp @@ -24,6 +24,14 @@ struct CameraLOL { using SpriteRender = std::pair, std::shared_ptr>; struct Raycaster { + sf::Texture $view_texture; + sf::Sprite $view_sprite; + int $width; + int $height; + int $screen_pos_x; + int $screen_pos_y; + std::vector $zbuffer; // width + int $pitch=0; sf::Clock $clock; std::shared_ptr $brightness = nullptr; @@ -37,18 +45,11 @@ struct Raycaster { // the 2d raycaster version of camera plane double $plane_x = 0.0; double $plane_y = 0.66; - sf::Texture $view_texture; - sf::Sprite $view_sprite; Point aiming_at{0,0}; Point camera_at{0,0}; CameraLOL $camera; - std::unique_ptr $pixels = nullptr; - int $width; - int $height; - int $screen_pos_x = RAY_VIEW_X; - int $screen_pos_y = RAY_VIEW_Y; std::unordered_map $sprites; // BUG: this can be way better I think std::vector $sprites_to_render; @@ -57,9 +58,8 @@ struct Raycaster { GameDB::Level $level; Matrix $tiles; Matrix $walls; - std::vector $zbuffer; // width - Raycaster(int width, int height); + Raycaster(int x, int y, int width, int height); void cast_rays(); void draw_ceiling_floor(); @@ -93,4 +93,5 @@ struct Raycaster { // BUG: these should go away when Bug #42 is solved void apply_sprite_effect(std::shared_ptr effect, float width, float height); + std::shared_ptr apply_lighting_effect(components::Position& sprite_pos, matrix::Matrix &lights); }; diff --git a/src/gui/main_ui.cpp b/src/gui/main_ui.cpp index 1805f22..1d5cd2b 100644 --- a/src/gui/main_ui.cpp +++ b/src/gui/main_ui.cpp @@ -11,7 +11,7 @@ namespace gui { MainUI::MainUI(sf::RenderWindow& window) : $window(window), - $rayview(std::make_shared(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)) + $rayview(std::make_shared(RAY_VIEW_X, RAY_VIEW_Y, RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT)) { $window.setVerticalSyncEnabled(VSYNC); $window.setFramerateLimit(FRAME_LIMIT); @@ -31,7 +31,6 @@ namespace gui { auto player = player_position.location; $rayview->init_shaders(); - $rayview->set_position(RAY_VIEW_X, RAY_VIEW_Y); $rayview->position_camera(player.x + 0.5, player.y + 0.5); $overlay_ui.init();