diff options
-rw-r--r-- | main/camera.cpp | 4 | ||||
-rw-r--r-- | main/update.cpp | 2 | ||||
-rw-r--r-- | shaders/tile-shader.cpp | 16 | ||||
-rw-r--r-- | shaders/tile-shader.hpp | 16 | ||||
-rw-r--r-- | src/global-coords.hpp | 26 |
5 files changed, 38 insertions, 26 deletions
diff --git a/main/camera.cpp b/main/camera.cpp index b7e2b893..aa4cad00 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -53,7 +53,7 @@ void app::recalc_cursor_tile() global_coords app::pixel_to_tile(Vector2d position) const { - const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset(); + const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset()*.5; const Vector2d vec = tile_shader::unproject(px) / Vector2d{dTILE_SIZE[0]*.5, dTILE_SIZE[1]*.5} + Vector2d{.5, .5}; const auto x = (std::int32_t)std::floor(vec[0]), y = (std::int32_t)std::floor(vec[1]); return { x, y }; @@ -80,7 +80,7 @@ constexpr double ce_sqrt(double x) std::array<std::int16_t, 4> app::get_draw_bounds() const noexcept { const auto center = pixel_to_tile(Vector2d(windowSize()/2)).chunk(); - constexpr auto N = 3; + constexpr auto N = 1; return { std::int16_t(center.x - N), std::int16_t(center.x + N), diff --git a/main/update.cpp b/main/update.cpp index b1f6d945..9a65e182 100644 --- a/main/update.cpp +++ b/main/update.cpp @@ -6,7 +6,7 @@ void app::make_test_chunk(chunk& c) { constexpr auto N = TILE_MAX_DIM; for (auto [x, k, pt] : c) { - const auto& atlas = pt.x > N/2 && pt.y >= N/2 ? floor2 : floor1; + const auto& atlas = pt.x == N/2 || pt.y == N/2 ? floor2 : floor1; x.ground_image = { atlas, (std::uint8_t)(k % atlas->num_tiles().product()) }; } constexpr auto K = N/2; diff --git a/shaders/tile-shader.cpp b/shaders/tile-shader.cpp index bda7114c..93c6a5c8 100644 --- a/shaders/tile-shader.cpp +++ b/shaders/tile-shader.cpp @@ -42,11 +42,7 @@ tile_shader& tile_shader::set_camera_offset(Vector2d camera_offset) static constexpr auto MAX = std::numeric_limits<std::int32_t>::max(); ASSERT(std::fabs(camera_offset[0]) <= MAX); ASSERT(std::fabs(camera_offset[1]) <= MAX); - if (camera_offset != _camera_offset) - { - _camera_offset = camera_offset; - setUniform(OffsetUniform, Vector2i{std::int32_t(camera_offset[0]*2), std::int32_t(camera_offset[1]*2)}); - } + _camera_offset = camera_offset; return *this; } @@ -58,4 +54,14 @@ tile_shader& tile_shader::set_tint(const Vector4& tint) return *this; } +void tile_shader::on_draw() +{ + if (const auto offset = Vector2i{(std::int32_t)_camera_offset[0], (std::int32_t)_camera_offset[1]}; + offset != _real_camera_offset) + { + _real_camera_offset = offset; + setUniform(OffsetUniform, offset); + } +} + } // namespace floormat diff --git a/shaders/tile-shader.hpp b/shaders/tile-shader.hpp index 50402030..1856f63b 100644 --- a/shaders/tile-shader.hpp +++ b/shaders/tile-shader.hpp @@ -23,6 +23,10 @@ struct tile_shader : GL::AbstractShaderProgram static constexpr Vector2d project(Vector3d pt); static constexpr Vector2d unproject(Vector2d px); + template<typename T, typename... Xs> + auto draw(T&& mesh, Xs&&... xs) -> + decltype(GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...)); + private: void on_draw(); @@ -34,10 +38,18 @@ private: enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, }; }; +template<typename T, typename... Xs> +auto tile_shader::draw(T&& mesh, Xs&&... xs) -> + decltype(GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...)) +{ + on_draw(); + return GL::AbstractShaderProgram::draw(std::forward<T>(mesh), std::forward<Xs>(xs)...); +} + constexpr Vector2d tile_shader::project(const Vector3d pt) { - const auto x = -pt[0]*.5, y = pt[1]*.5, z = pt[2]; - return { (x-y), (x+y+z)*.59 }; + const auto x = -pt[0], y = pt[1], z = pt[2]; + return { (x-y), (x+y+z*2)*.59 }; } constexpr Vector2d tile_shader::unproject(const Vector2d px) diff --git a/src/global-coords.hpp b/src/global-coords.hpp index d3c36c96..182511ea 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -13,15 +13,18 @@ struct chunk_coords final { }; struct global_coords final { - std::uint32_t x = 1 << 15, y = 1 << 15; + static constexpr std::uint32_t _0u = 1 << 15; + static constexpr auto _0s = std::int32_t(_0u); + + std::uint32_t x = _0u, y = _0u; constexpr global_coords(chunk_coords c, local_coords xy) : - x{ std::uint32_t(c.x + (1 << 15)) << 4 | (xy.x & 0x0f) }, - y{ std::uint32_t(c.y + (1 << 15)) << 4 | (xy.y & 0x0f) } + x{ std::uint32_t(c.x + _0s) << 4 | (xy.x & 0x0f) }, + y{ std::uint32_t(c.y + _0s) << 4 | (xy.y & 0x0f) } {} constexpr global_coords(std::uint32_t x, std::uint32_t y) noexcept : x{x}, y{y} {} constexpr global_coords(std::int32_t x, std::int32_t y) noexcept : - x{std::uint32_t(x + (1 << 15))}, y{std::uint32_t(y + (1 << 15))} + x{std::uint32_t(x + _0s)}, y{std::uint32_t(y + _0s)} {} constexpr global_coords() noexcept = default; @@ -35,26 +38,17 @@ struct global_coords final { constexpr local_coords global_coords::local() const noexcept { - return { - std::uint8_t(x & 0x0f), - std::uint8_t(y & 0x0f), - }; + return { std::uint8_t(x & 0x0f), std::uint8_t(y & 0x0f), }; } constexpr chunk_coords global_coords::chunk() const noexcept { - return { - std::int16_t((x - (1 << 15)) >> 4), - std::int16_t((y - (1 << 15)) >> 4), - }; + return { std::int16_t((x - _0u) >> 4), std::int16_t((y - _0u) >> 4), }; } constexpr Vector2i global_coords::to_signed() const noexcept { - return { - std::int32_t(x - (1 << 15)), - std::int32_t(y - (1 << 15)), - }; + return { std::int32_t(x - _0s), std::int32_t(y - _0s), }; } } // namespace floormat |