diff --git a/src/engine/engine.h b/src/engine/engine.h index 9d1fffc3..b56b8790 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -33,6 +33,9 @@ typedef struct { /** Save Manager for the game */ savemanager_t save; + + /** Scene Manager for the game */ + scenemanager_t scene; } engine_t; /** diff --git a/src/games/poker/game.c b/src/games/poker/game.c index d3115ce8..5db4a8da 100644 --- a/src/games/poker/game.c +++ b/src/games/poker/game.c @@ -7,6 +7,7 @@ #include "game.h" + bool gameInit(game_t *game) { ASSERT_NOT_NULL(game); diff --git a/src/games/poker/game.h b/src/games/poker/game.h index e3450a28..fe0139c7 100644 --- a/src/games/poker/game.h +++ b/src/games/poker/game.h @@ -17,11 +17,13 @@ typedef struct { engine_t engine; + + camera_t camera; standardshader_t shader; primitive_t quad; - assetmanagerholder_t owner; assetmanageritem_t *item; + scenemana } game_t; /** diff --git a/src/scene/CMakeLists.txt b/src/scene/CMakeLists.txt index cb895194..8e8e5e1c 100644 --- a/src/scene/CMakeLists.txt +++ b/src/scene/CMakeLists.txt @@ -6,5 +6,5 @@ # Sources target_sources(${PROJECT_NAME} PRIVATE - scene.c + scenemanager.c ) \ No newline at end of file diff --git a/src/scene/scene.c b/src/scene/scene.c deleted file mode 100644 index 1e91b140..00000000 --- a/src/scene/scene.c +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "scene.h" - -void sceneInit(scene_t *scene, engine_t *engine, void *user) { - scene->engine = engine; - scene->assetOwner = assetManagerHolderCreate(&engine->assetManager); - scene->user = user; - scene->onLoaded = NULL; - scene->onRender = NULL; - scene->loaded = false; -} - -float sceneUpdate(scene_t *scene) { - // First, lets check how loading has been going - float n = assetManagerProgressGetForHolder( - &scene->engine->assetManager, scene->assetOwner - ); - - // TODO: Loading screen - if(n < 1.0f) return n; - - // Fire event - if(!scene->loaded) { - scene->loaded = true; - if(scene->onLoaded != NULL) scene->onLoaded(scene); - } - - // Fire callback - if(scene->onRender != NULL) scene->onRender(scene); - - return n; -} - -void sceneDispose(scene_t *scene) { - assetManagerHolderRelease(&scene->engine->assetManager, scene->assetOwner); -} \ No newline at end of file diff --git a/src/scene/scene.h b/src/scene/scene.h deleted file mode 100644 index f0da4c71..00000000 --- a/src/scene/scene.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright (c) 2021 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once -#include "../libs.h" -#include "../file/assetmanager.h" -#include "../engine/engine.h" -#include "../display/renderlist.h" -#include "../display/shaderprogram.h" - -// Forwarder for scene_t -typedef struct _scene_t scene_t; - -// Callbacks -typedef void scenecallback_t(scene_t *scene); - -// Scene Definition -typedef struct _scene_t { - engine_t *engine; - assetmanagerholder_t assetOwner; - void *user; - - /** Whether or not the assets that this scene depends upon have loaded */ - bool loaded; - - /** Scene Callbacks */ - scenecallback_t *onLoaded; - scenecallback_t *onRender; -} scene_t; - -void sceneInit(scene_t *scene, engine_t *engine, void *user); - -float sceneUpdate(scene_t *scene); - -void sceneDispose(scene_t *scene); \ No newline at end of file diff --git a/src/scene/scenemanager.c b/src/scene/scenemanager.c new file mode 100644 index 00000000..c5e6a9d5 --- /dev/null +++ b/src/scene/scenemanager.c @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "scenemanager.h" + +bool _sceneManagerItemFilter(int32_t i, void *item) { + return ((sceneitem_t *)item)->onRender != NULL; +} + +void sceneManagerInit(scenemanager_t *sceneManager) { + ASSERT_NOT_NULL(sceneManager); + + sceneManager->itemCount = 0; +} + +sceneitem_t * sceneManagerAddItem(scenemanager_t *man, scenerender_t *onRender) { + sceneitem_t *item; + + ASSERT_NOT_NULL(man); + ASSERT_NOT_NULL(onRender); + ASSERT_GREATER_THAN_EQUAL_TO(man->itemCount, 0); + ASSERT_LESS_THAN(man->itemCount, SCENE_ITEM_COUNT_MAX); + + item = man->items + man->itemCount++; + item->onRender = onRender; + + return item; +} + +void sceneManagerRemoveItem(scenemanager_t *man, sceneitem_t *item) { + ASSERT_NOT_NULL(man); + ASSERT_NOT_NULL(item); + item->onRender = NULL; +} + +void sceneManagerRestack(scenemanager_t *man) { + ASSERT_NOT_NULL(man); + arrayFilter( + sizeof(sceneitem_t), man->items, &man->itemCount, &_sceneManagerItemFilter + ); +} + +void sceneManagerClear(scenemanager_t *man) { + ASSERT_NOT_NULL(man); + man->itemCount = 0; +} \ No newline at end of file diff --git a/src/scene/scenemanager.h b/src/scene/scenemanager.h new file mode 100644 index 00000000..8a0b9eb1 --- /dev/null +++ b/src/scene/scenemanager.h @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2022 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "../libs.h" +#include "../assert/assert.h" +#include "../util/array.h" + +#define SCENE_ITEM_COUNT_MAX 1024 + +typedef struct _sceneitem_t sceneitem_t; +typedef struct _scenemanager_t scenemanager_t; + +typedef void scenerender_t(sceneitem_t *item); + +typedef struct _sceneitem_t { + void *user; + uint8_t flags; + scenerender_t *onRender; +} sceneitem_t; + +typedef struct _scenemanager_t { + sceneitem_t items[SCENE_ITEM_COUNT_MAX]; + int32_t itemCount; +} scenemanager_t; + +void sceneManagerInit(scenemanager_t *sceneManager); \ No newline at end of file diff --git a/src/util/array.c b/src/util/array.c index b1b66bea..4099366a 100644 --- a/src/util/array.c +++ b/src/util/array.c @@ -215,4 +215,51 @@ int32_t arraySum( count++; } return count; +} + +int32_t arrayFilter( + size_t size, void *array, int32_t *length, arrayiterationcallback_t *callback +) { + int32_t i, count, removed; + bool result; + + ASSERT_GREATER_THAN(size, 0); + ASSERT_NOT_NULL(array); + ASSERT_GREATER_THAN_EQUAL_TO(length, 0); + ASSERT_NOT_NULL(callback); + + if(length == 0) return 0; + + count = 0; + removed = 0; + i = 0; + + while(i < *length) { + result = callback(i, arrayGet(size, array, i)); + + if(!result) { + count++; + i++; + continue; + } + + if(count == 0) { + i++; + continue; + } + + arraySplice(size, array, i - count, count, *length); + i -= count; + *length = *length - count; + removed += count; + count = 0; + } + + if(count != 0) { + arraySplice(size, array, i - count, count, *length); + removed += count; + *length = *length - count; + } + + return removed; } \ No newline at end of file diff --git a/src/util/array.h b/src/util/array.h index 000a2f03..12c587b2 100644 --- a/src/util/array.h +++ b/src/util/array.h @@ -194,4 +194,19 @@ void arraySplice( */ int32_t arraySum( size_t size, void *array, int32_t length, arrayiterationcallback_t *callback +); + +/** + * Filter an array by the result of a callback function provided. The callback + * function will need to return TRUE if the item is to be KEPT, otherwise it the + * item will be removed. + * + * @param size Size of each element within the array. + * @param array Array itself, the array will be modified directly. + * @param length Pointer to the length of the array, this will be modified. + * @param callback Callback to test everything against. + * @return The count of elements that were removed. + */ +int32_t arrayFilter( + size_t size, void *array, int32_t *length, arrayiterationcallback_t *callback ); \ No newline at end of file