diff --git a/amt/pixel.hpp b/amt/pixel.hpp index 6a96e71..13bba72 100644 --- a/amt/pixel.hpp +++ b/amt/pixel.hpp @@ -3,7 +3,6 @@ #include "matrix.hpp" #include -#include #include #include #include @@ -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(this->r()); - auto b = static_cast(this->b()); - auto g = static_cast(this->g()); - auto a = static_cast(this->a()); - return (r << (8 * 3)) | (g << (8 * 2)) | (b << (8 * 1)) | (a << (8 * 0)); - } else { - return m_data.color; - } + auto r = static_cast(this->r()); + auto b = static_cast(this->b()); + auto g = static_cast(this->g()); + auto a = static_cast(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 constexpr auto operator/(T val) const noexcept { auto d = static_cast(val); - auto tr = float(r()); - auto tg = float(g()); - auto tb = float(b()); return RGBA( - static_cast(tr / d), - static_cast(tg / d), - static_cast(tb / d), + static_cast(r() / d), + static_cast(g() / d), + static_cast(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 { diff --git a/amt/raycaster.cpp b/amt/raycaster.cpp index 254e975..517a546 100644 --- a/amt/raycaster.cpp +++ b/amt/raycaster.cpp @@ -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 + } }