summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-29 18:59:39 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-29 18:59:39 +0200
commitd9cf49f11a5767fab52994e5c38d58ba16b13af6 (patch)
tree0d9adf6eb81bc75f0c1aaf616efe554ff6c64b56
parentd309ed0b2ac06d7ba48322303d56bf39c8b57fe4 (diff)
use vectors without open-coding vector ops
Suggested by mosra.
-rw-r--r--draw/floor.cpp10
-rw-r--r--draw/wall.cpp8
-rw-r--r--editor/camera.cpp3
-rw-r--r--editor/draw.cpp11
-rw-r--r--editor/imgui.cpp3
-rw-r--r--main/draw.cpp4
-rw-r--r--src/camera-offset.cpp4
-rw-r--r--src/tile-defs.hpp18
8 files changed, 30 insertions, 31 deletions
diff --git a/draw/floor.cpp b/draw/floor.cpp
index bfba0c00..444ee127 100644
--- a/draw/floor.cpp
+++ b/draw/floor.cpp
@@ -74,13 +74,9 @@ std::array<std::array<UnsignedShort, 6>, TILE_COUNT> floor_mesh::make_index_arra
std::array<std::array<Vector3, 4>, TILE_COUNT> floor_mesh::make_position_array()
{
std::array<std::array<Vector3, 4>, TILE_COUNT> array;
- constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1];
- for (std::size_t j = 0, k = 0; j < TILE_MAX_DIM; j++)
- for (std::size_t i = 0; i < TILE_MAX_DIM; i++, k++)
- {
- Vector3 center {(float)(X*i), (float)(Y*j), 0};
- array[k] = { tile_atlas::floor_quad(center, {X, Y}) };
- }
+ for (std::uint8_t j = 0, k = 0; j < TILE_MAX_DIM; j++)
+ for (std::uint8_t i = 0; i < TILE_MAX_DIM; i++, k++)
+ array[k] = { tile_atlas::floor_quad(Vector3(i, j, 0) * TILE_SIZE, TILE_SIZE2) };
return array;
}
diff --git a/draw/wall.cpp b/draw/wall.cpp
index ff3969fd..ae2e848c 100644
--- a/draw/wall.cpp
+++ b/draw/wall.cpp
@@ -78,15 +78,13 @@ std::array<std::array<UnsignedShort, 6>, wall_mesh::COUNT> wall_mesh::make_index
std::array<std::array<Vector3, 4>, wall_mesh::COUNT> wall_mesh::make_position_array()
{
std::array<std::array<Vector3, 4>, COUNT> array;
- constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2];
- constexpr Vector3 size = {X, Y, Z};
for (std::size_t j = 0; j < TILE_MAX_DIM; j++)
for (std::size_t i = 0; i < TILE_MAX_DIM; i++)
{
const auto idx = (j*TILE_MAX_DIM + i) * 2;
- Vector3 center{(float)(X*i), (float)(Y*j), 0};
- array[idx + 0] = tile_atlas::wall_quad_N(center, size);
- array[idx + 1] = tile_atlas::wall_quad_W(center, size);
+ const auto center = Vector3((float)i, (float)j, 0) * TILE_SIZE;
+ array[idx + 0] = tile_atlas::wall_quad_N(center, TILE_SIZE);
+ array[idx + 1] = tile_atlas::wall_quad_W(center, TILE_SIZE);
}
return array;
}
diff --git a/editor/camera.cpp b/editor/camera.cpp
index 2d46cba6..bbb7d766 100644
--- a/editor/camera.cpp
+++ b/editor/camera.cpp
@@ -49,7 +49,8 @@ void app::do_camera(float dt)
void app::reset_camera_offset()
{
- M->shader().set_camera_offset(tile_shader::project({TILE_MAX_DIM*-.5*dTILE_SIZE[0], TILE_MAX_DIM*-.5*dTILE_SIZE[1], 0}));
+ constexpr Vector3d size = TILE_MAX_DIM20d*dTILE_SIZE*-.5;
+ M->shader().set_camera_offset(tile_shader::project(size));
update_cursor_tile(cursor.pixel);
}
diff --git a/editor/draw.cpp b/editor/draw.cpp
index 535d95da..b2a7b659 100644
--- a/editor/draw.cpp
+++ b/editor/draw.cpp
@@ -3,6 +3,7 @@
#include "floormat/settings.hpp"
#include "shaders/tile.hpp"
#include <Magnum/GL/DebugOutput.h>
+#include <Magnum/Math/Vector3.h>
namespace floormat {
@@ -13,9 +14,9 @@ void app::draw_wireframe_quad(global_coords pos)
auto& shader = M->shader();
{
- const Vector3 center{pt[0]*TILE_SIZE[0], pt[1]*TILE_SIZE[1], 0};
+ const Vector3 center{Vector3i(pt[0], pt[1], 0) * iTILE_SIZE};
shader.set_tint({1, 0, 0, 1});
- _wireframe_quad.draw(shader, {center, {TILE_SIZE[0], TILE_SIZE[1]}, LINE_WIDTH});
+ _wireframe_quad.draw(shader, {center, TILE_SIZE2, LINE_WIDTH});
//_wireframe_wall_n.draw(shader, {center, {TILE_SIZE[0], TILE_SIZE[1], TILE_SIZE[2]}, LINE_WIDTH});
//_wireframe_wall_w.draw(shader, {center, {TILE_SIZE[0], TILE_SIZE[1], TILE_SIZE[2]}, LINE_WIDTH});
}
@@ -26,12 +27,10 @@ void app::draw_wireframe_box(global_coords pos)
constexpr float LINE_WIDTH = 1.5;
auto& shader = M->shader();
- constexpr auto X = TILE_SIZE[0], Y = TILE_SIZE[1], Z = TILE_SIZE[2];
- constexpr Vector3 size{X, Y, Z};
const auto pt = pos.to_signed();
- const Vector3 center{pt[0]*TILE_SIZE[0], pt[1]*TILE_SIZE[1], 0};
+ const auto center = Vector3((float)pt[0], (float)pt[1], 0) * TILE_SIZE;
shader.set_tint({0, 1, 0, 1});
- _wireframe_box.draw(shader, {center, size, LINE_WIDTH});
+ _wireframe_box.draw(shader, {center, TILE_SIZE, LINE_WIDTH});
}
void app::draw_cursor_tile()
diff --git a/editor/imgui.cpp b/editor/imgui.cpp
index 49d65b2e..acb0e3b0 100644
--- a/editor/imgui.cpp
+++ b/editor/imgui.cpp
@@ -164,7 +164,8 @@ void app::draw_editor_pane(tile_editor& type, float main_menu_height)
snprintf(buf, sizeof(buf), "##item_%zu", i);
const auto uv = v->texcoords_for_id(i);
- ImGui::ImageButton(buf, (void*)&v->texture(), {TILE_SIZE[0]/2, TILE_SIZE[1]/2},
+ ImGui::ImageButton(buf, (void*)&v->texture(),
+ { TILE_SIZE[0]*.5f, TILE_SIZE[1]*.5f },
{ uv[3][0], uv[3][1] }, { uv[0][0], uv[0][1] });
if (ed)
{
diff --git a/main/draw.cpp b/main/draw.cpp
index 33601c80..3b7c1c42 100644
--- a/main/draw.cpp
+++ b/main/draw.cpp
@@ -21,7 +21,7 @@ void main_impl::recalc_viewport(Vector2i size) noexcept
global_coords main_impl::pixel_to_tile(Vector2d position) const noexcept
{
- constexpr Vector2d pixel_size{dTILE_SIZE[0], dTILE_SIZE[1]};
+ constexpr Vector2d pixel_size(TILE_SIZE2);
constexpr Vector2d half{.5, .5};
const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset()*.5;
const Vector2d vec = tile_shader::unproject(px) / pixel_size + half;
@@ -77,7 +77,7 @@ void main_impl::draw_world() noexcept
bool main_impl::check_chunk_visible(const Vector2d& offset, const Vector2i& size) noexcept
{
- constexpr Vector3d len{ dTILE_SIZE[0]*TILE_MAX_DIM, dTILE_SIZE[1]*TILE_MAX_DIM, 0 };
+ constexpr Vector3d len = dTILE_SIZE * Vector3d(TILE_MAX_DIM, TILE_MAX_DIM, 0);
enum : std::size_t { x, y, };
constexpr Vector2d p00 = tile_shader::project(Vector3d(0, 0, 0)),
p10 = tile_shader::project(Vector3d(len[x], 0, 0)),
diff --git a/src/camera-offset.cpp b/src/camera-offset.cpp
index 40bf66dd..f837fd1f 100644
--- a/src/camera-offset.cpp
+++ b/src/camera-offset.cpp
@@ -10,9 +10,7 @@ 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({double(x)*TILE_MAX_DIM*dTILE_SIZE[0],
- double(y)*TILE_MAX_DIM*dTILE_SIZE[1],
- 0});
+ const auto offset = _offset + tile_shader::project(Vector3d(x, y, 0) * TILE_MAX_DIM20d);
_shader.set_camera_offset(offset);
}
diff --git a/src/tile-defs.hpp b/src/tile-defs.hpp
index 51b04259..af0b0194 100644
--- a/src/tile-defs.hpp
+++ b/src/tile-defs.hpp
@@ -1,13 +1,19 @@
#pragma once
-#include <cstddef>
-#include <cstdint>
-#include <type_traits>
+#include "compat/integer-types.hpp"
+#include <Magnum/Math/Vector3.h>
namespace floormat {
-constexpr inline std::size_t TILE_MAX_DIM = 16;
+constexpr inline std::uint8_t TILE_MAX_DIM = 16;
constexpr inline std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM;
-constexpr inline float TILE_SIZE[3] = { 64, 64, 64 };
-constexpr inline double dTILE_SIZE[3] = { 64, 64, 64 };
+
+constexpr inline auto TILE_MAX_DIM20d = Magnum::Math::Vector3<double> { TILE_MAX_DIM, TILE_MAX_DIM, 0 };
+constexpr inline auto iTILE_SIZE = Magnum::Math::Vector3<int> { 64, 64, 64 };
+constexpr inline auto iTILE_SIZE2 = Magnum::Math::Vector2<int> { iTILE_SIZE[0], iTILE_SIZE[1] };
+constexpr inline auto TILE_SIZE = Magnum::Math::Vector3<float> { iTILE_SIZE };
+constexpr inline auto dTILE_SIZE = Magnum::Math::Vector3<double> { iTILE_SIZE };
+constexpr inline auto TILE_SIZE2 = Magnum::Math::Vector2<float> { iTILE_SIZE2 };
+constexpr inline auto dTILE_SIZE2 = Magnum::Math::Vector2<double> { TILE_SIZE2 };
+constexpr inline auto TILE_SIZE20 = Magnum::Math::Vector3<float> { (float)iTILE_SIZE[0], (float)iTILE_SIZE[1], 0 };
} // namespace floormat