Brought in Amit's latest code and converted it to use either dumb lighting or his new torch lighting, then threw in the FPS counter from last night.

This commit is contained in:
Zed A. Shaw 2025-01-18 11:10:43 -05:00
parent 53a151511e
commit 831e15ca18
6 changed files with 500 additions and 247 deletions

View file

@ -1,44 +1,51 @@
#include "amt/raycaster.hpp"
#include "amt/texture.hpp"
#include "amt/pixel.hpp"
#include "constants.hpp"
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;
};
static constexpr auto room_brightness = 0.3f; // increse this to increase the room brightness. Higher value means brighter room.
inline uint32_t dumb_lighting(uint32_t pixel, double distance) {
if(distance < 0.9) return pixel;
#ifdef AMT_LIGHT
inline static constexpr amt::RGBA dumb_lighting(amt::RGBA pixel, double distance, double distance_from_center) {
auto const dim_pixel = pixel * room_brightness;
if (distance_from_center >= 0) {
auto const min_brightness = 1. / std::max(distance_from_center, 0.5); // farther away from the center darker it gets
auto const max_brightness = 1.; // brighness should not exceed 1
auto const pixel_brightness = std::max(min_brightness, std::min(max_brightness, distance));
auto const yellow_brightness = float(distance_from_center * 60);
ColorConv conv{.as_int=pixel};
conv.as_color.r /= distance;
conv.as_color.g /= distance;
conv.as_color.b /= distance;
amt::RGBA const yellow = amt::HSLA(40, 20, yellow_brightness);
return conv.as_int;
auto temp = (pixel / pixel_brightness).blend<amt::BlendMode::softLight>(yellow);
return temp.brightness() < 0.1f ? dim_pixel : temp;
} else {
return dim_pixel;
}
}
#else
inline static constexpr amt::RGBA dumb_lighting(amt::RGBA pixel, double distance, double distance_from_center) {
if(distance < 0.9) return pixel;
return pixel / distance;
}
#endif
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, int width, int height) :
view_texture({(unsigned int)width, (unsigned int)height}),
Raycaster::Raycaster(sf::RenderWindow& window, Matrix &map, unsigned width, unsigned height) :
view_texture(sf::Vector2u{width, height}),
view_sprite(view_texture),
$width(width), $height(height),
pixels(static_cast<size_t>(height), static_cast<std::size_t>(width)),
$width(static_cast<int>(width)),
$height(static_cast<int>(height)),
pixels(height, width),
$window(window),
$map(map),
spriteOrder(textures.NUM_SPRITES),
spriteDistance(textures.NUM_SPRITES),
ZBuffer(width)
{
$window.setVerticalSyncEnabled(true);
$window.setVerticalSyncEnabled(VSYNC);
view_sprite.setPosition({0, 0});
textures.load_textures();
}
@ -139,9 +146,7 @@ void Raycaster::sprite_casting() {
// BUG: this crashes sometimes when the math goes out of bounds
auto color = sprite_texture[texY][texX];
// poor person's transparency, get current color from the texture
if((color.to_hex() & 0xFFFFFF00) != 0) {
pixels[y][stripe] = color;
}
pixels[y][stripe] = (color.to_hex() & 0xffffff00) ? color: pixels[y][stripe];
}
}
}
@ -149,7 +154,12 @@ void Raycaster::sprite_casting() {
}
void Raycaster::cast_rays() {
double perpWallDist;
double perpWallDist = 0;
auto const cx = $width / 2;
auto const cy = $height / 2;
double const radius = std::min($height, $width) / 2;
double const r_sq = radius * radius;
// WALL CASTING
for(int x = 0; x < $width; x++) {
@ -247,7 +257,11 @@ void Raycaster::cast_rays() {
for(int y = drawStart; y < drawEnd; y++) {
int texY = (int)texPos & (textures.TEXTURE_HEIGHT - 1);
texPos += step;
pixels[y][x] = texture[texY][texX];
auto const dx = cx - x;
auto const dy = cy - y;
double const dist = dx * dx + dy * dy;
auto color = dumb_lighting(texture[texY][texX], perpWallDist, (r_sq - dist) / r_sq);
pixels[y][x] = color;
}
// SET THE ZBUFFER FOR THE SPRITE CASTING
@ -309,10 +323,10 @@ void Raycaster::draw_ceiling_floor() {
// floorX cellX to find the texture x/y. How?
// FLOOR
pixels[y][x] = textures.floor[ty][tx];
pixels[y][x] = textures.floor[ty][tx] * room_brightness;
// CEILING
pixels[$height - y - 1][x] = textures.ceiling[ty][tx];
pixels[$height - y - 1][x] = textures.ceiling[ty][tx] * room_brightness;
}
}