diff options
-rw-r--r-- | main/app.hpp | 3 | ||||
-rw-r--r-- | main/camera.cpp | 61 | ||||
-rw-r--r-- | main/draw.cpp | 34 | ||||
-rw-r--r-- | main/gui.cpp | 32 | ||||
-rw-r--r-- | shaders/tile-shader.cpp | 14 | ||||
-rw-r--r-- | shaders/tile-shader.hpp | 23 | ||||
-rw-r--r-- | shaders/tile-shader.vert | 2 | ||||
-rw-r--r-- | src/camera-offset.cpp | 5 | ||||
-rw-r--r-- | src/camera-offset.hpp | 2 | ||||
-rw-r--r-- | src/tile-defs.hpp | 1 |
10 files changed, 108 insertions, 69 deletions
diff --git a/main/app.hpp b/main/app.hpp index 861b4193..89439dc0 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -63,13 +63,14 @@ struct app final : Platform::Application float draw_main_menu(); void draw_editor_pane(tile_type& type, float main_menu_height); void draw_fps(); + void draw_cursor_coord(); void display_menu(); void debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id, GL::DebugOutput::Severity severity, const std::string& str) const; void* register_debug_callback(); - global_coords pixel_to_tile(Vector2 position) const; + global_coords pixel_to_tile(Vector2d position) const; enum class key : int { camera_up, camera_left, camera_right, camera_down, camera_reset, diff --git a/main/camera.cpp b/main/camera.cpp index 85aa4443..b7e2b893 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -3,21 +3,23 @@ namespace floormat { -void app::do_camera(float dt) +void app::do_camera(float dt_) { + constexpr int pixels_per_second = 768; + const auto dt = (double)dt_; auto camera_offset = _shader.camera_offset(); - constexpr float pixels_per_second = 768; + if (keys[key::camera_up]) - camera_offset += Vector2{0, 1} * dt * pixels_per_second; + camera_offset += Vector2d{0, 1} * dt * pixels_per_second; else if (keys[key::camera_down]) - camera_offset += Vector2{0, -1} * dt * pixels_per_second; + camera_offset += Vector2d{0, -1} * dt * pixels_per_second; if (keys[key::camera_left]) - camera_offset += Vector2{1, 0} * dt * pixels_per_second; + camera_offset += Vector2d{1, 0} * dt * pixels_per_second; else if (keys[key::camera_right]) - camera_offset += Vector2{-1, 0} * dt * pixels_per_second; + camera_offset += Vector2d{-1, 0} * dt * pixels_per_second; { - const auto max_camera_offset = Vector2(windowSize() * 10); + const auto max_camera_offset = Vector2d(windowSize() * 10); camera_offset[0] = std::clamp(camera_offset[0], -max_camera_offset[0], max_camera_offset[0]); camera_offset[1] = std::clamp(camera_offset[1], -max_camera_offset[1], max_camera_offset[1]); } @@ -31,7 +33,8 @@ void app::do_camera(float dt) void app::reset_camera_offset() { - _shader.set_camera_offset(tile_shader::project({TILE_MAX_DIM*.25f*TILE_SIZE[0], TILE_MAX_DIM*.25f*TILE_SIZE[1], 0})); + //_shader.set_camera_offset(tile_shader::project({TILE_MAX_DIM*.25*dTILE_SIZE[0], TILE_MAX_DIM*.25*dTILE_SIZE[1], 0})); + _shader.set_camera_offset({}); recalc_cursor_tile(); } @@ -43,22 +46,46 @@ void app::update_window_scale(Vector2i sz) void app::recalc_cursor_tile() { if (_cursor_pos) - { - constexpr Vector2 base_offset = - tile_shader::project({(float)TILE_MAX_DIM*BASE_X*TILE_SIZE[0], - (float)TILE_MAX_DIM*BASE_Y*TILE_SIZE[1], 0}); - _cursor_tile = pixel_to_tile(Vector2(*_cursor_pos) - base_offset); - } + _cursor_tile = pixel_to_tile(Vector2d(*_cursor_pos)); else _cursor_tile = std::nullopt; } -global_coords app::pixel_to_tile(Vector2 position) const +global_coords app::pixel_to_tile(Vector2d position) const { - const Vector2 px = position - Vector2{windowSize()}*.5f - _shader.camera_offset(); - const Vector2 vec = tile_shader::unproject(px) / Vector2{TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f} + Vector2{.5f, .5f}; + const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset(); + 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 }; } +namespace sqrt_detail +{ +constexpr double sqrt_Newton_Raphson(double x, double curr, double prev) +{ + constexpr auto abs = [](double x) constexpr { return x < 0 ? -x : x; }; + while (abs(curr - prev) > 1e-16) + { + prev = curr; + curr = .5 * (curr + x/curr); + } + return curr; +} +} // namespace sqrt_detail +constexpr double ce_sqrt(double x) +{ + return sqrt_detail::sqrt_Newton_Raphson(x, x, 0); +} + +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; + + return { + std::int16_t(center.x - N), std::int16_t(center.x + N), + std::int16_t(center.y - N), std::int16_t(center.y + N), + }; +} + } // namespace floormat diff --git a/main/draw.cpp b/main/draw.cpp index b7dd420a..18319f95 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -41,42 +41,14 @@ void app::drawEvent() { timeline.nextFrame(); } -std::array<std::int16_t, 4> app::get_draw_bounds() const noexcept -{ - using limits = std::numeric_limits<std::int16_t>; - constexpr auto MIN = limits::min(), MAX = limits::max(); - std::int16_t minx = MAX, maxx = MIN, miny = MAX, maxy = MIN; - { - auto fn = [&](std::int32_t x, std::int32_t y) { - const auto pos = pixel_to_tile(Vector2(x, y)).chunk(); - minx = std::min(minx, pos.x); - maxx = std::max(maxx, pos.x); - miny = std::min(miny, pos.y); - maxy = std::max(maxy, pos.y); - }; - const auto sz = windowSize(); - const auto x = sz[0]-1, y = sz[1]-1; - fn(0, 0); - fn(x, 0); - fn(0, y); - fn(x, y); - } - return { - std::int16_t(minx), std::int16_t(maxx), - std::int16_t(miny), std::int16_t(maxy), - }; -} - void app::draw_world() { #if 0 _floor_mesh.draw(_shader, *_world[chunk_coords{0, 0}]); _wall_mesh.draw(_shader, *_world[chunk_coords{0, 0}]); #else - auto [minx, maxx, miny, maxy] = get_draw_bounds(); - - printf("%hd %hd -> %hd %hd\n", minx, miny, maxx, maxy); - fflush(stdout); + auto foo = get_draw_bounds(); + auto [minx, maxx, miny, maxy] = foo; for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) @@ -85,7 +57,9 @@ void app::draw_world() make_test_chunk(*_world[chunk_coords{x, y}]); const with_shifted_camera_offset o{_shader, x, y}; _floor_mesh.draw(_shader, *_world[chunk_coords{x, y}]); + printf("OFFSET %hd %hd ---> %f %f\n", x, y, _shader.camera_offset()[0], _shader.camera_offset()[1]); } + fflush(stdout); for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) diff --git a/main/gui.cpp b/main/gui.cpp index 37ca1227..8d2cb97e 100644 --- a/main/gui.cpp +++ b/main/gui.cpp @@ -1,6 +1,6 @@ #include "app.hpp" #include <Magnum/GL/Renderer.h> -#ifndef __CLION_IDE__zz +#ifndef __CLION_IDE__ #include "imgui-raii.hpp" #include <Magnum/ImGuiIntegration/Integration.h> #endif @@ -57,6 +57,7 @@ void app::draw_ui() const float main_menu_height = draw_main_menu(); draw_editor_pane(_editor.floor(), main_menu_height); draw_fps(); + draw_cursor_coord(); } void app::draw_editor_pane(tile_type& type, float main_menu_height) @@ -166,4 +167,33 @@ void app::draw_fps() } } +void app::draw_cursor_coord() +{ + if (!_cursor_tile) + return; + + auto c1 = push_style_var(ImGuiStyleVar_FramePadding, {0, 0}); + auto c2 = push_style_var(ImGuiStyleVar_WindowPadding, {0, 0}); + auto c3 = push_style_var(ImGuiStyleVar_WindowBorderSize, 0); + auto c4 = push_style_var(ImGuiStyleVar_WindowMinSize, {1, 1}); + auto c5 = push_style_var(ImGuiStyleVar_ScrollbarSize, 0); + auto c6 = push_style_color(ImGuiCol_Text, {.9, .9, .9, 1}); + + char buf[64]; + const auto coord = *_cursor_tile; + const auto chunk = coord.chunk(); + const auto local = coord.local(); + snprintf(buf, sizeof(buf), "%hd:%hd - %hhu:%hhu", chunk.x, chunk.y, local.x, local.y); + const auto size = ImGui::CalcTextSize(buf); + + ImGui::SetNextWindowPos({windowSize()[0]/2 - size.x/2, 3}); + ImGui::SetNextWindowSize(size); + if (auto flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | + ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; + auto b = begin_window("tile-coord", ImGuiWindowFlags_(flags))) + { + ImGui::Text("%s", buf); + } +} + } // namespace floormat diff --git a/shaders/tile-shader.cpp b/shaders/tile-shader.cpp index 20b2230f..4c694c9e 100644 --- a/shaders/tile-shader.cpp +++ b/shaders/tile-shader.cpp @@ -1,5 +1,6 @@ #include "shaders/tile-shader.hpp" #include "loader.hpp" +#include "compat/assert.hpp" #include <algorithm> #include <Corrade/Containers/Reference.h> #include <Corrade/Utility/Resource.h> @@ -36,12 +37,17 @@ tile_shader& tile_shader::set_scale(const Vector2& scale) return *this; } -tile_shader& tile_shader::set_camera_offset(Vector2 camera_offset) +tile_shader& tile_shader::set_camera_offset(Vector2d camera_offset) { - CORRADE_INTERNAL_ASSERT(std::fabs(camera_offset[0]) <= std::scalbn(1.f, std::numeric_limits<float>::digits)); - CORRADE_INTERNAL_ASSERT(std::fabs(camera_offset[1]) <= std::scalbn(1.f, std::numeric_limits<float>::digits)); + 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_) - setUniform(OffsetUniform, 2*(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)}); + } + return *this; } diff --git a/shaders/tile-shader.hpp b/shaders/tile-shader.hpp index 0dd2b631..af0d778c 100644 --- a/shaders/tile-shader.hpp +++ b/shaders/tile-shader.hpp @@ -15,31 +15,32 @@ struct tile_shader : GL::AbstractShaderProgram Vector2 scale() const { return scale_; } tile_shader& set_scale(const Vector2& scale); - Vector2 camera_offset() const { return camera_offset_; } - tile_shader& set_camera_offset(Vector2 camera_offset); + Vector2d camera_offset() const { return camera_offset_; } + tile_shader& set_camera_offset(Vector2d camera_offset); Vector4 tint() const { return tint_; } tile_shader& set_tint(const Vector4& tint); - static constexpr Vector2 project(Vector3 pt); - static constexpr Vector2 unproject(Vector2 px); + static constexpr Vector2d project(Vector3d pt); + static constexpr Vector2d unproject(Vector2d px); private: - Vector2 scale_, camera_offset_; + Vector2d camera_offset_; + Vector2 scale_; Vector4 tint_; enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, }; }; -constexpr Vector2 tile_shader::project(const Vector3 pt) +constexpr Vector2d tile_shader::project(const Vector3d pt) { - const float x = -pt[1], y = -pt[0], z = pt[2]; - return { x-y, (x+y+z*2)*.59f }; + const auto x = -pt[0]*.5, y = pt[1]*.5, z = pt[2]; + return { (x-y), (x+y+z)*.59 }; } -constexpr Vector2 tile_shader::unproject(const Vector2 px) +constexpr Vector2d tile_shader::unproject(const Vector2d px) { - const float X = px[0], Y = px[1]; - return { X/2 + 50.f * Y / 59, 50 * Y / 59 - X/2 }; + const auto X = px[0], Y = px[1]; + return { X/2 + 50 * Y / 59, 50 * Y / 59 - X/2 }; } } // namespace floormat diff --git a/shaders/tile-shader.vert b/shaders/tile-shader.vert index 8b3f2dba..d28f25fb 100644 --- a/shaders/tile-shader.vert +++ b/shaders/tile-shader.vert @@ -1,7 +1,7 @@ precision highp float; layout (location = 0) uniform vec2 scale; -layout (location = 1) uniform vec2 offset; +layout (location = 1) uniform ivec2 offset; layout (location = 0) in vec4 position; layout (location = 1) in vec2 texcoords; diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp index 0bf51c3f..63de0b8b 100644 --- a/src/camera-offset.cpp +++ b/src/camera-offset.cpp @@ -12,11 +12,10 @@ with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, shor _shader{shader}, _offset{shader.camera_offset()} { - const auto offset = _offset + tile_shader::project({float(x)*TILE_MAX_DIM*TILE_SIZE[0]*.5f, - float(y)*TILE_MAX_DIM*TILE_SIZE[1]*.5f, + const auto offset = _offset + tile_shader::project({-double(x)*TILE_MAX_DIM*dTILE_SIZE[0], + double(y)*TILE_MAX_DIM*dTILE_SIZE[1], 0}); _shader.set_camera_offset(offset); - ASSERT(std::abs(offset[0]) < 1 << 24 && std::abs(offset[1]) < 1 << 24); } with_shifted_camera_offset::~with_shifted_camera_offset() diff --git a/src/camera-offset.hpp b/src/camera-offset.hpp index 927d5693..07b9f398 100644 --- a/src/camera-offset.hpp +++ b/src/camera-offset.hpp @@ -12,7 +12,7 @@ struct with_shifted_camera_offset final ~with_shifted_camera_offset(); private: tile_shader& _shader; // NOLINT - Vector2 _offset; + Vector2d _offset; }; } // namespace floormat diff --git a/src/tile-defs.hpp b/src/tile-defs.hpp index ec59e19f..df26fe8e 100644 --- a/src/tile-defs.hpp +++ b/src/tile-defs.hpp @@ -6,5 +6,6 @@ namespace floormat { constexpr inline std::size_t TILE_MAX_DIM = 16; constexpr inline std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM; constexpr inline float TILE_SIZE[3] = { 128, 128, 384 }; +constexpr inline double dTILE_SIZE[3] = { 128, 128, 384 }; } // namespace floormat |