gridcoarsefine is a single-header c++ library for 2d vectors. Features conversions for different grid sizes. namespace gcf
It's basically a fancy wrapper for
struct V2D
{
int x;
int y;
};#include <cstdint>
#include <unordered_map>
using namespace gcf;
const int chunk_size = 64;
using pixelV2D = intV2D<int64_t, 16>;
using tileV2D = intV2D<int32_t, 16 * 16>;
using chunkV2D = intV2D<int32_t, 16 * 16 * chunk_size>;
struct tile {};
struct Chunk
{
tile tiles[chunk_size][chunk_size];
};
const Chunk * get_chunk(chunkV2D); //maybe an unordered_map with chunkV2D key
tile get_tile_at_pixel(pixelV2D pixel_grid_position)
{
tileV2D tile_grid_position = tileV2D(pixel_grid_position); //these conversions will use divPos_binary, which compiles to a bit shift
chunkV2D chunk_grid_position = chunkV2D(tile_grid_position);
const Chunk * chunk = get_chunk(chunk_grid_position);
tileV2D tile_within_chunk = tileV2D(modPos_binary<chunk_size>(tile_grid_position.x),
modPos_binary<chunk_size>(tile_grid_position.y)); //modPos_binary is just a binary &
return chunk->tiles[tile_within_chunk.y][tile_within_chunk.x];
}-
genericV2D <type>Basic {x, y} struct with mathematical operations.typecan be int, float, orclass my_custom_rational_typeas long as it has numeric operators. -
genericV2D_rGrandparent of other types. Implements mathematical operations expected of a 2d vector.Exists because some children types need to stay the same type when returned (thats the
_r) from basic mathimatical operations -
intV2D <std::integral, int es>integer {x, y} type with a tag,es, that defines the courseness of a grid, with 1 being the tiniest squares.intV2D<int, 10>{5, 6}can be easily converted tointV2D<int, 1>{50, 60}with a conversion constructor. If bothesare a power of 2, this ends up just being a few bit shifts.Supports converting between different
std::integraltypes
-
dot,cross, and mathematical operators -
divPos(a, b)andmodPos(a, b)- does a / b division with a remainder that is always positive.divPos(-7,3)= -3modPos(-7,3)= 2 -
divPos_binary<b>(a)andmodPos_binary<b>(a)- binary optimisation. denominator of division must be a positive power of two, and is passed as a template parameter -
static intV2D intV2D::trim_cast(intV2D)- used to convertintV2Dfrom a largestd::integralto a smallerstd::integral, possibly trimming bits