diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-18 01:55:30 +0200 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-18 01:55:30 +0200 |
| commit | aebec6e9b095149252bf1e4642c211ae98efdf84 (patch) | |
| tree | dfb9919de3fa8bb1a7ff99c9cb3fcbf17186e7c2 /main | |
| parent | 2ce80cb4a85cc47dc10f0b70839b2d0f2381cca8 (diff) | |
a
Diffstat (limited to 'main')
| -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 |
4 files changed, 81 insertions, 49 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 |
