Restructure/refactor of main.c

This commit is contained in:
2023-11-12 16:14:30 +01:00
parent df911c65b7
commit 8edb7b7ea9
13 changed files with 280 additions and 159 deletions

View File

@@ -17,22 +17,23 @@ typedef struct BzAppDesc {
BzAppInitFunc init;
BzAppUpdateFunc update;
BzAppRenderFunc render;
BzAppRenderFunc imguiRender;
BzAppDeinitFunc deinit;
bool useNuklear;
bool useFlecs;
void *userData;
} BzAppDesc;
extern struct nk_context *NK;
typedef struct ecs_world_t ecs_world_t;
extern ecs_world_t *ECS;
extern bool bzMain(BzAppDesc *appDesc, int argc, const char **argv);
#ifdef BZ_ENTRYPOINT
#include <flecs.h>
#include <rlImGui.h>
struct nk_context *NK = NULL;
ecs_world_t *ECS = NULL;
// https://www.raylib.com/examples/core/loader.html?name=core_custom_logging
@@ -93,9 +94,10 @@ int main(int argc, const char **argv) {
SetTargetFPS(appDesc.fps);
// Initialize modules
if (appDesc.useFlecs) {
if (appDesc.useFlecs)
ECS = ecs_init();
}
if (appDesc.imguiRender)
rlImGuiSetup(true);
// User initialize
if (appDesc.init && !appDesc.init(appDesc.userData)) {
@@ -113,6 +115,12 @@ int main(int argc, const char **argv) {
BeginDrawing();
if (appDesc.render)
appDesc.render(dt, appDesc.userData);
if (appDesc.imguiRender) {
rlImGuiBegin();
appDesc.imguiRender(dt, appDesc.userData);
rlImGuiEnd();
}
EndDrawing();
}
@@ -121,6 +129,8 @@ int main(int argc, const char **argv) {
appDesc.deinit(appDesc.userData);
// Deinitialize modules
if (appDesc.imguiRender)
rlImGuiShutdown();
if (ECS) {
ecs_fini(ECS);
ECS = NULL;

View File

@@ -9,20 +9,20 @@
BzTileMap BZ_TILEMAP_INVALID = {.isValid = false};
void bzTileLayerSkipRender(BzTileLayer *layer) {
void bzTileLayerSkipRender(BzTileMap *map, BzTileLayer *layer) {
}
void bzTileObjectGroupSkipRender(BzTileObjectGroup *objectGroup) {
void bzTileObjectGroupSkipRender(BzTileMap *map, BzTileObjectGroup *objectGroup) {
}
BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER = bzTileLayerSkipRender;
BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER = bzTileObjectGroupSkipRender;
bool bzTileLayerClear(BzTileLayer *layer, BzTile *data, i32 dataCount) {
bool bzTileLayerClear(BzTileMap *map, BzTileLayer *layer) {
return true;
}
bool bzTileObjectsClear(BzTileObjectGroup *objectGroup, BzTileObject *objects, i32 objectCount) {
bool bzTileObjectsClear(BzTileMap *map, BzTileObjectGroup *objectGroup) {
return true;
}
@@ -45,6 +45,12 @@ BzTileset *bzTileLayerGetTileset(BzTileMap *map, BzTileLayer *layer) {
return NULL;
return &map->tilesets[idx];
}
BzTileset *bzTileObjectGroupGetTileset(BzTileMap *map, BzTileObjectGroup *objectGroup) {
i32 idx = objectGroup->tilesetIdx;
if (idx < 0 || idx >= map->tilesetCount)
return NULL;
return &map->tilesets[idx];
}
static void handleTileLayer(BzTileLayer *layer, cute_tiled_layer_t *cuteLayer) {
layer->id = cuteLayer->id;
@@ -283,7 +289,7 @@ void bzTileMapOverrideLayer(BzTileMap *map, i32 slotID, BzTileLayerFunc func) {
BzTileLayer *layer = map->layers + slotID;
i32 dataCount = layer->dataCount;
BzTile *data = layer->data;
if (func(layer, data, dataCount)) {
if (func(map, layer)) {
if (!layer->hasOwnership) return;
bzFree(layer->data);
if (layer->data == data) {
@@ -300,7 +306,7 @@ void bzTileMapOverrideObjectGroup(BzTileMap *map, i32 slotID, BzTileObjectsFunc
BzTileObjectGroup *objectGroup = map->objectGroups + slotID;
BzTileObject *objects = objectGroup->objects;
i32 objectCount = objectGroup->objectCount;
if (func(objectGroup, objects, objectCount)) {
if (func(map, objectGroup)) {
bzFree(objectGroup->objects);
if (objectGroup->objects == objects) {
objectGroup->objects = NULL;

View File

@@ -11,9 +11,10 @@
typedef struct BzTileLayer BzTileLayer;
typedef struct BzTileObject BzTileObject;
typedef struct BzTileObjectGroup BzTileObjectGroup;
typedef struct BzTileMap BzTileMap;
typedef void (*BzTileLayerRenderFunc)(BzTileLayer *layer);
typedef void (*BzTileObjectGroupRenderFunc)(BzTileObjectGroup *objectGroup);
typedef void (*BzTileLayerRenderFunc)(BzTileMap *map, BzTileLayer *layer);
typedef void (*BzTileObjectGroupRenderFunc)(BzTileMap *map, BzTileObjectGroup *objectGroup);
extern BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER;
extern BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER;
@@ -105,8 +106,8 @@ typedef struct BzTileMap {
extern BzTileMap BZ_TILEMAP_INVALID;
// Return true, if you want to override data pointer (you are responsible for cleanup, if you override)
typedef bool (*BzTileLayerFunc)(BzTileLayer *layer, BzTile *data, i32 dataCount);
typedef bool (*BzTileObjectsFunc)(BzTileObjectGroup *objectGroup, BzTileObject *objects, i32 objectsCount);
typedef bool (*BzTileLayerFunc)(BzTileMap *map, BzTileLayer *layer);
typedef bool (*BzTileObjectsFunc)(BzTileMap *map, BzTileObjectGroup *objectGroup);
extern BzTileLayerFunc BZ_TILE_LAYER_CLEAR;
extern BzTileObjectsFunc BZ_TILE_OBJECTS_CLEAR;
@@ -114,6 +115,7 @@ extern BzTileObjectsFunc BZ_TILE_OBJECTS_CLEAR;
BzTile bzTileLayerGetTile(BzTileLayer *layer, i32 x, i32 y);
void bzTileLayerSetTile(BzTileLayer *layer, BzTile tile, i32 x, i32 y, i32 w, i32 h);
BzTileset *bzTileLayerGetTileset(BzTileMap *map, BzTileLayer *layer);
BzTileset *bzTileObjectGroupGetTileset(BzTileMap *map, BzTileObjectGroup *objectGroup);
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc);
void bzTileMapDestroy(BzTileMap *map);