Files
dusk/src/dusk/engine/engine.c
T
YourWishesandClaude Sonnet 5 9a24b3d7db Bring the entity-component system online: rendering, scene manager, scripting
entityRenderAll() (src/dusk/entity/entityrender.c) ports ac2's
scene.c:sceneRender() camera/view-matrix/draw-loop logic as a standalone
function - find the active camera, bind SHADER_UNLIT with its projection/
view, draw every COMPONENT_TYPE_RENDERABLE entity. display.c calls it each
frame between the screen clear and screenUnbind(), the same spot the old
sceneRender() used to sit.

A single-scene scene manager (src/dusk/scene/, ported from ac2 minus its
prefab system and physics/trigger stepping - that module hasn't been
ported yet) sits alongside this but isn't wired into the engine loop yet;
only one scene can ever exist (no SCENE_COUNT_MAX array).

entitymanager.c/h removed entirely: its three arrays (entities/components/
entitiesWithComponent) are now plain globals in entity.h/.c
(ENTITIES/COMPONENTS/ENTITIES_WITH_COMPONENT), and every entity/component
function across the whole ECS (entity, component, position, camera,
renderable, physics, trigger, animation) drops its entitymanager_t *mgr
parameter to operate on them directly - there was only ever one instance of
that struct in the whole engine, so the indirection bought nothing.

New duskscript bindings (src/dusk/script/native/entity/) expose entityAdd,
entityAddComponent/entityGetComponent, entitySetName/entityGetName/
entityFindByName, entityDispose/entityDisposeDeep,
entityPositionSetLocalPosition/entityPositionLookAt,
entityRenderableSetMesh (by name - cube/plane/sphere/capsule/triprism/quad),
and COMPONENT_TYPE_*/ENTITY_ID_INVALID/COMPONENT_ID_INVALID constants.
assets/init.ds uses them to spawn a camera (via LookAt from (3,3,3) at the
origin), a cube and a plane as the engine's first real content on the new
ECS.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-10-15 09:08:20 -05:00

114 lines
2.5 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "engine.h"
#include "util/memory.h"
#include "perf/perf.h"
#include "time/time.h"
#include "input/input.h"
#include "locale/localemanager.h"
#include "display/display.h"
#include "entity/entity.h"
#include "asset/asset.h"
#include "assert/assert.h"
#ifdef DUSK_NETWORK
#include "network/network.h"
#endif
#include "script/script.h"
#include "system/system.h"
#include "console/console.h"
#include "save/save.h"
#include "audio/audio.h"
#include "audio/mixer/audiomixer.h"
engine_t ENGINE;
errorret_t engineInit(const int32_t argc, const char_t **argv) {
assertInit();
memoryZero(&ENGINE, sizeof(engine_t));
ENGINE.running = true;
ENGINE.argc = argc;
ENGINE.argv = argv;
ENGINE.version = DUSK_VERSION;
// Init systems. Order is important.
perfInit();
timeInit();
consoleInit();
errorChain(systemInit());
errorChain(inputInit());
errorChain(assetInit());
errorChain(saveInit());
errorChain(localeManagerInit());
errorChain(displayInit());
errorChain(audioInit());
entityManagerInit();
#ifdef DUSK_NETWORK
errorChain(networkInit());
#endif
errorChain(scriptInit());
// Runs once every other system is ready, so init.ds can use any of them.
scripterror_t scriptInitError;
if(scriptRuntimeRunFile(SCRIPT.runtime, "init.dsb", false, &scriptInitError) != SCRIPT_OK) {
errorThrow("init.ds failed: %s", scriptInitError.message);
}
consolePrint("Engine initialized");
#ifdef DUSK_ASSERTIONS_FAKED
consolePrint("Assertions faked");
#else
consolePrint("Assertions real");
#endif
errorOk();
}
errorret_t engineUpdate(void) {
// Order here is important.
#ifdef DUSK_NETWORK
errorChain(networkUpdate());
#endif
errorChain(saveUpdate());
timeUpdate();
inputUpdate();
consoleUpdate();
errorChain(audioUpdateEarly());
errorChain(assetUpdate());
entityManagerUpdate();
// Render
errorChain(displayUpdate());
errorChain(audioUpdateLate());
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
errorOk();
}
void engineExit(void) {
ENGINE.running = false;
}
errorret_t engineDispose(void) {
errorChain(scriptDispose());
entityManagerDispose();
#ifdef DUSK_NETWORK
errorChain(networkDispose());
#endif
localeManagerDispose();
errorChain(audioDispose());
consoleDispose();
errorChain(displayDispose());
errorChain(saveDispose());
errorChain(assetDispose());
perfDispose();
memoryCheckUnfreed();
errorOk();
}