diff options
-rw-r--r-- | draw/anim.cpp | 4 | ||||
-rw-r--r-- | editor/draw.cpp | 13 | ||||
-rw-r--r-- | shaders/tile.cpp | 2 | ||||
-rw-r--r-- | src/camera-offset.cpp | 2 | ||||
-rw-r--r-- | src/chunk-collision.cpp | 4 | ||||
-rw-r--r-- | src/chunk-render.cpp | 6 | ||||
-rw-r--r-- | src/global-coords.hpp | 4 | ||||
-rw-r--r-- | src/local-coords.hpp | 9 |
8 files changed, 25 insertions, 19 deletions
diff --git a/draw/anim.cpp b/draw/anim.cpp index 566decb5..1e0d1bc4 100644 --- a/draw/anim.cpp +++ b/draw/anim.cpp @@ -32,7 +32,7 @@ void anim_mesh::add_clickable(tile_shader& shader, const Vector2i& win_size, const local_coords xy{i}; const auto& g = atlas->group(s.r); const auto& f = atlas->frame(s.r, s.frame); - const auto world_pos = TILE_SIZE20 * Vector3(xy.x, xy.y, 0) + Vector3(g.offset) + Vector3(s.offset[0], s.offset[1], 0); + const auto world_pos = TILE_SIZE20 * Vector3(Vector2(xy), 0) + Vector3(g.offset) + Vector3(Vector2(s.offset), 0); const Vector2ui offset((Vector2(shader.camera_offset()) + Vector2(win_size)*.5f) + shader.project(world_pos) - Vector2(f.ground)); clickable_scenery item = { @@ -129,7 +129,7 @@ void anim_mesh::draw(tile_shader& shader, anim_atlas& atlas, rotation r, std::si void anim_mesh::draw(tile_shader& shader, anim_atlas& atlas, rotation r, std::size_t frame, local_coords xy, Vector2b offset) { - const auto pos = Vector3(xy.x, xy.y, 0.f) * TILE_SIZE + Vector3(offset[0], offset[1], 0); + const auto pos = Vector3(Vector2(xy), 0) * TILE_SIZE + Vector3(Vector2(offset), 0); const float depth = tile_shader::depth_value(xy, tile_shader::scenery_depth_offset); draw(shader, atlas, r, frame, pos, depth); } diff --git a/editor/draw.cpp b/editor/draw.cpp index 5f56f01e..f5161b7d 100644 --- a/editor/draw.cpp +++ b/editor/draw.cpp @@ -24,7 +24,7 @@ void app::draw_cursor() { const auto draw = [&, pos = *cursor.tile](auto& mesh, const auto& size) { const auto pt = pos.to_signed(); - const Vector3 center{Vector3i(pt[0], pt[1], 0) * iTILE_SIZE}; + const auto center = Vector3(Vector3i(pt, 0) * iTILE_SIZE); mesh.draw(shader, {center, size, LINE_WIDTH}); }; @@ -99,11 +99,10 @@ void app::draw_collision_boxes() if (cursor.tile) { - const auto tile_ = M->pixel_to_tile_(Vector2d(*cursor.pixel)); - const auto subpixel = Vector2(Vector2ub(Vector2d(std::fmod(tile_[0], 1.), std::fmod(tile_[1], 1.)) * dTILE_SIZE2)); + const auto tile_ = Vector2(M->pixel_to_tile_(Vector2d(*cursor.pixel))); + const auto subpixel = Vector2(Vector2ub(Vector2d(std::fmod(tile_[0], 1.), std::fmod(tile_[1], 1.))*dTILE_SIZE2)); const auto tile = *cursor.tile; - const auto curchunk = tile.chunk(); - const auto curtile = tile.local(); + const auto curchunk = Vector2(tile.chunk()), curtile = Vector2(tile.local()); for (std::int16_t y = miny; y <= maxy; y++) for (std::int16_t x = minx; x <= maxx; x++) @@ -117,8 +116,8 @@ void app::draw_collision_boxes() if (floormat_main::check_chunk_visible(shader.camera_offset(), sz)) { constexpr auto chunk_size = TILE_SIZE2 * TILE_MAX_DIM, half_tile = TILE_SIZE2/2; - auto chunk_dist = (Vector2(pos.x, pos.y) - Vector2(curchunk.x, curchunk.y))*chunk_size; - auto t0 = chunk_dist + Vector2(curtile.x, curtile.y)*TILE_SIZE2 + subpixel - half_tile; + auto chunk_dist = (Vector2(pos) - curchunk)*chunk_size; + auto t0 = chunk_dist + curtile*TILE_SIZE2 + subpixel - half_tile; auto t1 = t0+Vector2(1e-4f); const auto* rtree = c.rtree(); rtree->Search(t0.data(), t1.data(), [&](std::uint64_t data, const rect_type& rect) { diff --git a/shaders/tile.cpp b/shaders/tile.cpp index 9df05f8e..8d859ff3 100644 --- a/shaders/tile.cpp +++ b/shaders/tile.cpp @@ -64,7 +64,7 @@ void tile_shader::_draw() if (_tint != _real_tint) setUniform(TintUniform, _real_tint = _tint); - if (const auto offset = Vector3((float)_camera_offset[0], (float)_camera_offset[1], _depth_offset); + if (const auto offset = Vector3(Vector2(_camera_offset), _depth_offset); offset != _real_camera_offset) { _real_camera_offset = offset; diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp index 1c77659c..15401bb0 100644 --- a/src/camera-offset.cpp +++ b/src/camera-offset.cpp @@ -13,7 +13,7 @@ with_shifted_camera_offset::with_shifted_camera_offset(tile_shader& shader, chun fm_assert(shader.depth_offset() == 0.f); constexpr auto chunk_size = TILE_MAX_DIM20d*dTILE_SIZE; - const auto offset = _camera + tile_shader::project(Vector3d(c.x, c.y, 0) * chunk_size); + const auto offset = _camera + tile_shader::project(Vector3d(Vector2d(c), 0) * chunk_size); const auto len_x = (float)(last.x - first.x), cx = (float)(c.x - first.x), cy = (float)(c.y - first.y); const float depth_offset = shader.depth_tile_size*(cy*TILE_MAX_DIM*len_x*TILE_MAX_DIM + cx*TILE_MAX_DIM); diff --git a/src/chunk-collision.cpp b/src/chunk-collision.cpp index de4f484e..d41c02c8 100644 --- a/src/chunk-collision.cpp +++ b/src/chunk-collision.cpp @@ -15,13 +15,13 @@ constexpr Vector2 tile_start(std::size_t k) { constexpr auto half_tile = Vector2(TILE_SIZE2)/2; const local_coords coord{k}; - return TILE_SIZE2 * Vector2(coord.x, coord.y) - half_tile; + return TILE_SIZE2 * Vector2(coord) - half_tile; } constexpr Pair<Vector2, Vector2> scenery_tile(std::size_t k, const scenery& sc) { const local_coords coord{k}; - auto center = TILE_SIZE2 * Vector2(coord.x, coord.y) + Vector2(sc.offset) + Vector2(sc.bbox_offset); + auto center = TILE_SIZE2 * Vector2(coord) + Vector2(sc.offset) + Vector2(sc.bbox_offset); auto start = center - Vector2(sc.bbox_size); auto size = Vector2(sc.bbox_size)*2; return { start, start + size, }; diff --git a/src/chunk-render.cpp b/src/chunk-render.cpp index d00424df..48425f72 100644 --- a/src/chunk-render.cpp +++ b/src/chunk-render.cpp @@ -45,7 +45,7 @@ auto chunk::ensure_ground_mesh() noexcept -> ground_mesh_tuple const std::uint8_t i = ground_indexes[k]; const auto& atlas = _ground_atlases[i]; const local_coords pos{i}; - const auto quad = atlas->floor_quad(Vector3(pos.x, pos.y, 0) * TILE_SIZE, TILE_SIZE2); + const auto quad = atlas->floor_quad(Vector3(Vector2(pos), 0) * TILE_SIZE, TILE_SIZE2); const auto texcoords = atlas->texcoords_for_id(_ground_variants[i]); const float depth = tile_shader::depth_value(pos); auto& v = vertexes[k]; @@ -88,7 +88,7 @@ auto chunk::ensure_wall_mesh() noexcept -> wall_mesh_tuple const auto& atlas = _wall_atlases[i]; const auto& variant = _wall_variants[i]; const local_coords pos{i / 2u}; - const auto center = Vector3(pos.x, pos.y, 0) * TILE_SIZE; + const auto center = Vector3(Vector2(pos), 0) * TILE_SIZE; const auto quad = i & 1 ? atlas->wall_quad_W(center, TILE_SIZE) : atlas->wall_quad_N(center, TILE_SIZE); const float depth = tile_shader::depth_value(pos); const auto texcoords = atlas->texcoords_for_id(variant); @@ -133,7 +133,7 @@ auto chunk::ensure_scenery_mesh() noexcept -> scenery_mesh_tuple const local_coords pos{i}; const auto& atlas = _scenery_atlases[i]; const auto& fr = _scenery_variants[i]; - const auto coord = Vector3(pos.x, pos.y, 0) * TILE_SIZE + Vector3(Vector2(fr.offset), 0); + const auto coord = Vector3(Vector2(pos), 0) * TILE_SIZE + Vector3(Vector2(fr.offset), 0); const auto quad = atlas->frame_quad(coord, fr.r, fr.frame); const auto& group = atlas->group(fr.r); const auto texcoords = atlas->texcoords_for_frame(fr.r, fr.frame, !group.mirror_from.isEmpty()); diff --git a/src/global-coords.hpp b/src/global-coords.hpp index 709b2a3a..1dc99af7 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -11,6 +11,10 @@ struct chunk_coords final { constexpr bool operator==(const chunk_coords& other) const noexcept = default; constexpr Vector2i operator-(chunk_coords other) const noexcept; + + template<typename T> + requires (std::is_floating_point_v<T> || std::is_integral_v<T> && (sizeof(T) > sizeof(x) || std::is_same_v<T, std::decay_t<decltype(x)>>)) + explicit constexpr operator Math::Vector2<T>() const noexcept { return Math::Vector2<T>(T(x), T(y)); } }; constexpr Vector2i chunk_coords::operator-(chunk_coords other) const noexcept diff --git a/src/local-coords.hpp b/src/local-coords.hpp index 30fb3617..f53c87ee 100644 --- a/src/local-coords.hpp +++ b/src/local-coords.hpp @@ -2,6 +2,7 @@ #include "compat/assert.hpp" #include "tile-defs.hpp" #include <concepts> +#include <Magnum/Math/Vector2.h> namespace floormat { @@ -9,10 +10,12 @@ struct local_coords final { std::uint8_t x : 4 = 0, y : 4 = 0; explicit constexpr local_coords(std::size_t idx) noexcept; constexpr local_coords() noexcept = default; - template<std::integral T> requires (sizeof(T) <= sizeof(std::size_t)) + template<typename T> requires (std::is_integral_v<T> && sizeof(T) <= sizeof(std::size_t)) constexpr local_coords(T x, T y) noexcept; constexpr local_coords(std::uint8_t x, std::uint8_t y) noexcept : x{x}, y{y} {} constexpr std::uint8_t to_index() const noexcept { return y*TILE_MAX_DIM + x; } + + template<typename T> explicit constexpr operator Math::Vector2<T>() const noexcept { return Math::Vector2<T>(T(x), T(y)); } }; constexpr local_coords::local_coords(std::size_t index) noexcept : @@ -22,8 +25,8 @@ constexpr local_coords::local_coords(std::size_t index) noexcept : fm_assert(index < TILE_COUNT); } -template<std::integral T> -requires (sizeof(T) <= sizeof(std::size_t)) +template<typename T> +requires (std::is_integral_v<T> && sizeof(T) <= sizeof(std::size_t)) constexpr local_coords::local_coords(T x, T y) noexcept : x{(std::uint8_t)x}, y{(std::uint8_t)y} { |