Add Game as singleton

This commit is contained in:
2023-11-15 08:18:56 +01:00
parent 274612b035
commit d37936ce7f
6 changed files with 55 additions and 27 deletions

View File

@@ -10,14 +10,14 @@
#include "utils/pathfinding.h"
Game *GAME = NULL;
ECS_COMPONENT_DECLARE(Game);
bool init(Game *game);
void deinit(Game *game);
bool init(void *userData);
void deinit(void *userData);
void update(float dt, Game *game);
void render(float dt, Game *game);
void imguiRender(float dt, Game *game);
void update(float dt, void *userData);
void render(float dt, void *userData);
void imguiRender(float dt, void *userData);
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
@@ -32,17 +32,28 @@ bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
appDesc->render = (BzAppRenderFunc) render;
appDesc->imguiRender = (BzAppRenderFunc) imguiRender;
GAME = bzAlloc(sizeof(*GAME));
appDesc->userData = GAME;
Game *game = bzAlloc(sizeof(*game));
bzMemSet(game, 0, sizeof(*game));
appDesc->userData = NULL;
appDesc->useFlecs = true;
return true;
}
bool init(Game *game) {
bool init(void *userData) {
BZ_UNUSED(userData);
SetExitKey(-1);
ECS_COMPONENT_DEFINE(ECS, Game);
initComponentIDs(ECS);
// For ecs explorer
//ecs_singleton_set(ECS, EcsRest, {0});
//ECS_IMPORT(ECS, FlecsMonitor);
ecs_singleton_set(ECS, Game, {});
Game *game = ecs_singleton_get_mut(ECS, Game);
int screenWidth = 1280;
int screenHeight = 720;
@@ -97,18 +108,23 @@ bool init(Game *game) {
ECS_OBSERVER(ECS, startPath, EcsOnSet, Path);
return true;
}
void deinit(Game *game) {
void deinit(void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
bzTileMapDestroy(&game->map);
bzTilesetDestroy(&game->terrainTileset);
bzTilesetDestroy(&game->buildingsTileset);
bzTilesetDestroy(&game->entitiesTileset);
bzFree(GAME);
GAME = NULL;
ecs_singleton_remove(ECS, Game);
}
void update(float dt, Game *game) {
void update(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
char titleBuf[32];
snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000);
SetWindowTitle(titleBuf);
@@ -166,7 +182,10 @@ void update(float dt, Game *game) {
}
}
void render(float dt, Game *game) {
void render(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
ClearBackground(RAYWHITE);
BeginMode2D(game->camera);
@@ -204,7 +223,10 @@ void render(float dt, Game *game) {
EndMode2D();
}
void imguiRender(float dt, Game *game) {
void imguiRender(float dt, void *userData) {
BZ_UNUSED(userData);
Game *game = ecs_singleton_get_mut(ECS, Game);
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
igBegin("Debug Menu", NULL, 0);
if (igCollapsingHeader_TreeNodeFlags("Resources", 0)) {