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. 55
      amt/pixel.hpp
  2. 13
      amt/raycaster.cpp

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

@ -7,9 +7,10 @@
using namespace fmt; 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 #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) { inline static constexpr amt::RGBA dumb_lighting(amt::RGBA pixel, double distance, double distance_from_center) {
auto const dim_pixel = pixel * room_brightness; auto const dim_pixel = pixel * room_brightness;
if (distance_from_center >= 0) { if (distance_from_center >= 0) {
@ -145,6 +146,7 @@ void Raycaster::sprite_casting() {
int texY = ((d * textureHeight) / spriteHeight) / 256; int texY = ((d * textureHeight) / spriteHeight) / 256;
//get current color from the texture //get current color from the texture
// BUG: this crashes sometimes when the math goes out of bounds // 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]; auto color = sprite_texture[texY][texX];
// poor person's transparency, get current color from the texture // poor person's transparency, get current color from the texture
pixels[y][stripe] = (color.to_hex() & 0xffffff00) ? color: pixels[y][stripe]; 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 // this uses the previous ty/tx fractional parts of
// floorX cellX to find the texture x/y. How? // floorX cellX to find the texture x/y. How?
#ifdef AMT_LIGHT
// FLOOR // FLOOR
pixels[y][x] = textures.floor[ty][tx] * room_brightness; pixels[y][x] = textures.floor[ty][tx] * room_brightness;
// CEILING // CEILING
pixels[$height - y - 1][x] = textures.ceiling[ty][tx] * room_brightness; 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