diff options
| author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-17 16:37:25 +0200 |
|---|---|---|
| committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-17 16:37:25 +0200 |
| commit | d9f58950e8cd58b7048f5f505db91323e0237063 (patch) | |
| tree | 43f4eacd4cf0a025d6bf45b346d52ac60a2c4cb1 | |
| parent | 1291f836ede29c23aea7bea20998105aa9fbea84 (diff) | |
a
| -rw-r--r-- | main/app.cpp | 43 | ||||
| -rw-r--r-- | main/app.hpp | 27 | ||||
| -rw-r--r-- | main/camera.cpp | 8 | ||||
| -rw-r--r-- | main/draw.cpp | 51 | ||||
| -rw-r--r-- | main/gui.cpp | 2 | ||||
| -rw-r--r-- | main/imgui-raii.hpp | 2 | ||||
| -rw-r--r-- | main/update.cpp | 8 | ||||
| -rw-r--r-- | shaders/tile-shader.hpp | 15 | ||||
| -rw-r--r-- | src/camera-offset.cpp | 27 | ||||
| -rw-r--r-- | src/camera-offset.hpp | 18 |
10 files changed, 129 insertions, 72 deletions
diff --git a/main/app.cpp b/main/app.cpp index 4150e452..569cc3b9 100644 --- a/main/app.cpp +++ b/main/app.cpp @@ -50,18 +50,21 @@ void app::mousePressEvent(Platform::Sdl2Application::MouseEvent& event) if (_imgui.handleMousePressEvent(event)) return event.setAccepted(); { - const auto tile = pixel_to_tile(Vector2(*_cursor_pos)); - int button; - switch (event.button()) + if (_cursor_tile) { - case MouseEvent::Button::Left: button = 0; break; - case MouseEvent::Button::Right: button = 1; break; - case MouseEvent::Button::Middle: button = 2; break; - case MouseEvent::Button::X1: button = 5; break; - case MouseEvent::Button::X2: button = 6; break; - default: button = -1; break; + const auto& tile = *_cursor_tile; + int button; + switch (event.button()) + { + case MouseEvent::Button::Left: button = 0; break; + case MouseEvent::Button::Right: button = 1; break; + case MouseEvent::Button::Middle: button = 2; break; + case MouseEvent::Button::X1: button = 5; break; + case MouseEvent::Button::X2: button = 6; break; + default: button = -1; break; + } + do_mouse_click(tile, button); } - do_mouse_click(tile, button); } } @@ -80,8 +83,23 @@ void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event) { if (_imgui.handleMouseMoveEvent(event)) - return _cursor_pos = {}, event.setAccepted(); + return _cursor_tile = std::nullopt, event.setAccepted(); + _cursor_pos = event.position(); + recalc_cursor_tile(); +} + +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); + } + else + _cursor_tile = std::nullopt; } void app::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event) @@ -117,16 +135,17 @@ void app::anyEvent(SDL_Event& event) void app::event_leave() { _cursor_pos = std::nullopt; + _cursor_tile = std::nullopt; } void app::event_enter() { - } void app::event_mouse_leave() { _cursor_pos = std::nullopt; + _cursor_tile = std::nullopt; } void app::event_mouse_enter() diff --git a/main/app.hpp b/main/app.hpp index b1aef955..54e2ee4d 100644 --- a/main/app.hpp +++ b/main/app.hpp @@ -34,6 +34,8 @@ struct app final : Platform::Application void do_camera(float dt); void do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated); + void do_mouse_click(global_coords pos, int button); + void recalc_cursor_tile(); void keyPressEvent(KeyEvent& event) override; void keyReleaseEvent(KeyEvent& event) override; @@ -52,6 +54,7 @@ struct app final : Platform::Application void drawEvent() override; std::array<std::int16_t, 4> get_draw_bounds() const noexcept; void draw_world(); + void draw_cursor_tile(); void draw_wireframe_quad(global_coords pt); void draw_wireframe_box(local_coords pt); @@ -66,13 +69,6 @@ struct app final : Platform::Application void* register_debug_callback(); global_coords pixel_to_tile(Vector2 position) const; - void draw_cursor_tile(); - void do_mouse_click(global_coords pos, int button); - - std::optional<Vector2i> _cursor_pos; - - static constexpr Vector2 project(Vector3 pt); - static constexpr Vector2 unproject(Vector2 px); enum class key : int { camera_up, camera_left, camera_right, camera_down, camera_reset, @@ -100,20 +96,11 @@ struct app final : Platform::Application enum_bitset<key> keys; Magnum::Timeline timeline; editor _editor; + std::optional<Vector2i> _cursor_pos; + std::optional<global_coords> _cursor_tile; - static constexpr std::int32_t BASE_X = 0, BASE_Y = 0; -}; - -constexpr Vector2 app::project(const Vector3 pt) -{ - const float x = -pt[1], y = -pt[0], z = pt[2]; - return { x-y, (x+y+z*2)*.59f }; -} -constexpr Vector2 app::unproject(const Vector2 px) -{ - const float X = px[0], Y = px[1]; - return { X/2 + 50.f * Y / 59, 50 * Y / 59 - X/2 }; -} + static constexpr std::int16_t BASE_X = 0, BASE_Y = 0; +}; } // namespace floormat diff --git a/main/camera.cpp b/main/camera.cpp index b0b8f001..dbead9a3 100644 --- a/main/camera.cpp +++ b/main/camera.cpp @@ -5,7 +5,7 @@ namespace floormat { void app::do_camera(float dt) { - constexpr float pixels_per_second = 256; + constexpr float pixels_per_second = 384; if (keys[key::camera_up]) camera_offset += Vector2(0, 1) * dt * pixels_per_second; else if (keys[key::camera_down]) @@ -24,12 +24,14 @@ void app::do_camera(float dt) if (keys[key::camera_reset]) reset_camera_offset(); + + recalc_cursor_tile(); } void app::reset_camera_offset() { - camera_offset = project({TILE_MAX_DIM*TILE_SIZE[0]/2.f, TILE_MAX_DIM*TILE_SIZE[1]/2.f, 0}); - //camera_offset = {}; + camera_offset = tile_shader::project({TILE_MAX_DIM/2.f*TILE_SIZE[0], TILE_MAX_DIM/2.f*TILE_SIZE[1], 0}); + recalc_cursor_tile(); } void app::update_window_scale(Vector2i sz) diff --git a/main/draw.cpp b/main/draw.cpp index e000bc51..a7ee2ec3 100644 --- a/main/draw.cpp +++ b/main/draw.cpp @@ -1,5 +1,6 @@ #include "app.hpp" #include "tile-defs.hpp" +#include "camera-offset.hpp" #include <Magnum/Math/Vector3.h> #include <Magnum/GL/DefaultFramebuffer.h> #include <Magnum/GL/Renderer.h> @@ -8,25 +9,21 @@ namespace floormat { void app::drawEvent() { -#if 0 - GL::defaultFramebuffer.clear(GL::FramebufferClear::Color | GL::FramebufferClear::Depth); - GL::Renderer::setDepthMask(true); - GL::Renderer::setDepthFunction(GL::Renderer::DepthFunction::LessOrEqual); - GL::Renderer::enable(GL::Renderer::Feature::DepthTest); -#else GL::defaultFramebuffer.clear(GL::FramebufferClear::Color); GL::Renderer::setDepthFunction(GL::Renderer::DepthFunction::Never); -#endif - //update_window_scale(windowSize()); { - float dt = std::min(1.f/20, timeline.previousFrameDuration()); + const float dt = std::min(1.f/10, timeline.previousFrameDuration()); update(dt); } _shader.set_tint({1, 1, 1, 1}); - draw_world(); - draw_cursor_tile(); + { + const with_shifted_camera_offset o{_shader, BASE_X, BASE_Y}; + draw_world(); + draw_cursor_tile(); + } + display_menu(); swapBuffers(); @@ -60,16 +57,11 @@ void app::draw_world() { auto [minx, maxx, miny, maxy] = get_draw_bounds(); - const auto old_camera_offset = _shader.camera_offset(); _shader.set_tint({1, 1, 1, 1}); for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) { - const auto offset = project({float(x + BASE_X)*TILE_MAX_DIM*TILE_SIZE[0], - float(y + BASE_Y)*TILE_MAX_DIM*TILE_SIZE[1], - 0}); - _shader.set_camera_offset(offset + old_camera_offset); auto c = _world[chunk_coords{x, y}]; _floor_mesh.draw(_shader, *c); } @@ -77,29 +69,22 @@ void app::draw_world() for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) { - const auto offset = project({float(x + BASE_X)*TILE_MAX_DIM*TILE_SIZE[0], - float(y + BASE_Y)*TILE_MAX_DIM*TILE_SIZE[1], - 0}); - _shader.set_camera_offset(offset + old_camera_offset); auto c = _world[chunk_coords{x, y}]; _wall_mesh.draw(_shader, *c); } - - _shader.set_camera_offset(old_camera_offset); } void app::draw_wireframe_quad(global_coords pos) { constexpr float LINE_WIDTH = 1; - - if (const auto& [c, tile] = _world[pos]; !tile.ground_image) - return; - const auto pt = pos.to_signed(); - constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1]; - const Vector3 center {X*pt[0], Y*pt[1], 0}; - _shader.set_tint({1, 0, 0, 1}); - _wireframe_quad.draw(_shader, {center, {TILE_SIZE[0], TILE_SIZE[1]}, LINE_WIDTH}); + + if (const auto& [c, tile] = _world[pos]; tile.ground_image) + { + const Vector3 center{pt[0]*TILE_SIZE[0], pt[1]*TILE_SIZE[1], 0}; + _shader.set_tint({1, 0, 0, 1}); + _wireframe_quad.draw(_shader, {center, {TILE_SIZE[0], TILE_SIZE[1]}, LINE_WIDTH}); + } } void app::draw_wireframe_box(local_coords pt) @@ -113,4 +98,10 @@ void app::draw_wireframe_box(local_coords pt) _wireframe_box.draw(_shader, {center1, size, LINE_WIDTH}); } +void app::draw_cursor_tile() +{ + if (_cursor_tile) + draw_wireframe_quad(*_cursor_tile); +} + } // namespace floormat diff --git a/main/gui.cpp b/main/gui.cpp index 85641f0f..e665c27b 100644 --- a/main/gui.cpp +++ b/main/gui.cpp @@ -1,7 +1,9 @@ #include "app.hpp" #include "imgui-raii.hpp" #include <Magnum/GL/Renderer.h> +#ifndef __CLION_IDE__ #include <Magnum/ImGuiIntegration/Integration.h> +#endif namespace floormat { diff --git a/main/imgui-raii.hpp b/main/imgui-raii.hpp index 931b5189..f3da1f60 100644 --- a/main/imgui-raii.hpp +++ b/main/imgui-raii.hpp @@ -2,7 +2,9 @@ #include <Corrade/Containers/StringView.h> #include <Magnum/Math/Color.h> +#ifndef __CLION_IDE__ #include <ImGui.h> +#endif namespace floormat::imgui { diff --git a/main/update.cpp b/main/update.cpp index 28e1d985..eeeede2b 100644 --- a/main/update.cpp +++ b/main/update.cpp @@ -32,15 +32,9 @@ void app::update(float dt) global_coords app::pixel_to_tile(Vector2 position) const { const Vector2 px = position - Vector2{windowSize()}*.5f - camera_offset; - const Vector2 vec = unproject(px) / Vector2{TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f} + Vector2{.5f, .5f}; + const Vector2 vec = tile_shader::unproject(px) / Vector2{TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f} + Vector2{.5f, .5f}; const auto x = (std::int32_t)std::floor(vec[0]), y = (std::int32_t)std::floor(vec[1]); return { x, y }; } -void app::draw_cursor_tile() -{ - if (_cursor_pos) - draw_wireframe_quad(pixel_to_tile(Vector2(*_cursor_pos))); -} - } // namespace floormat diff --git a/shaders/tile-shader.hpp b/shaders/tile-shader.hpp index c53dfff5..0dd2b631 100644 --- a/shaders/tile-shader.hpp +++ b/shaders/tile-shader.hpp @@ -20,6 +20,9 @@ struct tile_shader : GL::AbstractShaderProgram Vector4 tint() const { return tint_; } tile_shader& set_tint(const Vector4& tint); + static constexpr Vector2 project(Vector3 pt); + static constexpr Vector2 unproject(Vector2 px); + private: Vector2 scale_, camera_offset_; Vector4 tint_; @@ -27,4 +30,16 @@ private: enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, }; }; +constexpr Vector2 tile_shader::project(const Vector3 pt) +{ + const float x = -pt[1], y = -pt[0], z = pt[2]; + return { x-y, (x+y+z*2)*.59f }; +} + +constexpr Vector2 tile_shader::unproject(const Vector2 px) +{ + const float X = px[0], Y = px[1]; + return { X/2 + 50.f * Y / 59, 50 * Y / 59 - X/2 }; +} + } // namespace floormat diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp new file mode 100644 index 00000000..47cb690d --- /dev/null +++ b/src/camera-offset.cpp @@ -0,0 +1,27 @@ +#include "camera-offset.hpp" +#include "shaders/tile-shader.hpp" + +namespace floormat { + +with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, std::int32_t x, std::int32_t y) : + with_shifted_camera_offset{shader, chunk_coords{std::int16_t(x), std::int16_t(y)}} +{ + ASSERT(std::abs(x) < (1 << 15) && std::abs(y) < (1 << 15)); +} + +with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, const chunk_coords c) : + s{shader}, + orig_offset(shader.camera_offset()) +{ + const auto offset = tile_shader::project({float(c.x)*TILE_MAX_DIM*TILE_SIZE[0], + float(c.y)*TILE_MAX_DIM*TILE_SIZE[1], + 0}); + s.set_camera_offset(orig_offset + offset); +} + +with_shifted_camera_offset::~with_shifted_camera_offset() +{ + s.set_camera_offset(orig_offset); +} + +} // namespace floormat diff --git a/src/camera-offset.hpp b/src/camera-offset.hpp new file mode 100644 index 00000000..da07cec0 --- /dev/null +++ b/src/camera-offset.hpp @@ -0,0 +1,18 @@ +#pragma once +#include "src/global-coords.hpp" + +namespace floormat { + +struct tile_shader; + +struct with_shifted_camera_offset final +{ + explicit with_shifted_camera_offset(tile_shader& shader, std::int32_t, std::int32_t); + explicit with_shifted_camera_offset(tile_shader& shader, chunk_coords c); + ~with_shifted_camera_offset(); +private: + tile_shader& s; // NOLINT + Vector2 orig_offset; +}; + +} // namespace floormat |
