Skip to content

Commit

Permalink
Added: map2d and map3d
Browse files Browse the repository at this point in the history
  • Loading branch information
zakarouf committed Feb 5, 2024
1 parent 4fd0f0c commit 5da43c2
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/lib/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions src/lib/map2d.h
Original file line number Diff line number Diff line change
@@ -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

46 changes: 46 additions & 0 deletions src/lib/map3d.h
Original file line number Diff line number Diff line change
@@ -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

3 changes: 1 addition & 2 deletions src/lib/mapch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "std/primitives.h"
#include "vector.h"
#include "utils.h"

#include "u8arr.h"

Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/lib/z__map2D.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdlib.h>
#include <memory.h>
#include <string.h>

#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);
}

1 change: 0 additions & 1 deletion src/lib/z__mapch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5da43c2

Please sign in to comment.