summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-02 03:23:05 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-02 03:23:05 +0200
commit35cf1d65b454985c38b1e00a91670663b1583670 (patch)
tree1e47893775fbea27a79bf799fb99360b954a85ca
parent5ec89ae3a42308dd5e43e5fed207824918123023 (diff)
a
-rw-r--r--attic/hash.hpp (renamed from hash.hpp)0
-rw-r--r--chunk.hpp50
-rw-r--r--floor-mesh.cpp62
-rw-r--r--floor-mesh.hpp45
-rw-r--r--main.cpp21
-rw-r--r--tile-mesh.cpp31
-rw-r--r--tile-mesh.hpp39
-rw-r--r--tile-shader.cpp3
-rw-r--r--tile.cpp6
-rw-r--r--tile.hpp10
10 files changed, 149 insertions, 118 deletions
diff --git a/hash.hpp b/attic/hash.hpp
index 75eea7a9..75eea7a9 100644
--- a/hash.hpp
+++ b/attic/hash.hpp
diff --git a/chunk.hpp b/chunk.hpp
index e783a0d2..7bfea8d7 100644
--- a/chunk.hpp
+++ b/chunk.hpp
@@ -5,37 +5,6 @@
namespace Magnum::Examples {
-constexpr inline std::size_t TILE_MAX_DIM = 16;
-constexpr inline std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM;
-
-struct local_coords final {
- std::uint8_t x = 0, y = 0;
- constexpr std::size_t to_index() const noexcept;
-};
-
-constexpr std::size_t local_coords::to_index() const noexcept {
- return y*TILE_MAX_DIM + x;
-}
-
-#if 0
-struct chunk_coords final {
- std::int16_t x = 0, y = 0;
- constexpr std::size_t to_index() const noexcept;
-
- static constexpr std::size_t max_bits = sizeof(chunk_coords::x)*8 * 3 / 4;
- static_assert(max_bits*4/3/8 == sizeof(decltype(chunk_coords::x)));
-};
-#endif
-
-#if 0
-struct global_coords final {
- std::uint32_t x = 0, y = 0;
- constexpr global_coords() noexcept = default;
- constexpr global_coords(decltype(x) x, decltype(y) y) noexcept : x{x}, y{y} {}
- constexpr global_coords(chunk_coords c, local_coords tile) noexcept;
-};
-#endif
-
struct chunk final
{
//using index_type = std::common_type_t<decltype(local_coords::x), decltype(local_coords::y)>;
@@ -93,4 +62,23 @@ constexpr void chunk::foreach_tile_(F&& fun)
local_coords{(std::uint8_t)i, (std::uint8_t)j});
}
+#if 0
+struct chunk_coords final {
+ std::int16_t x = 0, y = 0;
+ constexpr std::size_t to_index() const noexcept;
+
+ static constexpr std::size_t max_bits = sizeof(chunk_coords::x)*8 * 3 / 4;
+ static_assert(max_bits*4/3/8 == sizeof(decltype(chunk_coords::x)));
+};
+#endif
+
+#if 0
+struct global_coords final {
+ std::uint32_t x = 0, y = 0;
+ constexpr global_coords() noexcept = default;
+ constexpr global_coords(decltype(x) x, decltype(y) y) noexcept : x{x}, y{y} {}
+ constexpr global_coords(chunk_coords c, local_coords tile) noexcept;
+};
+#endif
+
} // namespace Magnum::Examples
diff --git a/floor-mesh.cpp b/floor-mesh.cpp
new file mode 100644
index 00000000..ecbcc8bb
--- /dev/null
+++ b/floor-mesh.cpp
@@ -0,0 +1,62 @@
+#include "floor-mesh.hpp"
+#include "tile-atlas.hpp"
+#include "tile-shader.hpp"
+#include "tile.hpp"
+#include "chunk.hpp"
+
+namespace Magnum::Examples {
+
+floor_mesh::floor_mesh()
+{
+ _floor_mesh.setCount((int)(quad_index_count * _index_data.size()))
+ .addVertexBuffer(_positions_buffer, 0, tile_shader::Position{})
+ .addVertexBuffer(_vertex_buffer, 0, tile_shader::TextureCoordinates{})
+ .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
+}
+
+void floor_mesh::set_tile(tile& x, const local_coords pt)
+{
+ CORRADE_INTERNAL_ASSERT(x.ground_image);
+
+ constexpr float X = TILE_SIZE[0], Y = TILE_SIZE[1];
+ const auto idx = pt.to_index();
+ auto texcoords = x.ground_image.atlas->texcoords_for_id(x.ground_image.variant);
+ //auto positions = img.atlas->floor_quad(center, { TILE_SIZE[0], TILE_SIZE[1] });
+ for (std::size_t i = 0; i < 4; i++)
+ _vertex_data[idx][i] = { texcoords[i] };
+ x.ground_image.atlas->texture().bind(0); // TODO
+}
+
+void floor_mesh::draw(tile_shader& shader)
+{
+ _vertex_buffer.setData(_vertex_data, Magnum::GL::BufferUsage::DynamicDraw);
+ shader.draw(_floor_mesh);
+}
+
+static auto make_index_array()
+{
+ constexpr auto quad_index_count = std::tuple_size_v<decltype(tile_atlas::indices(0))>;
+ std::array<std::array<UnsignedShort, quad_index_count>, TILE_COUNT> array; // NOLINT(cppcoreguidelines-pro-type-member-init)
+
+ for (std::size_t i = 0, k = 0; i < std::size(array); i++)
+ array[i] = tile_atlas::indices(i);
+ return array;
+}
+
+static auto make_floor_positions_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}) };
+ }
+ return array;
+}
+
+const auto floor_mesh::_index_data = make_index_array();
+const auto floor_mesh::_position_data = make_floor_positions_array();
+
+} // namespace Magnum::Examples
diff --git a/floor-mesh.hpp b/floor-mesh.hpp
new file mode 100644
index 00000000..a1b0fac5
--- /dev/null
+++ b/floor-mesh.hpp
@@ -0,0 +1,45 @@
+#pragma once
+#include "defs.hpp"
+#include "tile.hpp"
+#include "tile-atlas.hpp"
+#include <array>
+#include <Corrade/Containers/ArrayViewStl.h>
+#include <Magnum/Magnum.h>
+#include <Magnum/Math/Vector2.h>
+#include <Magnum/GL/Mesh.h>
+#include <Magnum/GL/Buffer.h>
+
+namespace Magnum::Examples {
+
+struct tile_shader;
+struct tile_image;
+
+struct floor_mesh final
+{
+ floor_mesh();
+ floor_mesh(floor_mesh&&) = delete;
+ floor_mesh(const floor_mesh&) = delete;
+
+ void set_tile(tile& x, local_coords pt);
+ void draw(tile_shader& shader);
+
+private:
+ struct vertex_data final {
+ Vector2 texcoords;
+ };
+ using quad_data = std::array<vertex_data, 4>;
+ using vertex_positions_data = std::array<Vector3, 4>;
+ static constexpr auto quad_index_count = std::tuple_size_v<decltype(tile_atlas::indices(0))>;
+ using index_type = std::array<UnsignedShort, quad_index_count>;
+
+ static const std::array<index_type, TILE_COUNT> _index_data;
+ static const std::array<vertex_positions_data, TILE_COUNT> _position_data;
+ std::array<quad_data, TILE_COUNT> _vertex_data = {};
+
+ GL::Mesh _floor_mesh;
+ GL::Buffer _vertex_buffer{_vertex_data, Magnum::GL::BufferUsage::DynamicDraw},
+ _index_buffer{_index_data, Magnum::GL::BufferUsage::StaticDraw},
+ _positions_buffer{_position_data, Magnum::GL::BufferUsage::StaticDraw};
+};
+
+} // namespace Magnum::Examples
diff --git a/main.cpp b/main.cpp
index 81f6abf2..182ce5ec 100644
--- a/main.cpp
+++ b/main.cpp
@@ -4,27 +4,16 @@
#include "defs.hpp"
#include "tile.hpp"
#include "chunk.hpp"
-#include "tile-mesh.hpp"
+#include "floor-mesh.hpp"
#include <bitset>
-#include <Corrade/Containers/ArrayViewStl.h>
-#include <Corrade/PluginManager/Manager.h>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector.h>
-#include <Magnum/ImageView.h>
-#include <Magnum/GL/Buffer.h>
#include <Magnum/GL/DefaultFramebuffer.h>
-#include <Magnum/GL/Mesh.h>
#include <Magnum/GL/Renderer.h>
-#include <Magnum/GL/TextureFormat.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/Trade/AbstractImporter.h>
-
-#include <glm/glm.hpp>
-#include <glm/ext/matrix_clip_space.hpp>
-#include <glm/ext/matrix_transform.hpp>
-#include <Magnum/GlmIntegration/Integration.h>
#include <SDL_timer.h>
namespace Magnum::Examples {
@@ -74,7 +63,7 @@ struct app final : Platform::Application
shared_tile_atlas wall1 =
loader.tile_atlas("../share/game/images/metal2.tga", {2, 2});
chunk _chunk = make_test_chunk();
- tile_mesh _tile_mesh;
+ floor_mesh _floor_mesh;
std::uint64_t time_ticks = 0, time_freq = SDL_GetPerformanceFrequency();
Vector2 camera_offset;
@@ -103,8 +92,8 @@ void app::draw_chunk(chunk& c)
for (std::size_t j = 0, k = 0; j < N; j++)
for (std::size_t i = 0; i < N; i++, k++)
- _tile_mesh.draw_floor_quad(_shader, c[k].ground_image,
- {(float)(X*i), (float)(Y*j), 0});
+ _floor_mesh.set_tile(c[k], {i, j});
+ _floor_mesh.draw(_shader);
}
app::app(const Arguments& arguments):
@@ -145,7 +134,7 @@ app::app(const Arguments& arguments):
k++;
}
- _mesh.setCount((int)indices.size())
+ _floor_mesh.setCount((int)indices.size())
.addVertexBuffer(GL::Buffer{vertices}, 0,
tile_shader::Position{}, tile_shader::TextureCoordinates{})
.addVertexBuffer(GL::Buffer{c.sampler_array()}, 0, tile_shader::TextureID{})
diff --git a/tile-mesh.cpp b/tile-mesh.cpp
deleted file mode 100644
index c1f819d8..00000000
--- a/tile-mesh.cpp
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "tile-mesh.hpp"
-#include "tile-shader.hpp"
-#include "tile.hpp"
-
-namespace Magnum::Examples {
-
-tile_mesh::tile_mesh()
-{
- _mesh.setCount((int)index_count)
- .addVertexBuffer(_vertex_buffer, 0,
- tile_shader::Position{}, tile_shader::TextureCoordinates{})
- .setIndexBuffer(_index_buffer, 0, GL::MeshIndexType::UnsignedShort);
-}
-
-void tile_mesh::draw_quad(tile_shader& shader, tile_image& img, const std::array<Vector3, 4>& positions)
-{
- auto texcoords = img.atlas->texcoords_for_id(img.variant);
- //auto positions = img.atlas->floor_quad(position, { TILE_SIZE[0], TILE_SIZE[1] });
- for (std::size_t i = 0; i < 4; i++)
- _vertex_data[i] = {positions[i], texcoords[i]};
- img.atlas->texture().bind(0);
- _vertex_buffer.setData(_vertex_data, Magnum::GL::BufferUsage::DynamicDraw);
- shader.draw(_mesh);
-}
-
-void tile_mesh::draw_floor_quad(tile_shader& shader, tile_image& img, Vector3 center)
-{
- draw_quad(shader, img, img.atlas->floor_quad(center, { TILE_SIZE[0], TILE_SIZE[1] }));
-}
-
-} // namespace Magnum::Examples
diff --git a/tile-mesh.hpp b/tile-mesh.hpp
deleted file mode 100644
index eef3b0a9..00000000
--- a/tile-mesh.hpp
+++ /dev/null
@@ -1,39 +0,0 @@
-#pragma once
-#include "tile-atlas.hpp"
-#include <array>
-#include <Corrade/Containers/ArrayViewStl.h>
-#include <Magnum/Magnum.h>
-#include <Magnum/Math/Vector2.h>
-#include <Magnum/GL/Mesh.h>
-#include <Magnum/GL/Buffer.h>
-
-namespace Magnum::Examples {
-
-struct tile_shader;
-struct tile_image;
-
-class tile_mesh final
-{
- static constexpr std::size_t index_count = std::tuple_size_v<decltype(tile_atlas{}.indices(0))>;
- static constexpr std::array<UnsignedShort, index_count> _indices =
- tile_atlas::indices(0);
-
- struct vertex_data final {
- Vector3 position;
- Vector2 texcoords;
- };
- std::array<vertex_data, 4> _vertex_data = {};
- GL::Mesh _mesh;
- GL::Buffer _vertex_buffer{_vertex_data, Magnum::GL::BufferUsage::DynamicDraw},
- _index_buffer{_indices, Magnum::GL::BufferUsage::StaticDraw};
-
-public:
- tile_mesh();
- tile_mesh(tile_mesh&&) = delete;
- tile_mesh(const tile_mesh&) = delete;
-
- void draw_quad(tile_shader& shader, tile_image& img, const std::array<Vector3, 4>& positions);
- void draw_floor_quad(tile_shader& shader, tile_image& img, Vector3 center);
-};
-
-} // namespace Magnum::Examples
diff --git a/tile-shader.cpp b/tile-shader.cpp
index 64e23ed1..02470502 100644
--- a/tile-shader.cpp
+++ b/tile-shader.cpp
@@ -31,7 +31,8 @@ tile_shader::tile_shader()
CORRADE_INTERNAL_ASSERT_OUTPUT(link());
- setUniform(ScaleUniform, Vector2{640, 480});
+ set_scale({640, 480});
+ set_camera_offset({0, 0});
}
tile_shader& tile_shader::set_scale(const Vector2& scale)
diff --git a/tile.cpp b/tile.cpp
index e1b22a21..e5cb9a97 100644
--- a/tile.cpp
+++ b/tile.cpp
@@ -1,6 +1,12 @@
#include "tile.hpp"
+
#include "tile-atlas.hpp"
namespace Magnum::Examples {
+local_coords::local_coords(std::size_t x, std::size_t y) : x{(std::uint8_t)x}, y{(std::uint8_t)y}
+{
+ CORRADE_INTERNAL_ASSERT(x <= 0xff && y <= 0xff);
+}
+
} // namespace Magnum::Examples
diff --git a/tile.hpp b/tile.hpp
index 6a717a26..39271cfb 100644
--- a/tile.hpp
+++ b/tile.hpp
@@ -10,6 +10,8 @@ namespace Magnum::Examples {
struct tile_atlas;
constexpr inline Vector3 TILE_SIZE = { 50, 50, 50 };
+constexpr inline std::size_t TILE_MAX_DIM = 16;
+constexpr inline std::size_t TILE_COUNT = TILE_MAX_DIM*TILE_MAX_DIM;
struct tile_image final
{
@@ -28,4 +30,12 @@ struct tile final
pass_mode passability = pass_shoot_through;
};
+struct local_coords final {
+ std::uint8_t x = 0, y = 0;
+ constexpr local_coords() = default;
+ local_coords(std::size_t x, std::size_t y);
+ constexpr local_coords(std::uint8_t x, std::uint8_t y) : x{x}, y{y} {}
+ constexpr std::size_t to_index() const { return y*TILE_MAX_DIM + x; }
+};
+
} //namespace Magnum::Examples