From 6533f950d2df07f166e0fcc973a88e2de731c100 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 14 Jan 2025 20:07:37 -0500 Subject: [PATCH] Quick and dirty idea for a poor-man's lighting effect. --- sfmlcaster.cpp | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/sfmlcaster.cpp b/sfmlcaster.cpp index 2ef0321..65331f5 100644 --- a/sfmlcaster.cpp +++ b/sfmlcaster.cpp @@ -85,8 +85,25 @@ double spriteDistance[numSprites]; std::vector texture[numTextures]; +union RGBA { + struct { + uint8_t r; + uint8_t g; + uint8_t b; + uint8_t a; + } color; + + uint32_t out; +}; + +inline void RGBA_brightness(RGBA& pixel, double distance) { + pixel.color.r /= distance; + pixel.color.g /= distance; + pixel.color.b /= distance; +} + #define pixcoord(X, Y) ((Y) * RAY_VIEW_WIDTH) + (X) -uint32_t pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {0}; +RGBA pixels[RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT] = {{.out=0}}; sf::Texture view_texture; sf::Sprite view_sprite; @@ -166,18 +183,19 @@ void draw_line(sf::RenderWindow &window, Point start, Point end, uint32_t color) y = y + sy; } - pixels[pixcoord(x,y)] = color; + pixels[pixcoord(x,y)].out = color; } } void clear(sf::RenderWindow &window) { - std::fill_n(pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0); - window.clear(); +// std::fill_n((uint32_t *)pixels, RAY_VIEW_WIDTH * RAY_VIEW_HEIGHT, 0); +// window.clear(); } void ray_casting(sf::RenderWindow &window, Matrix& map) { int w = RAY_VIEW_WIDTH; int h = RAY_VIEW_HEIGHT; + double perpWallDist; // WALL CASTING for(int x = 0; x < w; x++) { @@ -197,7 +215,6 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { // length of ray from one x or y-side to next x or y-side double deltaDistX = std::abs(1.0 / rayDirX); double deltaDistY = std::abs(1.0 / rayDirY); - double perpWallDist; int stepX = 0; int stepY = 0; @@ -276,14 +293,13 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { for(int y = drawStart; y < drawEnd; y++) { int texY = (int)texPos & (texHeight - 1); texPos += step; - uint32_t color = texture[texNum][texHeight * texY + texX]; - - pixels[pixcoord(x, y)] = color; + RGBA pixel{.out=texture[texNum][texHeight * texY + texX]}; + RGBA_brightness(pixel, perpWallDist); + pixels[pixcoord(x, y)] = pixel; } // SET THE ZBUFFER FOR THE SPRITE CASTING ZBuffer[x] = perpWallDist; - } // SPRITE CASTING @@ -357,7 +373,9 @@ void ray_casting(sf::RenderWindow &window, Matrix& map) { uint32_t color = sprite_texture[texWidth * texY + texX]; // poor person's transparency, get current color from the texture if((color & 0x00FFFFFF) != 0) { - pixels[pixcoord(stripe, y)] = color; + RGBA pixel{.out=color}; + RGBA_brightness(pixel, perpWallDist); + pixels[pixcoord(stripe, y)] = pixel; } } } @@ -421,11 +439,11 @@ void draw_ceiling_floor(sf::RenderWindow &window) { // FLOOR color = texture[floorTexture][texWidth * ty + tx]; - pixels[pixcoord(x, y)] = color; + pixels[pixcoord(x, y)].out = color; // CEILING color = texture[ceilingTexture][texWidth * ty + tx]; - pixels[pixcoord(x, screenHeight - y - 1)] = color; + pixels[pixcoord(x, screenHeight - y - 1)].out = color; } } }