pokemon/coordinates.hpp
Zack Buhman e6cae1c242 coordinates: rework map rendering
This also adds interactive player movement.

I am much more satisfied with the coordinate space math in this
commit compared to the previous commit.
2023-07-28 05:46:36 +00:00

68 lines
1.1 KiB
C++

#pragma once
#include <stdint.h>
namespace screen_offset
{
// the origin is at this offset in screen space
constexpr int32_t x = 4;
constexpr int32_t y = 4;
}
struct offset_t { // in pixels
int32_t x;
int32_t y;
};
struct block_t {
int32_t x;
int32_t y;
};
struct world_t;
struct screen_t {
int32_t x;
int32_t y;
constexpr inline world_t to_world(const world_t& origin) const;
};
struct world_t {
int32_t x;
int32_t y;
constexpr inline screen_t to_screen(const world_t& origin) const;
constexpr inline block_t to_block() const;
};
// screen_t
constexpr inline world_t screen_t::to_world(const world_t& origin) const
{
return {
(origin.x - screen_offset::x) + x,
(origin.y - screen_offset::y) + y,
};
}
// world_t
constexpr inline screen_t world_t::to_screen(const world_t& origin) const
{
return {
-origin.x + screen_offset::x + x,
-origin.y + screen_offset::y + y,
};
}
constexpr inline block_t world_t::to_block() const
{
// division function must round towards negative infinity
// right shift provides that property
return {
x >> 1,
y >> 1,
};
}