summaryrefslogtreecommitdiffhomepage
path: root/src/global-coords.hpp
blob: 284c63c5477c8bf0c19ca5ad06a77774dd75b695 (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
33
34
35
36
37
38
39
40
41
42
43
#pragma once
#include "local-coords.hpp"
#include "compat/assert.hpp"

namespace floormat {

struct chunk_coords final {
    std::int16_t x = 0, y = 0;

    constexpr bool operator==(const chunk_coords& other) const noexcept = default;
    constexpr operator std::size_t() const noexcept { return (std::uint32_t)y << 16 | (std::uint32_t)x; }
};

struct global_coords final {
    std::uint32_t x = 0, y = 0;

    constexpr global_coords(chunk_coords c, local_coords xy) :
        x{ std::uint32_t(c.x + (1 << 15)) << 4 | (xy.x & 0x0f) },
        y{ std::uint32_t(c.y + (1 << 15)) << 4 | (xy.y & 0x0f) }
    {}
    constexpr global_coords(std::uint32_t x, std::uint32_t y) noexcept : x{x}, y{y} {}
    constexpr global_coords() noexcept = default;

    constexpr local_coords local() const noexcept;
    constexpr chunk_coords chunk() const noexcept;

    constexpr bool operator==(const global_coords& other) const noexcept = default;
};

constexpr local_coords global_coords::local() const noexcept
{
    return { (std::uint8_t)(x % TILE_MAX_DIM), (std::uint8_t)(y % TILE_MAX_DIM) };
}

constexpr chunk_coords global_coords::chunk() const noexcept
{
    return {
        (std::int16_t)(std::int32_t(x >> 4) - (1 << 15)),
        (std::int16_t)(std::int32_t(y >> 4) - (1 << 15)),
    };
}

} // namespace floormat