diff options
-rw-r--r-- | chunk.hpp | 14 | ||||
-rw-r--r-- | hash.hpp | 1 | ||||
-rw-r--r-- | main.cpp | 49 | ||||
-rw-r--r-- | tile-mesh.cpp | 31 | ||||
-rw-r--r-- | tile-mesh.hpp | 39 | ||||
-rw-r--r-- | tile.cpp | 1 | ||||
-rw-r--r-- | tile.hpp | 12 |
7 files changed, 87 insertions, 60 deletions
@@ -1,7 +1,7 @@ #pragma once #include "tile.hpp" #include <type_traits> -#include <vector> +#include <array> namespace Magnum::Examples { @@ -17,6 +17,7 @@ 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; @@ -24,16 +25,16 @@ struct chunk_coords final { 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; }; - -static_assert(std::is_same_v<decltype(chunk_coords::x), decltype(chunk_coords::y)>); -static_assert(std::is_same_v<decltype(global_coords::x), decltype(global_coords::y)>); +#endif struct chunk final { @@ -94,6 +95,7 @@ constexpr void chunk::foreach_tile_(F&& fun) k); } +#if 0 constexpr std::size_t chunk_coords::to_index() const noexcept { using unsigned_type = std::make_unsigned_t<decltype(x)>; @@ -102,7 +104,9 @@ constexpr std::size_t chunk_coords::to_index() const noexcept static_assert(sizeof(unsigned_type) <= sizeof(UnsignedInt)/2); return (std::size_t)(unsigned_type)y * N + (std::size_t)(unsigned_type)x; } +#endif +#if 0 struct hash_chunk final { constexpr std::size_t operator()(chunk_coords xy) const noexcept { return hash<sizeof(std::size_t)*8>{}(xy.to_index()); @@ -143,4 +147,6 @@ constexpr global_coords::global_coords(chunk_coords c, local_coords tile) noexce { } +#endif + } // namespace Magnum::Examples @@ -1,4 +1,5 @@ #pragma once +#include <cstddef> #include <cstdint> namespace Magnum::Examples { @@ -4,6 +4,7 @@ #include "defs.hpp" #include "tile.hpp" #include "chunk.hpp" +#include "tile-mesh.hpp" #include <bitset> @@ -39,54 +40,6 @@ struct enum_bitset : std::bitset<(std::size_t)enum_type::MAX> { } }; -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::Buffer _vertex_buffer{_vertex_data, Magnum::GL::BufferUsage::DynamicDraw}, - _index_buffer{_indices, Magnum::GL::BufferUsage::StaticDraw}; - GL::Mesh _mesh; - -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); -}; - -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] })); -} - struct app final : Platform::Application { using dpi_policy = Platform::Implementation::Sdl2DpiScalingPolicy; diff --git a/tile-mesh.cpp b/tile-mesh.cpp new file mode 100644 index 00000000..c1f819d8 --- /dev/null +++ b/tile-mesh.cpp @@ -0,0 +1,31 @@ +#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 new file mode 100644 index 00000000..eef3b0a9 --- /dev/null +++ b/tile-mesh.hpp @@ -0,0 +1,39 @@ +#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 @@ -1,4 +1,5 @@ #include "tile.hpp" +#include "tile-atlas.hpp" namespace Magnum::Examples { @@ -1,18 +1,14 @@ #pragma once -#include "tile-atlas.hpp" -#include "hash.hpp" #include "defs.hpp" - -#include <concepts> +#include <Magnum/Magnum.h> +#include <Magnum/Math/Vector3.h> #include <cstddef> -#include <tuple> -#include <array> +#include <cstdint> #include <memory> -#include <unordered_map> -#include <utility> namespace Magnum::Examples { +struct tile_atlas; constexpr inline Vector3 TILE_SIZE = { 50, 50, 50 }; struct tile_image final |