[RELEASE] u22-v04
This version is submitted for U22 final presentation. (squashed 158 commits)
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
add_library(loground
|
||||
base.c
|
||||
island.c
|
||||
misc.c
|
||||
pool.c
|
||||
)
|
||||
target_benum_sources(loground
|
||||
type.h
|
||||
)
|
||||
target_crial_sources(loground
|
||||
base.crial
|
||||
)
|
||||
target_link_libraries(loground
|
||||
msgpackc
|
||||
|
||||
|
@@ -18,16 +18,15 @@
|
||||
#include "core/loshader/ground.h"
|
||||
|
||||
#include "./island.h"
|
||||
#include "./misc.h"
|
||||
#include "./type.h"
|
||||
|
||||
#define LOGROUND_BASE_PARAM_TO_PACK_EACH_(PROC, PROC_str, PROC_type) do { \
|
||||
PROC_str ("subclass", "ground"); \
|
||||
PROC_type ("type", type); \
|
||||
PROC ("id", super.super.id); \
|
||||
PROC ("pos", super.super.pos); \
|
||||
PROC ("size", super.size); \
|
||||
} while (0)
|
||||
#define LOGROUND_BASE_PARAM_TO_PACK_COUNT 5
|
||||
/* generated serializer */
|
||||
#include "core/loground/crial/base.h"
|
||||
|
||||
static bool
|
||||
(*const update_function_vtable_[LOGROUND_TYPE_COUNT])(loground_base_t* base) = {
|
||||
[LOGROUND_TYPE_ISLAND] = loground_island_update,
|
||||
};
|
||||
|
||||
static void loground_base_delete_(loentity_t* entity) {
|
||||
assert(entity != NULL);
|
||||
@@ -36,18 +35,6 @@ static void loground_base_delete_(loentity_t* entity) {
|
||||
if (!base->used) return;
|
||||
|
||||
base->used = false;
|
||||
|
||||
# define each_(NAME, name) do { \
|
||||
if (base->type == LOGROUND_TYPE_##NAME) { \
|
||||
loground_##name##_tear_down(base); \
|
||||
return; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
LOGROUND_TYPE_EACH_(each_);
|
||||
assert(false);
|
||||
|
||||
# undef each_
|
||||
}
|
||||
|
||||
static void loground_base_die_(loentity_t* entity) {
|
||||
@@ -61,16 +48,8 @@ static bool loground_base_update_(loentity_t* entity) {
|
||||
loground_base_t* base = (typeof(base)) entity;
|
||||
base->cache = (typeof(base->cache)) {0};
|
||||
|
||||
# define each_(NAME, name) do { \
|
||||
if (base->type == LOGROUND_TYPE_##NAME) { \
|
||||
return loground_##name##_update(base); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
LOGROUND_TYPE_EACH_(each_);
|
||||
return false;
|
||||
|
||||
# undef each_
|
||||
assert(update_function_vtable_[base->param.type] != NULL);
|
||||
return update_function_vtable_[base->param.type](base);
|
||||
}
|
||||
|
||||
static void loground_base_draw_(
|
||||
@@ -94,40 +73,8 @@ static void loground_base_pack_(
|
||||
|
||||
const loground_base_t* base = (typeof(base)) entity;
|
||||
|
||||
msgpack_pack_map(packer, LOGROUND_BASE_PARAM_TO_PACK_COUNT+1);
|
||||
|
||||
# define pack_(name, var) do { \
|
||||
mpkutil_pack_str(packer, name); \
|
||||
LOCOMMON_MSGPACK_PACK_ANY(packer, &base->var); \
|
||||
} while (0)
|
||||
# define pack_str_(name, str) do { \
|
||||
mpkutil_pack_str(packer, name); \
|
||||
mpkutil_pack_str(packer, str); \
|
||||
} while (0)
|
||||
# define pack_type_(name, var) do { \
|
||||
mpkutil_pack_str(packer, name); \
|
||||
mpkutil_pack_str(packer, loground_type_stringify(base->var)); \
|
||||
} while (0)
|
||||
|
||||
|
||||
LOGROUND_BASE_PARAM_TO_PACK_EACH_(pack_, pack_str_, pack_type_);
|
||||
|
||||
# undef pack_type_
|
||||
# undef pack_str_
|
||||
# undef pack_
|
||||
|
||||
# define each_(NAME, name) do { \
|
||||
if (base->type == LOGROUND_TYPE_##NAME) { \
|
||||
loground_##name##_pack_data(base, packer); \
|
||||
return; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
mpkutil_pack_str(packer, "data");
|
||||
LOGROUND_TYPE_EACH_(each_);
|
||||
assert(false);
|
||||
|
||||
# undef each_
|
||||
msgpack_pack_map(packer, CRIAL_PROPERTY_COUNT_);
|
||||
CRIAL_SERIALIZER_;
|
||||
}
|
||||
|
||||
void loground_base_initialize(
|
||||
@@ -136,93 +83,48 @@ void loground_base_initialize(
|
||||
assert(drawer != NULL);
|
||||
|
||||
*base = (typeof(*base)) {
|
||||
.super = {
|
||||
.super = {
|
||||
.vtable = {
|
||||
.delete = loground_base_delete_,
|
||||
.die = loground_base_die_,
|
||||
.update = loground_base_update_,
|
||||
.draw = loground_base_draw_,
|
||||
.pack = loground_base_pack_,
|
||||
},
|
||||
.subclass = LOENTITY_SUBCLASS_GROUND,
|
||||
},
|
||||
},
|
||||
.drawer = drawer,
|
||||
};
|
||||
}
|
||||
|
||||
void loground_base_reinitialize(loground_base_t* base, loentity_id_t id) {
|
||||
assert(base != NULL);
|
||||
assert(!base->used);
|
||||
|
||||
# define reset_(name, var) do { \
|
||||
base->var = (typeof(base->var)) {0}; \
|
||||
} while (0)
|
||||
# define reset_str_(name, str)
|
||||
|
||||
LOGROUND_BASE_PARAM_TO_PACK_EACH_(reset_, reset_str_, reset_);
|
||||
|
||||
# undef reset_str_
|
||||
# undef reset_
|
||||
|
||||
base->super.super.id = id;
|
||||
base->super = (typeof(base->super)) {
|
||||
.super = {
|
||||
.vtable = {
|
||||
.delete = loground_base_delete_,
|
||||
.die = loground_base_die_,
|
||||
.update = loground_base_update_,
|
||||
.draw = loground_base_draw_,
|
||||
.pack = loground_base_pack_,
|
||||
},
|
||||
.id = id,
|
||||
.subclass = LOENTITY_SUBCLASS_GROUND,
|
||||
},
|
||||
};
|
||||
base->param = (typeof(base->param)) {0};
|
||||
}
|
||||
|
||||
void loground_base_deinitialize(loground_base_t* base) {
|
||||
assert(base != NULL);
|
||||
assert(!base->used);
|
||||
|
||||
loground_base_delete_(&base->super.super);
|
||||
}
|
||||
|
||||
bool loground_base_unpack(loground_base_t* base, const msgpack_object *obj) {
|
||||
assert(base != NULL);
|
||||
assert(obj != NULL);
|
||||
|
||||
loground_base_reinitialize(base, 0);
|
||||
/* id will be overwritten below */
|
||||
|
||||
const char* v;
|
||||
size_t vlen;
|
||||
loground_base_reinitialize(base, 0); /* id will be overwritten */
|
||||
|
||||
const msgpack_object_map* root = mpkutil_get_map(obj);
|
||||
if (root == NULL) goto FAIL;
|
||||
CRIAL_DESERIALIZER_;
|
||||
return true;
|
||||
|
||||
# define item_(v) mpkutil_get_map_item_by_str(root, v)
|
||||
|
||||
# define unpack_(name, var) do { \
|
||||
if (!LOCOMMON_MSGPACK_UNPACK_ANY(item_(name), &base->var)) { \
|
||||
return NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
# define unpack_type_(name, var) do { \
|
||||
if (!mpkutil_get_str(item_(name), &v, &vlen) || \
|
||||
!loground_type_unstringify(&base->var, v, vlen)) { \
|
||||
return NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
# define unpack_str_(name, str) do { \
|
||||
if (!mpkutil_get_str(item_(name), &v, &vlen) || \
|
||||
!(strncmp(v, str, vlen) == 0 && str[vlen] == 0)) { \
|
||||
return NULL; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
LOGROUND_BASE_PARAM_TO_PACK_EACH_(unpack_, unpack_str_, unpack_type_);
|
||||
|
||||
# undef unpack_str_
|
||||
# undef unpack_type_
|
||||
# undef unpack_
|
||||
|
||||
const msgpack_object* data = item_("data");
|
||||
# define each_(NAME, name) do { \
|
||||
if (base->type == LOGROUND_TYPE_##NAME) { \
|
||||
return loground_##name##_unpack_data(base, data); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
LOGROUND_TYPE_EACH_(each_);
|
||||
FAIL:
|
||||
loground_base_delete_(&base->super.super);
|
||||
return false;
|
||||
|
||||
# undef each_
|
||||
|
||||
# undef item_
|
||||
}
|
||||
|
31
core/loground/base.crial
Normal file
31
core/loground/base.crial
Normal file
@@ -0,0 +1,31 @@
|
||||
/* CRIAL
|
||||
SERIALIZER_BEGIN
|
||||
mpkutil_pack_str(packer, "$name");
|
||||
mpkutil_pack_str(packer, $code);
|
||||
END
|
||||
DESERIALIZER_BEGIN
|
||||
const char* v;
|
||||
size_t vlen;
|
||||
if (!mpkutil_get_str(
|
||||
mpkutil_get_map_item_by_str(root, "$name"), &v, &vlen) ||
|
||||
strncmp(v, $code, vlen) != 0 || $code[vlen] != 0) {
|
||||
goto FAIL;
|
||||
}
|
||||
END
|
||||
PROPERTY subclass = "ground"
|
||||
|
||||
SERIALIZER_BEGIN
|
||||
mpkutil_pack_str(packer, "$name");
|
||||
LOCOMMON_MSGPACK_PACK_ANY(packer, &base->$code);
|
||||
END
|
||||
DESERIALIZER_BEGIN
|
||||
if (!LOCOMMON_MSGPACK_UNPACK_ANY(
|
||||
mpkutil_get_map_item_by_str(root, "$name"), &base->$code)) {
|
||||
goto FAIL;
|
||||
}
|
||||
END
|
||||
PROPERTY id = super.super.id
|
||||
PROPERTY pos = super.super.pos
|
||||
PROPERTY size = super.size
|
||||
PROPERTY type = param.type
|
||||
*/
|
@@ -9,25 +9,21 @@
|
||||
#include "core/loentity/ground.h"
|
||||
#include "core/loshader/ground.h"
|
||||
|
||||
#include "./misc.h"
|
||||
#include "./type.h"
|
||||
|
||||
typedef struct {
|
||||
loentity_ground_t super;
|
||||
bool used;
|
||||
|
||||
/* injected deps */
|
||||
loshader_ground_drawer_t* drawer;
|
||||
|
||||
/* params not to be packed */
|
||||
struct {
|
||||
loground_type_t type;
|
||||
} param;
|
||||
|
||||
struct {
|
||||
loshader_ground_drawer_instance_t instance;
|
||||
} cache;
|
||||
|
||||
/* params to be packed (includes id, pos, and size) */
|
||||
loground_type_t type;
|
||||
|
||||
# define LOGROUND_BASE_DATA_MAX_SIZE 256
|
||||
uint8_t data[LOGROUND_BASE_DATA_MAX_SIZE];
|
||||
} loground_base_t;
|
||||
|
||||
void
|
||||
|
@@ -7,7 +7,7 @@
|
||||
#include "core/locommon/position.h"
|
||||
|
||||
#include "./base.h"
|
||||
#include "./misc.h"
|
||||
#include "./type.h"
|
||||
|
||||
bool loground_island_update(loground_base_t* base) {
|
||||
assert(base != NULL);
|
||||
@@ -28,7 +28,9 @@ void loground_island_build(
|
||||
assert(vec2_valid(size));
|
||||
assert(size->x >= 0 && size->y >= 0);
|
||||
|
||||
base->type = LOGROUND_TYPE_ISLAND;
|
||||
base->param = (typeof(base->param)) {
|
||||
.type = LOGROUND_TYPE_ISLAND,
|
||||
};
|
||||
|
||||
base->super.super.pos = *pos;
|
||||
base->super.size = *size;
|
||||
|
@@ -2,8 +2,6 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <msgpack.h>
|
||||
|
||||
#include "util/math/vector.h"
|
||||
|
||||
#include "core/locommon/position.h"
|
||||
@@ -21,11 +19,3 @@ loground_island_build(
|
||||
const locommon_position_t* pos,
|
||||
const vec2_t* size
|
||||
);
|
||||
|
||||
#define loground_island_tear_down(base)
|
||||
|
||||
#define loground_island_pack_data(base, packer) \
|
||||
msgpack_pack_nil(packer)
|
||||
|
||||
#define loground_island_unpack_data(base, obj) \
|
||||
(obj != NULL)
|
||||
|
@@ -1,37 +0,0 @@
|
||||
#include "./misc.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
const char* loground_type_stringify(loground_type_t type) {
|
||||
# define each_(NAME, name) do { \
|
||||
if (type == LOGROUND_TYPE_##NAME) return #name; \
|
||||
} while (0)
|
||||
|
||||
LOGROUND_TYPE_EACH_(each_);
|
||||
|
||||
assert(false);
|
||||
return NULL;
|
||||
|
||||
# undef each_
|
||||
}
|
||||
|
||||
bool loground_type_unstringify(
|
||||
loground_type_t* type, const char* v, size_t len) {
|
||||
assert(type != NULL);
|
||||
assert(v != NULL || len == 0);
|
||||
|
||||
# define each_(NAME, name) do { \
|
||||
if (strncmp(v, #name, len) == 0 && #name[len] == 0) { \
|
||||
*type = LOGROUND_TYPE_##NAME; \
|
||||
return true; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
LOGROUND_TYPE_EACH_(each_);
|
||||
return false;
|
||||
|
||||
# undef each_
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/* dont forget to update EACH macro */
|
||||
typedef enum {
|
||||
LOGROUND_TYPE_ISLAND,
|
||||
} loground_type_t;
|
||||
|
||||
#define LOGROUND_TYPE_EACH_(PROC) do { \
|
||||
PROC(ISLAND, island); \
|
||||
} while (0)
|
||||
|
||||
const char*
|
||||
loground_type_stringify(
|
||||
loground_type_t type
|
||||
);
|
||||
|
||||
bool
|
||||
loground_type_unstringify(
|
||||
loground_type_t* type,
|
||||
const char* v,
|
||||
size_t len
|
||||
);
|
@@ -10,28 +10,12 @@
|
||||
#include "util/memory/memory.h"
|
||||
|
||||
#include "core/locommon/counter.h"
|
||||
#include "core/loentity/pool.h"
|
||||
#include "core/loshader/ground.h"
|
||||
|
||||
#include "./base.h"
|
||||
|
||||
struct loground_pool_t {
|
||||
loshader_ground_drawer_t* drawer;
|
||||
locommon_counter_t* idgen;
|
||||
|
||||
size_t length;
|
||||
loground_base_t items[1];
|
||||
};
|
||||
|
||||
static size_t loground_pool_find_unused_item_index_(
|
||||
const loground_pool_t* pool) {
|
||||
assert(pool != NULL);
|
||||
|
||||
for (size_t i = 0; i < pool->length; ++i) {
|
||||
if (!pool->items[i].used) return i;
|
||||
}
|
||||
fprintf(stderr, "ground pool overflow\n");
|
||||
abort();
|
||||
}
|
||||
LOENTITY_POOL_SOURCE_TEMPLATE(loground)
|
||||
|
||||
loground_pool_t* loground_pool_new(
|
||||
loshader_ground_drawer_t* drawer,
|
||||
@@ -44,7 +28,6 @@ loground_pool_t* loground_pool_new(
|
||||
loground_pool_t* pool = memory_new(
|
||||
sizeof(*pool) + (length-1)*sizeof(pool->items[0]));
|
||||
*pool = (typeof(*pool)) {
|
||||
.drawer = drawer,
|
||||
.idgen = idgen,
|
||||
.length = length,
|
||||
};
|
||||
@@ -54,36 +37,3 @@ loground_pool_t* loground_pool_new(
|
||||
}
|
||||
return pool;
|
||||
}
|
||||
|
||||
void loground_pool_delete(loground_pool_t* pool) {
|
||||
assert(pool != NULL);
|
||||
|
||||
for (size_t i = 0; i < pool->length; ++i) {
|
||||
loground_base_deinitialize(&pool->items[i]);
|
||||
}
|
||||
memory_delete(pool);
|
||||
}
|
||||
|
||||
loground_base_t* loground_pool_create(loground_pool_t* pool) {
|
||||
assert(pool != NULL);
|
||||
|
||||
const size_t i = loground_pool_find_unused_item_index_(pool);
|
||||
|
||||
loground_base_reinitialize(
|
||||
&pool->items[i], locommon_counter_count(pool->idgen));
|
||||
|
||||
pool->items[i].used = true;
|
||||
return &pool->items[i];
|
||||
}
|
||||
|
||||
loground_base_t* loground_pool_unpack_item(
|
||||
loground_pool_t* pool, const msgpack_object* obj) {
|
||||
assert(pool != NULL);
|
||||
|
||||
const size_t i = loground_pool_find_unused_item_index_(pool);
|
||||
|
||||
if (!loground_base_unpack(&pool->items[i], obj)) return NULL;
|
||||
|
||||
pool->items[i].used = true;
|
||||
return &pool->items[i];
|
||||
}
|
||||
|
10
core/loground/type.h
Normal file
10
core/loground/type.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum {
|
||||
/* BENUM BEGIN loground_type */
|
||||
LOGROUND_TYPE_ISLAND,
|
||||
/* BENUM END */
|
||||
} loground_type_t;
|
||||
|
||||
/* generated benum header */
|
||||
#include "core/loground/benum/type.h"
|
Reference in New Issue
Block a user