From 7fb2d5cf268a856a8cb74a8e5de3440f13e121c0 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 17 Jan 2025 15:29:13 -0500 Subject: [PATCH] Updated Amit's code to run in sfml 3.0 --- amt/main.cpp | 35 +++++++++++++++++++++-------------- amt/raycaster.cpp | 33 ++++++++++++++++++++++++++++----- meson.build | 18 +++++++++--------- 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/amt/main.cpp b/amt/main.cpp index 9e0cd7c..069d30f 100644 --- a/amt/main.cpp +++ b/amt/main.cpp @@ -24,10 +24,15 @@ Matrix MAP{ {1,1,1,1,1,1,1,1,1} }; -int main() { - using KB = sf::Keyboard; +void draw_gui(sf::RenderWindow &window) { + sf::RectangleShape rect({SCREEN_WIDTH - RAY_VIEW_WIDTH, 300}); + rect.setPosition({0,0}); + rect.setFillColor({100, 100, 100}); + window.draw(rect); +} - sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Zed's Ray Caster Game Thing"); +int main() { + sf::RenderWindow window(sf::VideoMode({SCREEN_WIDTH, SCREEN_HEIGHT}), "Zed's Ray Caster Game Thing"); //ZED this should set with a function float player_x = MAP.rows() / 2; @@ -40,6 +45,12 @@ int main() { double moveSpeed = 0.1; double rotSpeed = 0.1; + const auto onClose = [&window](const sf::Event::Closed&) + { + window.close(); + }; + + std::size_t const max_count = 100; std::vector frames(max_count); std::size_t it = 1; @@ -57,27 +68,23 @@ int main() { it = 1; } ++it; - // DRAW GUI + + draw_gui(window); window.display(); - if(KB::isKeyPressed(KB::W)) { + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { rayview.run(moveSpeed, 1); - } else if(KB::isKeyPressed(KB::S)) { + } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::S)) { rayview.run(moveSpeed, -1); } - if(KB::isKeyPressed(KB::D)) { + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::D)) { rayview.rotate(rotSpeed, -1); - } else if(KB::isKeyPressed(KB::A)) { + } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) { rayview.rotate(rotSpeed, 1); } - sf::Event event; - while(window.pollEvent(event)) { - if(event.type == sf::Event::Closed) { - window.close(); - } - } + window.handleEvents(onClose); } return 0; diff --git a/amt/raycaster.cpp b/amt/raycaster.cpp index 07b02cd..c765ff2 100644 --- a/amt/raycaster.cpp +++ b/amt/raycaster.cpp @@ -5,7 +5,31 @@ using namespace fmt; using std::make_unique; +union ColorConv { + struct { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + } as_color; + uint32_t as_int; +}; + +inline uint32_t dumb_lighting(uint32_t pixel, double distance) { + if(distance < 0.9) return pixel; + + ColorConv conv{.as_int=pixel}; + conv.as_color.r /= distance; + conv.as_color.g /= distance; + conv.as_color.b /= distance; + + return conv.as_int; +} + + Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) : + view_texture({(unsigned int)width, (unsigned int)height}), + view_sprite(view_texture), $width(width), $height(height), pixels(static_cast(height), static_cast(width)), $window(window), @@ -15,14 +39,12 @@ Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int heigh ZBuffer(width) { $window.setVerticalSyncEnabled(true); - view_texture.create($width, $height); - view_sprite.setTexture(view_texture); - view_sprite.setPosition(0, 0); + view_sprite.setPosition({0, 0}); textures.load_textures(); } void Raycaster::set_position(int x, int y) { - view_sprite.setPosition(x, y); + view_sprite.setPosition({(float)x, (float)y}); } void Raycaster::position_camera(float player_x, float player_y) { @@ -32,7 +54,7 @@ void Raycaster::position_camera(float player_x, float player_y) { } void Raycaster::draw_pixel_buffer() { - view_texture.update(pixels.to_raw_buf(), $width, $height, 0, 0); + view_texture.update(pixels.to_raw_buf(), {(unsigned int)$width, (unsigned int)$height}, {0, 0}); // BUG: can I do this once and just update it? $window.draw(view_sprite); } @@ -64,6 +86,7 @@ void Raycaster::sprite_casting() { int sprite_index = spriteOrder[i]; Sprite& sprite_rec = textures.get_sprite(sprite_index); auto& sprite_texture = textures.get_texture(sprite_rec.texture); + double spriteX = sprite_rec.x - posX; double spriteY = sprite_rec.y - posY; diff --git a/meson.build b/meson.build index 045152e..5044227 100644 --- a/meson.build +++ b/meson.build @@ -40,12 +40,12 @@ executable('zedcaster', [ ], dependencies: dependencies) -# executable('amtcaster', [ -# 'dbc.cpp', -# 'config.cpp', -# 'amt/texture.cpp', -# 'amt/raycaster.cpp', -# 'amt/main.cpp' -# ], -# cpp_args: ['-std=c++23'], -# dependencies: dependencies) +executable('amtcaster', [ + 'dbc.cpp', + 'config.cpp', + 'amt/texture.cpp', + 'amt/raycaster.cpp', + 'amt/main.cpp' + ], + cpp_args: ['-std=c++23'], + dependencies: dependencies)