From 88fa3cbb71a3b18f7028971aca88f10af82ab369 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 11 Jan 2021 15:48:17 +0100 Subject: [PATCH] world code --- code/apps/server/header/world.h | 2 +- code/apps/server/source/world.c | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/code/apps/server/header/world.h b/code/apps/server/header/world.h index d4fe9b6..0032941 100644 --- a/code/apps/server/header/world.h +++ b/code/apps/server/header/world.h @@ -10,4 +10,4 @@ int32_t world_gen(int32_t seed, uint8_t width, uint8_t height); int32_t world_destroy(void); -uint32_t world_buf(uint8_t const **ptr); +uint32_t world_buf(uint8_t const **ptr, uint32_t *width); diff --git a/code/apps/server/source/world.c b/code/apps/server/source/world.c index cf3c15a..09dbcf7 100644 --- a/code/apps/server/source/world.c +++ b/code/apps/server/source/world.c @@ -3,18 +3,32 @@ static uint8_t *world = NULL; static uint32_t world_size = 0; +static uint32_t world_width = 0; int32_t world_gen(int32_t seed, uint8_t width, uint8_t height) { + if (world) { + world_destroy(); + } + world_size = width*height; + world_width = width; + world = zpl_malloc(sizeof(uint8_t)*world_size); + + if (!world) { + return WORLD_ERROR_OUTOFMEM; + } return WORLD_ERROR_NONE; } int32_t world_destroy(void) { + zpl_mfree(world); + world = NULL; return WORLD_ERROR_NONE; } -uint32_t world_buf(uint8_t const **ptr) { +uint32_t world_buf(uint8_t const **ptr, uint32_t *width) { ZPL_ASSERT_NOT_NULL(world); ZPL_ASSERT_NOT_NULL(ptr); *ptr = world; + if (width) *width = world_width; return world_size; }