summaryrefslogtreecommitdiffhomepage
path: root/src/local-coords.hpp
blob: 0d62103c766875b22043e9c3c46cc73268474bcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once
#include "compat/assert.hpp"
#include "tile-defs.hpp"
#include <cstdint>
#include <Magnum/Magnum.h>
#include <Magnum/Math/Vector3.h>

namespace Magnum::Examples {

struct local_coords final {
    std::uint8_t x = 0, y = 0;
    explicit constexpr local_coords(std::size_t idx) noexcept;
    constexpr local_coords() noexcept = default;
    constexpr local_coords(std::size_t x, std::size_t y) noexcept;
    constexpr local_coords(std::uint8_t x, std::uint8_t y) noexcept : x{x}, y{y} {}
    constexpr std::size_t to_index() const noexcept { return y*TILE_MAX_DIM + x; }
};

constexpr local_coords::local_coords(std::size_t index) noexcept :
      x{(std::uint8_t)(index % TILE_MAX_DIM)},
      y{(std::uint8_t)(index / TILE_MAX_DIM)}
{
    ASSERT(index < TILE_COUNT);
}

constexpr local_coords::local_coords(std::size_t x, std::size_t y) noexcept
    : x{(std::uint8_t)x}, y{(std::uint8_t)y}
{
    ASSERT(x <= 0xff && y <= 0xff);
}

} // namespace Magnum::Examples