From 5da43c2391cd6394be7584b7cd838a708e20f622 Mon Sep 17 00:00:00 2001 From: zakarouf Date: Tue, 6 Feb 2024 00:06:24 +0530 Subject: [PATCH] Added: map2d and map3d --- src/lib/map.h | 1 + src/lib/map2d.h | 32 ++++++++++++++++++++++++++++++++ src/lib/map3d.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib/mapch.h | 3 +-- src/lib/z__map2D.c | 29 +++++++++++++++++++++++++++++ src/lib/z__mapch.c | 1 - 6 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/lib/map2d.h create mode 100644 src/lib/map3d.h create mode 100644 src/lib/z__map2D.c diff --git a/src/lib/map.h b/src/lib/map.h index 3fdcc00..b3cc520 100644 --- a/src/lib/map.h +++ b/src/lib/map.h @@ -38,4 +38,5 @@ int z__Map_layer_push_copy__raw(void *_map_, z__u64 plotsize, z__u32 src); int z__Map_layer_swap__raw(void *map, z__u32 l1, z__u32 l2); int z__Map_layer_swap_at_top__raw(void *map, z__u32 layer); + #endif diff --git a/src/lib/map2d.h b/src/lib/map2d.h new file mode 100644 index 0000000..27e7493 --- /dev/null +++ b/src/lib/map2d.h @@ -0,0 +1,32 @@ +#ifndef ZAKAROUF_Z_IMP__MAP2D_H +#define ZAKAROUF_Z_IMP__MAP2D_H + +#include "std/primitives.h" +#include "vector.h" + +#include "u8arr.h" + +/** + * z__Map2D() + * Tileset Oriented Map Type with layers or z-dimention. + * size: Dimention of Map + * layers: Total no. of z layers used + * layers_capacity: Total no. of z layers allocated + */ +#define z__Map2D(Name, T, ...) \ + typedef struct Name Name; \ + struct Name { \ + z__u32Vec2 size; \ + z__size plotsize; \ + T *plots; \ + __VA_ARGS__; \ + } + +void z__Map2D_new__raw(void *_map, z__size plotsize, const z__u32 x, z__u32 const y); +void z__Map2D_delete__raw(void *_map); + +#define z__Map2D_new(map, x, y) z__Map2D_new__raw(map, sizeof(*(map)->plots), x, y) +#define z__Map2D_delete(map) { (void)(map)->plots; z__Map2D_delete__raw(map); } + +#endif + diff --git a/src/lib/map3d.h b/src/lib/map3d.h new file mode 100644 index 0000000..d23a288 --- /dev/null +++ b/src/lib/map3d.h @@ -0,0 +1,46 @@ +#ifndef ZAKAROUF_Z__MAP3D_H +#define ZAKAROUF_Z__MAP3D_H + +#include "std/primitives.h" +#include "vector.h" +#include "utils.h" + +#include "u8arr.h" + +/** + * z__Map3D() + * Tileset Oriented Map Type with layers or z-dimention. + * size: Dimention of Map + * layers: Total no. of z layers used + * layers_capacity: Total no. of z layers allocated + */ +#define z__Map3D(Name, T, ...) \ + typedef struct Name Name; \ + struct Name { \ + z__u32Vec3 size; \ + z__u32 size_total; \ + T *plots; \ + __VA_ARGS__; \ + } + +#define z__Map3D_get(map, x, y, z) (map).plots[z__xyz3Dto1D(x, y, z, (map).size.x, (map).size.y)] +#define z__Map3D_set(map, x, y, z, value) { z__Map3D_get(map, x, y, z) = value; } + +#define z__Map3D_new(map, _x, _y, _z, ...)\ + { \ + (map)->size = (z__u32Vec3) { .x = _x, .y = _y, .z = _z }; \ + (map)->size_total = _x * _y * _z; \ + (map)->plots = z__MALLOC( sizeof(*(map)->plots) * (map)->size_total );\ + __VA_ARGS__;\ + } + +#define z__Map3D_delete(map, ...)\ + { \ + __VA_ARGS__; \ + z__FREE((map)->plots); \ + (map)-size = (z__u32Vec3){0}; \ + (map)-size_total = 0; \ + } \ + +#endif + diff --git a/src/lib/mapch.h b/src/lib/mapch.h index 0d387b5..350c860 100644 --- a/src/lib/mapch.h +++ b/src/lib/mapch.h @@ -3,6 +3,7 @@ #include "std/primitives.h" #include "vector.h" +#include "utils.h" #include "u8arr.h" @@ -46,8 +47,6 @@ #define z__MapCh_MAX_CHUNKCOUNT (0xFFFF) #define z__MapCh_MAX_CHUNKRADIUS (0xFFFF/0xFF) -#define z__xy2Dto1D(x, y, X) ((x) + ((y) * (X))) -#define z__xyz3Dto1D(x, y, z, X, Y) (z__xy2Dto1D(x, y, X) + ((z) * (X) * (Y))) #define z__PRIV__map__CH_DEFAULT_RADIUS_SIZE 1 #define z__PRIV__map__CH_DEFAULT_X_SIZE 32 diff --git a/src/lib/z__map2D.c b/src/lib/z__map2D.c new file mode 100644 index 0000000..4d45aa7 --- /dev/null +++ b/src/lib/z__map2D.c @@ -0,0 +1,29 @@ +#include +#include +#include + +#include "std/mem.h" +#include "utils.h" + +#include "u8arr.h" +#include "map2d.h" +#include "forrange.h" + +z__Map2D(VoidMap, void); +#define self VoidMap *map = _map_; + +void z__Map2D_new__raw(void *_map_, z__size plotsize, z__u32 const x, z__u32 const y) +{ + self; + map->plots = z__MALLOC(plotsize * x * y); + map->plotsize = plotsize; + map->size.x = x; + map->size.y = y; +} + +void z__Map2D_delete__raw(void *_map_) +{ + self; + z__FREE(map->plots); +} + diff --git a/src/lib/z__mapch.c b/src/lib/z__mapch.c index 14dada1..d8f3cd8 100644 --- a/src/lib/z__mapch.c +++ b/src/lib/z__mapch.c @@ -11,7 +11,6 @@ void z__MapCh_new__raw(void *_map, z__u32 plotsize, z__u32Vec3 size, z__u16 ch_c { self; - printf("%p\n", map);fflush(stdout); map->size = size; map->chunk_count = ch_count; map->chunk_radius = chunk_radius;