Comitting progress

This commit is contained in:
2022-01-06 16:14:56 -08:00
parent 7b61b590d7
commit af2f9bd2a4
10 changed files with 151 additions and 83 deletions

View File

@ -33,6 +33,9 @@ typedef struct {
/** Save Manager for the game */ /** Save Manager for the game */
savemanager_t save; savemanager_t save;
/** Scene Manager for the game */
scenemanager_t scene;
} engine_t; } engine_t;
/** /**

View File

@ -7,6 +7,7 @@
#include "game.h" #include "game.h"
bool gameInit(game_t *game) { bool gameInit(game_t *game) {
ASSERT_NOT_NULL(game); ASSERT_NOT_NULL(game);

View File

@ -17,11 +17,13 @@
typedef struct { typedef struct {
engine_t engine; engine_t engine;
camera_t camera; camera_t camera;
standardshader_t shader; standardshader_t shader;
primitive_t quad; primitive_t quad;
assetmanagerholder_t owner;
assetmanageritem_t *item; assetmanageritem_t *item;
scenemana
} game_t; } game_t;
/** /**

View File

@ -6,5 +6,5 @@
# Sources # Sources
target_sources(${PROJECT_NAME} target_sources(${PROJECT_NAME}
PRIVATE PRIVATE
scene.c scenemanager.c
) )

View File

@ -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);
}

View File

@ -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);

50
src/scene/scenemanager.c Normal file
View File

@ -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;
}

31
src/scene/scenemanager.h Normal file
View File

@ -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);

View File

@ -215,4 +215,51 @@ int32_t arraySum(
count++; count++;
} }
return 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;
} }

View File

@ -194,4 +194,19 @@ void arraySplice(
*/ */
int32_t arraySum( int32_t arraySum(
size_t size, void *array, int32_t length, arrayiterationcallback_t *callback 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
); );