summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-21 19:12:21 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-21 19:12:26 +0200
commit387ab8e946398c29bd462041689e9da4152ec52b (patch)
tree6be4a6f9c73d27460467fcaa7b3a366ec2ee011c
parenta4847d340b8a90cc538247280de281ca24790c82 (diff)
tile atlas shenanigans
-rw-r--r--main/loader-impl.cpp4
-rw-r--r--serialize/tile-atlas.cpp2
-rw-r--r--src/loader.hpp2
-rw-r--r--src/tile-atlas.cpp36
-rw-r--r--src/tile-atlas.hpp13
5 files changed, 28 insertions, 29 deletions
diff --git a/main/loader-impl.cpp b/main/loader-impl.cpp
index 2c37c487..4a489132 100644
--- a/main/loader-impl.cpp
+++ b/main/loader-impl.cpp
@@ -34,7 +34,7 @@ struct loader_impl final : loader_
std::string shader(Containers::StringView filename) override;
Trade::ImageData2D tile_texture(Containers::StringView filename) override;
- std::shared_ptr<struct tile_atlas> tile_atlas(Containers::StringView filename, Vector2ui size) override;
+ std::shared_ptr<struct tile_atlas> tile_atlas(Containers::StringView filename, Vector2ub size) override;
static void set_application_working_directory();
@@ -52,7 +52,7 @@ std::string loader_impl::shader(Containers::StringView filename)
return ret;
}
-std::shared_ptr<tile_atlas> loader_impl::tile_atlas(Containers::StringView name, Vector2ui size)
+std::shared_ptr<tile_atlas> loader_impl::tile_atlas(Containers::StringView name, Vector2ub size)
{
auto it = atlas_map.find(name);
if (it != atlas_map.end())
diff --git a/serialize/tile-atlas.cpp b/serialize/tile-atlas.cpp
index 896d29a4..4c80a212 100644
--- a/serialize/tile-atlas.cpp
+++ b/serialize/tile-atlas.cpp
@@ -11,7 +11,7 @@ using namespace floormat;
namespace nlohmann {
-using proxy_atlas = std::tuple<std::string, Vector2ui>;
+using proxy_atlas = std::tuple<std::string, Vector2ub>;
using shared_atlas = std::shared_ptr<floormat::tile_atlas>;
void adl_serializer<shared_atlas>::to_json(json& j, const shared_atlas& x)
diff --git a/src/loader.hpp b/src/loader.hpp
index 0bb541f5..f8e0e049 100644
--- a/src/loader.hpp
+++ b/src/loader.hpp
@@ -17,7 +17,7 @@ struct loader_
{
virtual std::string shader(Containers::StringView filename) = 0;
virtual Trade::ImageData2D tile_texture(Containers::StringView filename) = 0;
- virtual std::shared_ptr<struct tile_atlas> tile_atlas(Containers::StringView filename, Vector2ui size) = 0;
+ virtual std::shared_ptr<struct tile_atlas> tile_atlas(Containers::StringView filename, Vector2ub size) = 0;
static void destroy();
loader_(const loader_&) = delete;
diff --git a/src/tile-atlas.cpp b/src/tile-atlas.cpp
index 324b052c..6c2bd746 100644
--- a/src/tile-atlas.cpp
+++ b/src/tile-atlas.cpp
@@ -7,13 +7,12 @@
namespace floormat {
-tile_atlas::tile_atlas(Containers::StringView name, const ImageView2D& image, Vector2ui dims) :
- texcoords_{make_texcoords_array(Vector2ui(image.size()), dims)},
- name_{name}, size_{image.size()}, dims_{dims}
+tile_atlas::tile_atlas(Containers::StringView name, const ImageView2D& image, Vector2ub tile_count) :
+ texcoords_{make_texcoords_array(Vector2ui(image.size()), tile_count)},
+ name_{name}, size_{image.size()}, dims_{tile_count}
{
fm_assert(dims_[0] > 0 && dims_[1] > 0);
- fm_assert(size_ % dims_ == Vector2ui());
- fm_assert(dims_.product() < 256);
+ fm_assert(size_ % Vector2ui{tile_count} == Vector2ui());
tex_.setWrapping(GL::SamplerWrapping::ClampToEdge)
.setMagnificationFilter(GL::SamplerFilter::Linear)
.setMinificationFilter(GL::SamplerFilter::Linear)
@@ -25,31 +24,30 @@ tile_atlas::tile_atlas(Containers::StringView name, const ImageView2D& image, Ve
std::array<Vector2, 4> tile_atlas::texcoords_for_id(std::size_t i) const
{
- fm_assert(i < (size_/dims_).product());
+ fm_assert(i < num_tiles());
return texcoords_[i];
}
-auto tile_atlas::make_texcoords(Vector2ui size, Vector2ui dims, std::uint_fast16_t i) -> texcoords
+auto tile_atlas::make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, std::uint8_t i) -> texcoords
{
- const auto sz = size/dims;
- const Vector2ui id = { (UnsignedInt)i % dims[0], (UnsignedInt)i / dims[0] };
+ const auto sz = pixel_size/Vector2ui{tile_count};
+ const Vector2ui id = { std::uint32_t(i % tile_count[0]), std::uint32_t(i / tile_count[0]) };
const Vector2 p0(id * sz), p1(sz);
const auto x0 = p0.x(), x1 = p1.x()-1, y0 = p0.y(), y1 = p1.y()-1;
return {{
- { (x0+x1)/size[0], (y0+y1)/size[1] }, // bottom right
- { (x0+x1)/size[0], y0 /size[1] }, // top right
- { x0 /size[0], (y0+y1)/size[1] }, // bottom left
- { x0 /size[0], y0 /size[1] } // top left
+ { (x0+x1) / pixel_size[0], (y0+y1) / pixel_size[1] }, // bottom right
+ { (x0+x1) / pixel_size[0], y0 / pixel_size[1] }, // top right
+ { x0 / pixel_size[0], (y0+y1) / pixel_size[1] }, // bottom left
+ { x0 / pixel_size[0], y0 / pixel_size[1] } // top left
}};
}
-auto tile_atlas::make_texcoords_array(Vector2ui size, Vector2ui dims) -> std::unique_ptr<const texcoords[]>
+auto tile_atlas::make_texcoords_array(Vector2ui pixel_size, Vector2ub tile_count) -> std::unique_ptr<const texcoords[]>
{
- const auto sz = size/dims;
- const std::uint32_t max = sz.product();
- auto ptr = std::make_unique<std::array<Vector2, 4>[]>(max);
- for (std::uint_fast16_t i = 0; i < max; i++)
- ptr[i] = make_texcoords(size, dims, i);
+ const std::size_t N = Vector2ui{tile_count}.product();
+ auto ptr = std::make_unique<std::array<Vector2, 4>[]>(N);
+ for (std::uint8_t i = 0; i < N; i++)
+ ptr[i] = make_texcoords(pixel_size, tile_count, i);
return ptr;
}
diff --git a/src/tile-atlas.hpp b/src/tile-atlas.hpp
index a24ea96f..712c396c 100644
--- a/src/tile-atlas.hpp
+++ b/src/tile-atlas.hpp
@@ -14,7 +14,7 @@ struct tile_atlas final
using quad = std::array<Vector3, 4>;
using texcoords = std::array<Vector2, 4>;
- tile_atlas(Containers::StringView name, const ImageView2D& img, Vector2ui dimensions);
+ tile_atlas(Containers::StringView name, const ImageView2D& img, Vector2ub tile_count);
texcoords texcoords_for_id(std::size_t id) const;
static constexpr quad floor_quad(Vector3 center, Vector2 size);
@@ -22,19 +22,20 @@ struct tile_atlas final
static constexpr quad wall_quad_W(Vector3 center, Vector3 size);
static constexpr std::array<UnsignedShort, 6> indices(std::size_t N);
[[maybe_unused]] Vector2ui pixel_size() const { return size_; }
- std::size_t num_tiles() const { return dims_.product(); }
- Vector2ui num_tiles2() const { return dims_; }
+ std::size_t num_tiles() const { return Vector2ui{dims_}.product(); }
+ Vector2ub num_tiles2() const { return dims_; }
GL::Texture2D& texture() { return tex_; }
Containers::StringView name() const { return name_; }
private:
- static std::unique_ptr<const texcoords[]> make_texcoords_array(Vector2ui size, Vector2ui dims);
- static texcoords make_texcoords(Vector2ui size, Vector2ui dims, std::uint_fast16_t i);
+ static std::unique_ptr<const texcoords[]> make_texcoords_array(Vector2ui pixel_size, Vector2ub tile_count);
+ static texcoords make_texcoords(Vector2ui pixel_size, Vector2ub tile_count, std::uint8_t i);
std::unique_ptr<const texcoords[]> texcoords_;
GL::Texture2D tex_;
std::string name_;
- Vector2ui size_, dims_;
+ Vector2ui size_;
+ Vector2ub dims_;
};
constexpr std::array<UnsignedShort, 6> tile_atlas::indices(std::size_t N)