Some more rayview cleanup.

This commit is contained in:
Zed A. Shaw 2026-03-01 14:04:22 -05:00
parent dd3952d5c1
commit 91ab5eb624
3 changed files with 28 additions and 24 deletions

View file

@ -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<RGBA[]>($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<sf::Shader> effect, float width,
effect->setUniform("u_resolution", u_resolution);
}
std::shared_ptr<sf::Shader> 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<sf::Shader> 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<animation::Animation>(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;

View file

@ -24,6 +24,14 @@ struct CameraLOL {
using SpriteRender = std::pair<std::shared_ptr<sf::Sprite>, std::shared_ptr<sf::Shader>>;
struct Raycaster {
sf::Texture $view_texture;
sf::Sprite $view_sprite;
int $width;
int $height;
int $screen_pos_x;
int $screen_pos_y;
std::vector<double> $zbuffer; // width
int $pitch=0;
sf::Clock $clock;
std::shared_ptr<sf::Shader> $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<RGBA[]> $pixels = nullptr;
int $width;
int $height;
int $screen_pos_x = RAY_VIEW_X;
int $screen_pos_y = RAY_VIEW_Y;
std::unordered_map<DinkyECS::Entity, textures::SpriteTexture> $sprites;
// BUG: this can be way better I think
std::vector<SpriteRender> $sprites_to_render;
@ -57,9 +58,8 @@ struct Raycaster {
GameDB::Level $level;
Matrix $tiles;
Matrix $walls;
std::vector<double> $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<sf::Shader> effect, float width, float height);
std::shared_ptr<sf::Shader> apply_lighting_effect(components::Position& sprite_pos, matrix::Matrix &lights);
};

View file

@ -11,7 +11,7 @@ namespace gui {
MainUI::MainUI(sf::RenderWindow& window) :
$window(window),
$rayview(std::make_shared<Raycaster>(RAY_VIEW_WIDTH, RAY_VIEW_HEIGHT))
$rayview(std::make_shared<Raycaster>(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();