Bring Amit's latest fix in and protect against the sprites rendering the texY value wrong sometimes.

master
Zed A. Shaw 3 months ago
parent 379ec5846f
commit 6592e22075
  1. 65
      amt/pixel.hpp
  2. 13
      amt/raycaster.cpp

@ -3,7 +3,6 @@
#include "matrix.hpp"
#include <algorithm>
#include <bit>
#include <cmath>
#include <cstddef>
#include <cstdint>
@ -62,25 +61,18 @@ namespace amt {
constexpr RGBA() noexcept = default;
constexpr RGBA(RGBA const&) noexcept = default;
constexpr RGBA(RGBA &&) noexcept = default;
constexpr RGBA& operator=(RGBA const& other) noexcept {
this->m_data.color = other.m_data.color;
return *this;
}
constexpr RGBA& operator=(RGBA && other) noexcept {
this->m_data.color = other.m_data.color;
return *this;
}
constexpr RGBA& operator=(RGBA const&) noexcept = default;
constexpr RGBA& operator=(RGBA &&) noexcept = default;
constexpr ~RGBA() noexcept = default;
// NOTE: Accepts RRGGBBAA
explicit constexpr RGBA(pixels_t color) noexcept
: m_data({ .color = color })
: RGBA((color >> (8 * 3)) & 0xff, (color >> (8 * 2)) & 0xff, (color >> (8 * 1)) & 0xff, (color >> (8 * 0)) & 0xff)
{}
constexpr RGBA(pixel_t r, pixel_t g, pixel_t b, pixel_t a = 0xff) noexcept
: m_data({ .rgba = { r, g, b, a } })
: m_data {r, g, b, a}
{}
constexpr RGBA(pixel_t color, pixel_t a = 0xff) noexcept
@ -89,26 +81,22 @@ namespace amt {
// NOTE: Returns RRGGBBAA
constexpr auto to_hex() const noexcept -> pixels_t {
if constexpr (std::endian::native == std::endian::little) {
auto r = static_cast<pixels_t>(this->r());
auto b = static_cast<pixels_t>(this->b());
auto g = static_cast<pixels_t>(this->g());
auto a = static_cast<pixels_t>(this->a());
return (r << (8 * 3)) | (g << (8 * 2)) | (b << (8 * 1)) | (a << (8 * 0));
} else {
return m_data.color;
}
auto r = static_cast<pixels_t>(this->r());
auto b = static_cast<pixels_t>(this->b());
auto g = static_cast<pixels_t>(this->g());
auto a = static_cast<pixels_t>(this->a());
return (r << (8 * 3)) | (g << (8 * 2)) | (b << (8 * 1)) | (a << (8 * 0));
}
constexpr auto r() const noexcept -> pixel_t { return m_data.rgba.r; }
constexpr auto g() const noexcept -> pixel_t { return m_data.rgba.g; }
constexpr auto b() const noexcept -> pixel_t { return m_data.rgba.b; }
constexpr auto a() const noexcept -> pixel_t { return m_data.rgba.a; }
constexpr auto r() const noexcept -> pixel_t { return m_data[0]; }
constexpr auto g() const noexcept -> pixel_t { return m_data[1]; }
constexpr auto b() const noexcept -> pixel_t { return m_data[2]; }
constexpr auto a() const noexcept -> pixel_t { return m_data[3]; }
constexpr auto r() noexcept -> pixel_t& { return m_data.rgba.r; }
constexpr auto g() noexcept -> pixel_t& { return m_data.rgba.g; }
constexpr auto b() noexcept -> pixel_t& { return m_data.rgba.b; }
constexpr auto a() noexcept -> pixel_t& { return m_data.rgba.a; }
constexpr auto r() noexcept -> pixel_t& { return m_data[0]; }
constexpr auto g() noexcept -> pixel_t& { return m_data[1]; }
constexpr auto b() noexcept -> pixel_t& { return m_data[2]; }
constexpr auto a() noexcept -> pixel_t& { return m_data[3]; }
/**
* @returns the value is between 0 and 1
@ -125,13 +113,10 @@ namespace amt {
requires std::is_arithmetic_v<T>
constexpr auto operator/(T val) const noexcept {
auto d = static_cast<float>(val);
auto tr = float(r());
auto tg = float(g());
auto tb = float(b());
return RGBA(
static_cast<pixel_t>(tr / d),
static_cast<pixel_t>(tg / d),
static_cast<pixel_t>(tb / d),
static_cast<pixel_t>(r() / d),
static_cast<pixel_t>(g() / d),
static_cast<pixel_t>(b() / d),
a()
);
}
@ -373,15 +358,7 @@ namespace amt {
}
}
private:
union {
struct {
pixel_t r;
pixel_t g;
pixel_t b;
pixel_t a;
} rgba;
pixels_t color;
} m_data {};
pixel_t m_data[4]{};
};
struct HSLA {

@ -7,9 +7,10 @@
using namespace fmt;
static constexpr auto room_brightness = 0.3f; // increse this to increase the room brightness. Higher value means brighter room.
#ifdef AMT_LIGHT
static constexpr auto room_brightness = 0.3f; // increse this to increase the room brightness. Higher value means brighter room.
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) {
@ -145,6 +146,7 @@ void Raycaster::sprite_casting() {
int texY = ((d * textureHeight) / spriteHeight) / 256;
//get current color from the texture
// BUG: this crashes sometimes when the math goes out of bounds
if(texY < 0 || texY >= (int)sprite_texture.rows()) continue;
auto color = sprite_texture[texY][texX];
// poor person's transparency, get current color from the texture
pixels[y][stripe] = (color.to_hex() & 0xffffff00) ? color: pixels[y][stripe];
@ -323,11 +325,20 @@ void Raycaster::draw_ceiling_floor() {
// this uses the previous ty/tx fractional parts of
// floorX cellX to find the texture x/y. How?
#ifdef AMT_LIGHT
// FLOOR
pixels[y][x] = textures.floor[ty][tx] * room_brightness;
// CEILING
pixels[$height - y - 1][x] = textures.ceiling[ty][tx] * room_brightness;
#else
// FLOOR
pixels[y][x] = textures.floor[ty][tx];
// CEILING
pixels[$height - y - 1][x] = textures.ceiling[ty][tx];
#endif
}
}

Loading…
Cancel
Save