eco2d/code/apps/server/source/world/world.c

47 lines
971 B
C
Raw Normal View History

2021-01-11 14:42:36 +00:00
#include "zpl.h"
#include "world/world.h"
2021-01-11 13:47:14 +00:00
2021-01-14 16:37:20 +00:00
typedef struct {
uint8_t *data;
uint32_t seed;
uint32_t size;
uint32_t width;
uint32_t height;
} world_data;
static world_data world = {0};
2021-01-11 14:51:42 +00:00
2021-01-11 17:30:47 +00:00
int32_t world_gen();
2021-01-11 21:50:14 +00:00
int32_t world_init(int32_t seed, uint32_t width, uint32_t height) {
2021-01-14 16:37:20 +00:00
if (world.data) {
2021-01-11 14:48:17 +00:00
world_destroy();
}
2021-01-14 16:37:20 +00:00
world.seed = seed;
world.width = width;
world.height = height;
world.size = width*height;
world.data = zpl_malloc(sizeof(uint8_t)*world.size);
2021-01-11 14:48:17 +00:00
2021-01-14 16:37:20 +00:00
if (!world.data) {
2021-01-11 14:48:17 +00:00
return WORLD_ERROR_OUTOFMEM;
}
2021-01-11 17:30:47 +00:00
return world_gen();
2021-01-11 14:42:36 +00:00
}
int32_t world_destroy(void) {
2021-01-14 16:37:20 +00:00
zpl_mfree(world.data);
zpl_memset(&world, 0, sizeof(world));
2021-01-11 13:47:14 +00:00
return WORLD_ERROR_NONE;
}
2021-01-11 14:42:36 +00:00
2021-01-11 14:48:17 +00:00
uint32_t world_buf(uint8_t const **ptr, uint32_t *width) {
2021-01-14 16:37:20 +00:00
ZPL_ASSERT_NOT_NULL(world.data);
2021-01-11 14:42:36 +00:00
ZPL_ASSERT_NOT_NULL(ptr);
2021-01-14 16:37:20 +00:00
*ptr = world.data;
if (width) *width = world.width;
return world.size;
2021-01-11 14:42:36 +00:00
}
2021-01-11 16:49:00 +00:00
#include "world_gen.c"