diff options
Diffstat (limited to 'src/tile-atlas.cpp')
-rw-r--r-- | src/tile-atlas.cpp | 36 |
1 files changed, 17 insertions, 19 deletions
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; } |