Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 82c300b077 | |||
| 0f8b629e20 | |||
| 36f6ac65f2 | |||
| a25871a849 | |||
| 57766a9104 | |||
| 3770ae1645 | |||
| d73edb403f | |||
| b14196ff0d | |||
| 88903fee94 | |||
| 1e8311fc04 | |||
| 2b78370cb8 | |||
| 8f78bba9e9 | |||
| 41a4be678e | |||
| 8b2b4b7c3d | |||
| 1f3a29f89d | |||
| c4c93097cd | |||
| eedb7769e6 | |||
| 98db62a4bc | |||
| df48c8e500 | |||
| db9cc0f4c6 | |||
| a79ee429b4 | |||
| 6acfca6d48 | |||
| 1cd6f4cb72 | |||
| 3271e8c7d6 | |||
| 0bcde064af | |||
| 957980b3c5 | |||
| 03eb328d81 |
@@ -144,7 +144,7 @@ jobs:
|
||||
- name: Copy output files.
|
||||
run: |
|
||||
mkdir -p ./git-artifcats/Dusk/apps/Dusk
|
||||
cp build-wii/Dusk.dol ./git-artifcats/Dusk/apps/Dusk/boot.dol
|
||||
cp build-wii/boot.dol ./git-artifcats/Dusk/apps/Dusk/boot.dol
|
||||
cp build-wii/dusk.dsk ./git-artifcats/Dusk/apps/Dusk/dusk.dsk
|
||||
cp build-wii/meta.xml ./git-artifcats/Dusk/apps/Dusk/meta.xml
|
||||
- name: Upload Wii binary
|
||||
|
||||
+2
-1
@@ -105,4 +105,5 @@ yarn.lock
|
||||
/build2
|
||||
/build*
|
||||
/assets/test
|
||||
/tools_old
|
||||
/tools_old
|
||||
/assets/test.png
|
||||
@@ -0,0 +1,12 @@
|
||||
Console.print('This is called from JavaScript');
|
||||
|
||||
const platformNames = {
|
||||
[System.PLATFORM_LINUX]: 'Linux',
|
||||
[System.PLATFORM_KNULLI]: 'Knulli',
|
||||
[System.PLATFORM_PSP]: 'PSP',
|
||||
[System.PLATFORM_GAMECUBE]: 'GameCube',
|
||||
[System.PLATFORM_WII]: 'Wii',
|
||||
};
|
||||
|
||||
const platformName = platformNames[System.platform] || 'Unknown';
|
||||
Console.print('Platform: ' + platformName);
|
||||
@@ -0,0 +1,24 @@
|
||||
// Load rosa.
|
||||
Console.print('Asset time');
|
||||
const entry = Asset.lock('test.png', Asset.TYPE_TEXTURE, Texture.FORMAT_RGBA);
|
||||
Asset.requireLoaded(entry);
|
||||
Console.print('Asset loaded');
|
||||
|
||||
// Camera at (3,3,3) looking at origin
|
||||
const cam = Entity.create();
|
||||
const camPos = cam.add(Component.POSITION);
|
||||
cam.add(Component.CAMERA);
|
||||
camPos.localPosition = new Vec3(3, 3, 3);
|
||||
camPos.lookAt(new Vec3(0, 0, 0));
|
||||
|
||||
// Test entity at origin
|
||||
const testEntity = Entity.create();
|
||||
const testPos = testEntity.add(Component.POSITION);
|
||||
|
||||
/** @type {RenderableSpritebatch} */
|
||||
const testRenderable = testEntity.add(Component.RENDERABLE);
|
||||
testRenderable.texture = entry.texture;
|
||||
testRenderable.sprites = [
|
||||
[0, 0, 1, 1, 0, 1, 1, 0]
|
||||
];
|
||||
testPos.localPosition = new Vec3(0, 0, 0);
|
||||
@@ -0,0 +1,96 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Turn things off we don't need
|
||||
set(JERRY_CMDLINE OFF CACHE BOOL "" FORCE)
|
||||
set(JERRY_EXT ON CACHE BOOL "" FORCE)
|
||||
set(JERRY_DEBUGGER OFF CACHE BOOL "" FORCE)
|
||||
set(JERRY_BUILTIN_DATE OFF CACHE BOOL "" FORCE)
|
||||
set(ENABLE_LTO OFF CACHE BOOL "" FORCE)
|
||||
|
||||
# Fetch Jerry
|
||||
include(FetchContent)
|
||||
FetchContent_Declare(
|
||||
jerryscript
|
||||
GIT_REPOSITORY https://git.wish.moe/YourWishes/jerryscript
|
||||
GIT_TAG float32-fix
|
||||
)
|
||||
FetchContent_MakeAvailable(jerryscript)
|
||||
|
||||
# Mark found
|
||||
set(jerryscript_FOUND ON)
|
||||
|
||||
# Define targets
|
||||
if(TARGET jerryscript-core)
|
||||
set(JERRY_CORE_TARGET jerryscript-core)
|
||||
elseif(TARGET jerry-core)
|
||||
set(JERRY_CORE_TARGET jerry-core)
|
||||
endif()
|
||||
|
||||
if(TARGET jerryscript-ext)
|
||||
set(JERRY_EXT_TARGET jerryscript-ext)
|
||||
elseif(TARGET jerry-ext)
|
||||
set(JERRY_EXT_TARGET jerry-ext)
|
||||
endif()
|
||||
|
||||
if(TARGET jerryscript-port-default)
|
||||
set(JERRY_PORT_TARGET jerryscript-port-default)
|
||||
elseif(TARGET jerry-port-default)
|
||||
set(JERRY_PORT_TARGET jerry-port-default)
|
||||
elseif(TARGET jerryscript-port)
|
||||
set(JERRY_PORT_TARGET jerryscript-port)
|
||||
elseif(TARGET jerry-port)
|
||||
set(JERRY_PORT_TARGET jerry-port)
|
||||
endif()
|
||||
|
||||
if(NOT JERRY_CORE_TARGET)
|
||||
message(FATAL_ERROR "JerryScript core target not found")
|
||||
endif()
|
||||
|
||||
if(NOT JERRY_EXT_TARGET)
|
||||
message(FATAL_ERROR "JerryScript ext target not found")
|
||||
endif()
|
||||
|
||||
if(NOT JERRY_PORT_TARGET)
|
||||
message(FATAL_ERROR "JerryScript port target not found")
|
||||
endif()
|
||||
|
||||
foreach(tgt IN ITEMS
|
||||
${JERRY_CORE_TARGET}
|
||||
${JERRY_EXT_TARGET}
|
||||
${JERRY_PORT_TARGET}
|
||||
)
|
||||
if(TARGET ${tgt})
|
||||
set_property(TARGET ${tgt} PROPERTY INTERPROCEDURAL_OPTIMIZATION OFF)
|
||||
target_compile_definitions(${JERRY_CORE_TARGET} PRIVATE
|
||||
JERRY_NUMBER_TYPE_FLOAT64=0
|
||||
JERRY_BUILTIN_DATE=0
|
||||
)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
# Export include dirs through the targets
|
||||
target_include_directories(${JERRY_CORE_TARGET} INTERFACE
|
||||
${jerryscript_SOURCE_DIR}/jerry-core/include
|
||||
)
|
||||
|
||||
target_include_directories(${JERRY_EXT_TARGET} INTERFACE
|
||||
${jerryscript_SOURCE_DIR}/jerry-ext/include
|
||||
)
|
||||
|
||||
target_include_directories(${JERRY_PORT_TARGET} INTERFACE
|
||||
${jerryscript_SOURCE_DIR}/jerry-port/default/include
|
||||
)
|
||||
|
||||
# Suppress JerryScript-only warning
|
||||
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
|
||||
target_compile_options(${JERRY_CORE_TARGET} PRIVATE
|
||||
-Wno-error
|
||||
)
|
||||
endif()
|
||||
|
||||
add_library(jerryscript::core ALIAS ${JERRY_CORE_TARGET})
|
||||
add_library(jerryscript::ext ALIAS ${JERRY_EXT_TARGET})
|
||||
add_library(jerryscript::port ALIAS ${JERRY_PORT_TARGET})
|
||||
@@ -34,6 +34,7 @@ target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
DUSK_OPENGL
|
||||
DUSK_OPENGL_ES
|
||||
DUSK_LINUX
|
||||
DUSK_KNULLI
|
||||
DUSK_DISPLAY_SIZE_DYNAMIC
|
||||
DUSK_DISPLAY_WIDTH_DEFAULT=640
|
||||
DUSK_DISPLAY_HEIGHT_DEFAULT=480
|
||||
|
||||
@@ -16,9 +16,6 @@ else()
|
||||
)
|
||||
endif()
|
||||
|
||||
# Export symbols so backtrace_symbols() can resolve function names.
|
||||
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
|
||||
|
||||
# Link required libraries.
|
||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
SDL2
|
||||
@@ -29,6 +26,8 @@ target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
# CURL::libcurl
|
||||
)
|
||||
|
||||
set(DUSK_BACKTRACE ON CACHE BOOL "Enable backtrace support for assert failures.")
|
||||
|
||||
# Define platform-specific macros.
|
||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
DUSK_SDL2
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-gamecube.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-gamecube.sh"
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-gamecube-iso.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-gamecube-iso.sh"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-knulli -f docker/knulli/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-knulli /bin/bash -c "./scripts/build-knulli.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-knulli /bin/bash -c "./scripts/build-knulli.sh"
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-linux -f docker/linux/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-linux /bin/bash -c "./scripts/build-linux.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-linux /bin/bash -c "./scripts/build-linux.sh"
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-psp -f docker/psp/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-psp /bin/bash -c "./scripts/build-psp.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-psp /bin/bash -c "./scripts/build-psp.sh"
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-vita -f docker/vita/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-vita /bin/bash -c "./scripts/build-vita.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-vita /bin/bash -c "./scripts/build-vita.sh"
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-wii.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-wii.sh"
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-dolphin -f docker/dolphin/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-dolphin /bin/bash -c "./scripts/build-wii-iso.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-dolphin /bin/bash -c "./scripts/build-wii-iso.sh"
|
||||
|
||||
@@ -7,4 +7,5 @@ fi
|
||||
mkdir -p build-wii
|
||||
cmake -S. -Bbuild-wii -DDUSK_TARGET_SYSTEM=wii -DCMAKE_TOOLCHAIN_FILE="$DEVKITPRO/cmake/Wii.cmake"
|
||||
cd build-wii
|
||||
make -j$(nproc) VERBOSE=1
|
||||
make -j$(nproc) VERBOSE=1
|
||||
mv Dusk.dol boot.dol
|
||||
@@ -1,3 +1,3 @@
|
||||
#!/bin/bash
|
||||
docker build -t dusk-linux -f docker/linux/Dockerfile .
|
||||
docker run --rm -v $(pwd):/workdir dusk-linux /bin/bash -c "./scripts/test-linux.sh"
|
||||
docker run --rm -v "$(pwd):/workdir" dusk-linux /bin/bash -c "./scripts/test-linux.sh"
|
||||
@@ -1,4 +1,6 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
rm -rf build-tests
|
||||
cmake -S . -B build-tests -DDUSK_BUILD_TESTS=ON -DDUSK_TARGET_SYSTEM=linux
|
||||
cmake --build build-tests -- -j$(nproc)
|
||||
ctest --output-on-failure --test-dir build-tests
|
||||
@@ -32,6 +32,22 @@ if(NOT yyjson_FOUND)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(NOT jerryscript_FOUND)
|
||||
find_package(jerryscript REQUIRED)
|
||||
target_link_libraries(${DUSK_LIBRARY_TARGET_NAME} PUBLIC
|
||||
jerryscript::core
|
||||
jerryscript::ext
|
||||
jerryscript::port
|
||||
)
|
||||
endif()
|
||||
|
||||
if(DUSK_BACKTRACE)
|
||||
target_link_options(${DUSK_LIBRARY_TARGET_NAME} PUBLIC -rdynamic)
|
||||
target_compile_definitions(${DUSK_BINARY_TARGET_NAME} PUBLIC
|
||||
DUSK_BACKTRACE
|
||||
)
|
||||
endif()
|
||||
|
||||
# Includes
|
||||
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
@@ -62,6 +78,7 @@ add_subdirectory(input)
|
||||
add_subdirectory(locale)
|
||||
add_subdirectory(physics)
|
||||
add_subdirectory(scene)
|
||||
add_subdirectory(script)
|
||||
add_subdirectory(system)
|
||||
add_subdirectory(time)
|
||||
add_subdirectory(ui)
|
||||
|
||||
@@ -7,5 +7,4 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
easing.c
|
||||
animation.c
|
||||
animationproperty.c
|
||||
)
|
||||
|
||||
@@ -8,86 +8,45 @@
|
||||
#include "util/memory.h"
|
||||
#include "util/math.h"
|
||||
|
||||
void animationInit(animation_t *anim) {
|
||||
memoryZero(anim, sizeof(animation_t));
|
||||
eventInit(&anim->onStart);
|
||||
eventInit(&anim->onComplete);
|
||||
void animationInit(
|
||||
animation_t *anim,
|
||||
keyframe_t *keyframes,
|
||||
uint16_t keyframeCount
|
||||
) {
|
||||
assertNotNull(anim, "Animation pointer cannot be null.");
|
||||
assertNotNull(keyframes, "Keyframes pointer cannot be null.");
|
||||
assertTrue(keyframeCount > 0, "Keyframe count must be more than 0.");
|
||||
|
||||
anim->keyframes = keyframes;
|
||||
anim->keyframeCount = keyframeCount;
|
||||
}
|
||||
|
||||
animationproperty_t *animationAddProperty(animation_t *anim, float_t *target) {
|
||||
assertTrue(
|
||||
anim->propertyCount < ANIMATION_PROPERTY_COUNT_MAX,
|
||||
"Property count exceeds ANIMATION_PROPERTY_COUNT_MAX"
|
||||
);
|
||||
animationproperty_t *prop = &anim->properties[anim->propertyCount++];
|
||||
prop->keyframeCount = 0;
|
||||
prop->target = target;
|
||||
return prop;
|
||||
}
|
||||
float_t animationGetValue(animation_t *anim, const float_t time) {
|
||||
assertNotNull(anim, "Animation pointer cannot be null.");
|
||||
assertNotNull(anim->keyframes, "Keyframes pointer cannot be null.");
|
||||
assertTrue(anim->keyframeCount > 0, "Keyframe count invalid.");
|
||||
assertTrue(time >= 0, "Time must be non-negative.");
|
||||
|
||||
keyframe_t *start;
|
||||
keyframe_t *end;
|
||||
keyframe_t *last = anim->keyframes + anim->keyframeCount - 1;
|
||||
keyframe_t *current = anim->keyframes;
|
||||
start = current;
|
||||
|
||||
void animationUpdate(animation_t *anim, float_t delta) {
|
||||
assertNotNull(anim, "Animation cannot be null");
|
||||
|
||||
if(
|
||||
(anim->flags & ANIMATION_FLAG_FINISHED) &&
|
||||
!(anim->flags & ANIMATION_FLAG_LOOP_ENABLED)
|
||||
) return;
|
||||
|
||||
bool_t wasStarted = (anim->flags & ANIMATION_FLAG_STARTED) != 0;
|
||||
anim->flags |= ANIMATION_FLAG_STARTED;
|
||||
anim->time += delta;
|
||||
|
||||
float_t duration = animationGetDuration(anim);
|
||||
bool_t justFinished = false;
|
||||
if(anim->time >= duration) {
|
||||
if(anim->flags & ANIMATION_FLAG_LOOP_ENABLED) {
|
||||
anim->time = mathModFloat(anim->time, duration);
|
||||
} else {
|
||||
anim->time = duration;
|
||||
if(!(anim->flags & ANIMATION_FLAG_FINISHED)) {
|
||||
anim->flags |= ANIMATION_FLAG_FINISHED;
|
||||
justFinished = true;
|
||||
}
|
||||
do {
|
||||
if(current->time > time) {
|
||||
end = current;
|
||||
break;
|
||||
}
|
||||
}
|
||||
start = current;
|
||||
current++;
|
||||
|
||||
for(uint8_t i = 0; i < anim->propertyCount; i++) {
|
||||
animationproperty_t *prop = &anim->properties[i];
|
||||
if(prop->target != NULL) {
|
||||
*prop->target = animationPropertyGetValue(prop, anim->time);
|
||||
if(current > last) {
|
||||
end = start;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while(true);
|
||||
|
||||
if(!wasStarted) eventInvoke(&anim->onStart, anim);
|
||||
if(justFinished) eventInvoke(&anim->onComplete, anim);
|
||||
}
|
||||
|
||||
|
||||
void animationReset(animation_t *anim) {
|
||||
anim->time = 0.0f;
|
||||
anim->flags &= ~(ANIMATION_FLAG_FINISHED | ANIMATION_FLAG_STARTED);
|
||||
}
|
||||
|
||||
bool_t animationIsFinished(const animation_t *anim) {
|
||||
return (anim->flags & ANIMATION_FLAG_FINISHED) != 0;
|
||||
}
|
||||
|
||||
bool_t animationIsStarted(const animation_t *anim) {
|
||||
return (anim->flags & ANIMATION_FLAG_STARTED) != 0;
|
||||
}
|
||||
|
||||
bool_t animationIsLooping(const animation_t *anim) {
|
||||
return (anim->flags & ANIMATION_FLAG_LOOP_ENABLED) != 0;
|
||||
}
|
||||
|
||||
float_t animationGetDuration(const animation_t *anim) {
|
||||
float_t duration = 0.0f;
|
||||
for(uint8_t i = 0; i < anim->propertyCount; i++) {
|
||||
animationproperty_t *prop = &anim->properties[i];
|
||||
if(prop->keyframeCount == 0) continue;
|
||||
float_t lastKeyframeTime =
|
||||
prop->keyframes[prop->keyframeCount - 1].time;
|
||||
if(lastKeyframeTime > duration) duration = lastKeyframeTime;
|
||||
}
|
||||
return duration;
|
||||
float_t t = (time - start->time) / (end->time - start->time);
|
||||
return mathLerp(start->value, end->value, easingApply(start->easing, t));
|
||||
}
|
||||
@@ -4,87 +4,31 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "animationproperty.h"
|
||||
#include "event/event.h"
|
||||
|
||||
#define ANIMATION_PROPERTY_COUNT_MAX 8
|
||||
|
||||
#define ANIMATION_FLAG_FINISHED (1 << 0)
|
||||
#define ANIMATION_FLAG_STARTED (1 << 1)
|
||||
#define ANIMATION_FLAG_LOOP_ENABLED (1 << 2)
|
||||
#include "keyframe.h"
|
||||
|
||||
typedef struct {
|
||||
animationproperty_t properties[ANIMATION_PROPERTY_COUNT_MAX];
|
||||
uint8_t propertyCount;
|
||||
float_t time;
|
||||
uint8_t flags;
|
||||
event_t onStart;
|
||||
event_t onComplete;
|
||||
keyframe_t *keyframes;
|
||||
uint16_t keyframeCount;
|
||||
} animation_t;
|
||||
|
||||
/**
|
||||
* Initializes an animation.
|
||||
*
|
||||
* @param anim The animation to initialize.
|
||||
* @param keyframes The keyframes to use for the animation.
|
||||
* @param keyframeCount The number of keyframes in the animation.
|
||||
*/
|
||||
void animationInit(animation_t *anim);
|
||||
void animationInit(
|
||||
animation_t *anim,
|
||||
keyframe_t *keyframes,
|
||||
uint16_t keyframeCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Adds a new animated property. The caller owns target and must keep it valid
|
||||
* for the lifetime of the animation.
|
||||
* Gets the value of the animation at a given time.
|
||||
*
|
||||
* @param anim The animation to add the property to.
|
||||
* @param target Pointer to the float to write interpolated values into.
|
||||
* @return A pointer to the new property.
|
||||
* @param anim The animation to get the value from.
|
||||
* @param time The time at which to get the value, in seconds.
|
||||
* @return The value of the animation at the given time.
|
||||
*/
|
||||
animationproperty_t *animationAddProperty(animation_t *anim, float_t *target);
|
||||
|
||||
/**
|
||||
* Advances the animation by delta seconds and writes interpolated values to
|
||||
* each property's target pointer.
|
||||
*
|
||||
* @param anim The animation to update.
|
||||
* @param delta The time to advance the animation by, in seconds.
|
||||
*/
|
||||
void animationUpdate(animation_t *anim, float_t delta);
|
||||
|
||||
/**
|
||||
* Resets the animation to the beginning, clearing Started and Finished flags.
|
||||
*
|
||||
* @param anim The animation to reset.
|
||||
*/
|
||||
void animationReset(animation_t *anim);
|
||||
|
||||
/**
|
||||
* Returns true if the animation has finished (i.e. reached the end of its
|
||||
* duration and is not looping).
|
||||
*
|
||||
* @param anim The animation to check.
|
||||
* @return true if the animation has finished, false otherwise.
|
||||
*/
|
||||
bool_t animationIsFinished(const animation_t *anim);
|
||||
|
||||
/**
|
||||
* Returns true if the animation has been started (i.e. animationUpdate has
|
||||
* been called at least once).
|
||||
*
|
||||
* @param anim The animation to check.
|
||||
* @return true if the animation has been started, false otherwise.
|
||||
*/
|
||||
bool_t animationIsStarted(const animation_t *anim);
|
||||
|
||||
/**
|
||||
* Returns true if the animation is set to loop.
|
||||
*
|
||||
* @param anim The animation to check.
|
||||
* @return true if the animation is set to loop, false otherwise.
|
||||
*/
|
||||
bool_t animationIsLooping(const animation_t *anim);
|
||||
|
||||
/**
|
||||
* Gets the total duration of the animation (based on the keyframes).
|
||||
*
|
||||
* @param anim The animation to get the duration of.
|
||||
* @return The total duration of the animation, in seconds.
|
||||
*/
|
||||
float_t animationGetDuration(const animation_t *anim);
|
||||
float_t animationGetValue(animation_t *anim, const float_t time);
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "animationproperty.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void animationPropertyAddKeyframe(
|
||||
animationproperty_t *prop,
|
||||
const float_t time,
|
||||
const float_t value,
|
||||
const easingtype_t easing
|
||||
) {
|
||||
assertNotNull(prop, "Property cannot be null");
|
||||
assertTrue(
|
||||
prop->keyframeCount < ANIMATION_PROPERTY_KEYFRAME_COUNT_MAX,
|
||||
"Too many keyframes added to property"
|
||||
);
|
||||
assertTrue(time >= 0.0f, "Keyframe time cannot be negative");
|
||||
|
||||
keyframe_t *frame = &prop->keyframes[prop->keyframeCount++];
|
||||
frame->time = time;
|
||||
frame->value = value;
|
||||
frame->easing = easing;
|
||||
}
|
||||
|
||||
float_t animationPropertyGetValue(
|
||||
const animationproperty_t *prop,
|
||||
const float_t time
|
||||
) {
|
||||
assertNotNull(prop, "Property cannot be null");
|
||||
assertTrue(time >= 0.0f, "Time cannot be negative");
|
||||
|
||||
if(prop->keyframeCount == 0) return 0.0f;
|
||||
|
||||
uint8_t last = prop->keyframeCount - 1;
|
||||
|
||||
if(prop->keyframeCount == 1) return prop->keyframes[0].value;
|
||||
if(time <= prop->keyframes[0].time) return prop->keyframes[0].value;
|
||||
if(time >= prop->keyframes[last].time) return prop->keyframes[last].value;
|
||||
|
||||
for(uint8_t i = 0; i < last; i++) {
|
||||
const keyframe_t *a = &prop->keyframes[i];
|
||||
const keyframe_t *b = &prop->keyframes[i + 1];
|
||||
if(time < a->time || time >= b->time) continue;
|
||||
float_t t = (time - a->time) / (b->time - a->time);
|
||||
t = easingApply(a->easing, t);
|
||||
return a->value + (b->value - a->value) * t;
|
||||
}
|
||||
|
||||
return prop->keyframes[last].value;
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "keyframe.h"
|
||||
|
||||
#define ANIMATION_PROPERTY_KEYFRAME_COUNT_MAX 16
|
||||
|
||||
typedef struct {
|
||||
keyframe_t keyframes[ANIMATION_PROPERTY_KEYFRAME_COUNT_MAX];
|
||||
uint8_t keyframeCount;
|
||||
float_t *target;
|
||||
} animationproperty_t;
|
||||
|
||||
/**
|
||||
* Appends a keyframe to a property. Keyframes must be added in ascending time
|
||||
* order. Updates the animation's duration.
|
||||
*
|
||||
* @param property The property to add the keyframe to.
|
||||
* @param time The time of the keyframe, in seconds.
|
||||
* @param value The value of the keyframe.
|
||||
* @param easing The easing type to use when interpolating to the next keyframe.
|
||||
*/
|
||||
void animationPropertyAddKeyframe(
|
||||
animationproperty_t *property,
|
||||
const float_t time,
|
||||
const float_t value,
|
||||
const easingtype_t easing
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets the property's value at a given time.
|
||||
*
|
||||
* @param prop The property to get the value from.
|
||||
* @param time The time at which to get the value, in seconds.
|
||||
* @return The value of the property at the given time.
|
||||
*/
|
||||
float_t animationPropertyGetValue(
|
||||
const animationproperty_t *prop,
|
||||
const float_t time
|
||||
);
|
||||
@@ -10,4 +10,4 @@ typedef struct {
|
||||
float_t time;
|
||||
float_t value;
|
||||
easingtype_t easing;
|
||||
} keyframe_t;
|
||||
} keyframe_t;
|
||||
+21
-19
@@ -8,24 +8,7 @@
|
||||
#include "assert.h"
|
||||
#include "log/log.h"
|
||||
#include "util/string.h"
|
||||
|
||||
#ifdef DUSK_LINUX
|
||||
#include <execinfo.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void assertLogBacktrace(void) {
|
||||
void *frames[64];
|
||||
int count = backtrace(frames, 64);
|
||||
char **symbols = backtrace_symbols(frames, count);
|
||||
logError("Stack trace:\n");
|
||||
if(symbols) {
|
||||
for(int i = 0; i < count; i++) {
|
||||
logError(" %s\n", symbols[i]);
|
||||
}
|
||||
free(symbols);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#include "util/memory.h"
|
||||
|
||||
#ifndef DUSK_ASSERTIONS_FAKED
|
||||
#ifdef DUSK_TEST_ASSERT
|
||||
@@ -43,6 +26,25 @@
|
||||
);
|
||||
}
|
||||
#else
|
||||
#ifdef DUSK_BACKTRACE
|
||||
#include <execinfo.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static void assertLogBacktrace(void) {
|
||||
void *frames[64];
|
||||
int count = backtrace(frames, 64);
|
||||
char **symbols = backtrace_symbols(frames, count);
|
||||
memoryTrack(symbols);
|
||||
logError("Stack trace:\n");
|
||||
if(symbols) {
|
||||
for(int i = 0; i < count; i++) {
|
||||
logError(" %s\n", symbols[i]);
|
||||
}
|
||||
memoryFree(symbols);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void assertTrueImpl(
|
||||
const char *file,
|
||||
const int32_t line,
|
||||
@@ -56,7 +58,7 @@
|
||||
line,
|
||||
message
|
||||
);
|
||||
#ifdef DUSK_LINUX
|
||||
#ifdef DUSK_BACKTRACE
|
||||
assertLogBacktrace();
|
||||
#endif
|
||||
abort();
|
||||
|
||||
@@ -7,9 +7,8 @@
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
asset.c
|
||||
assetfile.c
|
||||
assetcache.c
|
||||
assetbatch.c
|
||||
assetfile.c
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
|
||||
+313
-15
@@ -11,17 +11,24 @@
|
||||
#include "assert/assert.h"
|
||||
#include "engine/engine.h"
|
||||
#include "util/string.h"
|
||||
#include "console/console.h"
|
||||
#include <unistd.h>
|
||||
|
||||
asset_t ASSET;
|
||||
|
||||
errorret_t assetInit(void) {
|
||||
memoryZero(&ASSET, sizeof(asset_t));
|
||||
|
||||
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
|
||||
threadMutexInit(&ASSET.loading[i].mutex);
|
||||
}
|
||||
|
||||
// assetInitPlatform must either define ASSET.zip or throw an error.
|
||||
errorChain(assetInitPlatform());
|
||||
assertNotNull(ASSET.zip, "Asset zip null without error.");
|
||||
threadInit(&ASSET.loadThread, assetUpdateAsync);
|
||||
threadStart(&ASSET.loadThread);
|
||||
|
||||
assetCacheInit(&ASSET.cache);
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -33,26 +40,317 @@ bool_t assetFileExists(const char_t *filename) {
|
||||
return true;
|
||||
}
|
||||
|
||||
errorret_t assetLoad(
|
||||
const char_t *filename,
|
||||
assetfileloader_t loader,
|
||||
void *params,
|
||||
void *output
|
||||
assetentry_t * assetGetEntry(
|
||||
const char_t *name,
|
||||
const assetloadertype_t type,
|
||||
assetloaderinput_t *input
|
||||
) {
|
||||
assertStrLenMax(filename, ASSET_FILE_NAME_MAX, "Filename too long.");
|
||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||
assertNotNull(loader, "Asset file loader cannot be NULL.");
|
||||
// Is there an existing asset?
|
||||
assetentry_t *entry = ASSET.entries;
|
||||
do {
|
||||
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
if(stringEquals(entry->name, name)) {
|
||||
assertTrue(entry->type == type, "Asset entry type mismatch.");
|
||||
return entry;
|
||||
}
|
||||
entry++;
|
||||
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||
|
||||
// We did not find one existing, Find first available slot.
|
||||
entry = ASSET.entries;
|
||||
do {
|
||||
if(entry->type != ASSET_LOADER_TYPE_NULL) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
assetfile_t file;
|
||||
errorChain(assetFileInit(&file, filename, params, output));
|
||||
errorChain(loader(&file));
|
||||
errorChain(assetFileDispose(&file));
|
||||
if(entry->state == ASSET_ENTRY_STATE_NOT_STARTED) {
|
||||
assetEntryInit(entry, name, type, input);
|
||||
return entry;
|
||||
}
|
||||
entry++;
|
||||
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||
|
||||
assertUnreachable("No available asset entry slots.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
errorret_t assetRequireLoaded(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Entry cannot be NULL.");
|
||||
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||
|
||||
if(entry->state == ASSET_ENTRY_STATE_LOADED) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
// Lock to prevent the reaper from collecting the entry mid-spin.
|
||||
assetEntryLock(entry);
|
||||
|
||||
while(entry->state != ASSET_ENTRY_STATE_LOADED) {
|
||||
usleep(1000);
|
||||
errorret_t ret = assetUpdate();
|
||||
if(errorIsNotOk(ret)) {
|
||||
assetEntryUnlock(entry);
|
||||
errorChain(ret);
|
||||
}
|
||||
}
|
||||
|
||||
assetEntryUnlock(entry);
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetDispose(void) {
|
||||
assetCacheDispose(&ASSET.cache);
|
||||
assetentry_t * assetLock(
|
||||
const char_t *name,
|
||||
const assetloadertype_t type,
|
||||
assetloaderinput_t *input
|
||||
) {
|
||||
assetentry_t *entry = assetGetEntry(name, type, input);
|
||||
assetEntryLock(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
void assetUnlock(const char_t *name) {
|
||||
assertNotNull(name, "Name cannot be NULL.");
|
||||
assetentry_t *entry = ASSET.entries;
|
||||
do {
|
||||
if(entry->type != ASSET_LOADER_TYPE_NULL && stringEquals(entry->name, name)) {
|
||||
assetEntryUnlock(entry);
|
||||
return;
|
||||
}
|
||||
entry++;
|
||||
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||
assertUnreachable("Asset entry not found for unlock.");
|
||||
}
|
||||
|
||||
void assetUnlockEntry(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Entry cannot be NULL.");
|
||||
assetEntryUnlock(entry);
|
||||
}
|
||||
|
||||
errorret_t assetUpdate(void) {
|
||||
// Determine how many available loading slots we have.
|
||||
assetloading_t *availableLoading[ASSET_LOADING_COUNT_MAX];
|
||||
uint8_t availableLoadingCount = 0;
|
||||
assetloading_t *loading = ASSET.loading;
|
||||
assetentry_t *entry;
|
||||
|
||||
|
||||
do {
|
||||
// We only care about NULL entry references. Nothing async touches this so
|
||||
// it's fine to use raw here.
|
||||
if(loading->entry != NULL) {
|
||||
loading++;
|
||||
continue;
|
||||
}
|
||||
availableLoading[availableLoadingCount++] = loading;
|
||||
loading++;
|
||||
} while(loading < ASSET.loading + ASSET_LOADING_COUNT_MAX);
|
||||
|
||||
|
||||
// Now we can check for pending asset entries, we can't do anything if there
|
||||
// is no available slots though.
|
||||
if(availableLoadingCount > 0) {
|
||||
entry = ASSET.entries;
|
||||
do {
|
||||
// Is this asset "ready to start loading" ?
|
||||
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// We only care about assets not started.
|
||||
if(entry->state != ASSET_ENTRY_STATE_NOT_STARTED) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Pop a loading slot for this asset entry.
|
||||
loading = availableLoading[--availableLoadingCount];
|
||||
|
||||
// Start loading this asset.
|
||||
assetEntryStartLoading(entry, loading);
|
||||
entry++;
|
||||
|
||||
// Did we run out of loading slots?
|
||||
if(availableLoadingCount == 0) {
|
||||
break;
|
||||
}
|
||||
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||
}
|
||||
|
||||
// Now walk over all the loading slots and see what needs to be done.
|
||||
loading = ASSET.loading;
|
||||
do {
|
||||
// Is the loading slot in use? Entry can only be modified synchronously.
|
||||
if(loading->entry == NULL) {
|
||||
loading++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Lock the loading slot. This will prevent any async modifications.
|
||||
threadMutexLock(&loading->mutex);
|
||||
|
||||
// Check the state of the entry.
|
||||
switch(loading->entry->state) {
|
||||
// This thing is pending synchronous loading.
|
||||
case ASSET_ENTRY_STATE_PENDING_SYNC:
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADING_SYNC;
|
||||
// Unlock before calling loadSync. The sync loader may re-enter
|
||||
// assetUpdate (e.g. a script loading another asset), and the async
|
||||
// thread never touches LOADING_SYNC entries, so this is safe.
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
|
||||
errorret_t ret = (
|
||||
ASSET_LOADER_CALLBACKS[loading->type].loadSync(loading)
|
||||
);
|
||||
|
||||
// After a sync load, these are the only valid states.
|
||||
assertTrue(
|
||||
loading->entry->state == ASSET_ENTRY_STATE_LOADED ||
|
||||
loading->entry->state == ASSET_ENTRY_STATE_ERROR ||
|
||||
loading->entry->state == ASSET_ENTRY_STATE_PENDING_SYNC ||
|
||||
loading->entry->state == ASSET_ENTRY_STATE_PENDING_ASYNC,
|
||||
"Loader did not set entry state to loaded or error on finished load."
|
||||
);
|
||||
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
assertTrue(
|
||||
loading->entry->state == ASSET_ENTRY_STATE_ERROR,
|
||||
"Loader did not set entry state to error on failed load."
|
||||
);
|
||||
}
|
||||
|
||||
loading++;
|
||||
break;
|
||||
|
||||
case ASSET_ENTRY_STATE_LOADING_SYNC:
|
||||
// A re-entrant assetUpdate call (e.g. from a script loading another
|
||||
// asset) will see this entry mid-sync-load. Skip it.
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
loading++;
|
||||
continue;
|
||||
|
||||
// Done loading, we can just free it up.
|
||||
case ASSET_ENTRY_STATE_LOADED:
|
||||
loading->entry = NULL;
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
loading++;
|
||||
break;
|
||||
|
||||
case ASSET_ENTRY_STATE_ERROR:
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
errorThrow("Failed to load asset asynchronously.");
|
||||
break;
|
||||
|
||||
default:
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
loading++;
|
||||
continue;
|
||||
}
|
||||
} while(loading < ASSET.loading + ASSET_LOADING_COUNT_MAX);
|
||||
|
||||
|
||||
// Reap entries that have no external locks (refs.count == 0) AND have been
|
||||
// explicitly locked at least once. Entries that were never locked are left
|
||||
// alone — raw-pointer callers hold no ref but should not lose the entry.
|
||||
entry = ASSET.entries;
|
||||
do {
|
||||
if(entry->state != ASSET_ENTRY_STATE_LOADED) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(entry->type == ASSET_LOADER_TYPE_NULL) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!entry->wasLocked) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(entry->refs.count > 0) {
|
||||
entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
consolePrint("Reaping asset %s", entry->name);
|
||||
errorChain(assetEntryDispose(entry));
|
||||
entry++;
|
||||
} while(entry < ASSET.entries + ASSET_ENTRY_COUNT_MAX);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void assetUpdateAsync(thread_t *thread) {
|
||||
while(!threadShouldStop(thread)) {
|
||||
// Walk over each asset
|
||||
assetloading_t *loading;
|
||||
loading = ASSET.loading;
|
||||
|
||||
do {
|
||||
threadMutexLock(&loading->mutex);
|
||||
|
||||
if(loading->entry == NULL) {
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
loading++;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(loading->entry->state) {
|
||||
case ASSET_ENTRY_STATE_PENDING_ASYNC:
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADING_ASYNC;
|
||||
assertNotNull(
|
||||
ASSET_LOADER_CALLBACKS[loading->type].loadAsync,
|
||||
"Loader does not support async loading."
|
||||
);
|
||||
errorret_t ret = (
|
||||
ASSET_LOADER_CALLBACKS[loading->type].loadAsync(loading)
|
||||
);
|
||||
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
assertTrue(
|
||||
loading->entry->state == ASSET_ENTRY_STATE_ERROR,
|
||||
"Loader did not set entry state to error on failed load."
|
||||
);
|
||||
}
|
||||
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
loading++;
|
||||
break;
|
||||
|
||||
case ASSET_ENTRY_STATE_LOADING_ASYNC:
|
||||
assertUnreachable(
|
||||
"Entry is in a pending async state still?"
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
threadMutexUnlock(&loading->mutex);
|
||||
loading++;
|
||||
continue;
|
||||
}
|
||||
} while(loading < ASSET.loading + ASSET_LOADING_COUNT_MAX);
|
||||
|
||||
if(threadShouldStop(thread)) break;
|
||||
usleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
errorret_t assetDispose(void) {
|
||||
threadStop(&ASSET.loadThread);
|
||||
|
||||
for(size_t i = 0; i < ASSET_LOADING_COUNT_MAX; i++) {
|
||||
threadMutexDispose(&ASSET.loading[i].mutex);
|
||||
}
|
||||
|
||||
// Cleanup zip file.
|
||||
if(ASSET.zip != NULL) {
|
||||
if(zip_close(ASSET.zip) != 0) {
|
||||
errorThrow("Failed to close asset zip archive.");
|
||||
|
||||
+80
-14
@@ -9,7 +9,9 @@
|
||||
#include "error/error.h"
|
||||
#include "asset/assetplatform.h"
|
||||
#include "assetfile.h"
|
||||
#include "assetcache.h"
|
||||
#include "thread/thread.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
#include "asset/loader/assetloading.h"
|
||||
|
||||
#ifndef assetInitPlatform
|
||||
#error "Platform must define assetInitPlatform function."
|
||||
@@ -21,16 +23,27 @@
|
||||
#define ASSET_FILE_NAME "dusk.dsk"
|
||||
#define ASSET_HEADER_SIZE 3
|
||||
|
||||
#define ASSET_LOADING_COUNT_MAX 4
|
||||
#define ASSET_ENTRY_COUNT_MAX 128
|
||||
|
||||
typedef struct asset_s {
|
||||
zip_t *zip;
|
||||
assetplatform_t platform;
|
||||
assetcache_t cache;
|
||||
|
||||
// Background loading thread.
|
||||
thread_t loadThread;
|
||||
|
||||
// Assets that are mid loading.
|
||||
assetloading_t loading[ASSET_LOADING_COUNT_MAX];
|
||||
assetentry_t entries[ASSET_ENTRY_COUNT_MAX];
|
||||
} asset_t;
|
||||
|
||||
extern asset_t ASSET;
|
||||
|
||||
/**
|
||||
* Initializes the asset system.
|
||||
*
|
||||
* @return An error code if the asset system could not be initialized properly.
|
||||
*/
|
||||
errorret_t assetInit(void);
|
||||
|
||||
@@ -43,21 +56,74 @@ errorret_t assetInit(void);
|
||||
bool_t assetFileExists(const char_t *filename);
|
||||
|
||||
/**
|
||||
* Loads an asset by its filename,
|
||||
*
|
||||
* @param filename The filename of the asset to retrieve.
|
||||
* @param loader Loader to use for loading the asset file.
|
||||
* @param params Parameters to pass to the loader.
|
||||
* @param output The output pointer to store the loaded asset data.
|
||||
* @return An error code if the asset could not be loaded.
|
||||
* Gets, or creates, a new asset entry. Internal — prefer assetLock.
|
||||
*
|
||||
* @param name Filename of the asset.
|
||||
* @param type Type of the asset.
|
||||
* @param input Loader-specific parameters.
|
||||
*/
|
||||
errorret_t assetLoad(
|
||||
const char_t *filename,
|
||||
assetfileloader_t loader,
|
||||
void *params,
|
||||
void *output
|
||||
assetentry_t * assetGetEntry(
|
||||
const char_t *name,
|
||||
const assetloadertype_t type,
|
||||
assetloaderinput_t *input
|
||||
);
|
||||
|
||||
/**
|
||||
* Gets, creates, and locks an asset entry. The asset will begin loading on
|
||||
* the next assetUpdate. Call assetUnlock when done to allow the entry to be
|
||||
* reclaimed.
|
||||
*
|
||||
* @param name Filename of the asset.
|
||||
* @param type Type of the asset.
|
||||
* @param input Loader-specific parameters.
|
||||
* @return The locked asset entry.
|
||||
*/
|
||||
assetentry_t * assetLock(
|
||||
const char_t *name,
|
||||
const assetloadertype_t type,
|
||||
assetloaderinput_t *input
|
||||
);
|
||||
|
||||
/**
|
||||
* Releases a lock on an asset entry by name. When all locks are released the
|
||||
* entry will be reclaimed at the start of the next assetUpdate.
|
||||
*
|
||||
* @param name Filename of the asset to unlock.
|
||||
*/
|
||||
void assetUnlock(const char_t *name);
|
||||
|
||||
/**
|
||||
* Releases a lock on an asset entry by pointer. When all locks are released
|
||||
* the entry will be reclaimed at the start of the next assetUpdate.
|
||||
*
|
||||
* @param entry The asset entry to unlock.
|
||||
*/
|
||||
void assetUnlockEntry(assetentry_t *entry);
|
||||
|
||||
/**
|
||||
* Requires an asset entry to be loaded. This will block until the asset entry
|
||||
* is fully loaded.
|
||||
*
|
||||
* @param entry The asset entry to require.
|
||||
* @return An error code if the asset entry could not be loaded properly.
|
||||
*/
|
||||
errorret_t assetRequireLoaded(assetentry_t *entry);
|
||||
|
||||
/**
|
||||
* Updates the asset system.
|
||||
*
|
||||
* @return An error code if the asset system could not be updated properly.
|
||||
*/
|
||||
errorret_t assetUpdate(void);
|
||||
|
||||
/**
|
||||
* Starts the background asset loading thread. The thread runs assetUpdate
|
||||
* in a loop with a short sleep until stopped.
|
||||
*
|
||||
* @param thread The thread runner.
|
||||
*/
|
||||
void assetUpdateAsync(thread_t *thread);
|
||||
|
||||
/**
|
||||
* Disposes/cleans up the asset system.
|
||||
*
|
||||
|
||||
+68
-55
@@ -7,75 +7,88 @@
|
||||
|
||||
#include "assetbatch.h"
|
||||
#include "asset.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void assetBatchInit(
|
||||
assetbatch_t *batch,
|
||||
assetbatchcomplete_t onComplete,
|
||||
assetbatcherror_t onError,
|
||||
void *context
|
||||
const uint16_t count,
|
||||
const assetbatchdesc_t *descs
|
||||
) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
assertNotNull(descs, "Descs cannot be NULL.");
|
||||
assertTrue(count > 0, "Count must be greater than 0.");
|
||||
assertTrue(count <= ASSET_BATCH_COUNT_MAX, "Count exceeds ASSET_BATCH_COUNT_MAX.");
|
||||
|
||||
memoryZero(batch, sizeof(assetbatch_t));
|
||||
batch->onComplete = onComplete;
|
||||
batch->onError = onError;
|
||||
batch->context = context;
|
||||
}
|
||||
batch->count = count;
|
||||
|
||||
void assetBatchAdd(
|
||||
assetbatch_t *batch,
|
||||
const char_t *path,
|
||||
assetfileloader_t loader,
|
||||
const void *params,
|
||||
size_t paramsSize,
|
||||
size_t dataSize,
|
||||
assetcachedisposer_t disposer,
|
||||
void **outPtr
|
||||
) {
|
||||
assertTrue(batch->count < ASSET_BATCH_MAX, "Asset batch is full.");
|
||||
assertTrue(paramsSize <= ASSET_BATCH_PARAMS_SIZE, "Asset batch params too large.");
|
||||
assertNotNull(outPtr, "Batch output pointer cannot be NULL.");
|
||||
|
||||
assetbatchitem_t *item = &batch->items[batch->count++];
|
||||
stringCopy(item->path, path, ASSET_FILE_NAME_MAX);
|
||||
item->loader = loader;
|
||||
if (params != NULL && paramsSize > 0) {
|
||||
memoryCopy(item->params, params, paramsSize);
|
||||
for(uint16_t i = 0; i < count; i++) {
|
||||
// Copy input into batch-owned storage so the descriptor need not persist.
|
||||
batch->inputs[i] = descs[i].input;
|
||||
batch->entries[i] = assetLock(descs[i].path, descs[i].type, &batch->inputs[i]);
|
||||
}
|
||||
item->dataSize = dataSize;
|
||||
item->disposer = disposer;
|
||||
item->outPtr = outPtr;
|
||||
}
|
||||
|
||||
errorret_t assetBatchLoad(assetbatch_t *batch) {
|
||||
for (uint8_t i = 0; i < batch->count; i++) {
|
||||
assetbatchitem_t *item = &batch->items[i];
|
||||
void assetBatchLock(assetbatch_t *batch) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
for(uint16_t i = 0; i < batch->count; i++) {
|
||||
assetEntryLock(batch->entries[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void *cached = assetCacheLookup(&ASSET.cache, item->path);
|
||||
if (cached != NULL) {
|
||||
assetCacheRetain(&ASSET.cache, item->path);
|
||||
*item->outPtr = cached;
|
||||
continue;
|
||||
}
|
||||
void assetBatchUnlock(assetbatch_t *batch) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
for(uint16_t i = 0; i < batch->count; i++) {
|
||||
assetEntryUnlock(batch->entries[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void *data = memoryAllocate(item->dataSize);
|
||||
errorret_t err = assetLoad(item->path, item->loader, item->params, data);
|
||||
if (err.code != ERROR_OK) {
|
||||
memoryFree(data);
|
||||
if (batch->onError != NULL) {
|
||||
batch->onError(batch, batch->context, err);
|
||||
bool_t assetBatchIsLoaded(const assetbatch_t *batch) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
for(uint16_t i = 0; i < batch->count; i++) {
|
||||
if(batch->entries[i]->state != ASSET_ENTRY_STATE_LOADED) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool_t assetBatchHasError(const assetbatch_t *batch) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
for(uint16_t i = 0; i < batch->count; i++) {
|
||||
if(batch->entries[i]->state == ASSET_ENTRY_STATE_ERROR) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
errorret_t assetBatchRequireLoaded(assetbatch_t *batch) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
|
||||
bool_t allDone;
|
||||
do {
|
||||
allDone = true;
|
||||
for(uint16_t i = 0; i < batch->count; i++) {
|
||||
const assetentrystate_t state = batch->entries[i]->state;
|
||||
if(state == ASSET_ENTRY_STATE_ERROR) {
|
||||
errorThrow("Asset '%s' failed to load.", batch->entries[i]->name);
|
||||
}
|
||||
if(state != ASSET_ENTRY_STATE_LOADED) {
|
||||
allDone = false;
|
||||
}
|
||||
return errorChainImpl(err, __FILE__, __func__, __LINE__);
|
||||
}
|
||||
|
||||
assetCacheInsert(&ASSET.cache, item->path, data, item->disposer);
|
||||
*item->outPtr = data;
|
||||
}
|
||||
|
||||
if (batch->onComplete != NULL) {
|
||||
batch->onComplete(batch, batch->context);
|
||||
}
|
||||
if(!allDone) {
|
||||
usleep(1000);
|
||||
errorChain(assetUpdate());
|
||||
}
|
||||
} while(!allDone);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void assetBatchDispose(assetbatch_t *batch) {
|
||||
assertNotNull(batch, "Batch cannot be NULL.");
|
||||
for(uint16_t i = 0; i < batch->count; i++) {
|
||||
assetUnlockEntry(batch->entries[i]);
|
||||
}
|
||||
memoryZero(batch, sizeof(assetbatch_t));
|
||||
}
|
||||
|
||||
+55
-63
@@ -6,85 +6,77 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "assetcache.h"
|
||||
#include "error/error.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
#include "asset/loader/assetloader.h"
|
||||
|
||||
#ifndef ASSET_BATCH_MAX
|
||||
#define ASSET_BATCH_MAX 16
|
||||
#endif
|
||||
|
||||
#define ASSET_BATCH_PARAMS_SIZE 16
|
||||
|
||||
typedef struct assetbatch_s assetbatch_t;
|
||||
|
||||
typedef void (*assetbatchcomplete_t)(assetbatch_t *batch, void *context);
|
||||
typedef void (*assetbatcherror_t)(
|
||||
assetbatch_t *batch,
|
||||
void *context,
|
||||
errorret_t error
|
||||
);
|
||||
#define ASSET_BATCH_COUNT_MAX 64
|
||||
|
||||
typedef struct {
|
||||
char_t path[ASSET_FILE_NAME_MAX];
|
||||
assetfileloader_t loader;
|
||||
uint8_t params[ASSET_BATCH_PARAMS_SIZE];
|
||||
size_t dataSize;
|
||||
assetcachedisposer_t disposer;
|
||||
void **outPtr;
|
||||
} assetbatchitem_t;
|
||||
const char_t *path;
|
||||
assetloadertype_t type;
|
||||
assetloaderinput_t input;
|
||||
} assetbatchdesc_t;
|
||||
|
||||
struct assetbatch_s {
|
||||
assetbatchitem_t items[ASSET_BATCH_MAX];
|
||||
uint8_t count;
|
||||
assetbatchcomplete_t onComplete;
|
||||
assetbatcherror_t onError;
|
||||
void *context;
|
||||
};
|
||||
typedef struct {
|
||||
assetentry_t *entries[ASSET_BATCH_COUNT_MAX];
|
||||
assetloaderinput_t inputs[ASSET_BATCH_COUNT_MAX];
|
||||
uint16_t count;
|
||||
} assetbatch_t;
|
||||
|
||||
/**
|
||||
* Initializes a batch, optionally with completion and error callbacks.
|
||||
* Initialises the batch from an array of descriptors. Each entry is locked
|
||||
* and queued for loading immediately.
|
||||
*
|
||||
* @param batch The batch to initialize.
|
||||
* @param onComplete Called when all items have loaded successfully. May be NULL.
|
||||
* @param onError Called if any item fails to load. May be NULL.
|
||||
* @param context Passed through to both callbacks.
|
||||
* @param batch Batch to initialise.
|
||||
* @param descs Array of entry descriptors (need not outlive this call).
|
||||
* @param count Number of descriptors (must be <= ASSET_BATCH_COUNT_MAX).
|
||||
*/
|
||||
void assetBatchInit(
|
||||
assetbatch_t *batch,
|
||||
assetbatchcomplete_t onComplete,
|
||||
assetbatcherror_t onError,
|
||||
void *context
|
||||
uint16_t count,
|
||||
const assetbatchdesc_t *descs
|
||||
);
|
||||
|
||||
/**
|
||||
* Adds an item to the batch. Params are copied into inline storage.
|
||||
* Acquires one additional lock on every entry in the batch.
|
||||
*
|
||||
* @param batch The batch to add to.
|
||||
* @param path Asset path, used as the cache key.
|
||||
* @param loader The loader function for this asset type.
|
||||
* @param params Loader-specific parameters. Copied — safe to pass stack address.
|
||||
* @param paramsSize Size of params in bytes. Must be <= ASSET_BATCH_PARAMS_SIZE.
|
||||
* @param dataSize Size in bytes to allocate for the output struct.
|
||||
* @param disposer Called on the data before freeing when released. May be NULL.
|
||||
* @param outPtr Caller's pointer variable. Set to the cache-owned data on load.
|
||||
* @param batch Batch to lock.
|
||||
*/
|
||||
void assetBatchAdd(
|
||||
assetbatch_t *batch,
|
||||
const char_t *path,
|
||||
assetfileloader_t loader,
|
||||
const void *params,
|
||||
size_t paramsSize,
|
||||
size_t dataSize,
|
||||
assetcachedisposer_t disposer,
|
||||
void **outPtr
|
||||
);
|
||||
void assetBatchLock(assetbatch_t *batch);
|
||||
|
||||
/**
|
||||
* Loads all items in the batch, checking the cache for each. Fires onComplete
|
||||
* on success or onError on the first failure. Also returns the error for
|
||||
* synchronous callers.
|
||||
* Releases one lock from every entry in the batch. When an entry's lock
|
||||
* count reaches zero it will be reaped on the next assetUpdate.
|
||||
*
|
||||
* @param batch The batch to execute.
|
||||
* @return Error if any item failed to load.
|
||||
* @param batch Batch to unlock.
|
||||
*/
|
||||
errorret_t assetBatchLoad(assetbatch_t *batch);
|
||||
void assetBatchUnlock(assetbatch_t *batch);
|
||||
|
||||
/**
|
||||
* Returns true if every entry in the batch has finished loading.
|
||||
*
|
||||
* @param batch Batch to query.
|
||||
*/
|
||||
bool_t assetBatchIsLoaded(const assetbatch_t *batch);
|
||||
|
||||
/**
|
||||
* Returns true if any entry in the batch is in an error state.
|
||||
*
|
||||
* @param batch Batch to query.
|
||||
*/
|
||||
bool_t assetBatchHasError(const assetbatch_t *batch);
|
||||
|
||||
/**
|
||||
* Blocks until every entry is loaded. Returns an error if any entry fails.
|
||||
*
|
||||
* @param batch Batch to wait on.
|
||||
*/
|
||||
errorret_t assetBatchRequireLoaded(assetbatch_t *batch);
|
||||
|
||||
/**
|
||||
* Releases the batch's lock on every entry and clears the batch. After this
|
||||
* call the batch struct may be reused with assetBatchInit.
|
||||
*
|
||||
* @param batch Batch to dispose.
|
||||
*/
|
||||
void assetBatchDispose(assetbatch_t *batch);
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetcache.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
void assetCacheInit(assetcache_t *cache) {
|
||||
memoryZero(cache, sizeof(assetcache_t));
|
||||
}
|
||||
|
||||
void *assetCacheLookup(assetcache_t *cache, const char_t *path) {
|
||||
for(uint8_t i = 0; i < cache->count; i++) {
|
||||
if(stringCompare(cache->entries[i].path, path) == 0) {
|
||||
return cache->entries[i].data;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void assetCacheInsert(
|
||||
assetcache_t *cache,
|
||||
const char_t *path,
|
||||
void *data,
|
||||
assetcachedisposer_t disposer
|
||||
) {
|
||||
assertTrue(cache->count < ASSET_CACHE_MAX, "Asset cache is full.");
|
||||
|
||||
assetcacheentry_t *entry = &cache->entries[cache->count++];
|
||||
stringCopy(entry->path, path, ASSET_FILE_NAME_MAX);
|
||||
entry->data = data;
|
||||
entry->refcount = 1;
|
||||
entry->disposer = disposer;
|
||||
}
|
||||
|
||||
void assetCacheRetain(assetcache_t *cache, const char_t *path) {
|
||||
for (uint8_t i = 0; i < cache->count; i++) {
|
||||
if (stringCompare(cache->entries[i].path, path) == 0) {
|
||||
cache->entries[i].refcount++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
assertTrue(false, "Asset not found in cache for retain.");
|
||||
}
|
||||
|
||||
static void assetCacheEntryDispose(assetcacheentry_t *entry) {
|
||||
if (entry->disposer != NULL) {
|
||||
entry->disposer(entry->data);
|
||||
}
|
||||
memoryFree(entry->data);
|
||||
}
|
||||
|
||||
void assetCacheRelease(assetcache_t *cache, const char_t *path) {
|
||||
for (uint8_t i = 0; i < cache->count; i++) {
|
||||
if (stringCompare(cache->entries[i].path, path) == 0) {
|
||||
assetcacheentry_t *entry = &cache->entries[i];
|
||||
entry->refcount--;
|
||||
if (entry->refcount == 0) {
|
||||
assetCacheEntryDispose(entry);
|
||||
for (uint8_t j = i; j < cache->count - 1; j++) {
|
||||
cache->entries[j] = cache->entries[j + 1];
|
||||
}
|
||||
cache->count--;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
assertTrue(false, "Asset not found in cache for release.");
|
||||
}
|
||||
|
||||
void assetCacheDispose(assetcache_t *cache) {
|
||||
for (uint8_t i = 0; i < cache->count; i++) {
|
||||
assetCacheEntryDispose(&cache->entries[i]);
|
||||
}
|
||||
memoryZero(cache, sizeof(assetcache_t));
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "assetfile.h"
|
||||
|
||||
#ifndef ASSET_CACHE_MAX
|
||||
#define ASSET_CACHE_MAX 64
|
||||
#endif
|
||||
|
||||
typedef void (*assetcachedisposer_t)(void *data);
|
||||
|
||||
typedef struct {
|
||||
char_t path[ASSET_FILE_NAME_MAX];
|
||||
void *data;
|
||||
uint32_t refcount;
|
||||
assetcachedisposer_t disposer;
|
||||
} assetcacheentry_t;
|
||||
|
||||
typedef struct {
|
||||
assetcacheentry_t entries[ASSET_CACHE_MAX];
|
||||
uint8_t count;
|
||||
} assetcache_t;
|
||||
|
||||
/**
|
||||
* Initializes the asset cache.
|
||||
*
|
||||
* @param cache The cache to initialize.
|
||||
*/
|
||||
void assetCacheInit(assetcache_t *cache);
|
||||
|
||||
/**
|
||||
* Looks up a cached asset by path.
|
||||
*
|
||||
* @param cache The cache to search.
|
||||
* @param path The asset path to look up.
|
||||
* @return Pointer to the cached data, or NULL if not found.
|
||||
*/
|
||||
void *assetCacheLookup(assetcache_t *cache, const char_t *path);
|
||||
|
||||
/**
|
||||
* Inserts a newly loaded asset into the cache with refcount 1.
|
||||
*
|
||||
* @param cache The cache to insert into.
|
||||
* @param path The asset path key.
|
||||
* @param data Heap-allocated asset data. The cache takes ownership.
|
||||
* @param disposer Called with data before freeing when refcount reaches 0.
|
||||
*/
|
||||
void assetCacheInsert(
|
||||
assetcache_t *cache,
|
||||
const char_t *path,
|
||||
void *data,
|
||||
assetcachedisposer_t disposer
|
||||
);
|
||||
|
||||
/**
|
||||
* Increments the refcount for a cached asset.
|
||||
*
|
||||
* @param cache The cache containing the asset.
|
||||
* @param path The asset path.
|
||||
*/
|
||||
void assetCacheRetain(assetcache_t *cache, const char_t *path);
|
||||
|
||||
/**
|
||||
* Decrements the refcount. Disposes and frees the asset when it reaches 0.
|
||||
*
|
||||
* @param cache The cache containing the asset.
|
||||
* @param path The asset path.
|
||||
*/
|
||||
void assetCacheRelease(assetcache_t *cache, const char_t *path);
|
||||
|
||||
/**
|
||||
* Disposes all remaining cache entries and resets the cache.
|
||||
*
|
||||
* @param cache The cache to dispose.
|
||||
*/
|
||||
void assetCacheDispose(assetcache_t *cache);
|
||||
@@ -15,6 +15,7 @@ typedef struct assetfile_s assetfile_t;
|
||||
|
||||
typedef errorret_t (*assetfileloader_t)(assetfile_t *file);
|
||||
|
||||
// Describes a file not yet loaded.
|
||||
typedef struct assetfile_s {
|
||||
char_t filename[ASSET_FILE_NAME_MAX];
|
||||
void *params;
|
||||
|
||||
@@ -4,8 +4,15 @@
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
assetentry.c
|
||||
assetloader.c
|
||||
)
|
||||
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(locale)
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(json)
|
||||
add_subdirectory(script)
|
||||
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetentry.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
|
||||
void assetEntryInit(
|
||||
assetentry_t *entry,
|
||||
const char_t *name,
|
||||
const assetloadertype_t type,
|
||||
assetloaderinput_t *input
|
||||
) {
|
||||
assertNotNull(entry, "Entry cannot be NULL");
|
||||
assertStrLenMin(name, 1, "Name cannot be empty");
|
||||
assertStrLenMax(name, ASSET_FILE_NAME_MAX - 1, "Name too long");
|
||||
assertTrue(type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||
assertTrue(type < ASSET_LOADER_TYPE_COUNT, "Invalid loader type.");
|
||||
|
||||
memoryZero(entry, sizeof(assetentry_t));
|
||||
stringCopy(entry->name, name, ASSET_FILE_NAME_MAX);
|
||||
entry->type = type;
|
||||
entry->state = ASSET_ENTRY_STATE_NOT_STARTED;
|
||||
if(input) {
|
||||
entry->inputData = *input;
|
||||
entry->input = &entry->inputData;
|
||||
}
|
||||
refInit(&entry->refs, entry, NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
void assetEntryLock(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Entry cannot be NULL");
|
||||
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||
entry->wasLocked = true;
|
||||
refLock(&entry->refs);
|
||||
}
|
||||
|
||||
void assetEntryUnlock(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Entry cannot be NULL");
|
||||
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||
refUnlock(&entry->refs);
|
||||
}
|
||||
|
||||
void assetEntryStartLoading(
|
||||
assetentry_t *entry,
|
||||
assetloading_t *loading
|
||||
) {
|
||||
assertNotNull(entry, "Entry cannot be NULL");
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||
assertTrue(
|
||||
entry->state == ASSET_ENTRY_STATE_NOT_STARTED,
|
||||
"Can only start loading from NOT_STARTED state."
|
||||
);
|
||||
|
||||
entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
memoryZero(&loading->loading, sizeof(assetloaderloading_t));
|
||||
loading->type = entry->type;
|
||||
loading->entry = entry;
|
||||
// At this point the asset manager will manage this thing's loading
|
||||
}
|
||||
|
||||
errorret_t assetEntryDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Entry cannot be NULL");
|
||||
|
||||
assertTrue(entry->type != ASSET_LOADER_TYPE_NULL, "Invalid loader type.");
|
||||
assertTrue(entry->type < ASSET_LOADER_TYPE_COUNT, "Invalid loader type.");
|
||||
|
||||
errorChain(ASSET_LOADER_CALLBACKS[entry->type].dispose(entry));
|
||||
memoryZero(entry, sizeof(assetentry_t));
|
||||
errorOk();
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "util/ref.h"
|
||||
|
||||
typedef enum {
|
||||
ASSET_ENTRY_STATE_NOT_STARTED,
|
||||
ASSET_ENTRY_STATE_PENDING_ASYNC,
|
||||
ASSET_ENTRY_STATE_LOADING_ASYNC,
|
||||
ASSET_ENTRY_STATE_PENDING_SYNC,
|
||||
ASSET_ENTRY_STATE_LOADING_SYNC,
|
||||
ASSET_ENTRY_STATE_LOADED,
|
||||
ASSET_ENTRY_STATE_ERROR
|
||||
} assetentrystate_t;
|
||||
|
||||
typedef struct assetentry_s {
|
||||
// Filename and cache key
|
||||
char_t name[ASSET_FILE_NAME_MAX];
|
||||
// What type of asset is this?
|
||||
assetloadertype_t type;
|
||||
// Data
|
||||
assetloaderoutput_t data;
|
||||
// What state is this asset entry in currently?
|
||||
assetentrystate_t state;
|
||||
// What is referencing this asset entry.
|
||||
ref_t refs;
|
||||
// True once assetEntryLock has been called at least once. The reaper only
|
||||
// collects entries that have been explicitly locked (and later unlocked to
|
||||
// zero). Entries that nobody has ever locked are left alone so raw-pointer
|
||||
// callers (tests, requireLoaded before locking) are not surprised.
|
||||
bool_t wasLocked;
|
||||
// Owned copy of the loader input. input points here when non-NULL.
|
||||
assetloaderinput_t inputData;
|
||||
// Pointer to inputData, or NULL if no input was provided.
|
||||
assetloaderinput_t *input;
|
||||
} assetentry_t;
|
||||
|
||||
/**
|
||||
* Initializes an asset entry with the given name and type. This does not load
|
||||
* the asset.
|
||||
*
|
||||
* @param entry The asset entry to initialize.
|
||||
* @param name The name of the asset, used as a key for loading and caching.
|
||||
* @param type The type of asset this entry represents.
|
||||
* @param input Data that will be passed to the loader about how it should load.
|
||||
*/
|
||||
void assetEntryInit(
|
||||
assetentry_t *entry,
|
||||
const char_t *name,
|
||||
const assetloadertype_t type,
|
||||
assetloaderinput_t *input
|
||||
);
|
||||
|
||||
/**
|
||||
* Locks an asset entry, preventing it from being freed until it is unlocked.
|
||||
*
|
||||
* @param entry The asset entry to lock.
|
||||
*/
|
||||
void assetEntryLock(assetentry_t *entry);
|
||||
|
||||
/**
|
||||
* Unlocks an asset entry, allowing it to be freed if there are no more locks.
|
||||
*
|
||||
* @param entry The asset entry to unlock.
|
||||
*/
|
||||
void assetEntryUnlock(assetentry_t *entry);
|
||||
|
||||
/**
|
||||
* Starts loading the given asset entry using an assetloading slot. This will
|
||||
* be called by the asset manager when it deems it's a good time to begin the
|
||||
* loading of this asset entry.
|
||||
*
|
||||
* Currently we return the error but in future this will not be returned.
|
||||
*
|
||||
* @param entry The asset entry to start loading.
|
||||
* @param loading The assetloading slot to use for loading this asset entry.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
void assetEntryStartLoading(assetentry_t *entry, assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Disposes an asset entry, freeing any resources it holds.
|
||||
*
|
||||
* @param entry The asset entry to dispose.
|
||||
* @return Any error that occurs during disposal.
|
||||
*/
|
||||
errorret_t assetEntryDispose(assetentry_t *entry);
|
||||
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetloader.h"
|
||||
|
||||
assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT] = {
|
||||
[ASSET_LOADER_TYPE_NULL] = { 0 },
|
||||
|
||||
[ASSET_LOADER_TYPE_MESH] = {
|
||||
.loadSync = assetMeshLoaderSync,
|
||||
.loadAsync = assetMeshLoaderAsync,
|
||||
.dispose = assetMeshDispose
|
||||
},
|
||||
|
||||
[ASSET_LOADER_TYPE_TEXTURE] = {
|
||||
.loadSync = assetTextureLoaderSync,
|
||||
.loadAsync = assetTextureLoaderAsync,
|
||||
.dispose = assetTextureDispose
|
||||
},
|
||||
|
||||
[ASSET_LOADER_TYPE_TILESET] = {
|
||||
.loadSync = assetTilesetLoaderSync,
|
||||
.loadAsync = assetTilesetLoaderAsync,
|
||||
.dispose = assetTilesetDispose
|
||||
},
|
||||
|
||||
[ASSET_LOADER_TYPE_LOCALE] = {
|
||||
.loadSync = assetLocaleLoaderSync,
|
||||
.loadAsync = assetLocaleLoaderAsync,
|
||||
.dispose = assetLocaleDispose
|
||||
},
|
||||
|
||||
[ASSET_LOADER_TYPE_JSON] = {
|
||||
.loadSync = assetJsonLoaderSync,
|
||||
.loadAsync = assetJsonLoaderAsync,
|
||||
.dispose = assetJsonDispose
|
||||
},
|
||||
|
||||
[ASSET_LOADER_TYPE_SCRIPT] = {
|
||||
.loadSync = assetScriptLoaderSync,
|
||||
.loadAsync = assetScriptLoaderAsync,
|
||||
.dispose = assetScriptDispose
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/loader/display/assetmeshloader.h"
|
||||
#include "asset/loader/display/assettextureloader.h"
|
||||
#include "asset/loader/display/assettilesetloader.h"
|
||||
#include "asset/loader/locale/assetlocaleloader.h"
|
||||
#include "asset/loader/json/assetjsonloader.h"
|
||||
#include "asset/loader/script/assetscriptloader.h"
|
||||
|
||||
typedef enum {
|
||||
ASSET_LOADER_TYPE_NULL,
|
||||
|
||||
ASSET_LOADER_TYPE_MESH,
|
||||
ASSET_LOADER_TYPE_TEXTURE,
|
||||
ASSET_LOADER_TYPE_TILESET,
|
||||
ASSET_LOADER_TYPE_LOCALE,
|
||||
ASSET_LOADER_TYPE_JSON,
|
||||
ASSET_LOADER_TYPE_SCRIPT,
|
||||
|
||||
ASSET_LOADER_TYPE_COUNT
|
||||
} assetloadertype_t;
|
||||
|
||||
typedef union {
|
||||
assetmeshloaderinput_t mesh;
|
||||
assettextureloaderinput_t texture;
|
||||
assettilesetloaderinput_t tileset;
|
||||
assetlocaleloaderinput_t locale;
|
||||
assetjsonloaderinput_t json;
|
||||
assetscriptloaderinput_t script;
|
||||
} assetloaderinput_t;
|
||||
|
||||
typedef union {
|
||||
assetmeshloaderloading_t mesh;
|
||||
assettextureloaderloading_t texture;
|
||||
assettilesetloaderloading_t tileset;
|
||||
assetlocaleloaderloading_t locale;
|
||||
assetjsonloaderloading_t json;
|
||||
assetscriptloaderloading_t script;
|
||||
} assetloaderloading_t;
|
||||
|
||||
typedef union {
|
||||
assetmeshoutput_t mesh;
|
||||
assettextureoutput_t texture;
|
||||
assettilesetoutput_t tileset;
|
||||
assetlocaleoutput_t locale;
|
||||
assetjsonoutput_t json;
|
||||
assetscriptoutput_t script;
|
||||
} assetloaderoutput_t;
|
||||
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
typedef errorret_t (assetloadersynccallback_t)(assetloading_t *loading);
|
||||
typedef errorret_t (assetloaderasynccallback_t)(assetloading_t *loading);
|
||||
typedef errorret_t (assetloaderdisposecallback_t)(assetentry_t *entry);
|
||||
|
||||
typedef struct {
|
||||
assetloadersynccallback_t *loadSync;
|
||||
assetloaderasynccallback_t *loadAsync;
|
||||
assetloaderdisposecallback_t *dispose;
|
||||
} assetloadercallbacks_t;
|
||||
|
||||
extern assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT];
|
||||
|
||||
/**
|
||||
* Shorthand method to both chain an error (against the loader state) and to
|
||||
* set the asset entry state to error.
|
||||
*
|
||||
* @param loading The asset loading slot.
|
||||
* @param ret The error return value to check and chain if it's an error.
|
||||
*/
|
||||
#define assetLoaderErrorChain(loading, _expr) {\
|
||||
errorret_t _alec = (_expr); \
|
||||
if(errorIsNotOk(_alec)) { \
|
||||
(loading)->entry->state = ASSET_ENTRY_STATE_ERROR; \
|
||||
errorChain(_alec); \
|
||||
} \
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorthand method to both throw an error (against the loader state) and to
|
||||
* set the asset entry state to error.
|
||||
*
|
||||
* @param loading The asset loading slot.
|
||||
* @param ... Format string and arguments for the error message.
|
||||
*/
|
||||
#define assetLoaderErrorThrow(loading, ...) {\
|
||||
loading->entry->state = ASSET_ENTRY_STATE_ERROR; \
|
||||
errorThrow(__VA_ARGS__); \
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "assetloader.h"
|
||||
#include "asset/assetfile.h"
|
||||
#include "thread/threadmutex.h"
|
||||
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
typedef struct assetloading_s {
|
||||
// Protects entry pointer and entry->state from concurrent access.
|
||||
threadmutex_t mutex;
|
||||
// What type of asset is being loaded.
|
||||
assetloadertype_t type;
|
||||
// Referral back to the asset entry that will be kept alive after load done.
|
||||
assetentry_t *entry;
|
||||
// Information used during the load operation only.
|
||||
assetloaderloading_t loading;
|
||||
} assetloading_t;
|
||||
|
||||
typedef errorret_t (assetloadingcallback_t)(assetloading_t *loading);
|
||||
|
||||
typedef struct {
|
||||
assetloadingcallback_t *loadSync;
|
||||
} assetloadingcallbacks_t;
|
||||
@@ -1,182 +1,174 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "asset/loader/display/assetmeshloader.h"
|
||||
#include "asset/asset.h"
|
||||
#include "assetmeshloader.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/endian.h"
|
||||
#include "util/memory.h"
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
|
||||
errorret_t assetMeshLoader(assetfile_t *file) {
|
||||
assertNotNull(file, "Asset file cannot be null");
|
||||
|
||||
assetmeshoutput_t *output = (assetmeshoutput_t *)file->output;
|
||||
assertNotNull(output, "Output cannot be null");
|
||||
assertNotNull(output->outMesh, "Output mesh cannot be null");
|
||||
assertNotNull(output->outVertices, "Output vertices cannot be null");
|
||||
errorret_t assetMeshLoaderAsync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
// STL file loading
|
||||
errorChain(assetFileOpen(file));
|
||||
if(loading->loading.mesh.state != ASSET_MESH_LOADING_STATE_READ_FILE) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
// Skip the 80 byte header
|
||||
errorChain(assetFileRead(file, NULL, 80));
|
||||
if(file->lastRead != 80) errorThrow("Failed to skip STL header");
|
||||
assetmeshoutput_t *out = &loading->entry->data.mesh;
|
||||
assetfile_t *file = &loading->loading.mesh.file;
|
||||
assetmeshinputaxis_t axis = loading->entry->input->mesh;
|
||||
|
||||
assetLoaderErrorChain(loading, assetFileInit(file, loading->entry->name, NULL, NULL));
|
||||
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||
|
||||
// Skip the 80-byte STL header.
|
||||
assetLoaderErrorChain(loading, assetFileRead(file, NULL, 80));
|
||||
if(file->lastRead != 80) {
|
||||
assetLoaderErrorThrow(loading, "Failed to skip STL header.");
|
||||
}
|
||||
|
||||
uint32_t triangleCount;
|
||||
errorChain(assetFileRead(file, &triangleCount, sizeof(uint32_t)));
|
||||
if(file->lastRead != sizeof(uint32_t)) errorThrow("Failed read tri count");
|
||||
// normalize
|
||||
assetLoaderErrorChain(loading, assetFileRead(file, &triangleCount, sizeof(uint32_t)));
|
||||
if(file->lastRead != sizeof(uint32_t)) {
|
||||
assetLoaderErrorThrow(loading, "Failed to read tri count");
|
||||
}
|
||||
triangleCount = endianLittleToHost32(triangleCount);
|
||||
|
||||
// Allocate mesh and vertex data
|
||||
errorret_t ret;
|
||||
meshvertex_t *verts = memoryAllocate(
|
||||
sizeof(meshvertex_t) * triangleCount * 3
|
||||
);
|
||||
*output->outVertices = verts;
|
||||
|
||||
// Read triangle data
|
||||
out->vertices = memoryAllocate(sizeof(meshvertex_t) * triangleCount * 3);
|
||||
meshvertex_t *verts = out->vertices;
|
||||
|
||||
errorret_t ret;
|
||||
for(uint32_t i = 0; i < triangleCount; i++) {
|
||||
assetmeshstltriangle_t triData;
|
||||
ret = assetFileRead(file, &triData, sizeof(triData));
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
memoryFree(verts);
|
||||
errorChain(ret);
|
||||
out->vertices = NULL;
|
||||
assetLoaderErrorChain(loading, ret);
|
||||
}
|
||||
|
||||
if(file->lastRead != sizeof(triData)) {
|
||||
memoryFree(verts);
|
||||
errorThrow("Failed to read triangle data");
|
||||
out->vertices = NULL;
|
||||
assetLoaderErrorThrow(loading, "Failed to read triangle data");
|
||||
}
|
||||
|
||||
// Skip normals, we don't use them
|
||||
|
||||
// Fix endianess of of data
|
||||
for(uint8_t j = 0; j < 3; j++) {
|
||||
#if MESH_ENABLE_COLOR
|
||||
verts[i * 3 + j].color.r = (uint8_t)(endianLittleToHostFloat(
|
||||
triData.normal[0]
|
||||
) * 255.0f);
|
||||
verts[i * 3 + j].color.g = (uint8_t)(endianLittleToHostFloat(
|
||||
triData.normal[1]
|
||||
) * 255.0f);
|
||||
verts[i * 3 + j].color.b = (uint8_t)(endianLittleToHostFloat(
|
||||
triData.normal[2]
|
||||
) * 255.0f);
|
||||
verts[i * 3 + j].color.r = (
|
||||
(uint8_t)(endianLittleToHostFloat(triData.normal[0]) * 255.0f)
|
||||
);
|
||||
verts[i * 3 + j].color.g = (
|
||||
(uint8_t)(endianLittleToHostFloat(triData.normal[1]) * 255.0f)
|
||||
);
|
||||
verts[i * 3 + j].color.b = (
|
||||
(uint8_t)(endianLittleToHostFloat(triData.normal[2]) * 255.0f)
|
||||
);
|
||||
verts[i * 3 + j].color.a = 0xFF;
|
||||
#endif
|
||||
|
||||
verts[i * 3 + j].uv[0] = 0.0f; // No UV data in STL, just set to 0
|
||||
|
||||
verts[i * 3 + j].uv[0] = 0.0f;
|
||||
verts[i * 3 + j].uv[1] = 0.0f;
|
||||
|
||||
for(uint8_t k = 0; k < 3; k++) {
|
||||
verts[i * 3 + j].pos[k] = endianLittleToHostFloat(
|
||||
triData.positions[j][k]
|
||||
);
|
||||
verts[i * 3 + j].pos[k] = endianLittleToHostFloat(triData.positions[j][k]);
|
||||
}
|
||||
|
||||
switch(output->inputAxis) {
|
||||
case MESH_INPUT_AXIS_Z_UP:
|
||||
// Convert Z-Up to Y-Up
|
||||
{
|
||||
float_t temp = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2];
|
||||
verts[i * 3 + j].pos[2] = temp;
|
||||
}
|
||||
switch(axis) {
|
||||
case MESH_INPUT_AXIS_Z_UP: {
|
||||
float_t temp = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = verts[i * 3 + j].pos[2];
|
||||
verts[i * 3 + j].pos[2] = temp;
|
||||
break;
|
||||
|
||||
case MESH_INPUT_AXIS_X_UP:
|
||||
// Convert X-Up to Y-Up
|
||||
{
|
||||
float_t temp = verts[i * 3 + j].pos[0];
|
||||
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = temp;
|
||||
}
|
||||
}
|
||||
case MESH_INPUT_AXIS_X_UP: {
|
||||
float_t temp = verts[i * 3 + j].pos[0];
|
||||
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = temp;
|
||||
break;
|
||||
|
||||
}
|
||||
case MESH_INPUT_AXIS_Y_DOWN:
|
||||
// Invert Y axis
|
||||
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[1];
|
||||
break;
|
||||
|
||||
case MESH_INPUT_AXIS_Z_DOWN:
|
||||
// Convert Z-Up to Y-Up and invert Y axis
|
||||
{
|
||||
float_t temp = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[2];
|
||||
verts[i * 3 + j].pos[2] = temp;
|
||||
}
|
||||
case MESH_INPUT_AXIS_Z_DOWN: {
|
||||
float_t temp = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = -verts[i * 3 + j].pos[2];
|
||||
verts[i * 3 + j].pos[2] = temp;
|
||||
break;
|
||||
|
||||
case MESH_INPUT_AXIS_X_DOWN:
|
||||
// Convert X-Up to Y-Up and invert Y axis
|
||||
{
|
||||
float_t temp = verts[i * 3 + j].pos[0];
|
||||
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = -temp;
|
||||
}
|
||||
}
|
||||
case MESH_INPUT_AXIS_X_DOWN: {
|
||||
float_t temp = verts[i * 3 + j].pos[0];
|
||||
verts[i * 3 + j].pos[0] = verts[i * 3 + j].pos[1];
|
||||
verts[i * 3 + j].pos[1] = -temp;
|
||||
break;
|
||||
|
||||
}
|
||||
case MESH_INPUT_AXIS_Y_UP:
|
||||
default:
|
||||
// No covnersion possible / Needed
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, init mesh
|
||||
ret = meshInit(
|
||||
output->outMesh,
|
||||
MESH_PRIMITIVE_TYPE_TRIANGLES,
|
||||
triangleCount * 3,
|
||||
verts
|
||||
);
|
||||
if(ret.code != ERROR_OK) {
|
||||
memoryFree(verts);
|
||||
errorChain(ret);
|
||||
}
|
||||
|
||||
ret = assetFileClose(file);
|
||||
if(ret.code != ERROR_OK) {
|
||||
errorCatch(errorPrint(meshDispose(output->outMesh)));
|
||||
if(errorIsNotOk(ret)) {
|
||||
memoryFree(verts);
|
||||
errorChain(ret);
|
||||
out->vertices = NULL;
|
||||
assetLoaderErrorChain(loading, ret);
|
||||
}
|
||||
assetFileDispose(file);
|
||||
|
||||
loading->loading.mesh.triangleCount = triangleCount;
|
||||
loading->loading.mesh.state = ASSET_MESH_LOADING_STATE_CREATE_MESH;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetMeshLoadToOutput(
|
||||
const char_t *path,
|
||||
assetmeshoutput_t *output
|
||||
) {
|
||||
assertNotNull(path, "Path cannot be null");
|
||||
assertNotNull(output, "Output cannot be null");
|
||||
assertNotNull(output->outMesh, "Output mesh cannot be null");
|
||||
assertNotNull(output->outVertices, "Output vertices cannot be null");
|
||||
|
||||
return assetLoad(path, &assetMeshLoader, NULL, output);
|
||||
errorret_t assetMeshLoaderSync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
assertTrue(loading->type == ASSET_LOADER_TYPE_MESH, "Invalid type.");
|
||||
|
||||
switch(loading->loading.mesh.state) {
|
||||
case ASSET_MESH_LOADING_STATE_INITIAL:
|
||||
loading->loading.mesh.state = ASSET_MESH_LOADING_STATE_READ_FILE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||
errorOk();
|
||||
break;
|
||||
|
||||
case ASSET_MESH_LOADING_STATE_CREATE_MESH:
|
||||
break;
|
||||
|
||||
default:
|
||||
errorOk();
|
||||
}
|
||||
|
||||
assetmeshoutput_t *out = &loading->entry->data.mesh;
|
||||
assertNotNull(out->vertices, "Mesh vertices should have been loaded by now.");
|
||||
|
||||
errorret_t ret = meshInit(
|
||||
&out->mesh,
|
||||
MESH_PRIMITIVE_TYPE_TRIANGLES,
|
||||
loading->loading.mesh.triangleCount * 3,
|
||||
out->vertices
|
||||
);
|
||||
if(errorIsNotOk(ret)) {
|
||||
loading->entry->state = ASSET_ENTRY_STATE_ERROR;
|
||||
memoryFree(out->vertices);
|
||||
out->vertices = NULL;
|
||||
assetLoaderErrorChain(loading, ret);
|
||||
}
|
||||
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetMeshLoad(
|
||||
const char_t *path,
|
||||
mesh_t *outMesh,
|
||||
meshvertex_t **outVertices,
|
||||
const assetmeshinputaxis_t inputAxis
|
||||
) {
|
||||
assertNotNull(path, "Path cannot be null");
|
||||
assertNotNull(outMesh, "Output mesh cannot be null");
|
||||
assertNotNull(outVertices, "Output vertices cannot be null");
|
||||
|
||||
assetmeshoutput_t output = {
|
||||
outMesh,
|
||||
outVertices,
|
||||
inputAxis
|
||||
};
|
||||
return assetMeshLoadToOutput(path, &output);
|
||||
}
|
||||
errorret_t assetMeshDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||
assertTrue(entry->type == ASSET_LOADER_TYPE_MESH, "Invalid type.");
|
||||
errorChain(meshDispose(&entry->data.mesh.mesh));
|
||||
memoryFree(entry->data.mesh.vertices);
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "asset/assetfile.h"
|
||||
#include "display/mesh/mesh.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
typedef enum {
|
||||
MESH_INPUT_AXIS_Y_UP,// Default
|
||||
MESH_INPUT_AXIS_Y_UP,
|
||||
MESH_INPUT_AXIS_Z_UP,
|
||||
MESH_INPUT_AXIS_X_UP,
|
||||
|
||||
@@ -20,10 +23,24 @@ typedef enum {
|
||||
MESH_INPUT_AXIS_X_DOWN,
|
||||
} assetmeshinputaxis_t;
|
||||
|
||||
typedef assetmeshinputaxis_t assetmeshloaderinput_t;
|
||||
|
||||
typedef enum {
|
||||
ASSET_MESH_LOADING_STATE_INITIAL,
|
||||
ASSET_MESH_LOADING_STATE_READ_FILE,
|
||||
ASSET_MESH_LOADING_STATE_CREATE_MESH,
|
||||
ASSET_MESH_LOADING_STATE_DONE
|
||||
} assetmeshloadingstate_t;
|
||||
|
||||
typedef struct {
|
||||
mesh_t *outMesh;
|
||||
meshvertex_t **outVertices;
|
||||
assetmeshinputaxis_t inputAxis;
|
||||
assetfile_t file;
|
||||
assetmeshloadingstate_t state;
|
||||
uint32_t triangleCount;
|
||||
} assetmeshloaderloading_t;
|
||||
|
||||
typedef struct {
|
||||
mesh_t mesh;
|
||||
meshvertex_t *vertices;
|
||||
} assetmeshoutput_t;
|
||||
|
||||
#pragma pack(push, 1)
|
||||
@@ -36,26 +53,6 @@ typedef struct {
|
||||
|
||||
assertStructSize(assetmeshstltriangle_t, 50);
|
||||
|
||||
/**
|
||||
* Loader for mesh assets.
|
||||
*
|
||||
* @param file Asset file to load the mesh from.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetMeshLoader(assetfile_t *file);
|
||||
|
||||
/**
|
||||
* Handler for mesh assets.
|
||||
*
|
||||
* @param file Asset file to load the mesh from.
|
||||
* @param outMesh Output mesh to load the data into.
|
||||
* @param outVertices Output pointer to the vertex data, used for cleanup.
|
||||
* @param inputAxis The axis orientation of the input mesh data.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetMeshLoad(
|
||||
const char_t *path,
|
||||
mesh_t *outMesh,
|
||||
meshvertex_t **outVertices,
|
||||
const assetmeshinputaxis_t inputAxis
|
||||
);
|
||||
errorret_t assetMeshLoaderAsync(assetloading_t *loading);
|
||||
errorret_t assetMeshLoaderSync(assetloading_t *loading);
|
||||
errorret_t assetMeshDispose(assetentry_t *entry);
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
*/
|
||||
|
||||
#include "assettextureloader.h"
|
||||
#include "asset/assetbatch.h"
|
||||
#include "assert/assert.h"
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
||||
#include "log/log.h"
|
||||
#include "util/endian.h"
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
|
||||
stbi_io_callbacks ASSET_TEXTURE_STB_CALLBACKS = {
|
||||
.read = assetTextureReader,
|
||||
@@ -26,7 +27,7 @@ int assetTextureReader(void *user, char *data, int size) {
|
||||
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||
|
||||
errorret_t ret = assetFileRead(file, data, (size_t)size);
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
return -1;
|
||||
}
|
||||
@@ -39,7 +40,7 @@ void assetTextureSkipper(void *user, int n) {
|
||||
assertNotNull(file, "Asset file in stb_image callbacks cannot be NULL.");
|
||||
|
||||
errorret_t ret = assetFileRead(file, NULL, (size_t)n);
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
}
|
||||
}
|
||||
@@ -51,88 +52,114 @@ int assetTextureEOF(void *user) {
|
||||
return file->position >= file->size;
|
||||
}
|
||||
|
||||
errorret_t assetTextureLoader(assetfile_t *file) {
|
||||
assertNotNull(file, "Asset file cannot be NULL.");
|
||||
assertNotNull(file->params, "Asset file parameters cannot be NULL.");
|
||||
assertNotNull(file->output, "Asset file output cannot be NULL.");
|
||||
errorret_t assetTextureLoaderAsync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
assettextureloaderparams_t *p = (assettextureloaderparams_t*)file->params;
|
||||
assertNotNull(p, "Asset texture loader parameters cannot be NULL.");
|
||||
// Only care about loading pixels.
|
||||
if(loading->loading.texture.state != ASSET_TEXTURE_LOADING_STATE_LOAD_PIXELS){
|
||||
errorOk();
|
||||
}
|
||||
|
||||
// Init the file
|
||||
assertNull(
|
||||
loading->loading.texture.data, "Pixels already defined?"
|
||||
);
|
||||
|
||||
assetfile_t *file = &loading->loading.texture.file;
|
||||
assetLoaderErrorChain(loading, assetFileInit(
|
||||
file,
|
||||
loading->entry->name,
|
||||
NULL,
|
||||
&loading->entry->data.texture
|
||||
));
|
||||
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||
|
||||
// Determine channels
|
||||
int channelsDesired;
|
||||
switch(p->format) {
|
||||
switch(loading->entry->input->texture) {
|
||||
case TEXTURE_FORMAT_RGBA:
|
||||
channelsDesired = 4;
|
||||
break;
|
||||
|
||||
default:
|
||||
errorThrow("Bad texture format.");
|
||||
assetLoaderErrorThrow(loading, "Bad texture format.");
|
||||
}
|
||||
|
||||
int width, height, channels;
|
||||
errorChain(assetFileOpen(file));
|
||||
uint8_t *data = stbi_load_from_callbacks(
|
||||
// Load image pixels.
|
||||
loading->loading.texture.data = stbi_load_from_callbacks(
|
||||
&ASSET_TEXTURE_STB_CALLBACKS,
|
||||
file,
|
||||
&width,
|
||||
&height,
|
||||
&channels,
|
||||
&loading->loading.texture.width,
|
||||
&loading->loading.texture.height,
|
||||
&loading->loading.texture.channels,
|
||||
channelsDesired
|
||||
);
|
||||
errorChain(assetFileClose(file));
|
||||
|
||||
if(data == NULL) {
|
||||
// Close out the file.
|
||||
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||
|
||||
// Ensure we loaded correctly.
|
||||
if(loading->loading.texture.data == NULL) {
|
||||
const char_t *errorStr = stbi_failure_reason();
|
||||
errorThrow("Failed to load texture from file %s.", errorStr);
|
||||
assetLoaderErrorThrow(loading, "Failed to load texture from file %s.", errorStr);
|
||||
}
|
||||
|
||||
// Fixes a specific bug probably with Dolphin but for now just assuming endian
|
||||
if(!isHostLittleEndian()) {
|
||||
stbi__vertical_flip(data, width, height, channelsDesired);
|
||||
stbi__vertical_flip(
|
||||
loading->loading.texture.data,
|
||||
loading->loading.texture.width,
|
||||
loading->loading.texture.height,
|
||||
loading->loading.texture.channels
|
||||
);
|
||||
}
|
||||
|
||||
errorChain(textureInit(
|
||||
(texture_t*)file->output,
|
||||
(int32_t)width, (int32_t)height,
|
||||
p->format,
|
||||
(texturedata_t){
|
||||
.rgbaColors = (color_t*)data
|
||||
}
|
||||
));
|
||||
|
||||
stbi_image_free(data);
|
||||
loading->loading.texture.state = ASSET_TEXTURE_LOADING_STATE_CREATE_TEXTURE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetTextureLoad(
|
||||
const char_t *path,
|
||||
texture_t *out,
|
||||
const textureformat_t format
|
||||
) {
|
||||
assettextureloaderparams_t params = {
|
||||
.format = format
|
||||
};
|
||||
return assetLoad(path, assetTextureLoader, ¶ms, out);
|
||||
}
|
||||
errorret_t assetTextureLoaderSync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
static void assetTextureCacheDispose(void *data) {
|
||||
errorCatch(textureDispose((texture_t*)data));
|
||||
}
|
||||
switch(loading->loading.texture.state) {
|
||||
case ASSET_TEXTURE_LOADING_STATE_INITIAL:
|
||||
loading->loading.texture.state = ASSET_TEXTURE_LOADING_STATE_LOAD_PIXELS;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||
errorOk();
|
||||
break;
|
||||
|
||||
case ASSET_TEXTURE_LOADING_STATE_CREATE_TEXTURE:
|
||||
break;
|
||||
|
||||
void assetBatchTexture(
|
||||
assetbatch_t *batch,
|
||||
const char_t *path,
|
||||
textureformat_t format,
|
||||
texture_t **out
|
||||
) {
|
||||
assettextureloaderparams_t params = { .format = format };
|
||||
assetBatchAdd(
|
||||
batch,
|
||||
path,
|
||||
assetTextureLoader,
|
||||
¶ms, sizeof(params),
|
||||
sizeof(texture_t),
|
||||
assetTextureCacheDispose,
|
||||
(void**)out
|
||||
default:
|
||||
errorOk();
|
||||
}
|
||||
|
||||
// Create the texture.
|
||||
assertNotNull(
|
||||
loading->loading.texture.data, "Pixels should have been loaded by now."
|
||||
);
|
||||
|
||||
assetLoaderErrorChain(loading, textureInit(
|
||||
(texture_t*)&loading->entry->data.texture,
|
||||
loading->loading.texture.width,
|
||||
loading->loading.texture.height,
|
||||
loading->entry->input->texture,
|
||||
(texturedata_t){
|
||||
.rgbaColors = (color_t*)loading->loading.texture.data
|
||||
}
|
||||
));
|
||||
|
||||
// Free the pixels.
|
||||
stbi_image_free(loading->loading.texture.data);
|
||||
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetTextureDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||
return textureDispose(&entry->data.texture);
|
||||
}
|
||||
@@ -6,13 +6,29 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "asset/assetbatch.h"
|
||||
#include "asset/assetfile.h"
|
||||
#include "display/texture/texture.h"
|
||||
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
typedef textureformat_t assettextureloaderinput_t;
|
||||
|
||||
typedef enum {
|
||||
ASSET_TEXTURE_LOADING_STATE_INITIAL,
|
||||
ASSET_TEXTURE_LOADING_STATE_LOAD_PIXELS,
|
||||
ASSET_TEXTURE_LOADING_STATE_CREATE_TEXTURE,
|
||||
ASSET_TEXTURE_LOADING_STATE_DONE
|
||||
} assettextureloadingstate_t;
|
||||
|
||||
typedef struct {
|
||||
textureformat_t format;
|
||||
} assettextureloaderparams_t;
|
||||
assetfile_t file;
|
||||
assettextureloadingstate_t state;
|
||||
|
||||
int channels, width, height;
|
||||
uint8_t *data;
|
||||
} assettextureloaderloading_t;
|
||||
|
||||
typedef texture_t assettextureoutput_t;
|
||||
|
||||
/**
|
||||
* STB image read callback for asset files.
|
||||
@@ -24,44 +40,42 @@ typedef struct {
|
||||
*/
|
||||
int assetTextureReader(void *user, char *data, int size);
|
||||
|
||||
/**
|
||||
* STB image skip callback for asset files.
|
||||
*
|
||||
* @param user User data passed to the callback, should be an assetfile_t*.
|
||||
* @param n Number of bytes to skip in the file.
|
||||
*/
|
||||
void assetTextureSkipper(void *user, int n);
|
||||
|
||||
/**
|
||||
* STB image EOF callback for asset files.
|
||||
*
|
||||
* @param user User data passed to the callback, should be an assetfile_t*.
|
||||
* @return Non-zero if end of file has been reached, zero otherwise.
|
||||
*/
|
||||
int assetTextureEOF(void *user);
|
||||
|
||||
/**
|
||||
* Handler for texture assets.
|
||||
* Synchronous loader for texture assets.
|
||||
*
|
||||
* @param file Asset file to load the texture from.
|
||||
* @return Any error that occurs during loading.
|
||||
* @param loading Loading information for the asset being loaded.
|
||||
* @return Error code indicating success or failure of the load operation.
|
||||
*/
|
||||
errorret_t assetTextureLoader(assetfile_t *file);
|
||||
errorret_t assetTextureLoaderAsync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Loads a texture from the specified path.
|
||||
* Synchronous loader for texture assets.
|
||||
*
|
||||
* @param path Path to the texture asset.
|
||||
* @param out Output texture to load into.
|
||||
* @param format Format of the texture to load.
|
||||
* @return Any error that occurs during loading.
|
||||
* @param loading Loading information for the asset being loaded.
|
||||
* @return Error code indicating success or failure of the load operation.
|
||||
*/
|
||||
errorret_t assetTextureLoad(
|
||||
const char_t *path,
|
||||
texture_t *out,
|
||||
const textureformat_t format
|
||||
);
|
||||
errorret_t assetTextureLoaderSync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Adds a texture load request to a batch. The cache-owned texture_t* is
|
||||
* written to *out when the batch completes.
|
||||
*
|
||||
* @param batch The batch to add to.
|
||||
* @param path Path to the texture asset.
|
||||
* @param format Format of the texture to load.
|
||||
* @param out Receives a pointer to the loaded texture on completion.
|
||||
* Disposer for texture assets.
|
||||
*
|
||||
* @param entry Asset entry containing the texture to dispose.
|
||||
* @return Error code indicating success or failure of the dispose operation.
|
||||
*/
|
||||
void assetBatchTexture(
|
||||
assetbatch_t *batch,
|
||||
const char_t *path,
|
||||
textureformat_t format,
|
||||
texture_t **out
|
||||
);
|
||||
errorret_t assetTextureDispose(assetentry_t *entry);
|
||||
@@ -1,97 +1,115 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assettilesetloader.h"
|
||||
#include "asset/assetbatch.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "util/endian.h"
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
|
||||
errorret_t assetTilesetLoader(assetfile_t *file) {
|
||||
assertNotNull(file, "Asset file pointer for tileset loader is null.");
|
||||
errorret_t assetTilesetLoaderAsync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
tileset_t *out = (tileset_t *)file->output;
|
||||
assertNotNull(out, "Output pointer for tileset loader is null.");
|
||||
|
||||
uint8_t *entire = memoryAllocate(file->size);
|
||||
errorChain(assetFileOpen(file));
|
||||
errorChain(assetFileRead(file, entire, file->size));
|
||||
errorChain(assetFileClose(file));
|
||||
assertTrue(file->lastRead == file->size, "Failed to read entire file.");
|
||||
|
||||
|
||||
if(
|
||||
entire[0] != 'D' ||
|
||||
entire[1] != 'T' ||
|
||||
entire[2] != 'F'
|
||||
) {
|
||||
errorThrow("Invalid tileset header");
|
||||
if(loading->loading.tileset.state != ASSET_TILESET_LOADING_STATE_READ_FILE) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
if(entire[3] != 0x00) {
|
||||
errorThrow("Unsupported tileset version");
|
||||
}
|
||||
assertNull(loading->loading.tileset.data, "Data already defined?");
|
||||
|
||||
// Fix endianness
|
||||
assetfile_t *file = &loading->loading.tileset.file;
|
||||
assetLoaderErrorChain(loading, assetFileInit(file, loading->entry->name, NULL, NULL));
|
||||
|
||||
out->tileWidth = endianLittleToHost16(*(uint16_t *)(entire + 4));
|
||||
out->tileHeight = endianLittleToHost16(*(uint16_t *)(entire + 6));
|
||||
out->columns = endianLittleToHost16(*(uint16_t *)(entire + 8));
|
||||
out->rows = endianLittleToHost16(*(uint16_t *)(entire + 10));
|
||||
// out->right = endianLittleToHost16(*(uint16_t *)(entire + 12));
|
||||
// out->bottom = endianLittleToHost16(*(uint16_t *)(entire + 14));
|
||||
|
||||
if(out->tileWidth == 0) {
|
||||
errorThrow("Tile width cannot be 0");
|
||||
}
|
||||
if(out->tileHeight == 0) {
|
||||
errorThrow("Tile height cannot be 0");
|
||||
}
|
||||
if(out->columns == 0) {
|
||||
errorThrow("Column count cannot be 0");
|
||||
}
|
||||
if(out->rows == 0) {
|
||||
errorThrow("Row count cannot be 0");
|
||||
}
|
||||
|
||||
out->uv[0] = endianLittleToHostFloat(*(float *)(entire + 16));
|
||||
out->uv[1] = endianLittleToHostFloat(*(float *)(entire + 20));
|
||||
|
||||
if(out->uv[1] < 0.0f || out->uv[1] > 1.0f) {
|
||||
errorThrow("Invalid v0 value in tileset");
|
||||
}
|
||||
|
||||
// Setup tileset
|
||||
out->tileCount = out->columns * out->rows;
|
||||
|
||||
memoryFree(entire);
|
||||
uint8_t *data = memoryAllocate(file->size);
|
||||
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||
assetLoaderErrorChain(loading, assetFileRead(file, data, file->size));
|
||||
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||
assertTrue(file->lastRead == file->size, "Failed to read entire tileset file.");
|
||||
|
||||
loading->loading.tileset.data = data;
|
||||
loading->loading.tileset.state = ASSET_TILESET_LOADING_STATE_PARSE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetTilesetLoad(
|
||||
const char_t *path,
|
||||
tileset_t *out
|
||||
) {
|
||||
return assetLoad(path, assetTilesetLoader, NULL, out);
|
||||
errorret_t assetTilesetLoaderSync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
assertTrue(loading->type == ASSET_LOADER_TYPE_TILESET, "Invalid type.");
|
||||
|
||||
switch(loading->loading.tileset.state) {
|
||||
case ASSET_TILESET_LOADING_STATE_INITIAL:
|
||||
loading->loading.tileset.state = ASSET_TILESET_LOADING_STATE_READ_FILE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||
errorOk();
|
||||
break;
|
||||
|
||||
case ASSET_TILESET_LOADING_STATE_PARSE:
|
||||
break;
|
||||
|
||||
default:
|
||||
errorOk();
|
||||
}
|
||||
|
||||
uint8_t *data = loading->loading.tileset.data;
|
||||
assertNotNull(data, "Tileset data should have been loaded by now.");
|
||||
|
||||
tileset_t *out = &loading->entry->data.tileset;
|
||||
|
||||
if(data[0] != 'D' || data[1] != 'T' || data[2] != 'F') {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Invalid tileset header");
|
||||
}
|
||||
|
||||
if(data[3] != 0x00) {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Unsupported tileset version");
|
||||
}
|
||||
|
||||
out->tileWidth = endianLittleToHost16(*(uint16_t *)(data + 4));
|
||||
out->tileHeight = endianLittleToHost16(*(uint16_t *)(data + 6));
|
||||
out->columns = endianLittleToHost16(*(uint16_t *)(data + 8));
|
||||
out->rows = endianLittleToHost16(*(uint16_t *)(data + 10));
|
||||
|
||||
if(out->tileWidth == 0) {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Tile width cannot be 0");
|
||||
}
|
||||
if(out->tileHeight == 0) {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Tile height cannot be 0");
|
||||
}
|
||||
if(out->columns == 0) {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Column count cannot be 0");
|
||||
}
|
||||
if(out->rows == 0) {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Row count cannot be 0");
|
||||
}
|
||||
|
||||
out->uv[0] = endianLittleToHostFloat(*(float *)(data + 16));
|
||||
out->uv[1] = endianLittleToHostFloat(*(float *)(data + 20));
|
||||
|
||||
if(out->uv[1] < 0.0f || out->uv[1] > 1.0f) {
|
||||
memoryFree(data);
|
||||
assetLoaderErrorThrow(loading, "Invalid v0 value in tileset");
|
||||
}
|
||||
|
||||
out->tileCount = out->columns * out->rows;
|
||||
memoryFree(data);
|
||||
loading->loading.tileset.data = NULL;
|
||||
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void assetBatchTileset(
|
||||
assetbatch_t *batch,
|
||||
const char_t *path,
|
||||
tileset_t **out
|
||||
) {
|
||||
assetBatchAdd(
|
||||
batch,
|
||||
path,
|
||||
assetTilesetLoader,
|
||||
NULL, 0,
|
||||
sizeof(tileset_t),
|
||||
NULL,
|
||||
(void**)out
|
||||
);
|
||||
}
|
||||
errorret_t assetTilesetDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Entry cannot be NULL");
|
||||
assertTrue(entry->type == ASSET_LOADER_TYPE_TILESET, "Invalid type.");
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -1,45 +1,60 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "asset/assetbatch.h"
|
||||
#include "asset/assetfile.h"
|
||||
#include "display/texture/tileset.h"
|
||||
|
||||
/**
|
||||
* Handler for tileset assets.
|
||||
*
|
||||
* @param file Asset file to load the tileset from.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetTilesetLoader(assetfile_t *file);
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
typedef struct {
|
||||
void *nothing;
|
||||
} assettilesetloaderinput_t;
|
||||
|
||||
typedef enum {
|
||||
ASSET_TILESET_LOADING_STATE_INITIAL,
|
||||
ASSET_TILESET_LOADING_STATE_READ_FILE,
|
||||
ASSET_TILESET_LOADING_STATE_PARSE,
|
||||
ASSET_TILESET_LOADING_STATE_DONE
|
||||
} assettilesetloadingstate_t;
|
||||
|
||||
typedef struct {
|
||||
assetfile_t file;
|
||||
assettilesetloadingstate_t state;
|
||||
|
||||
uint8_t *data;
|
||||
} assettilesetloaderloading_t;
|
||||
|
||||
typedef tileset_t assettilesetoutput_t;
|
||||
|
||||
/**
|
||||
* Loads a tileset from the specified path.
|
||||
*
|
||||
* @param path Path to the tileset asset.
|
||||
* @param out Output tileset to load into.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetTilesetLoad(
|
||||
const char_t *path,
|
||||
tileset_t *out
|
||||
);
|
||||
|
||||
/**
|
||||
* Adds a tileset load request to a batch. The cache-owned tileset_t* is
|
||||
* written to *out when the batch completes.
|
||||
* Asynchronous loader for tileset assets. Reads the raw DTF file bytes into
|
||||
* the loading buffer so the sync phase can parse without blocking the main
|
||||
* thread on I/O.
|
||||
*
|
||||
* @param batch The batch to add to.
|
||||
* @param path Path to the tileset asset.
|
||||
* @param out Receives a pointer to the loaded tileset on completion.
|
||||
* @param loading Loading information for the asset being loaded.
|
||||
* @return Error code indicating success or failure of the load operation.
|
||||
*/
|
||||
void assetBatchTileset(
|
||||
assetbatch_t *batch,
|
||||
const char_t *path,
|
||||
tileset_t **out
|
||||
);
|
||||
errorret_t assetTilesetLoaderAsync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Synchronous loader for tileset assets. Parses the DTF binary previously
|
||||
* read by the async phase and populates the output tileset_t.
|
||||
*
|
||||
* @param loading Loading information for the asset being loaded.
|
||||
* @return Error code indicating success or failure of the load operation.
|
||||
*/
|
||||
errorret_t assetTilesetLoaderSync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Disposer for tileset assets.
|
||||
*
|
||||
* @param entry Asset entry containing the tileset to dispose.
|
||||
* @return Error code indicating success or failure of the dispose operation.
|
||||
*/
|
||||
errorret_t assetTilesetDispose(assetentry_t *entry);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
@@ -8,46 +8,81 @@
|
||||
#include "assetjsonloader.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
|
||||
errorret_t assetJsonLoadFileToDoc(assetfile_t *file, yyjson_doc **outDoc) {
|
||||
assertNotNull(file, "Asset file pointer for JSON loader is null.");
|
||||
assertNotNull(outDoc, "Output pointer for JSON loader is null.");
|
||||
errorret_t assetJsonLoaderAsync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
if(file->size > ASSET_JSON_FILE_SIZE_MAX) {
|
||||
errorThrow("JSON exceeds maximum allowed size");
|
||||
if(loading->loading.json.state != ASSET_JSON_LOADING_STATE_READ_FILE) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
// Create buffer
|
||||
uint8_t *buffer = memoryAllocate(file->size);
|
||||
assertNull(loading->loading.json.buffer, "Buffer already defined?");
|
||||
|
||||
errorChain(assetFileOpen(file));
|
||||
assetfile_t *file = &loading->loading.json.file;
|
||||
assetLoaderErrorChain(loading, assetFileInit(file, loading->entry->name, NULL, NULL));
|
||||
|
||||
// Read entire file
|
||||
errorChain(assetFileRead(file, buffer, file->size));
|
||||
if(file->size > ASSET_JSON_FILE_SIZE_MAX) {
|
||||
assetLoaderErrorThrow(loading, "JSON exceeds maximum allowed size");
|
||||
}
|
||||
|
||||
size_t fileSize = (size_t)file->size;
|
||||
uint8_t *buffer = memoryAllocate(fileSize);
|
||||
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||
assetLoaderErrorChain(loading, assetFileRead(file, buffer, fileSize));
|
||||
assertTrue(file->lastRead == file->size, "Failed to read entire JSON file.");
|
||||
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||
|
||||
errorChain(assetFileClose(file));
|
||||
|
||||
*outDoc = yyjson_read(
|
||||
buffer,
|
||||
file->size,
|
||||
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
|
||||
);
|
||||
memoryFree(buffer);
|
||||
|
||||
if(!*outDoc) errorThrow("Failed to parse JSON");
|
||||
loading->loading.json.buffer = buffer;
|
||||
loading->loading.json.size = fileSize;
|
||||
loading->loading.json.state = ASSET_JSON_LOADING_STATE_PARSE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetJsonLoader(assetfile_t *file) {
|
||||
yyjson_doc **outDoc = (yyjson_doc **)file->output;
|
||||
assertNotNull(outDoc, "Output pointer for JSON loader is null.");
|
||||
return assetJsonLoadFileToDoc(file, outDoc);
|
||||
errorret_t assetJsonLoaderSync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
assertTrue(loading->type == ASSET_LOADER_TYPE_JSON, "Invalid type.");
|
||||
|
||||
switch(loading->loading.json.state) {
|
||||
case ASSET_JSON_LOADING_STATE_INITIAL:
|
||||
loading->loading.json.state = ASSET_JSON_LOADING_STATE_READ_FILE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||
errorOk();
|
||||
break;
|
||||
|
||||
case ASSET_JSON_LOADING_STATE_PARSE:
|
||||
break;
|
||||
|
||||
default:
|
||||
errorOk();
|
||||
}
|
||||
|
||||
uint8_t *buffer = loading->loading.json.buffer;
|
||||
assertNotNull(buffer, "JSON buffer should have been loaded by now.");
|
||||
|
||||
loading->entry->data.json = yyjson_read(
|
||||
(char *)buffer,
|
||||
loading->loading.json.size,
|
||||
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
|
||||
);
|
||||
memoryFree(buffer);
|
||||
loading->loading.json.buffer = NULL;
|
||||
|
||||
if(!loading->entry->data.json) {
|
||||
assetLoaderErrorThrow(loading, "Failed to parse JSON");
|
||||
}
|
||||
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetJsonLoad(
|
||||
const char_t *path,
|
||||
yyjson_doc **outDoc
|
||||
) {
|
||||
return assetLoad(path, assetJsonLoader, NULL, outDoc);
|
||||
}
|
||||
errorret_t assetJsonDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||
assertTrue(entry->type == ASSET_LOADER_TYPE_JSON, "Invalid type.");
|
||||
yyjson_doc_free(entry->data.json);
|
||||
entry->data.json = NULL;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -1,45 +1,37 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "asset/assetfile.h"
|
||||
#include "yyjson.h"
|
||||
|
||||
#define ASSET_JSON_FILE_SIZE_MAX 1024*256
|
||||
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
typedef struct { void *nothing; } assetjsonloaderinput_t;
|
||||
|
||||
typedef enum {
|
||||
ASSET_JSON_LOADING_STATE_INITIAL,
|
||||
ASSET_JSON_LOADING_STATE_READ_FILE,
|
||||
ASSET_JSON_LOADING_STATE_PARSE,
|
||||
ASSET_JSON_LOADING_STATE_DONE
|
||||
} assetjsonloadingstate_t;
|
||||
|
||||
typedef struct {
|
||||
void *nothing;
|
||||
} assetjsonloaderparams_t;
|
||||
assetfile_t file;
|
||||
assetjsonloadingstate_t state;
|
||||
uint8_t *buffer;
|
||||
size_t size;
|
||||
} assetjsonloaderloading_t;
|
||||
|
||||
/**
|
||||
* Loads a JSON document from the specified asset file.
|
||||
*
|
||||
* @param file Asset file to load the JSON document from.
|
||||
* @param outDoc Pointer to store the loaded JSON document.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetJsonLoadFileToDoc(assetfile_t *file, yyjson_doc **outDoc);
|
||||
typedef yyjson_doc * assetjsonoutput_t;
|
||||
|
||||
/**
|
||||
* Handler for locale assets.
|
||||
*
|
||||
* @param file Asset file to load the locale from.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetJsonLoader(assetfile_t *file);
|
||||
|
||||
/**
|
||||
* Loads a locale from the specified path.
|
||||
*
|
||||
* @param path Path to the locale asset.
|
||||
* @param outDoc Pointer to store the loaded JSON document.
|
||||
* @return Any error that occurs during loading.
|
||||
*/
|
||||
errorret_t assetJsonLoad(
|
||||
const char_t *path,
|
||||
yyjson_doc **outDoc
|
||||
);
|
||||
errorret_t assetJsonLoaderAsync(assetloading_t *loading);
|
||||
errorret_t assetJsonLoaderSync(assetloading_t *loading);
|
||||
errorret_t assetJsonDispose(assetentry_t *entry);
|
||||
|
||||
@@ -11,38 +11,60 @@
|
||||
#include "util/string.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
errorret_t assetLocaleFileInit(
|
||||
assetlocalefile_t *localeFile,
|
||||
const char_t *path
|
||||
) {
|
||||
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||
assertNotNull(path, "Locale file path cannot be NULL.");
|
||||
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
|
||||
errorret_t assetLocaleLoaderAsync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
if(loading->loading.locale.state != ASSET_LOCALE_LOADER_STATE_LOAD_HEADER) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
assetlocalefile_t *localeFile = &loading->entry->data.locale;
|
||||
memoryZero(localeFile, sizeof(assetlocalefile_t));
|
||||
assetLoaderErrorChain(loading, assetFileInit(&localeFile->file, loading->entry->name, NULL, NULL));
|
||||
assetLoaderErrorChain(loading, assetFileOpen(&localeFile->file));
|
||||
|
||||
// Init the asset file.
|
||||
errorChain(assetFileInit(&localeFile->file, path, NULL, NULL));
|
||||
|
||||
// Open the file handle
|
||||
errorChain(assetFileOpen(&localeFile->file));
|
||||
|
||||
// Get the blank key, this is basically the header info for po files
|
||||
char_t buffer[1024];
|
||||
errorChain(assetLocaleGetString(localeFile, "", 0, buffer, sizeof(buffer)));
|
||||
errorChain(assetLocaleParseHeader(localeFile, buffer, sizeof(buffer)));
|
||||
assetLoaderErrorChain(loading, assetLocaleGetString(localeFile, "", 0, buffer, sizeof(buffer)));
|
||||
assetLoaderErrorChain(loading, assetLocaleParseHeader(localeFile, buffer, sizeof(buffer)));
|
||||
|
||||
loading->loading.locale.state = ASSET_LOCALE_LOADER_STATE_DONE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile) {
|
||||
assertNotNull(localeFile, "Locale file cannot be NULL.");
|
||||
errorret_t assetLocaleLoaderSync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
assertTrue(loading->type == ASSET_LOADER_TYPE_LOCALE, "Invalid type.");
|
||||
|
||||
errorChain(assetFileClose(&localeFile->file));
|
||||
errorChain(assetFileDispose(&localeFile->file));
|
||||
switch(loading->loading.locale.state) {
|
||||
case ASSET_LOCALE_LOADER_STATE_INITIAL:
|
||||
loading->loading.locale.state = ASSET_LOCALE_LOADER_STATE_LOAD_HEADER;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||
errorOk();
|
||||
break;
|
||||
|
||||
case ASSET_LOCALE_LOADER_STATE_DONE:
|
||||
break;
|
||||
|
||||
default:
|
||||
errorOk();
|
||||
}
|
||||
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetLocaleDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||
assertTrue(entry->type == ASSET_LOADER_TYPE_LOCALE, "Invalid type.");
|
||||
assetlocalefile_t *localeFile = &entry->data.locale;
|
||||
errorChain(assetFileClose(&localeFile->file));
|
||||
return assetFileDispose(&localeFile->file);
|
||||
}
|
||||
|
||||
errorret_t assetLocaleParseHeader(
|
||||
assetlocalefile_t *localeFile,
|
||||
char_t *headerBuffer,
|
||||
@@ -307,16 +329,24 @@ errorret_t assetLocaleLineSkipBlanks(
|
||||
while(!reader->eof) {
|
||||
// Skip blank lines
|
||||
if(lineBuffer[0] == '\0') {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// Skip comment lines
|
||||
if(lineBuffer[0] == '#') {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Is line only spaces?
|
||||
size_t lineLength = strlen((char_t *)lineBuffer);
|
||||
size_t i;
|
||||
@@ -329,7 +359,11 @@ errorret_t assetLocaleLineSkipBlanks(
|
||||
}
|
||||
|
||||
if(onlySpaces) {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
@@ -365,10 +399,18 @@ errorret_t assetLocaleLineUnbuffer(
|
||||
|
||||
// Now start buffering lines out
|
||||
while(!reader->eof) {
|
||||
errorChain(assetFileLineReaderNext(reader));
|
||||
errorret_t r = assetFileLineReaderNext(reader);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
|
||||
// Skip blank lines
|
||||
errorChain(assetLocaleLineSkipBlanks(reader, lineBuffer));
|
||||
r = assetLocaleLineSkipBlanks(reader, lineBuffer);
|
||||
if(errorIsNotOk(r)) {
|
||||
errorCatch(r);
|
||||
break;
|
||||
}
|
||||
|
||||
// Skip starting spaces
|
||||
char_t *ptr = (char_t *)lineBuffer;
|
||||
@@ -593,7 +635,7 @@ errorret_t assetLocaleGetStringWithVA(
|
||||
tempBuffer,
|
||||
bufferSize
|
||||
);
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
memoryFree(tempBuffer);
|
||||
return ret;
|
||||
}
|
||||
@@ -641,7 +683,7 @@ errorret_t assetLocaleGetStringWithArgs(
|
||||
format,
|
||||
bufferSize
|
||||
);
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
memoryFree(format);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,40 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/asset.h"
|
||||
#include "asset/assetfile.h"
|
||||
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
/** Input passed to the locale loader — currently unused. */
|
||||
typedef struct { void *nothing; } assetlocaleloaderinput_t;
|
||||
|
||||
typedef enum {
|
||||
ASSET_LOCALE_LOADER_STATE_INITIAL,
|
||||
ASSET_LOCALE_LOADER_STATE_LOAD_HEADER,
|
||||
ASSET_LOCALE_LOADER_STATE_DONE
|
||||
} assetlocaleloaderstate_t;
|
||||
|
||||
/** Per-slot scratch data used while the locale file is loading. */
|
||||
typedef struct {
|
||||
assetlocaleloaderstate_t state;
|
||||
} assetlocaleloaderloading_t;
|
||||
|
||||
/** Maximum number of distinct plural forms a locale file may declare. */
|
||||
#define ASSET_LOCALE_FILE_PLURAL_FORM_COUNT 6
|
||||
|
||||
/**
|
||||
* Comparison operator used in a plural-form expression.
|
||||
*
|
||||
* Each condition in the plural-form header is evaluated as
|
||||
* `n <op> value`
|
||||
* where `n` is the runtime plural count.
|
||||
*/
|
||||
typedef enum {
|
||||
ASSET_LOCALE_PLURAL_OP_EQUAL,
|
||||
ASSET_LOCALE_PLURAL_OP_NOT_EQUAL,
|
||||
@@ -19,13 +44,24 @@ typedef enum {
|
||||
ASSET_LOCALE_PLURAL_OP_GREATER_EQUAL
|
||||
} assetlocalepluraloperation_t;
|
||||
|
||||
/**
|
||||
* Discriminator tag for a locale string argument.
|
||||
* @see assetlocalearg_t
|
||||
*/
|
||||
typedef enum {
|
||||
ASSET_LOCALE_ARG_STRING,
|
||||
ASSET_LOCALE_ARG_INT,
|
||||
ASSET_LOCALE_ARG_FLOAT
|
||||
} assetlocaleargtype_t;
|
||||
|
||||
/**
|
||||
* A single typed argument for locale string substitution.
|
||||
*
|
||||
* Used with @ref assetLocaleGetStringWithArgs to fill `%s`, `%d`, or `%f`
|
||||
* placeholders inside a translated string.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Which union member is active. */
|
||||
assetlocaleargtype_t type;
|
||||
union {
|
||||
const char_t *stringValue;
|
||||
@@ -34,35 +70,81 @@ typedef struct {
|
||||
};
|
||||
} assetlocalearg_t;
|
||||
|
||||
/**
|
||||
* Runtime state for an open locale file.
|
||||
*
|
||||
* Loaded once by @ref assetLocaleLoaderSync and kept alive for the lifetime
|
||||
* of the asset entry. The plural-form fields are populated from the PO header
|
||||
* by @ref assetLocaleParseHeader.
|
||||
*
|
||||
* Plural evaluation works as a linear scan over `pluralStateCount - 1`
|
||||
* conditions; if none match, `pluralDefaultIndex` is returned.
|
||||
*/
|
||||
typedef struct {
|
||||
/** Underlying file handle used to rewind and re-read the PO data. */
|
||||
assetfile_t file;
|
||||
|
||||
/** Comparison operator for each conditional plural clause. */
|
||||
assetlocalepluraloperation_t pluralOps[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||
|
||||
/** Right-hand value for each conditional plural clause. */
|
||||
int32_t pluralValues[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||
|
||||
/** Form index returned when the corresponding condition is true. */
|
||||
int32_t pluralIndices[ASSET_LOCALE_FILE_PLURAL_FORM_COUNT];
|
||||
|
||||
/** Total number of plural forms declared by `nplurals=`. */
|
||||
uint8_t pluralStateCount;
|
||||
|
||||
/** Form index used when no conditional clause matches. */
|
||||
uint8_t pluralDefaultIndex;
|
||||
} assetlocalefile_t;
|
||||
|
||||
/**
|
||||
* Initialize a locale asset file.
|
||||
*
|
||||
* @param localeFile The locale file to initialize.
|
||||
* @param path The path to the locale file.
|
||||
* @return An error code if a failure occurs.
|
||||
*/
|
||||
errorret_t assetLocaleFileInit(
|
||||
assetlocalefile_t *localeFile,
|
||||
const char_t *path
|
||||
);
|
||||
/** Convenience alias — the loaded output type of a locale asset entry. */
|
||||
typedef assetlocalefile_t assetlocaleoutput_t;
|
||||
|
||||
/**
|
||||
* Dispose of a locale asset file.
|
||||
*
|
||||
* @param localeFile The locale file to dispose of.
|
||||
* @return An error code if a failure occurs.
|
||||
* Asynchronous loader callback. Opens the locale file, reads the PO header,
|
||||
* and parses plural-form rules. All I/O happens here so the main thread is
|
||||
* not blocked. Sets entry state to `ASSET_ENTRY_STATE_PENDING_SYNC` on
|
||||
* success or `ASSET_ENTRY_STATE_ERROR` on failure.
|
||||
*
|
||||
* @param loading The loading slot for this asset entry.
|
||||
* @return OK on success, error otherwise.
|
||||
*/
|
||||
errorret_t assetLocaleFileDispose(assetlocalefile_t *localeFile);
|
||||
errorret_t assetLocaleLoaderAsync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Synchronous loader callback. Confirms the async phase completed and marks
|
||||
* the entry as `ASSET_ENTRY_STATE_LOADED`.
|
||||
*
|
||||
* @param loading The loading slot for this asset entry.
|
||||
* @return OK on success, error otherwise.
|
||||
*/
|
||||
errorret_t assetLocaleLoaderSync(assetloading_t *loading);
|
||||
|
||||
/**
|
||||
* Dispose callback. Closes the open file handle and zeros the locale data.
|
||||
*
|
||||
* @param entry The asset entry to dispose.
|
||||
* @return OK on success, error otherwise.
|
||||
*/
|
||||
errorret_t assetLocaleDispose(assetentry_t *entry);
|
||||
|
||||
/**
|
||||
* Parses the `Plural-Forms:` line from a PO header string and populates the
|
||||
* plural-form fields of `localeFile`. If no `Plural-Forms:` key is present
|
||||
* the function returns OK without modifying the struct.
|
||||
*
|
||||
* Supports the `nplurals=N; plural=(<expr>);` syntax where `<expr>` is a
|
||||
* chain of `n <op> value ? index : ...` ternary conditions ending with a
|
||||
* fallback index.
|
||||
*
|
||||
* @param localeFile Struct whose plural fields will be filled in.
|
||||
* @param headerBuffer The raw PO header string (msgstr of msgid "").
|
||||
* @param headerBufferSize Size of `headerBuffer` in bytes.
|
||||
* @return OK on success, error if the plural expression is malformed.
|
||||
*/
|
||||
errorret_t assetLocaleParseHeader(
|
||||
assetlocalefile_t *localeFile,
|
||||
char_t *headerBuffer,
|
||||
@@ -70,11 +152,29 @@ errorret_t assetLocaleParseHeader(
|
||||
);
|
||||
|
||||
/**
|
||||
* Skips blank lines and comment lines in the line reader.
|
||||
*
|
||||
* @param reader Line reader to read from.
|
||||
* @param lineBuffer Buffer to use for reading lines.
|
||||
* @return Any error that occurs during skipping.
|
||||
* Evaluates which plural form index to use for a given count.
|
||||
*
|
||||
* Walks the conditions stored in `file` in order; returns the index for the
|
||||
* first condition that matches `pluralCount`. Falls back to
|
||||
* `file->pluralDefaultIndex` if none match.
|
||||
*
|
||||
* @param file Locale file with parsed plural rules.
|
||||
* @param pluralCount The runtime count (e.g. number of items).
|
||||
* @return Zero-based plural form index.
|
||||
*/
|
||||
uint8_t assetLocaleEvaluatePlural(
|
||||
assetlocalefile_t *file,
|
||||
const int32_t pluralCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Advances the line reader past blank lines, comment lines (starting with
|
||||
* `#`), and lines containing only spaces. Stops at the first content line or
|
||||
* end of file.
|
||||
*
|
||||
* @param reader Line reader positioned at the current line.
|
||||
* @param lineBuffer Output buffer that the reader writes each line into.
|
||||
* @return OK on success, error if reading fails.
|
||||
*/
|
||||
errorret_t assetLocaleLineSkipBlanks(
|
||||
assetfilelinereader_t *reader,
|
||||
@@ -82,16 +182,19 @@ errorret_t assetLocaleLineSkipBlanks(
|
||||
);
|
||||
|
||||
/**
|
||||
* Unbuffers a potentially multi-line quoted string from the line reader.
|
||||
*
|
||||
* This will read lines until it finds a line that starts with a quote, then
|
||||
* read until the closing quote.
|
||||
*
|
||||
* @param reader Line reader to read from.
|
||||
* @param lineBuffer Buffer to use for reading lines.
|
||||
* @param stringBuffer Buffer to write the unbuffered string to.
|
||||
* @param stringBufferSize Size of the string buffer.
|
||||
* @return Any error that occurs during unbuffering.
|
||||
* Reads a PO quoted string value from the current line and any immediately
|
||||
* following continuation lines that begin with `"`.
|
||||
*
|
||||
* Handles standard C escape sequences (`\n`, `\t`, `\\`, `\"`).
|
||||
*
|
||||
* @param reader Line reader positioned at the line containing the opening
|
||||
* quote (e.g. `msgstr "..."`).
|
||||
* @param lineBuffer Buffer the reader fills on each @ref assetFileLineReaderNext
|
||||
* call; also used to detect continuation lines.
|
||||
* @param stringBuffer Destination for the unescaped string content.
|
||||
* @param stringBufferSize Capacity of `stringBuffer` in bytes.
|
||||
* @return OK on success, error if a quote or escape sequence is malformed or
|
||||
* the buffer would overflow.
|
||||
*/
|
||||
errorret_t assetLocaleLineUnbuffer(
|
||||
assetfilelinereader_t *reader,
|
||||
@@ -101,14 +204,19 @@ errorret_t assetLocaleLineUnbuffer(
|
||||
);
|
||||
|
||||
/**
|
||||
* Test function for locale asset loading.
|
||||
*
|
||||
* @param file Asset file to test loading from.
|
||||
* @param messageId The message ID to retrieve.
|
||||
* @param pluralCount Count for formulating the plural variant.
|
||||
* @param stringBuffer Buffer to write the retrieved string to.
|
||||
* @param stringBufferSize Size of the string buffer.
|
||||
* @return Any error that occurs during testing.
|
||||
* Looks up a translated string by message ID from the open locale file.
|
||||
*
|
||||
* Rewinds the file and scans from the beginning on every call. For plural
|
||||
* entries (`msgid_plural`) the `pluralCount` is evaluated against the loaded
|
||||
* plural rules to select the correct `msgstr[N]` form.
|
||||
*
|
||||
* @param file Locale file to search. Must be open.
|
||||
* @param messageId PO message ID to find (`""` retrieves the header entry).
|
||||
* @param pluralCount Count used to select the plural form (ignored for
|
||||
* singular entries).
|
||||
* @param stringBuffer Buffer to receive the translated string.
|
||||
* @param stringBufferSize Capacity of `stringBuffer` in bytes.
|
||||
* @return OK on success, error if the message ID is not found or I/O fails.
|
||||
*/
|
||||
errorret_t assetLocaleGetString(
|
||||
assetlocalefile_t *file,
|
||||
@@ -119,15 +227,21 @@ errorret_t assetLocaleGetString(
|
||||
);
|
||||
|
||||
/**
|
||||
* Test function for locale asset loading with a variable argument list.
|
||||
*
|
||||
* @param file Asset file to test loading from.
|
||||
* @param messageId The message ID to retrieve.
|
||||
* @param pluralCount Count for formulating the plural variant.
|
||||
* @param buffer Buffer to write the retrieved string to.
|
||||
* @param bufferSize Size of the buffer.
|
||||
* @param ... Additional arguments for formatting the string.
|
||||
* @return Any error that occurs during testing.
|
||||
* Looks up a translated string and formats it with a `printf`-style variadic
|
||||
* argument list.
|
||||
*
|
||||
* Retrieves the raw format string via @ref assetLocaleGetString then applies
|
||||
* `vsnprintf` with the provided arguments.
|
||||
*
|
||||
* @param file Locale file to search. Must be open.
|
||||
* @param messageId PO message ID to find.
|
||||
* @param pluralCount Count used to select the plural form.
|
||||
* @param buffer Buffer to receive the formatted string.
|
||||
* @param bufferSize Capacity of `buffer` in bytes.
|
||||
* @param ... Format arguments matching the specifiers in the translated
|
||||
* string.
|
||||
* @return OK on success, error if the message is not found or formatting
|
||||
* fails.
|
||||
*/
|
||||
errorret_t assetLocaleGetStringWithVA(
|
||||
assetlocalefile_t *file,
|
||||
@@ -139,16 +253,22 @@ errorret_t assetLocaleGetStringWithVA(
|
||||
);
|
||||
|
||||
/**
|
||||
* Test function for locale asset loading with a list of arguments.
|
||||
*
|
||||
* @param file Asset file to test loading from.
|
||||
* @param messageId The message ID to retrieve.
|
||||
* @param pluralCount Count for formulating the plural variant.
|
||||
* @param buffer Buffer to write the retrieved string to.
|
||||
* @param bufferSize Size of the buffer.
|
||||
* @param args List of arguments for formatting the string.
|
||||
* @param argCount Number of arguments in the list.
|
||||
* @return Any error that occurs during testing.
|
||||
* Looks up a translated string and substitutes typed arguments into its
|
||||
* `%s`, `%d`, and `%f` placeholders.
|
||||
*
|
||||
* Unlike @ref assetLocaleGetStringWithVA this variant accepts an explicit
|
||||
* array of @ref assetlocalearg_t values, which is safer to use from
|
||||
* generated or scripted call sites.
|
||||
*
|
||||
* @param file Locale file to search. Must be open.
|
||||
* @param messageId PO message ID to find.
|
||||
* @param pluralCount Count used to select the plural form.
|
||||
* @param buffer Buffer to receive the formatted string.
|
||||
* @param bufferSize Capacity of `buffer` in bytes.
|
||||
* @param args Array of typed arguments; may be NULL when `argCount` is 0.
|
||||
* @param argCount Number of elements in `args`.
|
||||
* @return OK on success, error if the message is not found, an argument type
|
||||
* mismatches the specifier, or the buffer would overflow.
|
||||
*/
|
||||
errorret_t assetLocaleGetStringWithArgs(
|
||||
assetlocalefile_t *file,
|
||||
@@ -158,4 +278,4 @@ errorret_t assetLocaleGetStringWithArgs(
|
||||
const size_t bufferSize,
|
||||
const assetlocalearg_t *args,
|
||||
const size_t argCount
|
||||
);
|
||||
);
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
assetscriptloader.c
|
||||
)
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assetscriptloader.h"
|
||||
#include "asset/loader/assetloading.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
#include "asset/loader/assetloader.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
#include <jerryscript.h>
|
||||
|
||||
errorret_t assetScriptLoaderAsync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
|
||||
if(loading->loading.script.state != ASSET_SCRIPT_LOADING_STATE_READ_FILE) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
assertNull(loading->loading.script.buffer, "Buffer already defined?");
|
||||
|
||||
assetfile_t *file = &loading->loading.script.file;
|
||||
assetLoaderErrorChain(loading, assetFileInit(file, loading->entry->name, NULL, NULL));
|
||||
assetLoaderErrorChain(loading, assetFileOpen(file));
|
||||
|
||||
size_t capacity = ASSET_SCRIPT_CHUNK_SIZE;
|
||||
uint8_t *buffer = memoryAllocate(capacity + 1);
|
||||
size_t offset = 0;
|
||||
|
||||
while(1) {
|
||||
if(offset + ASSET_SCRIPT_CHUNK_SIZE > capacity) {
|
||||
size_t oldCapacity = capacity + 1;
|
||||
capacity += ASSET_SCRIPT_CHUNK_SIZE;
|
||||
memoryResize((void **)&buffer, oldCapacity, capacity + 1);
|
||||
}
|
||||
assetLoaderErrorChain(loading, assetFileRead(
|
||||
file, buffer + offset, ASSET_SCRIPT_CHUNK_SIZE
|
||||
));
|
||||
size_t chunk = (size_t)file->lastRead;
|
||||
offset += chunk;
|
||||
if(chunk == 0) break;
|
||||
}
|
||||
|
||||
buffer[offset] = '\0';
|
||||
assetLoaderErrorChain(loading, assetFileClose(file));
|
||||
assetLoaderErrorChain(loading, assetFileDispose(file));
|
||||
|
||||
loading->loading.script.buffer = buffer;
|
||||
loading->loading.script.size = offset;
|
||||
loading->loading.script.state = ASSET_SCRIPT_LOADING_STATE_EXEC;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetScriptLoaderSync(assetloading_t *loading) {
|
||||
assertNotNull(loading, "Loading cannot be NULL");
|
||||
assertTrue(loading->type == ASSET_LOADER_TYPE_SCRIPT, "Invalid type.");
|
||||
|
||||
switch(loading->loading.script.state) {
|
||||
case ASSET_SCRIPT_LOADING_STATE_INITIAL:
|
||||
loading->loading.script.state = ASSET_SCRIPT_LOADING_STATE_READ_FILE;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_PENDING_ASYNC;
|
||||
errorOk();
|
||||
break;
|
||||
|
||||
case ASSET_SCRIPT_LOADING_STATE_EXEC:
|
||||
break;
|
||||
|
||||
default:
|
||||
errorOk();
|
||||
}
|
||||
|
||||
uint8_t *buffer = loading->loading.script.buffer;
|
||||
assertNotNull(buffer, "Script buffer should have been loaded by now.");
|
||||
|
||||
jerry_value_t result = jerry_eval(
|
||||
(const jerry_char_t *)buffer,
|
||||
loading->loading.script.size,
|
||||
JERRY_PARSE_NO_OPTS
|
||||
);
|
||||
memoryFree(buffer);
|
||||
loading->loading.script.buffer = NULL;
|
||||
|
||||
if(jerry_value_is_exception(result)) {
|
||||
jerry_value_t errVal = jerry_exception_value(result, false);
|
||||
jerry_value_t errStr = jerry_value_to_string(errVal);
|
||||
char_t buf[256];
|
||||
jerry_size_t len = jerry_string_to_buffer(
|
||||
errStr, JERRY_ENCODING_UTF8, (jerry_char_t *)buf, sizeof(buf) - 1
|
||||
);
|
||||
buf[len] = '\0';
|
||||
jerry_value_free(errStr);
|
||||
jerry_value_free(errVal);
|
||||
jerry_value_free(result);
|
||||
assetLoaderErrorThrow(loading, "Script error in '%s': %s",
|
||||
loading->entry->name, buf
|
||||
);
|
||||
}
|
||||
|
||||
loading->entry->data.script = (assetscriptoutput_t)result;
|
||||
loading->entry->state = ASSET_ENTRY_STATE_LOADED;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t assetScriptDispose(assetentry_t *entry) {
|
||||
assertNotNull(entry, "Asset entry cannot be NULL");
|
||||
assertTrue(entry->type == ASSET_LOADER_TYPE_SCRIPT, "Invalid type.");
|
||||
if(entry->data.script != 0) {
|
||||
jerry_value_free((jerry_value_t)entry->data.script);
|
||||
entry->data.script = 0;
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "asset/assetfile.h"
|
||||
|
||||
#define ASSET_SCRIPT_CHUNK_SIZE 1024
|
||||
|
||||
typedef struct assetloading_s assetloading_t;
|
||||
typedef struct assetentry_s assetentry_t;
|
||||
|
||||
typedef struct { void *nothing; } assetscriptloaderinput_t;
|
||||
typedef uint32_t assetscriptoutput_t;
|
||||
|
||||
typedef enum {
|
||||
ASSET_SCRIPT_LOADING_STATE_INITIAL,
|
||||
ASSET_SCRIPT_LOADING_STATE_READ_FILE,
|
||||
ASSET_SCRIPT_LOADING_STATE_EXEC,
|
||||
ASSET_SCRIPT_LOADING_STATE_DONE
|
||||
} assetscriptloadingstate_t;
|
||||
|
||||
typedef struct {
|
||||
assetfile_t file;
|
||||
assetscriptloadingstate_t state;
|
||||
uint8_t *buffer;
|
||||
size_t size;
|
||||
} assetscriptloaderloading_t;
|
||||
|
||||
errorret_t assetScriptLoaderAsync(assetloading_t *loading);
|
||||
errorret_t assetScriptLoaderSync(assetloading_t *loading);
|
||||
errorret_t assetScriptDispose(assetentry_t *entry);
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "util/memory.h"
|
||||
#include "display/spritebatch/spritebatch.h"
|
||||
#include "asset/asset.h"
|
||||
#include "asset/assetbatch.h"
|
||||
#include "asset/loader/display/assettextureloader.h"
|
||||
#include "asset/loader/display/assettilesetloader.h"
|
||||
#include "display/shader/shaderunlit.h"
|
||||
@@ -18,19 +17,26 @@
|
||||
font_t FONT_DEFAULT;
|
||||
|
||||
errorret_t textInit(void) {
|
||||
assetbatch_t batch;
|
||||
assetBatchInit(&batch, NULL, NULL, NULL);
|
||||
assetBatchTexture(&batch, "ui/minogram.png", TEXTURE_FORMAT_RGBA, &FONT_DEFAULT.texture);
|
||||
assetBatchTileset(&batch, "ui/minogram.dtf", &FONT_DEFAULT.tileset);
|
||||
errorChain(assetBatchLoad(&batch));
|
||||
assetloaderinput_t input = { .texture = TEXTURE_FORMAT_RGBA };
|
||||
assetentry_t *entryTexture = assetLock(
|
||||
"ui/minogram.png", ASSET_LOADER_TYPE_TEXTURE, &input
|
||||
);
|
||||
assetentry_t *entryTileset = assetLock(
|
||||
"ui/minogram.dtf", ASSET_LOADER_TYPE_TILESET, NULL
|
||||
);
|
||||
errorChain(assetRequireLoaded(entryTexture));
|
||||
errorChain(assetRequireLoaded(entryTileset));
|
||||
|
||||
FONT_DEFAULT.texture = &entryTexture->data.texture;
|
||||
FONT_DEFAULT.tileset = &entryTileset->data.tileset;
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t textDispose(void) {
|
||||
assetCacheRelease(&ASSET.cache, "ui/minogram.png");
|
||||
assetCacheRelease(&ASSET.cache, "ui/minogram.dtf");
|
||||
FONT_DEFAULT.texture = NULL;
|
||||
FONT_DEFAULT.tileset = NULL;
|
||||
assetUnlock("ui/minogram.png");
|
||||
assetUnlock("ui/minogram.dtf");
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -87,7 +93,9 @@ errorret_t textDraw(
|
||||
float_t posX = x;
|
||||
float_t posY = y;
|
||||
|
||||
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, font->texture));
|
||||
errorChain(shaderSetTexture(
|
||||
&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, font->texture
|
||||
));
|
||||
|
||||
#if MESH_ENABLE_COLOR
|
||||
#else
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "network/network.h"
|
||||
#include "system/system.h"
|
||||
#include "console/console.h"
|
||||
#include "script/script.h"
|
||||
#include "item/backpack.h"
|
||||
#include "save/save.h"
|
||||
|
||||
@@ -46,17 +47,18 @@ errorret_t engineInit(const int32_t argc, const char_t **argv) {
|
||||
errorChain(displayInit());
|
||||
errorChain(uiInit());
|
||||
errorChain(uiTextboxInit());
|
||||
|
||||
errorChain(cutsceneInit());
|
||||
errorChain(sceneInit());
|
||||
entityManagerInit();
|
||||
backpackInit();
|
||||
physicsManagerInit();
|
||||
errorChain(networkInit());
|
||||
errorChain(scriptInit());
|
||||
|
||||
/* Run the init script. */
|
||||
consolePrint("Engine initialized");
|
||||
sceneSet(SCENE_TYPE_OVERWORLD);
|
||||
|
||||
errorChain(scriptExecFile("init.js"));
|
||||
sceneSet(SCENE_TYPE_INITIAL);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
@@ -74,6 +76,7 @@ errorret_t engineUpdate(void) {
|
||||
errorChain(displayUpdate());
|
||||
errorChain(cutsceneUpdate());
|
||||
errorChain(sceneUpdate());
|
||||
errorChain(assetUpdate());
|
||||
|
||||
if(inputPressed(INPUT_ACTION_RAGEQUIT)) ENGINE.running = false;
|
||||
errorOk();
|
||||
@@ -95,6 +98,7 @@ errorret_t engineDispose(void) {
|
||||
errorChain(displayDispose());
|
||||
// errorChain(saveDispose());
|
||||
errorChain(assetDispose());
|
||||
errorChain(scriptDispose());
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ void entityRenderableDispose(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
void entityRenderableSetType(
|
||||
@@ -80,6 +79,49 @@ void entityRenderableSetDraw(
|
||||
r->data.custom.drawUser = user;
|
||||
}
|
||||
|
||||
static errorret_t entityRenderableDrawSpritebatch(
|
||||
const entityrenderablespritebatch_t *sb
|
||||
) {
|
||||
if(sb->spriteCount == 0) errorOk();
|
||||
|
||||
errorChain(displaySetState((displaystate_t){
|
||||
.flags = DISPLAY_STATE_FLAG_BLEND
|
||||
}));
|
||||
|
||||
spriteBatchClear();
|
||||
shadermaterial_t mat;
|
||||
memoryZero(&mat, sizeof(shadermaterial_t));
|
||||
mat.unlit.texture = sb->texture;
|
||||
mat.unlit.color = COLOR_WHITE;
|
||||
errorChain(spriteBatchBuffer(
|
||||
sb->sprites, sb->spriteCount,
|
||||
SHADER_LIST_DEFS[SHADER_LIST_SHADER_UNLIT].shader, mat
|
||||
));
|
||||
return spriteBatchFlush();
|
||||
}
|
||||
|
||||
static errorret_t entityRenderableDrawMaterial(
|
||||
const entityrenderablematerial_t *m
|
||||
) {
|
||||
errorChain(displaySetState(m->state));
|
||||
shader_t *shader = SHADER_LIST_DEFS[m->shaderType].shader;
|
||||
assertNotNull(shader, "Shader cannot be null for material type");
|
||||
errorChain(shaderBind(shader));
|
||||
errorChain(shaderSetMaterial(shader, &m->material));
|
||||
for(uint8_t i = 0; i < m->meshCount; i++) {
|
||||
errorChain(meshDraw(m->meshes[i], m->meshOffsets[i], m->meshCounts[i]));
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
|
||||
static errorret_t entityRenderableDrawCustom(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId,
|
||||
const entityrenderablecustom_t *custom
|
||||
) {
|
||||
return custom->draw(entityId, componentId, custom->drawUser);
|
||||
}
|
||||
|
||||
errorret_t entityRenderableDraw(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId
|
||||
@@ -87,41 +129,13 @@ errorret_t entityRenderableDraw(
|
||||
entityrenderable_t *r = componentGetData(
|
||||
entityId, componentId, COMPONENT_TYPE_RENDERABLE
|
||||
);
|
||||
|
||||
switch(r->type) {
|
||||
case ENTITY_RENDERABLE_TYPE_SPRITEBATCH: {
|
||||
const entityrenderablespritebatch_t *sb = &r->data.spritebatch;
|
||||
errorChain(displaySetState((displaystate_t){
|
||||
.flags = DISPLAY_STATE_FLAG_BLEND
|
||||
}));
|
||||
spriteBatchClear();
|
||||
shadermaterial_t mat;
|
||||
memoryZero(&mat, sizeof(shadermaterial_t));
|
||||
mat.unlit.texture = sb->texture;
|
||||
mat.unlit.color = COLOR_WHITE;
|
||||
errorChain(spriteBatchBuffer(
|
||||
sb->sprites, sb->spriteCount,
|
||||
SHADER_LIST_DEFS[SHADER_LIST_SHADER_UNLIT].shader, mat
|
||||
));
|
||||
return spriteBatchFlush();
|
||||
}
|
||||
|
||||
case ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL: {
|
||||
const entityrenderablematerial_t *m = &r->data.material;
|
||||
errorChain(displaySetState(m->state));
|
||||
shader_t *shader = SHADER_LIST_DEFS[m->shaderType].shader;
|
||||
assertNotNull(shader, "Shader cannot be null for material type");
|
||||
errorChain(shaderBind(shader));
|
||||
errorChain(shaderSetMaterial(shader, &m->material));
|
||||
for(uint8_t i = 0; i < m->meshCount; i++) {
|
||||
errorChain(meshDraw(m->meshes[i], m->meshOffsets[i], m->meshCounts[i]));
|
||||
}
|
||||
errorOk();
|
||||
}
|
||||
|
||||
case ENTITY_RENDERABLE_TYPE_SPRITEBATCH:
|
||||
return entityRenderableDrawSpritebatch(&r->data.spritebatch);
|
||||
case ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL:
|
||||
return entityRenderableDrawMaterial(&r->data.material);
|
||||
case ENTITY_RENDERABLE_TYPE_CUSTOM:
|
||||
return r->data.custom.draw(entityId, componentId, r->data.custom.drawUser);
|
||||
|
||||
return entityRenderableDrawCustom(entityId, componentId, &r->data.custom);
|
||||
default:
|
||||
assertUnreachable("Invalid renderable type");
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "util/string.h"
|
||||
#include "log/log.h"
|
||||
|
||||
errorstate_t ERROR_STATE = { 0 };
|
||||
THREAD_LOCAL errorstate_t ERROR_STATE = { 0 };
|
||||
|
||||
errorret_t errorThrowImpl(
|
||||
errorstate_t *state,
|
||||
@@ -64,7 +64,7 @@ errorret_t errorOkImpl() {
|
||||
ERROR_STATE.code == ERROR_OK,
|
||||
"Global error state is not OK (Likely missing errorCatch)"
|
||||
);
|
||||
|
||||
|
||||
return (errorret_t) {
|
||||
.code = ERROR_OK,
|
||||
.state = NULL
|
||||
@@ -77,7 +77,7 @@ errorret_t errorChainImpl(
|
||||
const char_t *function,
|
||||
const int32_t line
|
||||
) {
|
||||
if(retval.code == ERROR_OK) return retval;
|
||||
if(errorIsOk(retval)) return retval;
|
||||
|
||||
assertNotNull(retval.state, "Error state NULL (Likely missing errorOk)");
|
||||
assertNotNull(retval.state->message, "Message cannot be NULL");
|
||||
@@ -111,18 +111,19 @@ errorret_t errorChainImpl(
|
||||
}
|
||||
|
||||
void errorCatch(errorret_t retval) {
|
||||
if(retval.code == ERROR_OK) return;
|
||||
if(errorIsOk(retval)) return;
|
||||
|
||||
assertNotNull(retval.state, "Error state cannot be NULL");
|
||||
assertNotNull(retval.state->message, "Message cannot be NULL");
|
||||
|
||||
// Clear the error state
|
||||
memoryFree((void*)retval.state->message);
|
||||
memoryFree((void*)retval.state->lines);
|
||||
retval.state->code = ERROR_OK;
|
||||
}
|
||||
|
||||
errorret_t errorPrint(const errorret_t retval) {
|
||||
if(retval.code == ERROR_OK) return retval;
|
||||
if(errorIsOk(retval)) return retval;
|
||||
|
||||
assertNotNull(retval.state, "Error state cannot be NULL");
|
||||
assertNotNull(retval.state->message, "Message cannot be NULL");
|
||||
|
||||
+19
-31
@@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
#include "thread/threadlocal.h"
|
||||
|
||||
typedef uint8_t errorcode_t;
|
||||
|
||||
@@ -26,7 +27,7 @@ static const errorcode_t ERROR_NOT_OK = 1;
|
||||
|
||||
static const char_t *ERROR_PRINT_FORMAT = "Error (%d): %s\n%s";
|
||||
static const char_t *ERROR_LINE_FORMAT = " at %s:%d in function %s\n";
|
||||
extern errorstate_t ERROR_STATE;
|
||||
extern THREAD_LOCAL errorstate_t ERROR_STATE;
|
||||
|
||||
/**
|
||||
* Sets the error state with the provided code and message.
|
||||
@@ -52,7 +53,7 @@ errorret_t errorThrowImpl(
|
||||
|
||||
/**
|
||||
* Returns an error state with no error.
|
||||
*
|
||||
*
|
||||
* @return An error state with code ERROR_OK.
|
||||
*/
|
||||
errorret_t errorOkImpl();
|
||||
@@ -88,34 +89,6 @@ void errorCatch(errorret_t retval);
|
||||
*/
|
||||
errorret_t errorPrint(const errorret_t retval);
|
||||
|
||||
/**
|
||||
* Creates an error with a specific error state.
|
||||
*
|
||||
* @param state The error state to set.
|
||||
* @param message The format string for the error message.
|
||||
* @param ... Additional arguments for the format string.
|
||||
* @return The error code.
|
||||
*/
|
||||
#define errorCreate(state, message, ... ) \
|
||||
errorThrowImpl(\
|
||||
(state), ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
|
||||
##__VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Throws an error with a specific error state.
|
||||
*
|
||||
* @param state The error state to set.
|
||||
* @param message The format string for the error message.
|
||||
* @param ... Additional arguments for the format string.
|
||||
* @return The error code.
|
||||
*/
|
||||
#define errorThrowState(state, message, ... ) \
|
||||
return errorThrowImpl(\
|
||||
(state), ERROR_NOT_OK, __FILE__, __func__, __LINE__, (message), \
|
||||
##__VA_ARGS__ \
|
||||
)
|
||||
|
||||
/**
|
||||
* Throws an error with a formatted message.
|
||||
*
|
||||
@@ -154,12 +127,27 @@ errorret_t errorPrint(const errorret_t retval);
|
||||
return errorChainImpl(errorChainRetval, __FILE__, __func__, __LINE__); \
|
||||
} \
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns without an error.
|
||||
*
|
||||
* @return An error state with code ERROR_OK.
|
||||
*/
|
||||
#define errorOk() \
|
||||
return errorOkImpl()
|
||||
|
||||
/**
|
||||
* Returns non-zero if retval indicates success.
|
||||
*
|
||||
* @param retval errorret_t to test.
|
||||
*/
|
||||
#define errorIsOk(retval) ((retval).code == ERROR_OK)
|
||||
|
||||
/**
|
||||
* Returns non-zero if retval indicates failure.
|
||||
*
|
||||
* @param retval errorret_t to test.
|
||||
*/
|
||||
#define errorIsNotOk(retval) ((retval).code != ERROR_OK)
|
||||
|
||||
// EOF
|
||||
+31
-19
@@ -9,43 +9,53 @@
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
void eventInit(event_t *event) {
|
||||
void eventInit(event_t *event, eventcallback_t *callbacks, void **users, size_t size) {
|
||||
assertNotNull(event, "event must not be NULL");
|
||||
memoryZero(event, sizeof(event_t));
|
||||
assertNotNull((void *)callbacks, "callbacks must not be NULL");
|
||||
assertTrue(size > 0, "size must be greater than 0");
|
||||
|
||||
event->callbacks = callbacks;
|
||||
event->users = users;
|
||||
event->size = size;
|
||||
event->count = 0;
|
||||
memoryZero(callbacks, sizeof(eventcallback_t) * size);
|
||||
if(users) memoryZero(users, sizeof(void *) * size);
|
||||
}
|
||||
|
||||
void eventSubscribe(event_t *event, eventcallback_t callback, void *user) {
|
||||
assertNotNull(event, "event must not be NULL");
|
||||
assertNotNull((void *)callback, "callback must not be NULL");
|
||||
assertNotNull(callback, "callback must not be NULL");
|
||||
|
||||
for(uint8_t i = 0; i < event->count; i++) {
|
||||
if(event->callbacks[i] == callback && event->users[i] == user) return;
|
||||
// Ensure callback isn't already susbcribed
|
||||
for(uint32_t i = 0; i < event->count; i++) {
|
||||
if(event->callbacks[i] != callback) continue;
|
||||
assertUnreachable("Callback already registered, cannot subscribe twice.");
|
||||
}
|
||||
|
||||
assertTrue(
|
||||
event->count < EVENT_SUBSCRIBER_COUNT_MAX,
|
||||
"EVENT_SUBSCRIBER_COUNT_MAX exceeded"
|
||||
);
|
||||
assertTrue(event->count < event->size, "event subscriber capacity exceeded");
|
||||
|
||||
event->callbacks[event->count] = callback;
|
||||
event->users[event->count] = user;
|
||||
if(user) {
|
||||
assertNotNull(event->users, "Cannot add user pointer.");
|
||||
event->users[event->count] = user;
|
||||
}
|
||||
event->count++;
|
||||
}
|
||||
|
||||
void eventUnsubscribe(event_t *event, eventcallback_t callback, void *user) {
|
||||
void eventUnsubscribe(event_t *event, eventcallback_t callback) {
|
||||
assertNotNull(event, "event must not be NULL");
|
||||
assertNotNull((void *)callback, "callback must not be NULL");
|
||||
assertNotNull(callback, "callback must not be NULL");
|
||||
|
||||
for(uint8_t i = 0; i < event->count; i++) {
|
||||
if(event->callbacks[i] != callback || event->users[i] != user) continue;
|
||||
for(uint32_t i = 0; i < event->count; i++) {
|
||||
if(event->callbacks[i] != callback) continue;
|
||||
|
||||
uint8_t last = event->count - 1;
|
||||
uint32_t last = event->count - 1;
|
||||
if(i != last) {
|
||||
event->callbacks[i] = event->callbacks[last];
|
||||
event->users[i] = event->users[last];
|
||||
if(event->users) event->users[i] = event->users[last];
|
||||
}
|
||||
event->callbacks[last] = NULL;
|
||||
event->users[last] = NULL;
|
||||
if(event->users) event->users[last] = NULL;
|
||||
event->count--;
|
||||
return;
|
||||
}
|
||||
@@ -53,7 +63,9 @@ void eventUnsubscribe(event_t *event, eventcallback_t callback, void *user) {
|
||||
|
||||
void eventInvoke(const event_t *event, void *params) {
|
||||
assertNotNull(event, "event must not be NULL");
|
||||
for(uint8_t i = 0; i < event->count; i++) {
|
||||
event->callbacks[i](params, event->users[i]);
|
||||
|
||||
for(uint32_t i = 0; i < event->count; i++) {
|
||||
void *u = event->users ? event->users[i] : NULL;
|
||||
event->callbacks[i](params, u);
|
||||
}
|
||||
}
|
||||
|
||||
+21
-13
@@ -8,31 +8,40 @@
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
#define EVENT_SUBSCRIBER_COUNT_MAX 8
|
||||
|
||||
typedef void (*eventcallback_t)(void *params, void *user);
|
||||
|
||||
typedef struct {
|
||||
eventcallback_t callbacks[EVENT_SUBSCRIBER_COUNT_MAX];
|
||||
void *users[EVENT_SUBSCRIBER_COUNT_MAX];
|
||||
uint8_t count;
|
||||
eventcallback_t *callbacks;
|
||||
void **users;
|
||||
size_t size;
|
||||
uint32_t count;
|
||||
} event_t;
|
||||
|
||||
/**
|
||||
* Initializes an event, clearing all subscribers.
|
||||
* Initializes an event, binding it to the provided backing arrays and clearing
|
||||
* all subscribers. May also be called to reset an event (re-clears subscribers
|
||||
* without changing the backing arrays or size).
|
||||
*
|
||||
* @param event The event to initialize.
|
||||
* @param callbacks Caller-owned array of at least `size` callback slots.
|
||||
* @param users Array of user pointers, matching each callback, or NULL.
|
||||
* @param size Capacity of both arrays, must match.
|
||||
*/
|
||||
void eventInit(event_t *event);
|
||||
void eventInit(
|
||||
event_t *event,
|
||||
eventcallback_t *callbacks,
|
||||
void **users,
|
||||
size_t size
|
||||
);
|
||||
|
||||
/**
|
||||
* Subscribes a callback to an event. The callback is invoked with params and
|
||||
* the provided user pointer each time the event fires. The same (callback,
|
||||
* user) pair may only be subscribed once.
|
||||
*
|
||||
* @param event The event to subscribe to.
|
||||
* @param event The event to subscribe to.
|
||||
* @param callback The function to call when the event fires.
|
||||
* @param user Arbitrary pointer forwarded to the callback unchanged.
|
||||
* @param user Arbitrary pointer forwarded to the callback unchanged.
|
||||
*/
|
||||
void eventSubscribe(event_t *event, eventcallback_t callback, void *user);
|
||||
|
||||
@@ -40,17 +49,16 @@ void eventSubscribe(event_t *event, eventcallback_t callback, void *user);
|
||||
* Removes a previously subscribed (callback, user) pair. Does nothing if the
|
||||
* pair is not currently subscribed.
|
||||
*
|
||||
* @param event The event to unsubscribe from.
|
||||
* @param event The event to unsubscribe from.
|
||||
* @param callback The callback that was passed to eventSubscribe.
|
||||
* @param user The user pointer that was passed to eventSubscribe.
|
||||
*/
|
||||
void eventUnsubscribe(event_t *event, eventcallback_t callback, void *user);
|
||||
void eventUnsubscribe(event_t *event, eventcallback_t callback);
|
||||
|
||||
/**
|
||||
* Invokes all subscribed callbacks, passing params and each subscriber's user
|
||||
* pointer.
|
||||
*
|
||||
* @param event The event to invoke.
|
||||
* @param event The event to invoke.
|
||||
* @param params Arbitrary pointer forwarded to every callback unchanged.
|
||||
*/
|
||||
void eventInvoke(const event_t *event, void *params);
|
||||
|
||||
@@ -22,8 +22,8 @@ errorret_t inputInit(void) {
|
||||
INPUT.actions[i].action = (inputaction_t)i;
|
||||
INPUT.actions[i].lastValue = 0.0f;
|
||||
INPUT.actions[i].currentValue = 0.0f;
|
||||
eventInit(&INPUT.actions[i].onPressed);
|
||||
eventInit(&INPUT.actions[i].onReleased);
|
||||
eventInit(&INPUT.actions[i].onPressed, INPUT.actions[i].onPressedCallbacks, INPUT.actions[i].onPressedUsers, 4);
|
||||
eventInit(&INPUT.actions[i].onReleased, INPUT.actions[i].onReleasedCallbacks, INPUT.actions[i].onReleasedUsers, 4);
|
||||
}
|
||||
|
||||
#ifdef inputInitPlatform
|
||||
|
||||
@@ -20,7 +20,11 @@ typedef struct {
|
||||
float_t currentDynamicValue;
|
||||
#endif
|
||||
|
||||
eventcallback_t onPressedCallbacks[4];
|
||||
void *onPressedUsers[4];
|
||||
event_t onPressed;
|
||||
eventcallback_t onReleasedCallbacks[4];
|
||||
void *onReleasedUsers[4];
|
||||
event_t onReleased;
|
||||
} inputactiondata_t;
|
||||
|
||||
|
||||
@@ -13,28 +13,31 @@ localemanager_t LOCALE;
|
||||
|
||||
errorret_t localeManagerInit() {
|
||||
memoryZero(&LOCALE, sizeof(localemanager_t));
|
||||
|
||||
errorChain(localeManagerSetLocale(&LOCALE_EN_US));
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t localeManagerSetLocale(const localeinfo_t *locale) {
|
||||
if(LOCALE.fileOpen) {
|
||||
errorChain(assetLocaleFileDispose(&LOCALE.file));
|
||||
LOCALE.fileOpen = false;
|
||||
assertNotNull(locale, "Locale cannot be NULL");
|
||||
|
||||
if(LOCALE.entry != NULL) {
|
||||
assetEntryUnlock(LOCALE.entry);
|
||||
LOCALE.entry = NULL;
|
||||
}
|
||||
|
||||
// Init the asset file
|
||||
errorChain(assetLocaleFileInit(&LOCALE.file, locale->file));
|
||||
LOCALE.fileOpen = true;
|
||||
LOCALE.locale = locale;
|
||||
LOCALE.entry = assetGetEntry(locale->file, ASSET_LOADER_TYPE_LOCALE, NULL);
|
||||
assetEntryLock(LOCALE.entry);
|
||||
errorChain(assetRequireLoaded(LOCALE.entry));
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void localeManagerDispose() {
|
||||
if(LOCALE.fileOpen) {
|
||||
errorCatch(errorPrint(assetLocaleFileDispose(&LOCALE.file)));
|
||||
LOCALE.fileOpen = false;
|
||||
if(LOCALE.entry != NULL) {
|
||||
assetEntryUnlock(LOCALE.entry);
|
||||
LOCALE.entry = NULL;
|
||||
}
|
||||
|
||||
LOCALE.locale = NULL;
|
||||
}
|
||||
@@ -9,12 +9,11 @@
|
||||
#include "error/error.h"
|
||||
#include "localemanager.h"
|
||||
#include "locale/localeinfo.h"
|
||||
#include "asset/loader/locale/assetlocaleloader.h"
|
||||
#include "asset/asset.h"
|
||||
|
||||
typedef struct {
|
||||
const localeinfo_t *locale;
|
||||
assetlocalefile_t file;
|
||||
bool_t fileOpen;
|
||||
assetentry_t *entry;
|
||||
} localemanager_t;
|
||||
|
||||
extern localemanager_t LOCALE;
|
||||
@@ -46,7 +45,7 @@ errorret_t localeManagerSetLocale(const localeinfo_t *locale);
|
||||
*/
|
||||
#define localeManagerGetText(id, buffer, bufferSize, plural, ...) \
|
||||
assetLocaleGetStringWithVA( \
|
||||
&LOCALE.file, \
|
||||
&LOCALE.entry->data.locale, \
|
||||
id, \
|
||||
plural, \
|
||||
buffer, \
|
||||
@@ -69,7 +68,7 @@ errorret_t localeManagerSetLocale(const localeinfo_t *locale);
|
||||
id, buffer, bufferSize, plural, args, argCount \
|
||||
) \
|
||||
assetLocaleGetStringWithArgs( \
|
||||
&LOCALE.file, \
|
||||
&LOCALE.entry->data.locale, \
|
||||
id, \
|
||||
plural, \
|
||||
buffer, \
|
||||
|
||||
+3
-3
@@ -15,7 +15,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
// Init engine
|
||||
ret = engineInit(argc, (const char_t **)argv);
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
return ret.code;
|
||||
}
|
||||
@@ -23,14 +23,14 @@ int main(int argc, char **argv) {
|
||||
// Begin main loop
|
||||
do {
|
||||
ret = engineUpdate();
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
return ret.code;
|
||||
}
|
||||
} while(ENGINE.running);
|
||||
|
||||
ret = engineDispose();
|
||||
if(ret.code != ERROR_OK) {
|
||||
if(errorIsNotOk(ret)) {
|
||||
errorCatch(errorPrint(ret));
|
||||
return ret.code;
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ errorret_t saveLoad(const uint8_t slot) {
|
||||
saveStreamClosePlatform(&stream);
|
||||
#endif
|
||||
|
||||
if(ret.code != ERROR_OK) return ret;
|
||||
if(errorIsNotOk(ret)) return ret;
|
||||
|
||||
errorChain(saveStreamVerifyChecksumImpl(&stream, slot));
|
||||
|
||||
@@ -72,7 +72,7 @@ errorret_t saveWrite(const uint8_t slot) {
|
||||
|
||||
errorret_t ret = saveFileWrite(&stream, file);
|
||||
|
||||
if(ret.code == ERROR_OK) {
|
||||
if(errorIsOk(ret)) {
|
||||
ret = saveStreamFinalizeWriteImpl(&stream);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ errorret_t saveWrite(const uint8_t slot) {
|
||||
saveStreamClosePlatform(&stream);
|
||||
#endif
|
||||
|
||||
if(ret.code != ERROR_OK) return ret;
|
||||
if(errorIsNotOk(ret)) return ret;
|
||||
|
||||
file->exists = true;
|
||||
errorOk();
|
||||
|
||||
@@ -464,4 +464,5 @@ errorret_t saveFileWrite(savestream_t *stream, savefile_t *file);
|
||||
#define saveFileReadDate(stream, out) \
|
||||
errorChain(saveStreamReadDateImpl(stream, out))
|
||||
#define saveFileWriteDate(stream, input) \
|
||||
errorChain(saveStreamWriteDateImpl(stream, input))
|
||||
errorChain(saveStreamWriteDateImpl(stream, input))
|
||||
|
||||
|
||||
@@ -8,28 +8,21 @@
|
||||
#include "initialscene.h"
|
||||
#include "console/console.h"
|
||||
#include "scene/scene.h"
|
||||
#include "time/time.h"
|
||||
#include "ui/uiloading.h"
|
||||
#include "script/script.h"
|
||||
#include "entity/entitymanager.h"
|
||||
#include "entity/entity.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
#include "entity/component/display/entityrenderable.h"
|
||||
|
||||
void initialSceneInit(void) {
|
||||
consolePrint("Initial scene initialized");
|
||||
SCENE.data.initial.timer = 0.0f;
|
||||
SCENE.data.initial.hiding = false;
|
||||
uiLoadingShow(NULL, NULL);
|
||||
errorCatch(errorPrint(scriptExecFile("testentity.js")));
|
||||
}
|
||||
|
||||
errorret_t initialSceneUpdate(void) {
|
||||
initialscene_t *scene = &SCENE.data.initial;
|
||||
if(scene->hiding) errorOk();
|
||||
|
||||
scene->timer += TIME.delta;
|
||||
if(scene->timer >= INITIAL_SCENE_WAIT) {
|
||||
scene->hiding = true;
|
||||
uiLoadingHide(NULL, NULL);
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void initialSceneDispose(void) {
|
||||
entityDispose(SCENE.data.initial.cubeEntityId);
|
||||
}
|
||||
|
||||
@@ -7,12 +7,10 @@
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
#define INITIAL_SCENE_WAIT 2.0f
|
||||
#include "entity/entitybase.h"
|
||||
|
||||
typedef struct {
|
||||
float_t timer;
|
||||
bool_t hiding;
|
||||
entityid_t cubeEntityId;
|
||||
} initialscene_t;
|
||||
|
||||
void initialSceneInit(void);
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
script.c
|
||||
scriptproto.c
|
||||
)
|
||||
|
||||
# Subdirectories
|
||||
add_subdirectory(module)
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
modulebase.c
|
||||
)
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/display/moduletexture.h"
|
||||
#include "script/module/asset/moduleassetentry.h"
|
||||
#include "asset/asset.h"
|
||||
#include "asset/loader/assetloader.h"
|
||||
|
||||
static scriptproto_t MODULE_ASSET_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleAssetExists) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireString(0);
|
||||
|
||||
char_t buf[256];
|
||||
moduleBaseToString(args[0], buf, sizeof(buf));
|
||||
|
||||
return jerry_boolean(assetFileExists(buf));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetLock) {
|
||||
moduleBaseRequireArgs(2);
|
||||
moduleBaseRequireString(0);
|
||||
moduleBaseRequireNumber(1);
|
||||
|
||||
char_t buf[256];
|
||||
moduleBaseToString(args[0], buf, sizeof(buf));
|
||||
assetloadertype_t type = (assetloadertype_t)moduleBaseArgInt(1);
|
||||
|
||||
assetloaderinput_t input;
|
||||
assetloaderinput_t *inputPtr = NULL;
|
||||
|
||||
if(argc >= 3 && jerry_value_is_number(args[2])) {
|
||||
int32_t inputVal = moduleBaseArgInt(2);
|
||||
switch(type) {
|
||||
case ASSET_LOADER_TYPE_TEXTURE:
|
||||
input.texture = (textureformat_t)inputVal;
|
||||
inputPtr = &input;
|
||||
break;
|
||||
|
||||
case ASSET_LOADER_TYPE_MESH:
|
||||
input.mesh = (assetmeshinputaxis_t)inputVal;
|
||||
inputPtr = &input;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assetentry_t *entry = assetLock(buf, type, inputPtr);
|
||||
if(!entry) return moduleBaseThrow("Asset.lock: failed to lock asset");
|
||||
jsassetentry_t e = { .entry = entry };
|
||||
|
||||
return scriptProtoCreateValue(&MODULE_ASSET_ENTRY_PROTO, &e);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetRequireLoaded) {
|
||||
moduleBaseRequireArgs(1);
|
||||
|
||||
jsassetentry_t *e = (jsassetentry_t *)scriptProtoGetValue(
|
||||
&MODULE_ASSET_ENTRY_PROTO, args[0]
|
||||
);
|
||||
if(!e || !e->entry) {
|
||||
return moduleBaseThrow("Asset.requireLoaded: expected AssetEntry");
|
||||
}
|
||||
errorret_t err = assetRequireLoaded(e->entry);
|
||||
if(errorIsNotOk(err)) return moduleBaseThrowError(err);
|
||||
jerry_value_t ref = jerry_value_copy(args[0]);
|
||||
|
||||
return ref;
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetUnlock) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireString(0);
|
||||
char_t buf[256];
|
||||
moduleBaseToString(args[0], buf, sizeof(buf));
|
||||
assetUnlock(buf);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
static void moduleAssetInit(void) {
|
||||
moduleAssetEntryInit();
|
||||
scriptProtoInit(&MODULE_ASSET_PROTO, "Asset", sizeof(uint8_t), NULL);
|
||||
scriptProtoDefineStaticFunc(&MODULE_ASSET_PROTO, "exists", moduleAssetExists);
|
||||
scriptProtoDefineStaticFunc(&MODULE_ASSET_PROTO, "lock", moduleAssetLock);
|
||||
scriptProtoDefineStaticFunc(&MODULE_ASSET_PROTO, "unlock", moduleAssetUnlock);
|
||||
scriptProtoDefineStaticFunc(&MODULE_ASSET_PROTO, "requireLoaded", moduleAssetRequireLoaded);
|
||||
|
||||
jerry_value_t global = MODULE_ASSET_PROTO.prototype;
|
||||
|
||||
/* Asset.TYPE_* loader type constants */
|
||||
struct { const char_t *name; int val; } types[] = {
|
||||
{ "TYPE_MESH", ASSET_LOADER_TYPE_MESH },
|
||||
{ "TYPE_TEXTURE", ASSET_LOADER_TYPE_TEXTURE },
|
||||
{ "TYPE_TILESET", ASSET_LOADER_TYPE_TILESET },
|
||||
{ "TYPE_LOCALE", ASSET_LOADER_TYPE_LOCALE },
|
||||
{ "TYPE_JSON", ASSET_LOADER_TYPE_JSON },
|
||||
{ "TYPE_SCRIPT", ASSET_LOADER_TYPE_SCRIPT },
|
||||
};
|
||||
for(int i = 0; i < 6; i++) {
|
||||
jerry_value_t k = jerry_string_sz(types[i].name);
|
||||
jerry_value_t v = jerry_number((double)types[i].val);
|
||||
jerry_object_set(global, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
|
||||
/* Asset.MESH_AXIS_* input constants for TYPE_MESH */
|
||||
struct { const char_t *name; int val; } axes[] = {
|
||||
{ "MESH_AXIS_Y_UP", MESH_INPUT_AXIS_Y_UP },
|
||||
{ "MESH_AXIS_Z_UP", MESH_INPUT_AXIS_Z_UP },
|
||||
{ "MESH_AXIS_X_UP", MESH_INPUT_AXIS_X_UP },
|
||||
{ "MESH_AXIS_Y_DOWN", MESH_INPUT_AXIS_Y_DOWN },
|
||||
{ "MESH_AXIS_Z_DOWN", MESH_INPUT_AXIS_Z_DOWN },
|
||||
{ "MESH_AXIS_X_DOWN", MESH_INPUT_AXIS_X_DOWN },
|
||||
};
|
||||
for(int i = 0; i < 6; i++) {
|
||||
jerry_value_t k = jerry_string_sz(axes[i].name);
|
||||
jerry_value_t v = jerry_number((double)axes[i].val);
|
||||
jerry_object_set(global, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleAssetDispose(void) {
|
||||
scriptProtoDispose(&MODULE_ASSET_PROTO);
|
||||
moduleAssetEntryDispose();
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/display/moduletexture.h"
|
||||
#include "asset/asset.h"
|
||||
#include "asset/loader/assetloader.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
static scriptproto_t MODULE_ASSET_ENTRY_PROTO;
|
||||
|
||||
typedef struct {
|
||||
assetentry_t *entry;
|
||||
} jsassetentry_t;
|
||||
|
||||
/** Releases the asset lock when the JS object is GC'd. */
|
||||
static void moduleAssetEntryFree(
|
||||
void *ptr,
|
||||
jerry_object_native_info_t *info
|
||||
) {
|
||||
(void)info;
|
||||
jsassetentry_t *e = (jsassetentry_t *)ptr;
|
||||
if(e && e->entry) {
|
||||
assetUnlockEntry(e->entry);
|
||||
e->entry = NULL;
|
||||
}
|
||||
memoryFree(ptr);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetEntryCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("AssetEntry cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jsassetentry_t *moduleAssetEntrySelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jsassetentry_t *)scriptProtoGetValue(
|
||||
&MODULE_ASSET_ENTRY_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetEntryGetName) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_undefined();
|
||||
return jerry_string_sz(e->entry->name);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetEntryGetState) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_undefined();
|
||||
return jerry_number((double)e->entry->state);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetEntryGetType) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_undefined();
|
||||
return jerry_number((double)e->entry->type);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetEntryGetIsLoaded) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_boolean(false);
|
||||
return jerry_boolean(e->entry->state == ASSET_ENTRY_STATE_LOADED);
|
||||
}
|
||||
|
||||
/* requireLoaded() — blocks until fully loaded, returns this for chaining. */
|
||||
moduleBaseFunction(moduleAssetEntryRequireLoaded) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return moduleBaseThrow("AssetEntry.requireLoaded: invalid entry");
|
||||
errorret_t err = assetRequireLoaded(e->entry);
|
||||
if(errorIsNotOk(err)) return moduleBaseThrowError(err);
|
||||
jerry_value_t self = jerry_value_copy(callInfo->this_value);
|
||||
return self;
|
||||
}
|
||||
|
||||
/*
|
||||
* texture — returns a Texture wrapping this entry's loaded texture data.
|
||||
* Returns undefined if the entry is not a texture or not yet loaded.
|
||||
* Locks the entry a second time so the Texture holds its own independent
|
||||
* reference; the lock is released when the Texture is GC'd.
|
||||
*/
|
||||
moduleBaseFunction(moduleAssetEntryGetTexture) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_undefined();
|
||||
if(e->entry->type != ASSET_LOADER_TYPE_TEXTURE) return jerry_undefined();
|
||||
if(e->entry->state != ASSET_ENTRY_STATE_LOADED) return jerry_undefined();
|
||||
assetEntryLock(e->entry);
|
||||
jstexture_t tex = { .entry = e->entry };
|
||||
return scriptProtoCreateValue(&MODULE_TEXTURE_PROTO, &tex);
|
||||
}
|
||||
|
||||
/* unlock() — releases the lock early; subsequent access is undefined. */
|
||||
moduleBaseFunction(moduleAssetEntryUnlock) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_undefined();
|
||||
assetUnlockEntry(e->entry);
|
||||
e->entry = NULL;
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleAssetEntryToString) {
|
||||
jsassetentry_t *e = moduleAssetEntrySelf(callInfo);
|
||||
if(!e || !e->entry) return jerry_string_sz("AssetEntry:invalid");
|
||||
char_t buf[64];
|
||||
snprintf(buf, sizeof(buf), "AssetEntry(%s)", e->entry->name);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleAssetEntryInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "AssetEntry",
|
||||
sizeof(jsassetentry_t), moduleAssetEntryCtor
|
||||
);
|
||||
MODULE_ASSET_ENTRY_PROTO.info.free_cb = moduleAssetEntryFree;
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "name", moduleAssetEntryGetName, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "state", moduleAssetEntryGetState, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "type", moduleAssetEntryGetType, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "isLoaded", moduleAssetEntryGetIsLoaded, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "texture", moduleAssetEntryGetTexture, NULL
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "requireLoaded", moduleAssetEntryRequireLoaded
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_ASSET_ENTRY_PROTO, "unlock", moduleAssetEntryUnlock
|
||||
);
|
||||
scriptProtoDefineToString(
|
||||
&MODULE_ASSET_ENTRY_PROTO, moduleAssetEntryToString
|
||||
);
|
||||
|
||||
/* State constants */
|
||||
jerry_value_t ctor = MODULE_ASSET_ENTRY_PROTO.constructor;
|
||||
struct { const char_t *name; int val; } states[] = {
|
||||
{ "NOT_STARTED", ASSET_ENTRY_STATE_NOT_STARTED },
|
||||
{ "PENDING", ASSET_ENTRY_STATE_PENDING_ASYNC },
|
||||
{ "LOADING", ASSET_ENTRY_STATE_LOADING_ASYNC },
|
||||
{ "LOADED", ASSET_ENTRY_STATE_LOADED },
|
||||
{ "ERROR", ASSET_ENTRY_STATE_ERROR },
|
||||
};
|
||||
for(int i = 0; i < 5; i++) {
|
||||
jerry_value_t k = jerry_string_sz(states[i].name);
|
||||
jerry_value_t v = jerry_number((double)states[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static void moduleAssetEntryDispose(void) {
|
||||
scriptProtoDispose(&MODULE_ASSET_ENTRY_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "console/console.h"
|
||||
|
||||
static scriptproto_t MODULE_CONSOLE_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleConsolePrint) {
|
||||
char_t buf[512];
|
||||
char_t msg[4096];
|
||||
size_t msgLen = 0;
|
||||
|
||||
for(jerry_length_t i = 0; i < argc; ++i) {
|
||||
jerry_value_t strVal = jerry_value_to_string(args[i]);
|
||||
moduleBaseToString(strVal, buf, sizeof(buf));
|
||||
jerry_value_free(strVal);
|
||||
|
||||
size_t partLen = strlen(buf);
|
||||
if(msgLen + partLen + 1 < sizeof(msg)) {
|
||||
stringCopy(msg + msgLen, buf, sizeof(msg) - msgLen);
|
||||
msgLen += partLen;
|
||||
}
|
||||
|
||||
if(i + 1 < argc && msgLen + 1 < sizeof(msg)) {
|
||||
msg[msgLen++] = '\t';
|
||||
msg[msgLen] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
consolePrint("%s", msg);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleConsoleGetVisible) {
|
||||
return jerry_boolean(CONSOLE.visible);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleConsoleSetVisible) {
|
||||
moduleBaseRequireArgs(1);
|
||||
CONSOLE.visible = moduleBaseArgBool(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
static void moduleConsoleInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_CONSOLE_PROTO, "Console",
|
||||
sizeof(uint8_t), NULL
|
||||
);
|
||||
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_CONSOLE_PROTO, "print", moduleConsolePrint
|
||||
);
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_CONSOLE_PROTO, "visible",
|
||||
moduleConsoleGetVisible, moduleConsoleSetVisible
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleConsoleDispose(void) {
|
||||
scriptProtoDispose(&MODULE_CONSOLE_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "util/memory.h"
|
||||
#include "display/color.h"
|
||||
|
||||
static scriptproto_t MODULE_COLOR_PROTO;
|
||||
|
||||
/**
|
||||
* Returns the native color_t pointer from a Color JS instance.
|
||||
* Returns NULL if the value is not a Color.
|
||||
*/
|
||||
static inline color_t *moduleColorFrom(const jerry_value_t val) {
|
||||
return (color_t *)scriptProtoGetValue(&MODULE_COLOR_PROTO, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Color JS object from a C color_t value.
|
||||
*/
|
||||
static inline jerry_value_t moduleColorPush(const color_t c) {
|
||||
return scriptProtoCreateValue(&MODULE_COLOR_PROTO, &c);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorConstructor) {
|
||||
color_t *ptr = (color_t *)memoryAllocate(sizeof(color_t));
|
||||
ptr->r = (uint8_t)moduleBaseOptInt(0, 0);
|
||||
ptr->g = (uint8_t)moduleBaseOptInt(1, 0);
|
||||
ptr->b = (uint8_t)moduleBaseOptInt(2, 0);
|
||||
ptr->a = (uint8_t)moduleBaseOptInt(3, 255);
|
||||
jerry_object_set_native_ptr(
|
||||
callInfo->this_value, &MODULE_COLOR_PROTO.info, ptr
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorGetR) {
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->r);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorSetR) {
|
||||
moduleBaseRequireArgs(1);
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
c->r = (uint8_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorGetG) {
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->g);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorSetG) {
|
||||
moduleBaseRequireArgs(1);
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
c->g = (uint8_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorGetB) {
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->b);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorSetB) {
|
||||
moduleBaseRequireArgs(1);
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
c->b = (uint8_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorGetA) {
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->a);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorSetA) {
|
||||
moduleBaseRequireArgs(1);
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_undefined();
|
||||
c->a = (uint8_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleColorToString) {
|
||||
color_t *c = moduleColorFrom(callInfo->this_value);
|
||||
if(!c) return jerry_string_sz("Color:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Color(%u,%u,%u,%u)",
|
||||
(unsigned)c->r, (unsigned)c->g,
|
||||
(unsigned)c->b, (unsigned)c->a
|
||||
);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleColorInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_COLOR_PROTO, "Color",
|
||||
sizeof(color_t), moduleColorConstructor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(&MODULE_COLOR_PROTO, "r", moduleColorGetR, moduleColorSetR);
|
||||
scriptProtoDefineProp(&MODULE_COLOR_PROTO, "g", moduleColorGetG, moduleColorSetG);
|
||||
scriptProtoDefineProp(&MODULE_COLOR_PROTO, "b", moduleColorGetB, moduleColorSetB);
|
||||
scriptProtoDefineProp(&MODULE_COLOR_PROTO, "a", moduleColorGetA, moduleColorSetA);
|
||||
scriptProtoDefineToString(&MODULE_COLOR_PROTO, moduleColorToString);
|
||||
|
||||
/* Static named color constants on the constructor. */
|
||||
struct { const char_t *name; color_t val; } constants[] = {
|
||||
{ "WHITE", COLOR_WHITE },
|
||||
{ "BLACK", COLOR_BLACK },
|
||||
{ "RED", COLOR_RED },
|
||||
{ "GREEN", COLOR_GREEN },
|
||||
{ "BLUE", COLOR_BLUE },
|
||||
{ "YELLOW", COLOR_YELLOW },
|
||||
{ "CYAN", COLOR_CYAN },
|
||||
{ "MAGENTA", COLOR_MAGENTA },
|
||||
{ "TRANSPARENT", COLOR_TRANSPARENT },
|
||||
{ "GRAY", COLOR_GRAY },
|
||||
{ "LIGHT_GRAY", COLOR_LIGHT_GRAY },
|
||||
{ "DARK_GRAY", COLOR_DARK_GRAY },
|
||||
{ "ORANGE", COLOR_ORANGE },
|
||||
{ "PURPLE", COLOR_PURPLE },
|
||||
{ "PINK", COLOR_PINK },
|
||||
{ "TEAL", COLOR_TEAL },
|
||||
{ "CORNFLOWER_BLUE", COLOR_CORNFLOWER_BLUE },
|
||||
};
|
||||
jerry_value_t ctor = MODULE_COLOR_PROTO.constructor;
|
||||
for(int i = 0; i < (int)(sizeof(constants)/sizeof(constants[0])); i++) {
|
||||
jerry_value_t k = jerry_string_sz(constants[i].name);
|
||||
jerry_value_t v = moduleColorPush(constants[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleColorDispose(void) {
|
||||
scriptProtoDispose(&MODULE_COLOR_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "display/screen/screen.h"
|
||||
|
||||
static scriptproto_t MODULE_SCREEN_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleScreenGetWidth) {
|
||||
return jerry_number((double)SCREEN.width);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleScreenGetHeight) {
|
||||
return jerry_number((double)SCREEN.height);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleScreenGetAspect) {
|
||||
return jerry_number((double)SCREEN.aspect);
|
||||
}
|
||||
|
||||
static void moduleScreenInit(void) {
|
||||
scriptProtoInit(&MODULE_SCREEN_PROTO, "Screen", sizeof(uint8_t), NULL);
|
||||
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_SCREEN_PROTO, "width",
|
||||
moduleScreenGetWidth, NULL
|
||||
);
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_SCREEN_PROTO, "height",
|
||||
moduleScreenGetHeight, NULL
|
||||
);
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_SCREEN_PROTO, "aspect",
|
||||
moduleScreenGetAspect, NULL
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleScreenDispose(void) {
|
||||
scriptProtoDispose(&MODULE_SCREEN_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "asset/asset.h"
|
||||
#include "asset/loader/assetloader.h"
|
||||
#include "display/texture/texture.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
static scriptproto_t MODULE_TEXTURE_PROTO;
|
||||
|
||||
typedef struct {
|
||||
assetentry_t *entry;
|
||||
} jstexture_t;
|
||||
|
||||
/**
|
||||
* Custom free callback — unlocks the asset entry so it can be reclaimed
|
||||
* once the JS Texture object is garbage collected.
|
||||
*/
|
||||
static void moduleTextureFree(
|
||||
void *ptr,
|
||||
jerry_object_native_info_t *info
|
||||
) {
|
||||
(void)info;
|
||||
jstexture_t *tex = (jstexture_t *)ptr;
|
||||
if(tex && tex->entry) {
|
||||
assetUnlockEntry(tex->entry);
|
||||
tex->entry = NULL;
|
||||
}
|
||||
memoryFree(ptr);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTextureCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Texture cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jstexture_t *moduleTextureSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jstexture_t *)scriptProtoGetValue(
|
||||
&MODULE_TEXTURE_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTextureGetWidth) {
|
||||
jstexture_t *t = moduleTextureSelf(callInfo);
|
||||
if(!t || !t->entry) return jerry_undefined();
|
||||
return jerry_number((double)t->entry->data.texture.width);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTextureGetHeight) {
|
||||
jstexture_t *t = moduleTextureSelf(callInfo);
|
||||
if(!t || !t->entry) return jerry_undefined();
|
||||
return jerry_number((double)t->entry->data.texture.height);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTextureToString) {
|
||||
jstexture_t *t = moduleTextureSelf(callInfo);
|
||||
if(!t || !t->entry) return jerry_string_sz("Texture:invalid");
|
||||
char_t buf[64];
|
||||
snprintf(buf, sizeof(buf), "Texture(%dx%d)",
|
||||
t->entry->data.texture.width,
|
||||
t->entry->data.texture.height
|
||||
);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleTextureInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_TEXTURE_PROTO, "Texture",
|
||||
sizeof(jstexture_t), moduleTextureCtor
|
||||
);
|
||||
/* Override the default free callback so the asset lock is released on GC. */
|
||||
MODULE_TEXTURE_PROTO.info.free_cb = moduleTextureFree;
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_TEXTURE_PROTO, "width", moduleTextureGetWidth, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_TEXTURE_PROTO, "height", moduleTextureGetHeight, NULL
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_TEXTURE_PROTO, moduleTextureToString);
|
||||
|
||||
/* Texture.FORMAT_* constants */
|
||||
jerry_value_t ctor = MODULE_TEXTURE_PROTO.constructor;
|
||||
struct { const char_t *name; int val; } formats[] = {
|
||||
{ "FORMAT_RGBA", TEXTURE_FORMAT_RGBA },
|
||||
{ "FORMAT_PALETTE", TEXTURE_FORMAT_PALETTE },
|
||||
};
|
||||
for(int i = 0; i < 2; i++) {
|
||||
jerry_value_t k = jerry_string_sz(formats[i].name);
|
||||
jerry_value_t v = jerry_number((double)formats[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleTextureDispose(void) {
|
||||
scriptProtoDispose(&MODULE_TEXTURE_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "engine/engine.h"
|
||||
|
||||
static scriptproto_t MODULE_ENGINE_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleEngineGetRunning) {
|
||||
return jerry_boolean(ENGINE.running);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEngineExit) {
|
||||
ENGINE.running = false;
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
static void moduleEngineInit(void) {
|
||||
scriptProtoInit(&MODULE_ENGINE_PROTO, "Engine", sizeof(uint8_t), NULL);
|
||||
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_ENGINE_PROTO, "running",
|
||||
moduleEngineGetRunning, NULL
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_ENGINE_PROTO, "exit", moduleEngineExit
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleEngineDispose(void) {
|
||||
scriptProtoDispose(&MODULE_ENGINE_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/display/entitycamera.h"
|
||||
|
||||
static scriptproto_t MODULE_CAMERA_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleCameraCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Camera cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleCameraSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_CAMERA_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
static inline entitycamera_t *moduleCameraData(const jscomponent_t *c) {
|
||||
return (entitycamera_t *)componentGetData(
|
||||
c->entityId, c->componentId, COMPONENT_TYPE_CAMERA
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraGetEntity) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraGetId) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraGetFov) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->perspective.fov);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraSetFov) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->perspective.fov = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraGetNearClip) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->nearClip);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraSetNearClip) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->nearClip = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraGetFarClip) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->farClip);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraSetFarClip) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->farClip = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraGetProjType) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->projType);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraSetProjType) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitycamera_t *cam = moduleCameraData(c);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->projType = (entitycameraprojectiontype_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleCameraToString) {
|
||||
jscomponent_t *c = moduleCameraSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Camera:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Camera(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleCameraInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_CAMERA_PROTO, "Camera",
|
||||
sizeof(jscomponent_t), moduleCameraCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_CAMERA_PROTO, "entity", moduleCameraGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_CAMERA_PROTO, "id", moduleCameraGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_CAMERA_PROTO, "fov", moduleCameraGetFov, moduleCameraSetFov
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_CAMERA_PROTO, "nearClip",
|
||||
moduleCameraGetNearClip, moduleCameraSetNearClip
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_CAMERA_PROTO, "farClip",
|
||||
moduleCameraGetFarClip, moduleCameraSetFarClip
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_CAMERA_PROTO, "projType",
|
||||
moduleCameraGetProjType, moduleCameraSetProjType
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_CAMERA_PROTO, moduleCameraToString);
|
||||
|
||||
/* Camera.PERSPECTIVE, Camera.PERSPECTIVE_FLIPPED, Camera.ORTHOGRAPHIC */
|
||||
jerry_value_t ctor = MODULE_CAMERA_PROTO.constructor;
|
||||
struct { const char_t *name; int val; } projtypes[] = {
|
||||
{ "PERSPECTIVE", ENTITY_CAMERA_PROJECTION_TYPE_PERSPECTIVE },
|
||||
{ "PERSPECTIVE_FLIPPED", ENTITY_CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED },
|
||||
{ "ORTHOGRAPHIC", ENTITY_CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC },
|
||||
};
|
||||
for(int i = 0; i < 3; i++) {
|
||||
jerry_value_t k = jerry_string_sz(projtypes[i].name);
|
||||
jerry_value_t v = jerry_number((double)projtypes[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleCameraDispose(void) {
|
||||
scriptProtoDispose(&MODULE_CAMERA_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/overworld/entityinteractable.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static scriptproto_t MODULE_INTERACTABLE_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleInteractableCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Interactable cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleInteractableSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_INTERACTABLE_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleInteractableCb(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId,
|
||||
void *user
|
||||
) {
|
||||
(void)entityId; (void)componentId;
|
||||
jerry_value_t fn = *((jerry_value_t *)user);
|
||||
jerry_value_t ret = jerry_call(fn, jerry_undefined(), NULL, 0);
|
||||
jerry_value_free(ret);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInteractableGetEntity) {
|
||||
jscomponent_t *c = moduleInteractableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInteractableGetId) {
|
||||
jscomponent_t *c = moduleInteractableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
/*
|
||||
* onInteract getter — reads back the pinned JS function stored on this._cb.
|
||||
*/
|
||||
moduleBaseFunction(moduleInteractableGetOnInteract) {
|
||||
(void)args; (void)argc;
|
||||
jerry_value_t key = jerry_string_sz("_cb");
|
||||
jerry_value_t val = jerry_object_get(callInfo->this_value, key);
|
||||
jerry_value_free(key);
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* onInteract setter — pins the JS function on this._cb for GC safety, and
|
||||
* registers a C trampoline that calls it when the player interacts.
|
||||
* Passing null/undefined clears the callback.
|
||||
*/
|
||||
moduleBaseFunction(moduleInteractableSetOnInteract) {
|
||||
jscomponent_t *c = moduleInteractableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityinteractable_t *d = entityInteractableGet(c->entityId, c->componentId);
|
||||
if(!d) return jerry_undefined();
|
||||
|
||||
/* Free previously stored JS reference */
|
||||
if(d->user) {
|
||||
jerry_value_free(*((jerry_value_t *)d->user));
|
||||
free(d->user);
|
||||
d->user = NULL;
|
||||
}
|
||||
|
||||
jerry_value_t pin = (argc > 0) ? args[0] : jerry_undefined();
|
||||
jerry_value_t pinKey = jerry_string_sz("_cb");
|
||||
|
||||
if(jerry_value_is_function(pin)) {
|
||||
jerry_value_t *stored = (jerry_value_t *)malloc(sizeof(jerry_value_t));
|
||||
*stored = jerry_value_copy(pin);
|
||||
entityInteractableSetCallback(
|
||||
c->entityId, c->componentId, moduleInteractableCb, stored
|
||||
);
|
||||
jerry_object_set(callInfo->this_value, pinKey, pin);
|
||||
} else {
|
||||
entityInteractableSetCallback(c->entityId, c->componentId, NULL, NULL);
|
||||
jerry_value_t undef = jerry_undefined();
|
||||
jerry_object_set(callInfo->this_value, pinKey, undef);
|
||||
jerry_value_free(undef);
|
||||
}
|
||||
|
||||
jerry_value_free(pinKey);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInteractableTrigger) {
|
||||
(void)args; (void)argc;
|
||||
jscomponent_t *c = moduleInteractableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityInteractableTrigger(c->entityId, c->componentId);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInteractableToString) {
|
||||
jscomponent_t *c = moduleInteractableSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Interactable:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Interactable(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleInteractableInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_INTERACTABLE_PROTO, "Interactable",
|
||||
sizeof(jscomponent_t), moduleInteractableCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_INTERACTABLE_PROTO, "entity", moduleInteractableGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_INTERACTABLE_PROTO, "id", moduleInteractableGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_INTERACTABLE_PROTO, "onInteract",
|
||||
moduleInteractableGetOnInteract, moduleInteractableSetOnInteract
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_INTERACTABLE_PROTO, "trigger", moduleInteractableTrigger
|
||||
);
|
||||
scriptProtoDefineToString(
|
||||
&MODULE_INTERACTABLE_PROTO, moduleInteractableToString
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleInteractableDispose(void) {
|
||||
scriptProtoDispose(&MODULE_INTERACTABLE_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "camera/modulecamera.h"
|
||||
#include "interactable/moduleinteractable.h"
|
||||
#include "overworld/moduleoverworld.h"
|
||||
#include "overworldcamera/moduleoverworldcamera.h"
|
||||
#include "overworldtrigger/moduleoverworldtrigger.h"
|
||||
#include "physics/modulephysics.h"
|
||||
#include "player/moduleplayer.h"
|
||||
#include "position/moduleposition.h"
|
||||
#include "renderable/modulerenderable.h"
|
||||
#include "trigger/moduletrigger.h"
|
||||
|
||||
/**
|
||||
* Returns a typed JS instance for a newly-added component. Falls back to the
|
||||
* generic Component proto for types that have no specific module yet.
|
||||
*/
|
||||
static jerry_value_t moduleComponentListCreateInstance(
|
||||
const componenttype_t type,
|
||||
const jscomponent_t *comp
|
||||
) {
|
||||
switch(type) {
|
||||
case COMPONENT_TYPE_CAMERA:
|
||||
return scriptProtoCreateValue(&MODULE_CAMERA_PROTO, comp);
|
||||
case COMPONENT_TYPE_INTERACTABLE:
|
||||
return scriptProtoCreateValue(&MODULE_INTERACTABLE_PROTO, comp);
|
||||
case COMPONENT_TYPE_OVERWORLD:
|
||||
return scriptProtoCreateValue(&MODULE_OVERWORLD_PROTO, comp);
|
||||
case COMPONENT_TYPE_OVERWORLD_CAMERA:
|
||||
return scriptProtoCreateValue(&MODULE_OVERWORLD_CAMERA_PROTO, comp);
|
||||
case COMPONENT_TYPE_OVERWORLD_TRIGGER:
|
||||
return scriptProtoCreateValue(&MODULE_OVERWORLD_TRIGGER_PROTO, comp);
|
||||
case COMPONENT_TYPE_PHYSICS:
|
||||
return scriptProtoCreateValue(&MODULE_PHYSICS_PROTO, comp);
|
||||
case COMPONENT_TYPE_PLAYER:
|
||||
return scriptProtoCreateValue(&MODULE_PLAYER_PROTO, comp);
|
||||
case COMPONENT_TYPE_POSITION:
|
||||
return scriptProtoCreateValue(&MODULE_POSITION_PROTO, comp);
|
||||
case COMPONENT_TYPE_RENDERABLE:
|
||||
return scriptProtoCreateValue(&MODULE_RENDERABLE_PROTO, comp);
|
||||
case COMPONENT_TYPE_TRIGGER:
|
||||
return scriptProtoCreateValue(&MODULE_TRIGGER_PROTO, comp);
|
||||
default:
|
||||
return scriptProtoCreateValue(&MODULE_COMPONENT_PROTO, comp);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleComponentListInit(void) {
|
||||
moduleCameraInit();
|
||||
moduleInteractableInit();
|
||||
moduleOverworldInit();
|
||||
moduleOverworldCameraInit();
|
||||
moduleOverworldTriggerInit();
|
||||
modulePhysicsInit();
|
||||
modulePlayerInit();
|
||||
modulePositionInit();
|
||||
moduleRenderableInit();
|
||||
moduleTriggerInit();
|
||||
}
|
||||
|
||||
static void moduleComponentListDispose(void) {
|
||||
moduleTriggerDispose();
|
||||
moduleRenderableDispose();
|
||||
modulePositionDispose();
|
||||
modulePlayerDispose();
|
||||
modulePhysicsDispose();
|
||||
moduleOverworldTriggerDispose();
|
||||
moduleOverworldCameraDispose();
|
||||
moduleOverworldDispose();
|
||||
moduleInteractableDispose();
|
||||
moduleCameraDispose();
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/overworld/entityoverworld.h"
|
||||
#include "overworld/facingdir.h"
|
||||
|
||||
static scriptproto_t MODULE_OVERWORLD_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleOverworldCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Overworld cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleOverworldSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_OVERWORLD_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldGetEntity) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldGetId) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldGetType) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworld_t *o = entityOverworldGet(c->entityId, c->componentId);
|
||||
if(!o) return jerry_undefined();
|
||||
return jerry_number((double)o->type);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldSetType) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityOverworldSetType(
|
||||
c->entityId, c->componentId,
|
||||
(entityoverworldtype_t)moduleBaseArgInt(0)
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldGetFacing) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworld_t *o = entityOverworldGet(c->entityId, c->componentId);
|
||||
if(!o) return jerry_undefined();
|
||||
return jerry_number((double)o->facing);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldSetFacing) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworld_t *o = entityOverworldGet(c->entityId, c->componentId);
|
||||
if(!o) return jerry_undefined();
|
||||
o->facing = (facingdir_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldGetRenderCompId) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworld_t *o = entityOverworldGet(c->entityId, c->componentId);
|
||||
if(!o) return jerry_undefined();
|
||||
return jerry_number((double)o->renderCompId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldGetPhysCompId) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworld_t *o = entityOverworldGet(c->entityId, c->componentId);
|
||||
if(!o) return jerry_undefined();
|
||||
return jerry_number((double)o->physCompId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldToString) {
|
||||
jscomponent_t *c = moduleOverworldSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Overworld:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Overworld(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleOverworldInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_OVERWORLD_PROTO, "Overworld",
|
||||
sizeof(jscomponent_t), moduleOverworldCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_PROTO, "entity", moduleOverworldGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_PROTO, "id", moduleOverworldGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_PROTO, "type",
|
||||
moduleOverworldGetType, moduleOverworldSetType
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_PROTO, "facing",
|
||||
moduleOverworldGetFacing, moduleOverworldSetFacing
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_PROTO, "renderComponentId",
|
||||
moduleOverworldGetRenderCompId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_PROTO, "physicsComponentId",
|
||||
moduleOverworldGetPhysCompId, NULL
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_OVERWORLD_PROTO, moduleOverworldToString);
|
||||
|
||||
jerry_value_t ctor = MODULE_OVERWORLD_PROTO.constructor;
|
||||
|
||||
struct { const char_t *name; int val; } types[] = {
|
||||
{ "PLAYER", OVERWORLD_ENTITY_TYPE_PLAYER },
|
||||
{ "NPC", OVERWORLD_ENTITY_TYPE_NPC },
|
||||
};
|
||||
for(int i = 0; i < 2; i++) {
|
||||
jerry_value_t k = jerry_string_sz(types[i].name);
|
||||
jerry_value_t v = jerry_number((double)types[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
|
||||
struct { const char_t *name; int val; } dirs[] = {
|
||||
{ "FACING_DOWN", FACING_DIR_DOWN },
|
||||
{ "FACING_UP", FACING_DIR_UP },
|
||||
{ "FACING_LEFT", FACING_DIR_LEFT },
|
||||
{ "FACING_RIGHT", FACING_DIR_RIGHT },
|
||||
{ "FACING_SOUTH", FACING_DIR_SOUTH },
|
||||
{ "FACING_NORTH", FACING_DIR_NORTH },
|
||||
{ "FACING_WEST", FACING_DIR_WEST },
|
||||
{ "FACING_EAST", FACING_DIR_EAST },
|
||||
};
|
||||
for(int i = 0; i < 8; i++) {
|
||||
jerry_value_t k = jerry_string_sz(dirs[i].name);
|
||||
jerry_value_t v = jerry_number((double)dirs[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleOverworldDispose(void) {
|
||||
scriptProtoDispose(&MODULE_OVERWORLD_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/overworld/entityoverworldcamera.h"
|
||||
|
||||
static scriptproto_t MODULE_OVERWORLD_CAMERA_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("OverworldCamera cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleOverworldCameraSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetEntity) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetId) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetTargetEntity) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->targetEntityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraSetTargetEntity) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->targetEntityId = (entityid_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetTargetPosComp) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->targetPosCompId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraSetTargetPosComp) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->targetPosCompId = (componentid_t)moduleBaseArgInt(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetTargetOffset) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
return moduleVec3Push(cam->targetOffset);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraSetTargetOffset) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("OverworldCamera.targetOffset: expected Vec3");
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
glm_vec3_copy(v, cam->targetOffset);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetEyeOffset) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
return moduleVec3Push(cam->eyeOffset);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraSetEyeOffset) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("OverworldCamera.eyeOffset: expected Vec3");
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
glm_vec3_copy(v, cam->eyeOffset);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraGetScale) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
return jerry_number((double)cam->scale);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraSetScale) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldcamera_t *cam = entityOverworldCameraGet(c->entityId, c->componentId);
|
||||
if(!cam) return jerry_undefined();
|
||||
cam->scale = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraSetTarget) {
|
||||
moduleBaseRequireArgs(2);
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityOverworldCameraSetTarget(
|
||||
c->entityId, c->componentId,
|
||||
(entityid_t)moduleBaseArgInt(0),
|
||||
(componentid_t)moduleBaseArgInt(1)
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldCameraToString) {
|
||||
jscomponent_t *c = moduleOverworldCameraSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("OverworldCamera:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "OverworldCamera(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleOverworldCameraInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "OverworldCamera",
|
||||
sizeof(jscomponent_t), moduleOverworldCameraCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "entity",
|
||||
moduleOverworldCameraGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "id",
|
||||
moduleOverworldCameraGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "targetEntity",
|
||||
moduleOverworldCameraGetTargetEntity, moduleOverworldCameraSetTargetEntity
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "targetPositionComponent",
|
||||
moduleOverworldCameraGetTargetPosComp, moduleOverworldCameraSetTargetPosComp
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "targetOffset",
|
||||
moduleOverworldCameraGetTargetOffset, moduleOverworldCameraSetTargetOffset
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "eyeOffset",
|
||||
moduleOverworldCameraGetEyeOffset, moduleOverworldCameraSetEyeOffset
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "scale",
|
||||
moduleOverworldCameraGetScale, moduleOverworldCameraSetScale
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, "setTarget",
|
||||
moduleOverworldCameraSetTarget
|
||||
);
|
||||
scriptProtoDefineToString(
|
||||
&MODULE_OVERWORLD_CAMERA_PROTO, moduleOverworldCameraToString
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleOverworldCameraDispose(void) {
|
||||
scriptProtoDispose(&MODULE_OVERWORLD_CAMERA_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,374 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/overworld/entityoverworldtrigger.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static scriptproto_t MODULE_OVERWORLD_TRIGGER_PROTO;
|
||||
|
||||
/**
|
||||
* Heap-allocated struct stored in entityoverworldtrigger_t.user.
|
||||
* Holds jerry_value_t copies of the four JS callback functions.
|
||||
*/
|
||||
typedef struct {
|
||||
jerry_value_t onEnter;
|
||||
jerry_value_t onExit;
|
||||
jerry_value_t onStay;
|
||||
jerry_value_t onOutside;
|
||||
} jsoverworldtriggercbs_t;
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("OverworldTrigger cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleOverworldTriggerSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
/** Lazily allocates the callback struct and stores it in the trigger's user. */
|
||||
static jsoverworldtriggercbs_t *moduleOverworldTriggerGetCbs(
|
||||
entityoverworldtrigger_t *t
|
||||
) {
|
||||
if(t->user) return (jsoverworldtriggercbs_t *)t->user;
|
||||
jsoverworldtriggercbs_t *cbs = (jsoverworldtriggercbs_t *)malloc(
|
||||
sizeof(jsoverworldtriggercbs_t)
|
||||
);
|
||||
cbs->onEnter = jerry_undefined();
|
||||
cbs->onExit = jerry_undefined();
|
||||
cbs->onStay = jerry_undefined();
|
||||
cbs->onOutside = jerry_undefined();
|
||||
t->user = (void *)cbs;
|
||||
return cbs;
|
||||
}
|
||||
|
||||
static void moduleOverworldTriggerOnEnterCb(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId,
|
||||
void *user
|
||||
) {
|
||||
(void)entityId; (void)componentId;
|
||||
jsoverworldtriggercbs_t *cbs = (jsoverworldtriggercbs_t *)user;
|
||||
if(!jerry_value_is_function(cbs->onEnter)) return;
|
||||
jerry_value_t ret = jerry_call(cbs->onEnter, jerry_undefined(), NULL, 0);
|
||||
jerry_value_free(ret);
|
||||
}
|
||||
|
||||
static void moduleOverworldTriggerOnExitCb(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId,
|
||||
void *user
|
||||
) {
|
||||
(void)entityId; (void)componentId;
|
||||
jsoverworldtriggercbs_t *cbs = (jsoverworldtriggercbs_t *)user;
|
||||
if(!jerry_value_is_function(cbs->onExit)) return;
|
||||
jerry_value_t ret = jerry_call(cbs->onExit, jerry_undefined(), NULL, 0);
|
||||
jerry_value_free(ret);
|
||||
}
|
||||
|
||||
static void moduleOverworldTriggerOnStayCb(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId,
|
||||
void *user
|
||||
) {
|
||||
(void)entityId; (void)componentId;
|
||||
jsoverworldtriggercbs_t *cbs = (jsoverworldtriggercbs_t *)user;
|
||||
if(!jerry_value_is_function(cbs->onStay)) return;
|
||||
jerry_value_t ret = jerry_call(cbs->onStay, jerry_undefined(), NULL, 0);
|
||||
jerry_value_free(ret);
|
||||
}
|
||||
|
||||
static void moduleOverworldTriggerOnOutsideCb(
|
||||
const entityid_t entityId,
|
||||
const componentid_t componentId,
|
||||
void *user
|
||||
) {
|
||||
(void)entityId; (void)componentId;
|
||||
jsoverworldtriggercbs_t *cbs = (jsoverworldtriggercbs_t *)user;
|
||||
if(!jerry_value_is_function(cbs->onOutside)) return;
|
||||
jerry_value_t ret = jerry_call(cbs->onOutside, jerry_undefined(), NULL, 0);
|
||||
jerry_value_free(ret);
|
||||
}
|
||||
|
||||
// ---- entity / id ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetEntity) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetId) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
// ---- min / max ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetMin) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
return moduleVec3Push(t->min);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetMin) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("OverworldTrigger.min: expected Vec3");
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
glm_vec3_copy(v, t->min);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetMax) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
return moduleVec3Push(t->max);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetMax) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("OverworldTrigger.max: expected Vec3");
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
glm_vec3_copy(v, t->max);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
// ---- playerInside ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetPlayerInside) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
return jerry_boolean(t->playerInside);
|
||||
}
|
||||
|
||||
// ---- setBounds ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetBounds) {
|
||||
moduleBaseRequireArgs(2);
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *minV = moduleVec3From(args[0]);
|
||||
float_t *maxV = moduleVec3From(args[1]);
|
||||
if(!minV) return moduleBaseThrow("OverworldTrigger.setBounds: expected Vec3 for min");
|
||||
if(!maxV) return moduleBaseThrow("OverworldTrigger.setBounds: expected Vec3 for max");
|
||||
entityOverworldTriggerSetBounds(c->entityId, c->componentId, minV, maxV);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
// ---- callback helpers ----
|
||||
|
||||
/*
|
||||
* Pins the JS function on this._onXxx for GC safety, stores a copied
|
||||
* jerry_value_t reference in the cbs struct for the C trampoline, and
|
||||
* wires up the C callback pointer. Passing null/undefined clears it.
|
||||
*/
|
||||
static void moduleOverworldTriggerSetCb(
|
||||
const jerry_call_info_t *callInfo,
|
||||
const jerry_value_t *newArgs,
|
||||
const jerry_length_t newArgc,
|
||||
jerry_value_t *slot,
|
||||
entityoverworldtriggercallback_t *cslot,
|
||||
entityoverworldtriggercallback_t trampolineFn,
|
||||
const char_t *pinKey
|
||||
) {
|
||||
jerry_value_free(*slot);
|
||||
jerry_value_t pin = (newArgc > 0) ? newArgs[0] : jerry_undefined();
|
||||
jerry_value_t keyVal = jerry_string_sz(pinKey);
|
||||
if(jerry_value_is_function(pin)) {
|
||||
*slot = jerry_value_copy(pin);
|
||||
*cslot = trampolineFn;
|
||||
jerry_object_set(callInfo->this_value, keyVal, pin);
|
||||
} else {
|
||||
*slot = jerry_undefined();
|
||||
*cslot = NULL;
|
||||
jerry_value_t undef = jerry_undefined();
|
||||
jerry_object_set(callInfo->this_value, keyVal, undef);
|
||||
jerry_value_free(undef);
|
||||
}
|
||||
jerry_value_free(keyVal);
|
||||
}
|
||||
|
||||
static jerry_value_t moduleOverworldTriggerGetPinnedCb(
|
||||
const jerry_call_info_t *callInfo,
|
||||
const char_t *pinKey
|
||||
) {
|
||||
jerry_value_t key = jerry_string_sz(pinKey);
|
||||
jerry_value_t val = jerry_object_get(callInfo->this_value, key);
|
||||
jerry_value_free(key);
|
||||
return val;
|
||||
}
|
||||
|
||||
// ---- onEnter ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetOnEnter) {
|
||||
(void)args; (void)argc;
|
||||
return moduleOverworldTriggerGetPinnedCb(callInfo, "_onEnter");
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetOnEnter) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
jsoverworldtriggercbs_t *cbs = moduleOverworldTriggerGetCbs(t);
|
||||
moduleOverworldTriggerSetCb(
|
||||
callInfo, args, argc,
|
||||
&cbs->onEnter, &t->onEnter,
|
||||
moduleOverworldTriggerOnEnterCb, "_onEnter"
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
// ---- onExit ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetOnExit) {
|
||||
(void)args; (void)argc;
|
||||
return moduleOverworldTriggerGetPinnedCb(callInfo, "_onExit");
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetOnExit) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
jsoverworldtriggercbs_t *cbs = moduleOverworldTriggerGetCbs(t);
|
||||
moduleOverworldTriggerSetCb(
|
||||
callInfo, args, argc,
|
||||
&cbs->onExit, &t->onExit,
|
||||
moduleOverworldTriggerOnExitCb, "_onExit"
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
// ---- onStay ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetOnStay) {
|
||||
(void)args; (void)argc;
|
||||
return moduleOverworldTriggerGetPinnedCb(callInfo, "_onStay");
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetOnStay) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
jsoverworldtriggercbs_t *cbs = moduleOverworldTriggerGetCbs(t);
|
||||
moduleOverworldTriggerSetCb(
|
||||
callInfo, args, argc,
|
||||
&cbs->onStay, &t->onStay,
|
||||
moduleOverworldTriggerOnStayCb, "_onStay"
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
// ---- onOutside ----
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerGetOnOutside) {
|
||||
(void)args; (void)argc;
|
||||
return moduleOverworldTriggerGetPinnedCb(callInfo, "_onOutside");
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerSetOnOutside) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityoverworldtrigger_t *t = entityOverworldTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
jsoverworldtriggercbs_t *cbs = moduleOverworldTriggerGetCbs(t);
|
||||
moduleOverworldTriggerSetCb(
|
||||
callInfo, args, argc,
|
||||
&cbs->onOutside, &t->onOutside,
|
||||
moduleOverworldTriggerOnOutsideCb, "_onOutside"
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleOverworldTriggerToString) {
|
||||
jscomponent_t *c = moduleOverworldTriggerSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("OverworldTrigger:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "OverworldTrigger(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleOverworldTriggerInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "OverworldTrigger",
|
||||
sizeof(jscomponent_t), moduleOverworldTriggerCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "entity",
|
||||
moduleOverworldTriggerGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "id",
|
||||
moduleOverworldTriggerGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "min",
|
||||
moduleOverworldTriggerGetMin, moduleOverworldTriggerSetMin
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "max",
|
||||
moduleOverworldTriggerGetMax, moduleOverworldTriggerSetMax
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "playerInside",
|
||||
moduleOverworldTriggerGetPlayerInside, NULL
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "setBounds",
|
||||
moduleOverworldTriggerSetBounds
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "onEnter",
|
||||
moduleOverworldTriggerGetOnEnter, moduleOverworldTriggerSetOnEnter
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "onExit",
|
||||
moduleOverworldTriggerGetOnExit, moduleOverworldTriggerSetOnExit
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "onStay",
|
||||
moduleOverworldTriggerGetOnStay, moduleOverworldTriggerSetOnStay
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, "onOutside",
|
||||
moduleOverworldTriggerGetOnOutside, moduleOverworldTriggerSetOnOutside
|
||||
);
|
||||
scriptProtoDefineToString(
|
||||
&MODULE_OVERWORLD_TRIGGER_PROTO, moduleOverworldTriggerToString
|
||||
);
|
||||
}
|
||||
|
||||
static void moduleOverworldTriggerDispose(void) {
|
||||
scriptProtoDispose(&MODULE_OVERWORLD_TRIGGER_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/physics/entityphysics.h"
|
||||
|
||||
static scriptproto_t MODULE_PHYSICS_PROTO;
|
||||
|
||||
moduleBaseFunction(modulePhysicsCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Physics cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *modulePhysicsSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_PHYSICS_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetEntity) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetId) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetBodyType) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)entityPhysicsGetBodyType(c->entityId, c->componentId));
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsSetBodyType) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityPhysicsSetBodyType(
|
||||
c->entityId, c->componentId,
|
||||
(physicsbodytype_t)moduleBaseArgInt(0)
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetShape) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)entityPhysicsGetShape(c->entityId, c->componentId).type);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsSetShape) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
physicsshape_t shape = entityPhysicsGetShape(c->entityId, c->componentId);
|
||||
shape.type = (physicshapetype_t)moduleBaseArgInt(0);
|
||||
entityPhysicsSetShape(c->entityId, c->componentId, shape);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetVelocity) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPhysicsGetVelocity(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsSetVelocity) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Physics.velocity: expected Vec3");
|
||||
entityPhysicsSetVelocity(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetGravityScale) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityphysics_t *p = entityPhysicsGet(c->entityId, c->componentId);
|
||||
if(!p) return jerry_undefined();
|
||||
return jerry_number((double)p->gravityScale);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsSetGravityScale) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityphysics_t *p = entityPhysicsGet(c->entityId, c->componentId);
|
||||
if(!p) return jerry_undefined();
|
||||
p->gravityScale = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsGetOnGround) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_boolean(entityPhysicsIsOnGround(c->entityId, c->componentId));
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsApplyImpulse) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Physics.applyImpulse: expected Vec3");
|
||||
entityPhysicsApplyImpulse(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePhysicsToString) {
|
||||
jscomponent_t *c = modulePhysicsSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Physics:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Physics(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void modulePhysicsInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_PHYSICS_PROTO, "Physics",
|
||||
sizeof(jscomponent_t), modulePhysicsCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "entity", modulePhysicsGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "id", modulePhysicsGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "bodyType",
|
||||
modulePhysicsGetBodyType, modulePhysicsSetBodyType
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "shape",
|
||||
modulePhysicsGetShape, modulePhysicsSetShape
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "velocity",
|
||||
modulePhysicsGetVelocity, modulePhysicsSetVelocity
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "gravityScale",
|
||||
modulePhysicsGetGravityScale, modulePhysicsSetGravityScale
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PHYSICS_PROTO, "onGround", modulePhysicsGetOnGround, NULL
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_PHYSICS_PROTO, "applyImpulse", modulePhysicsApplyImpulse
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_PHYSICS_PROTO, modulePhysicsToString);
|
||||
|
||||
/* Body type constants */
|
||||
jerry_value_t ctor = MODULE_PHYSICS_PROTO.constructor;
|
||||
struct { const char_t *name; int val; } bodyTypes[] = {
|
||||
{ "STATIC", PHYSICS_BODY_STATIC },
|
||||
{ "DYNAMIC", PHYSICS_BODY_DYNAMIC },
|
||||
{ "KINEMATIC", PHYSICS_BODY_KINEMATIC },
|
||||
};
|
||||
for(int i = 0; i < 3; i++) {
|
||||
jerry_value_t k = jerry_string_sz(bodyTypes[i].name);
|
||||
jerry_value_t v = jerry_number((double)bodyTypes[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
|
||||
/* Shape type constants */
|
||||
struct { const char_t *name; int val; } shapes[] = {
|
||||
{ "SHAPE_CUBE", PHYSICS_SHAPE_CUBE },
|
||||
{ "SHAPE_SPHERE", PHYSICS_SHAPE_SPHERE },
|
||||
{ "SHAPE_CAPSULE", PHYSICS_SHAPE_CAPSULE },
|
||||
{ "SHAPE_PLANE", PHYSICS_SHAPE_PLANE },
|
||||
};
|
||||
for(int i = 0; i < 4; i++) {
|
||||
jerry_value_t k = jerry_string_sz(shapes[i].name);
|
||||
jerry_value_t v = jerry_number((double)shapes[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void modulePhysicsDispose(void) {
|
||||
scriptProtoDispose(&MODULE_PHYSICS_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/overworld/entityplayer.h"
|
||||
|
||||
static scriptproto_t MODULE_PLAYER_PROTO;
|
||||
|
||||
moduleBaseFunction(modulePlayerCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Player cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *modulePlayerSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_PLAYER_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerGetEntity) {
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerGetId) {
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerGetSpeed) {
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityplayer_t *p = entityPlayerGet(c->entityId, c->componentId);
|
||||
if(!p) return jerry_undefined();
|
||||
return jerry_number((double)p->speed);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerSetSpeed) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityplayer_t *p = entityPlayerGet(c->entityId, c->componentId);
|
||||
if(!p) return jerry_undefined();
|
||||
p->speed = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerGetRunSpeed) {
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityplayer_t *p = entityPlayerGet(c->entityId, c->componentId);
|
||||
if(!p) return jerry_undefined();
|
||||
return jerry_number((double)p->runSpeed);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerSetRunSpeed) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityplayer_t *p = entityPlayerGet(c->entityId, c->componentId);
|
||||
if(!p) return jerry_undefined();
|
||||
p->runSpeed = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePlayerToString) {
|
||||
jscomponent_t *c = modulePlayerSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Player:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Player(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void modulePlayerInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_PLAYER_PROTO, "Player",
|
||||
sizeof(jscomponent_t), modulePlayerCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PLAYER_PROTO, "entity", modulePlayerGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PLAYER_PROTO, "id", modulePlayerGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PLAYER_PROTO, "speed",
|
||||
modulePlayerGetSpeed, modulePlayerSetSpeed
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_PLAYER_PROTO, "runSpeed",
|
||||
modulePlayerGetRunSpeed, modulePlayerSetRunSpeed
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_PLAYER_PROTO, modulePlayerToString);
|
||||
}
|
||||
|
||||
static void modulePlayerDispose(void) {
|
||||
scriptProtoDispose(&MODULE_PLAYER_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/display/entityposition.h"
|
||||
|
||||
static scriptproto_t MODULE_POSITION_PROTO;
|
||||
|
||||
moduleBaseFunction(modulePositionCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Position cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *modulePositionSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_POSITION_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetEntity) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetId) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetLocalPos) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPositionGetLocalPosition(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetLocalPos) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Position.localPosition: expected Vec3");
|
||||
entityPositionSetLocalPosition(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetWorldPos) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPositionGetWorldPosition(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetWorldPos) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Position.worldPosition: expected Vec3");
|
||||
entityPositionSetWorldPosition(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetLocalRot) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPositionGetLocalRotation(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetLocalRot) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Position.localRotation: expected Vec3");
|
||||
entityPositionSetLocalRotation(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetWorldRot) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPositionGetWorldRotation(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetWorldRot) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Position.worldRotation: expected Vec3");
|
||||
entityPositionSetWorldRotation(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetLocalScale) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPositionGetLocalScale(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetLocalScale) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Position.localScale: expected Vec3");
|
||||
entityPositionSetLocalScale(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionGetWorldScale) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
vec3 v;
|
||||
entityPositionGetWorldScale(c->entityId, c->componentId, v);
|
||||
return moduleVec3Push(v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetWorldScale) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Position.worldScale: expected Vec3");
|
||||
entityPositionSetWorldScale(c->entityId, c->componentId, v);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionLookAt) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
moduleBaseRequireArgs(1);
|
||||
float_t *target = moduleVec3From(args[0]);
|
||||
if(!target) return moduleBaseThrow("Position.lookAt: expected Vec3 target");
|
||||
|
||||
vec3 eye;
|
||||
entityPositionGetLocalPosition(c->entityId, c->componentId, eye);
|
||||
|
||||
vec3 up = { 0.0f, 1.0f, 0.0f };
|
||||
if(argc >= 2) {
|
||||
float_t *upArg = moduleVec3From(args[1]);
|
||||
if(upArg) glm_vec3_copy(upArg, up);
|
||||
}
|
||||
|
||||
entityPositionLookAt(c->entityId, c->componentId, eye, target, up);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionSetParent) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
if(argc == 0 ||
|
||||
jerry_value_is_null(args[0]) ||
|
||||
jerry_value_is_undefined(args[0])) {
|
||||
entityPositionSetParent(
|
||||
c->entityId, c->componentId,
|
||||
ENTITY_ID_INVALID, COMPONENT_ID_INVALID
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
jscomponent_t *parent = (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_POSITION_PROTO, args[0]
|
||||
);
|
||||
if(!parent) return moduleBaseThrow("Position.setParent: expected Position or null");
|
||||
entityPositionSetParent(
|
||||
c->entityId, c->componentId,
|
||||
parent->entityId, parent->componentId
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(modulePositionToString) {
|
||||
jscomponent_t *c = modulePositionSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Position:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Position(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void modulePositionInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_POSITION_PROTO, "Position",
|
||||
sizeof(jscomponent_t), modulePositionCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "entity", modulePositionGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "id", modulePositionGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "localPosition",
|
||||
modulePositionGetLocalPos, modulePositionSetLocalPos
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "worldPosition",
|
||||
modulePositionGetWorldPos, modulePositionSetWorldPos
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "localRotation",
|
||||
modulePositionGetLocalRot, modulePositionSetLocalRot
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "worldRotation",
|
||||
modulePositionGetWorldRot, modulePositionSetWorldRot
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "localScale",
|
||||
modulePositionGetLocalScale, modulePositionSetLocalScale
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_POSITION_PROTO, "worldScale",
|
||||
modulePositionGetWorldScale, modulePositionSetWorldScale
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_POSITION_PROTO, "lookAt", modulePositionLookAt
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_POSITION_PROTO, "setParent", modulePositionSetParent
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_POSITION_PROTO, modulePositionToString);
|
||||
}
|
||||
|
||||
static void modulePositionDispose(void) {
|
||||
scriptProtoDispose(&MODULE_POSITION_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/module/display/modulecolor.h"
|
||||
#include "script/module/display/moduletexture.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "entity/component/display/entityrenderable.h"
|
||||
|
||||
static scriptproto_t MODULE_RENDERABLE_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleRenderableCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Renderable cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleRenderableSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_RENDERABLE_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
static inline entityrenderable_t *moduleRenderableData(const jscomponent_t *c) {
|
||||
return (entityrenderable_t *)componentGetData(
|
||||
c->entityId, c->componentId, COMPONENT_TYPE_RENDERABLE
|
||||
);
|
||||
}
|
||||
|
||||
/** Read a float from a JS array at index, returning def if out of range. */
|
||||
static inline float_t moduleRenderableArrayFloat(
|
||||
const jerry_value_t arr,
|
||||
const uint32_t idx,
|
||||
const float_t def
|
||||
) {
|
||||
if(idx >= jerry_array_length(arr)) return def;
|
||||
jerry_value_t v = jerry_object_get_index(arr, idx);
|
||||
float_t f = jerry_value_is_number(v) ? (float_t)jerry_value_as_number(v) : def;
|
||||
jerry_value_free(v);
|
||||
return f;
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableGetEntity) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableGetId) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableGetType) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
return jerry_number((double)r->type);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableSetType) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityRenderableSetType(
|
||||
c->entityId, c->componentId,
|
||||
(entityrenderabletype_t)moduleBaseArgInt(0)
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableGetPriority) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
return jerry_number((double)r->priority);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableSetPriority) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityRenderableSetPriority(
|
||||
c->entityId, c->componentId,
|
||||
(int8_t)moduleBaseArgInt(0)
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableGetColor) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
return moduleColorPush(r->data.material.material.unlit.color);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableSetColor) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
color_t *col = moduleColorFrom(args[0]);
|
||||
if(!col) return moduleBaseThrow("Renderable.color: expected Color");
|
||||
r->data.material.material.unlit.color = *col;
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
/*
|
||||
* texture getter — returns the pinned Texture instance, or undefined if none.
|
||||
*/
|
||||
moduleBaseFunction(moduleRenderableGetTexture) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
jerry_value_t key = jerry_string_sz("_tex");
|
||||
jerry_value_t val = jerry_object_get(callInfo->this_value, key);
|
||||
jerry_value_free(key);
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
* texture setter — switches to SPRITEBATCH, binds the texture, and pins the
|
||||
* Texture JS object so GC won't free the asset while the pointer is live.
|
||||
*/
|
||||
moduleBaseFunction(moduleRenderableSetTexture) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
jstexture_t *tex = (jstexture_t *)scriptProtoGetValue(
|
||||
&MODULE_TEXTURE_PROTO, args[0]
|
||||
);
|
||||
if(!tex || !tex->entry) {
|
||||
return moduleBaseThrow("Renderable.texture: expected Texture");
|
||||
}
|
||||
r->type = ENTITY_RENDERABLE_TYPE_SPRITEBATCH;
|
||||
r->data.spritebatch.texture = &tex->entry->data.texture;
|
||||
jerry_value_t pinKey = jerry_string_sz("_tex");
|
||||
jerry_object_set(callInfo->this_value, pinKey, args[0]);
|
||||
jerry_value_free(pinKey);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
/*
|
||||
* sprites getter — returns a JS array of sprite sub-arrays.
|
||||
* Each element is [x1,y1,z1, x2,y2,z2, u1,v1, u2,v2] (10 numbers).
|
||||
*/
|
||||
moduleBaseFunction(moduleRenderableGetSprites) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
const entityrenderablespritebatch_t *sb = &r->data.spritebatch;
|
||||
|
||||
jerry_value_t arr = jerry_array((uint32_t)sb->spriteCount);
|
||||
for(uint32_t i = 0; i < (uint32_t)sb->spriteCount; i++) {
|
||||
const spritebatchsprite_t *s = &sb->sprites[i];
|
||||
float_t vals[10] = {
|
||||
s->min[0], s->min[1], s->min[2],
|
||||
s->max[0], s->max[1], s->max[2],
|
||||
s->uvMin[0], s->uvMin[1],
|
||||
s->uvMax[0], s->uvMax[1],
|
||||
};
|
||||
jerry_value_t sprite = jerry_array(10);
|
||||
for(uint32_t j = 0; j < 10; j++) {
|
||||
jerry_value_t num = jerry_number((double)vals[j]);
|
||||
jerry_object_set_index(sprite, j, num);
|
||||
jerry_value_free(num);
|
||||
}
|
||||
jerry_object_set_index(arr, i, sprite);
|
||||
jerry_value_free(sprite);
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
/*
|
||||
* sprites setter — accepts an array of sub-arrays.
|
||||
* Each element: 10 numbers (3D) or 8 numbers (2D, z defaults to 0).
|
||||
*/
|
||||
moduleBaseFunction(moduleRenderableSetSprites) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entityrenderable_t *r = moduleRenderableData(c);
|
||||
if(!r) return jerry_undefined();
|
||||
if(!jerry_value_is_array(args[0])) {
|
||||
return moduleBaseThrow("Renderable.sprites: expected Array");
|
||||
}
|
||||
entityrenderablespritebatch_t *sb = &r->data.spritebatch;
|
||||
uint32_t count = jerry_array_length(args[0]);
|
||||
if(count > ENTITY_RENDERABLE_SPRITEBATCH_SPRITES_MAX) {
|
||||
return moduleBaseThrow("Renderable.sprites: exceeds sprite capacity");
|
||||
}
|
||||
sb->spriteCount = 0;
|
||||
for(uint32_t i = 0; i < count; i++) {
|
||||
jerry_value_t elem = jerry_object_get_index(args[0], i);
|
||||
if(!jerry_value_is_array(elem)) {
|
||||
jerry_value_free(elem);
|
||||
return moduleBaseThrow("Renderable.sprites: each element must be an Array");
|
||||
}
|
||||
spritebatchsprite_t s;
|
||||
if(jerry_array_length(elem) >= 10) {
|
||||
s.min[0] = moduleRenderableArrayFloat(elem, 0, 0.0f);
|
||||
s.min[1] = moduleRenderableArrayFloat(elem, 1, 0.0f);
|
||||
s.min[2] = moduleRenderableArrayFloat(elem, 2, 0.0f);
|
||||
s.max[0] = moduleRenderableArrayFloat(elem, 3, 0.0f);
|
||||
s.max[1] = moduleRenderableArrayFloat(elem, 4, 0.0f);
|
||||
s.max[2] = moduleRenderableArrayFloat(elem, 5, 0.0f);
|
||||
s.uvMin[0] = moduleRenderableArrayFloat(elem, 6, 0.0f);
|
||||
s.uvMin[1] = moduleRenderableArrayFloat(elem, 7, 0.0f);
|
||||
s.uvMax[0] = moduleRenderableArrayFloat(elem, 8, 1.0f);
|
||||
s.uvMax[1] = moduleRenderableArrayFloat(elem, 9, 1.0f);
|
||||
} else {
|
||||
s.min[0] = moduleRenderableArrayFloat(elem, 0, 0.0f);
|
||||
s.min[1] = moduleRenderableArrayFloat(elem, 1, 0.0f);
|
||||
s.min[2] = 0.0f;
|
||||
s.max[0] = moduleRenderableArrayFloat(elem, 2, 0.0f);
|
||||
s.max[1] = moduleRenderableArrayFloat(elem, 3, 0.0f);
|
||||
s.max[2] = 0.0f;
|
||||
s.uvMin[0] = moduleRenderableArrayFloat(elem, 4, 0.0f);
|
||||
s.uvMin[1] = moduleRenderableArrayFloat(elem, 5, 0.0f);
|
||||
s.uvMax[0] = moduleRenderableArrayFloat(elem, 6, 1.0f);
|
||||
s.uvMax[1] = moduleRenderableArrayFloat(elem, 7, 1.0f);
|
||||
}
|
||||
jerry_value_free(elem);
|
||||
sb->sprites[sb->spriteCount++] = s;
|
||||
}
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleRenderableToString) {
|
||||
jscomponent_t *c = moduleRenderableSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Renderable:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Renderable(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleRenderableInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_RENDERABLE_PROTO, "Renderable",
|
||||
sizeof(jscomponent_t), moduleRenderableCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "entity", moduleRenderableGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "id", moduleRenderableGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "type",
|
||||
moduleRenderableGetType, moduleRenderableSetType
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "priority",
|
||||
moduleRenderableGetPriority, moduleRenderableSetPriority
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "color",
|
||||
moduleRenderableGetColor, moduleRenderableSetColor
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "texture",
|
||||
moduleRenderableGetTexture, moduleRenderableSetTexture
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_RENDERABLE_PROTO, "sprites",
|
||||
moduleRenderableGetSprites, moduleRenderableSetSprites
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_RENDERABLE_PROTO, moduleRenderableToString);
|
||||
|
||||
/* Renderable.SHADER_MATERIAL, .SPRITEBATCH, .CUSTOM */
|
||||
jerry_value_t ctor = MODULE_RENDERABLE_PROTO.constructor;
|
||||
struct { const char_t *name; int val; } types[] = {
|
||||
{ "SHADER_MATERIAL", ENTITY_RENDERABLE_TYPE_SHADER_MATERIAL },
|
||||
{ "SPRITEBATCH", ENTITY_RENDERABLE_TYPE_SPRITEBATCH },
|
||||
{ "CUSTOM", ENTITY_RENDERABLE_TYPE_CUSTOM },
|
||||
};
|
||||
for(int i = 0; i < 3; i++) {
|
||||
jerry_value_t k = jerry_string_sz(types[i].name);
|
||||
jerry_value_t v = jerry_number((double)types[i].val);
|
||||
jerry_object_set(ctor, k, v);
|
||||
jerry_value_free(v);
|
||||
jerry_value_free(k);
|
||||
}
|
||||
}
|
||||
|
||||
static void moduleRenderableDispose(void) {
|
||||
scriptProtoDispose(&MODULE_RENDERABLE_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "entity/component/trigger/entitytrigger.h"
|
||||
|
||||
static scriptproto_t MODULE_TRIGGER_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleTriggerCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Trigger cannot be instantiated with new");
|
||||
}
|
||||
|
||||
static inline jscomponent_t *moduleTriggerSelf(
|
||||
const jerry_call_info_t *callInfo
|
||||
) {
|
||||
return (jscomponent_t *)scriptProtoGetValue(
|
||||
&MODULE_TRIGGER_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerGetEntity) {
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerGetId) {
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
return jerry_number((double)c->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerGetMin) {
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
return moduleVec3Push(t->min);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerSetMin) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Trigger.min: expected Vec3");
|
||||
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
glm_vec3_copy(v, t->min);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerGetMax) {
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
return moduleVec3Push(t->max);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerSetMax) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Trigger.max: expected Vec3");
|
||||
entitytrigger_t *t = entityTriggerGet(c->entityId, c->componentId);
|
||||
if(!t) return jerry_undefined();
|
||||
glm_vec3_copy(v, t->max);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerSetBounds) {
|
||||
moduleBaseRequireArgs(2);
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *minV = moduleVec3From(args[0]);
|
||||
float_t *maxV = moduleVec3From(args[1]);
|
||||
if(!minV) return moduleBaseThrow("Trigger.setBounds: expected Vec3 for min");
|
||||
if(!maxV) return moduleBaseThrow("Trigger.setBounds: expected Vec3 for max");
|
||||
entityTriggerSetBounds(c->entityId, c->componentId, minV, maxV);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerContains) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_undefined();
|
||||
float_t *v = moduleVec3From(args[0]);
|
||||
if(!v) return moduleBaseThrow("Trigger.contains: expected Vec3");
|
||||
return jerry_boolean(entityTriggerContains(c->entityId, c->componentId, v));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleTriggerToString) {
|
||||
jscomponent_t *c = moduleTriggerSelf(callInfo);
|
||||
if(!c) return jerry_string_sz("Trigger:invalid");
|
||||
char_t buf[32];
|
||||
snprintf(buf, sizeof(buf), "Trigger(%u)", (unsigned)c->componentId);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleTriggerInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_TRIGGER_PROTO, "Trigger",
|
||||
sizeof(jscomponent_t), moduleTriggerCtor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_TRIGGER_PROTO, "entity", moduleTriggerGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_TRIGGER_PROTO, "id", moduleTriggerGetId, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_TRIGGER_PROTO, "min", moduleTriggerGetMin, moduleTriggerSetMin
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_TRIGGER_PROTO, "max", moduleTriggerGetMax, moduleTriggerSetMax
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_TRIGGER_PROTO, "setBounds", moduleTriggerSetBounds
|
||||
);
|
||||
scriptProtoDefineFunc(
|
||||
&MODULE_TRIGGER_PROTO, "contains", moduleTriggerContains
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_TRIGGER_PROTO, moduleTriggerToString);
|
||||
}
|
||||
|
||||
static void moduleTriggerDispose(void) {
|
||||
scriptProtoDispose(&MODULE_TRIGGER_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "entity/component.h"
|
||||
|
||||
/** C struct wrapped by every Component JS instance. */
|
||||
typedef struct {
|
||||
entityid_t entityId;
|
||||
componentid_t componentId;
|
||||
} jscomponent_t;
|
||||
|
||||
static scriptproto_t MODULE_COMPONENT_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleComponentCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Component cannot be instantiated with new");
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleComponentGetEntity) {
|
||||
jscomponent_t *comp = scriptProtoGetValue(
|
||||
&MODULE_COMPONENT_PROTO, callInfo->this_value
|
||||
);
|
||||
if(!comp) return jerry_undefined();
|
||||
return jerry_number((double)comp->entityId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleComponentGetId) {
|
||||
jscomponent_t *comp = scriptProtoGetValue(
|
||||
&MODULE_COMPONENT_PROTO, callInfo->this_value
|
||||
);
|
||||
if(!comp) return jerry_undefined();
|
||||
return jerry_number((double)comp->componentId);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleComponentToString) {
|
||||
jscomponent_t *comp = scriptProtoGetValue(
|
||||
&MODULE_COMPONENT_PROTO, callInfo->this_value
|
||||
);
|
||||
if(!comp) return jerry_string_sz("Component:invalid");
|
||||
jerry_value_t num = jerry_number((double)comp->componentId);
|
||||
jerry_value_t str = jerry_value_to_string(num);
|
||||
jerry_value_free(num);
|
||||
return str;
|
||||
}
|
||||
|
||||
static void moduleComponentInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_COMPONENT_PROTO, "Component",
|
||||
sizeof(jscomponent_t), moduleComponentCtor
|
||||
);
|
||||
|
||||
/* Instance properties */
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_COMPONENT_PROTO, "entity",
|
||||
moduleComponentGetEntity, NULL
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_COMPONENT_PROTO, "id",
|
||||
moduleComponentGetId, NULL
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_COMPONENT_PROTO, moduleComponentToString);
|
||||
|
||||
/* Component.POSITION, Component.CAMERA, etc. from componentlist.h */
|
||||
jerry_value_t ctor = MODULE_COMPONENT_PROTO.constructor;
|
||||
#define X(enumName, type, field, init, dispose, render) \
|
||||
do { \
|
||||
jerry_value_t _key = jerry_string_sz(#enumName); \
|
||||
jerry_value_t _val = jerry_number((double)COMPONENT_TYPE_##enumName); \
|
||||
jerry_object_set(ctor, _key, _val); \
|
||||
jerry_value_free(_val); \
|
||||
jerry_value_free(_key); \
|
||||
} while(0);
|
||||
#include "entity/componentlist.h"
|
||||
#undef X
|
||||
|
||||
/* Component.INVALID */
|
||||
jerry_value_t _key = jerry_string_sz("INVALID");
|
||||
jerry_value_t _val = jerry_number((double)COMPONENT_ID_INVALID);
|
||||
jerry_object_set(ctor, _key, _val);
|
||||
jerry_value_free(_val);
|
||||
jerry_value_free(_key);
|
||||
}
|
||||
|
||||
static void moduleComponentDispose(void) {
|
||||
scriptProtoDispose(&MODULE_COMPONENT_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "script/module/entity/component/modulecomponentlist.h"
|
||||
#include "entity/entitymanager.h"
|
||||
|
||||
/** C struct wrapped by every Entity JS instance. */
|
||||
typedef struct {
|
||||
entityid_t id;
|
||||
} jsentity_t;
|
||||
|
||||
static scriptproto_t MODULE_ENTITY_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleEntityCtor) {
|
||||
(void)callInfo; (void)args; (void)argc;
|
||||
return moduleBaseThrow("Entity cannot be instantiated with new");
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityCreate) {
|
||||
entityid_t id = entityManagerAdd();
|
||||
if(id == ENTITY_ID_INVALID) {
|
||||
return moduleBaseThrow("Entity.create: no entity slots available");
|
||||
}
|
||||
jsentity_t ent = { .id = id };
|
||||
return scriptProtoCreateValue(&MODULE_ENTITY_PROTO, &ent);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityDisposeEntity) {
|
||||
moduleBaseRequireArgs(1);
|
||||
jsentity_t *ent = scriptProtoGetValue(&MODULE_ENTITY_PROTO, args[0]);
|
||||
if(!ent) return moduleBaseThrow("Entity.dispose: expected Entity object");
|
||||
entityDispose(ent->id);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityAdd) {
|
||||
jsentity_t *ent = scriptProtoGetValue(
|
||||
&MODULE_ENTITY_PROTO, callInfo->this_value
|
||||
);
|
||||
if(!ent) return moduleBaseThrow("Entity.add: invalid this");
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireNumber(0);
|
||||
|
||||
const componenttype_t type = (componenttype_t)moduleBaseArgInt(0);
|
||||
if(type <= COMPONENT_TYPE_NULL || type >= COMPONENT_TYPE_COUNT) {
|
||||
return moduleBaseThrow("Entity.add: invalid component type");
|
||||
}
|
||||
|
||||
componentid_t cid = entityAddComponent(ent->id, type);
|
||||
if(cid == COMPONENT_ID_INVALID) {
|
||||
return moduleBaseThrow("Entity.add: failed to add component");
|
||||
}
|
||||
|
||||
jscomponent_t comp = { .entityId = ent->id, .componentId = cid };
|
||||
return moduleComponentListCreateInstance(type, &comp);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleEntityToString) {
|
||||
jsentity_t *ent = scriptProtoGetValue(
|
||||
&MODULE_ENTITY_PROTO, callInfo->this_value
|
||||
);
|
||||
if(!ent) return jerry_string_sz("Entity:invalid");
|
||||
jerry_value_t num = jerry_number((double)ent->id);
|
||||
jerry_value_t str = jerry_value_to_string(num);
|
||||
jerry_value_free(num);
|
||||
return str;
|
||||
}
|
||||
|
||||
static void moduleEntityInit(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_ENTITY_PROTO, "Entity",
|
||||
sizeof(jsentity_t), moduleEntityCtor
|
||||
);
|
||||
|
||||
/* Static methods */
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_ENTITY_PROTO, "create", moduleEntityCreate
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_ENTITY_PROTO, "dispose", moduleEntityDisposeEntity
|
||||
);
|
||||
|
||||
/* Entity.INVALID */
|
||||
jerry_value_t ctor = MODULE_ENTITY_PROTO.constructor;
|
||||
jerry_value_t _key = jerry_string_sz("INVALID");
|
||||
jerry_value_t _val = jerry_number((double)ENTITY_ID_INVALID);
|
||||
jerry_object_set(ctor, _key, _val);
|
||||
jerry_value_free(_val);
|
||||
jerry_value_free(_key);
|
||||
|
||||
/* Instance methods */
|
||||
scriptProtoDefineFunc(&MODULE_ENTITY_PROTO, "add", moduleEntityAdd);
|
||||
scriptProtoDefineToString(&MODULE_ENTITY_PROTO, moduleEntityToString);
|
||||
}
|
||||
|
||||
static void moduleEntityDispose(void) {
|
||||
scriptProtoDispose(&MODULE_ENTITY_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "input/input.h"
|
||||
#include "input/inputbutton.h"
|
||||
|
||||
static scriptproto_t MODULE_INPUT_PROTO;
|
||||
|
||||
/**
|
||||
* Validates an inputaction_t argument and returns a type error if bad.
|
||||
*
|
||||
* @param i Argument index.
|
||||
* @param ctx Context string for error messages.
|
||||
*/
|
||||
#define moduleInputRequireAction(i, ctx) do { \
|
||||
if(!jerry_value_is_number(args[(i)])) { \
|
||||
return moduleBaseThrow(ctx ": action must be a number"); \
|
||||
} \
|
||||
const inputaction_t _act = (inputaction_t)moduleBaseArgInt(i); \
|
||||
if(_act <= INPUT_ACTION_NULL || _act >= INPUT_ACTION_COUNT) { \
|
||||
return moduleBaseThrow(ctx ": invalid action"); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
moduleBaseFunction(moduleInputIsDown) {
|
||||
moduleInputRequireAction(0, "Input.isDown");
|
||||
return jerry_boolean(
|
||||
inputIsDown((inputaction_t)moduleBaseArgInt(0))
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputWasDown) {
|
||||
moduleInputRequireAction(0, "Input.wasDown");
|
||||
return jerry_boolean(
|
||||
inputWasDown((inputaction_t)moduleBaseArgInt(0))
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputPressed) {
|
||||
moduleInputRequireAction(0, "Input.pressed");
|
||||
return jerry_boolean(
|
||||
inputPressed((inputaction_t)moduleBaseArgInt(0))
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputReleased) {
|
||||
moduleInputRequireAction(0, "Input.released");
|
||||
return jerry_boolean(
|
||||
inputReleased((inputaction_t)moduleBaseArgInt(0))
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputGetValue) {
|
||||
moduleInputRequireAction(0, "Input.getValue");
|
||||
return jerry_number(
|
||||
inputGetCurrentValue((inputaction_t)moduleBaseArgInt(0))
|
||||
);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputAxis) {
|
||||
moduleInputRequireAction(0, "Input.axis");
|
||||
moduleInputRequireAction(1, "Input.axis");
|
||||
return jerry_number(inputAxis(
|
||||
(inputaction_t)moduleBaseArgInt(0),
|
||||
(inputaction_t)moduleBaseArgInt(1)
|
||||
));
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleInputBind) {
|
||||
moduleBaseRequireArgs(2);
|
||||
moduleBaseRequireString(0);
|
||||
if(!jerry_value_is_number(args[1])) {
|
||||
return moduleBaseThrow("Input.bind: action must be a number");
|
||||
}
|
||||
const inputaction_t action = (inputaction_t)moduleBaseArgInt(1);
|
||||
if(action <= INPUT_ACTION_NULL || action >= INPUT_ACTION_COUNT) {
|
||||
return moduleBaseThrow("Input.bind: invalid action");
|
||||
}
|
||||
|
||||
char_t name[128];
|
||||
moduleBaseToString(args[0], name, sizeof(name));
|
||||
if(name[0] == '\0') {
|
||||
return moduleBaseThrow("Input.bind: button name cannot be empty");
|
||||
}
|
||||
|
||||
inputbutton_t btn = inputButtonGetByName(name);
|
||||
if(btn.type == INPUT_BUTTON_TYPE_NONE) {
|
||||
return moduleBaseThrow("Input.bind: unknown button name");
|
||||
}
|
||||
|
||||
inputBind(btn, action);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
static void moduleInputInit(void) {
|
||||
scriptProtoInit(&MODULE_INPUT_PROTO, "Input", sizeof(uint8_t), NULL);
|
||||
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "isDown", moduleInputIsDown
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "wasDown", moduleInputWasDown
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "pressed", moduleInputPressed
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "released", moduleInputReleased
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "getValue", moduleInputGetValue
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "axis", moduleInputAxis
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_INPUT_PROTO, "bind", moduleInputBind
|
||||
);
|
||||
|
||||
/* Register INPUT_ACTION_* integer constants into the global scope. */
|
||||
jerry_value_t result = jerry_eval(
|
||||
(const jerry_char_t *)INPUT_ACTION_SCRIPT,
|
||||
strlen(INPUT_ACTION_SCRIPT),
|
||||
JERRY_PARSE_NO_OPTS
|
||||
);
|
||||
jerry_value_free(result);
|
||||
}
|
||||
|
||||
static void moduleInputDispose(void) {
|
||||
scriptProtoDispose(&MODULE_INPUT_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "util/memory.h"
|
||||
#include "cglm/cglm.h"
|
||||
|
||||
static scriptproto_t MODULE_VEC3_PROTO;
|
||||
|
||||
/**
|
||||
* Returns the native float[3] pointer from the Vec3 instance that owns
|
||||
* the current call (this_value). Returns NULL if not a Vec3.
|
||||
*/
|
||||
static inline float_t *moduleVec3Get(const jerry_call_info_t *callInfo) {
|
||||
return (float_t *)scriptProtoGetValue(
|
||||
&MODULE_VEC3_PROTO, callInfo->this_value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the native float[3] pointer from any jerry value.
|
||||
* Returns NULL if the value is not a Vec3 instance.
|
||||
*/
|
||||
static inline float_t *moduleVec3From(const jerry_value_t val) {
|
||||
return (float_t *)scriptProtoGetValue(&MODULE_VEC3_PROTO, val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Vec3 JS object from a C vec3 array.
|
||||
*
|
||||
* @param v Source vec3 to copy.
|
||||
* @return A new Vec3 JS instance owning a copy of the data.
|
||||
*/
|
||||
static inline jerry_value_t moduleVec3Push(const vec3 v) {
|
||||
return scriptProtoCreateValue(&MODULE_VEC3_PROTO, v);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3Constructor) {
|
||||
float_t *ptr = (float_t *)memoryAllocate(sizeof(vec3));
|
||||
ptr[0] = moduleBaseOptFloat(0, 0.0f);
|
||||
ptr[1] = moduleBaseOptFloat(1, 0.0f);
|
||||
ptr[2] = moduleBaseOptFloat(2, 0.0f);
|
||||
jerry_object_set_native_ptr(
|
||||
callInfo->this_value, &MODULE_VEC3_PROTO.info, ptr
|
||||
);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3GetX) {
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_undefined();
|
||||
return jerry_number((double)v[0]);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3SetX) {
|
||||
moduleBaseRequireArgs(1);
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_undefined();
|
||||
v[0] = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3GetY) {
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_undefined();
|
||||
return jerry_number((double)v[1]);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3SetY) {
|
||||
moduleBaseRequireArgs(1);
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_undefined();
|
||||
v[1] = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3GetZ) {
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_undefined();
|
||||
return jerry_number((double)v[2]);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3SetZ) {
|
||||
moduleBaseRequireArgs(1);
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_undefined();
|
||||
v[2] = moduleBaseArgFloat(0);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleVec3ToString) {
|
||||
float_t *v = moduleVec3Get(callInfo);
|
||||
if(!v) return jerry_string_sz("Vec3:invalid");
|
||||
char_t buf[64];
|
||||
snprintf(buf, sizeof(buf), "Vec3(%g, %g, %g)",
|
||||
(double)v[0], (double)v[1], (double)v[2]
|
||||
);
|
||||
return jerry_string_sz(buf);
|
||||
}
|
||||
|
||||
static void moduleVec3Init(void) {
|
||||
scriptProtoInit(
|
||||
&MODULE_VEC3_PROTO, "Vec3",
|
||||
sizeof(vec3), moduleVec3Constructor
|
||||
);
|
||||
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_VEC3_PROTO, "x", moduleVec3GetX, moduleVec3SetX
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_VEC3_PROTO, "y", moduleVec3GetY, moduleVec3SetY
|
||||
);
|
||||
scriptProtoDefineProp(
|
||||
&MODULE_VEC3_PROTO, "z", moduleVec3GetZ, moduleVec3SetZ
|
||||
);
|
||||
scriptProtoDefineToString(&MODULE_VEC3_PROTO, moduleVec3ToString);
|
||||
}
|
||||
|
||||
static void moduleVec3Dispose(void) {
|
||||
scriptProtoDispose(&MODULE_VEC3_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "modulebase.h"
|
||||
#include "util/memory.h"
|
||||
|
||||
jerry_object_native_info_t MODULE_BASE_NATIVE_INFO = {
|
||||
.free_cb = moduleBaseFreeProto,
|
||||
.number_of_references = 0,
|
||||
.offset_of_references = 0
|
||||
};
|
||||
|
||||
void moduleBaseFreeProto(void *ptr, jerry_object_native_info_t *info) {
|
||||
assertNotNull(ptr, "Pointer must not be null");
|
||||
assertNotNull(info, "Native info must not be null");
|
||||
memoryFree(ptr);
|
||||
}
|
||||
|
||||
jerry_value_t moduleBaseThrow(const char_t *message) {
|
||||
assertStrLenMin(message, 1, "Error message must not be empty");
|
||||
return jerry_throw_sz(JERRY_ERROR_TYPE, message);
|
||||
}
|
||||
|
||||
jerry_value_t moduleBaseThrowError(const errorret_t err) {
|
||||
assertNotNull(err.state, "Error state must not be NULL");
|
||||
assertNotNull(err.state->message, "Error message must not be NULL");
|
||||
jerry_value_t jsErr = jerry_throw_sz(JERRY_ERROR_TYPE, err.state->message);
|
||||
errorCatch(err);
|
||||
return jsErr;
|
||||
}
|
||||
|
||||
void moduleBaseToString(jerry_value_t val, char_t *buf, jerry_size_t buflen) {
|
||||
jerry_size_t len = jerry_string_to_buffer(
|
||||
val, JERRY_ENCODING_UTF8, (jerry_char_t *)buf, buflen - 1
|
||||
);
|
||||
buf[len] = '\0';
|
||||
}
|
||||
|
||||
void moduleBaseExceptionMessage(
|
||||
jerry_value_t exception,
|
||||
char_t *buf,
|
||||
size_t buflen
|
||||
) {
|
||||
jerry_value_t errVal = jerry_exception_value(exception, false);
|
||||
jerry_value_t errStr = jerry_value_to_string(errVal);
|
||||
jerry_size_t len = jerry_string_to_buffer(
|
||||
errStr, JERRY_ENCODING_UTF8, (jerry_char_t *)buf, (jerry_size_t)(buflen - 1)
|
||||
);
|
||||
buf[len] = '\0';
|
||||
jerry_value_free(errStr);
|
||||
jerry_value_free(errVal);
|
||||
}
|
||||
|
||||
void moduleBaseDefineMethod(
|
||||
jerry_value_t obj,
|
||||
const char_t *name,
|
||||
jerry_external_handler_t fn
|
||||
) {
|
||||
jerry_value_t key = jerry_string_sz(name);
|
||||
jerry_value_t func = jerry_function_external(fn);
|
||||
jerry_object_set(obj, key, func);
|
||||
jerry_value_free(func);
|
||||
jerry_value_free(key);
|
||||
}
|
||||
|
||||
void moduleBaseDefineGlobalMethod(
|
||||
const char_t *name,
|
||||
jerry_external_handler_t fn
|
||||
) {
|
||||
jerry_value_t global = jerry_current_realm();
|
||||
moduleBaseDefineMethod(global, name, fn);
|
||||
jerry_value_free(global);
|
||||
}
|
||||
|
||||
void moduleBaseSetValue(const char_t *name, jerry_value_t value) {
|
||||
jerry_value_t global = jerry_current_realm();
|
||||
jerry_value_t key = jerry_string_sz(name);
|
||||
jerry_object_set(global, key, value);
|
||||
jerry_value_free(key);
|
||||
jerry_value_free(global);
|
||||
}
|
||||
|
||||
jerry_value_t moduleBaseWrapPointer(void *ptr) {
|
||||
jerry_value_t obj = jerry_object();
|
||||
jerry_object_set_native_ptr(obj, &MODULE_BASE_NATIVE_INFO, ptr);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void *moduleBaseUnwrapPointer(jerry_value_t val) {
|
||||
if(!jerry_value_is_object(val)) return NULL;
|
||||
return jerry_object_get_native_ptr(val, &MODULE_BASE_NATIVE_INFO);
|
||||
}
|
||||
|
||||
jerry_value_t moduleBaseGetProp(
|
||||
jerry_value_t obj, const char_t *name
|
||||
) {
|
||||
jerry_value_t key = jerry_string_sz(name);
|
||||
jerry_value_t val = jerry_object_get(obj, key);
|
||||
jerry_value_free(key);
|
||||
return val;
|
||||
}
|
||||
|
||||
float_t moduleBaseValueFloat(jerry_value_t val) {
|
||||
return (float_t)jerry_value_as_number(val);
|
||||
}
|
||||
|
||||
int32_t moduleBaseValueInt(jerry_value_t val) {
|
||||
return (int32_t)jerry_value_as_number(val);
|
||||
}
|
||||
|
||||
void moduleBaseSetNumber(const char_t *name, double value) {
|
||||
jerry_value_t global = jerry_current_realm();
|
||||
jerry_value_t key = jerry_string_sz(name);
|
||||
jerry_value_t val = jerry_number(value);
|
||||
jerry_object_set(global, key, val);
|
||||
jerry_value_free(val);
|
||||
jerry_value_free(key);
|
||||
jerry_value_free(global);
|
||||
}
|
||||
|
||||
void moduleBaseSetInt(const char_t *name, int32_t value) {
|
||||
moduleBaseSetNumber(name, (double)value);
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/string.h"
|
||||
#include <jerryscript.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
extern jerry_object_native_info_t MODULE_BASE_NATIVE_INFO;
|
||||
|
||||
/**
|
||||
* Frees a native pointer associated with a JavaScript object, uses the internal
|
||||
* memory functions.
|
||||
*
|
||||
* @param ptr The native pointer to free. Must not be NULL.
|
||||
* @param info The native info associated with the pointer. Must not be NULL.
|
||||
*/
|
||||
void moduleBaseFreeProto(void *ptr, jerry_object_native_info_t *info);
|
||||
|
||||
/**
|
||||
* Throws a JavaScript error with the given message.
|
||||
*
|
||||
* @param message The error message to throw. Must not be empty.
|
||||
* @return A jerry_value_t representing the thrown error.
|
||||
*/
|
||||
jerry_value_t moduleBaseThrow(const char_t *message);
|
||||
|
||||
/**
|
||||
* "Converts" a dusk error to a jerry type error.
|
||||
*
|
||||
* @param err Native dusk error to convert.
|
||||
* @return A jerry_value_t representing the thrown error.
|
||||
*/
|
||||
jerry_value_t moduleBaseThrowError(const errorret_t err);
|
||||
|
||||
/**
|
||||
* Converts a jerry value to a string and writes it to the provided buffer.
|
||||
*
|
||||
* @param val The jerry value to convert.
|
||||
* @param buf The buffer to write the string to.
|
||||
* @param buflen The length of the buffer.
|
||||
*/
|
||||
void moduleBaseToString(jerry_value_t val, char_t *buf, jerry_size_t buflen);
|
||||
|
||||
/**
|
||||
* Converts a jerry exception value to a string and writes it to the provided
|
||||
* buffer.
|
||||
*
|
||||
* @param exception The jerry exception value to convert.
|
||||
* @param buf The buffer to write the string to.
|
||||
* @param buflen The length of the buffer.
|
||||
*/
|
||||
void moduleBaseExceptionMessage(
|
||||
jerry_value_t exception,
|
||||
char_t *buf,
|
||||
size_t buflen
|
||||
);
|
||||
|
||||
/**
|
||||
* Defines a method on a given object.
|
||||
*
|
||||
* @param obj The object to define the method on.
|
||||
* @param name The name of the method.
|
||||
* @param fn The function pointer to the method implementation.
|
||||
*/
|
||||
void moduleBaseDefineMethod(
|
||||
jerry_value_t obj,
|
||||
const char_t *name,
|
||||
jerry_external_handler_t fn
|
||||
);
|
||||
|
||||
/**
|
||||
* Defines a global method.
|
||||
*
|
||||
* @param name The name of the method.
|
||||
* @param fn The function pointer to the method implementation.
|
||||
*/
|
||||
void moduleBaseDefineGlobalMethod(
|
||||
const char_t *name,
|
||||
jerry_external_handler_t fn
|
||||
);
|
||||
|
||||
/**
|
||||
* Sets a global variable to a given jerry value.
|
||||
*
|
||||
* @param name The name of the global variable.
|
||||
* @param value The jerry value to set the variable to.
|
||||
*/
|
||||
void moduleBaseSetValue(const char_t *name, jerry_value_t value);
|
||||
|
||||
/**
|
||||
* Wraps a native pointer in a jerry object.
|
||||
*
|
||||
* @param ptr The native pointer to wrap.
|
||||
* @return A jerry_value_t representing the wrapped pointer.
|
||||
*/
|
||||
jerry_value_t moduleBaseWrapPointer(void *ptr);
|
||||
|
||||
/**
|
||||
* Unwraps a native pointer from a jerry object.
|
||||
*
|
||||
* @param val The jerry value to unwrap the pointer from.
|
||||
* @return The unwrapped native pointer.
|
||||
*/
|
||||
void *moduleBaseUnwrapPointer(jerry_value_t val);
|
||||
|
||||
/**
|
||||
* Gets a property from a jerry object as a jerry value.
|
||||
*
|
||||
* @param obj The object to get the property from.
|
||||
* @param name The name of the property to get.
|
||||
* @return The value of the property.
|
||||
*/
|
||||
jerry_value_t moduleBaseGetProp(
|
||||
jerry_value_t obj, const char_t *name
|
||||
);
|
||||
|
||||
/**
|
||||
* Converts a jerry value to a float.
|
||||
*
|
||||
* @param val The jerry value to convert.
|
||||
* @return The converted float value.
|
||||
*/
|
||||
float_t moduleBaseValueFloat(jerry_value_t val);
|
||||
|
||||
/**
|
||||
* Converts a jerry value to an int.
|
||||
*
|
||||
* @param val The jerry value to convert.
|
||||
* @return The converted int value.
|
||||
*/
|
||||
int32_t moduleBaseValueInt(jerry_value_t val);
|
||||
|
||||
/**
|
||||
* Sets a global variable to a given number value.
|
||||
*
|
||||
* @param name The name of the global variable.
|
||||
* @param value The number value to set the variable to.
|
||||
*/
|
||||
void moduleBaseSetNumber(const char_t *name, double value);
|
||||
|
||||
/**
|
||||
* Sets a global variable to a given int value.
|
||||
*
|
||||
* @param name The name of the global variable.
|
||||
* @param value The int value to set the variable to.
|
||||
*/
|
||||
void moduleBaseSetInt(const char_t *name, int32_t value);
|
||||
|
||||
#define moduleBaseFunction(name) \
|
||||
static jerry_value_t name( \
|
||||
const jerry_call_info_t *callInfo, \
|
||||
const jerry_value_t args[], \
|
||||
const jerry_length_t argc)
|
||||
|
||||
#define moduleBaseRequireArgs(n) do { \
|
||||
if(argc < (jerry_length_t)(n)) { \
|
||||
return moduleBaseThrow("Expected at least " #n " argument(s)"); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define moduleBaseRequireNumber(i) do { \
|
||||
if(!jerry_value_is_number(args[(i)])) { \
|
||||
return moduleBaseThrow("Expected number argument"); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define moduleBaseRequireString(i) do { \
|
||||
if(!jerry_value_is_string(args[(i)])) { \
|
||||
return moduleBaseThrow("Expected string argument"); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
#define moduleBaseArgFloat(i) ((float_t)jerry_value_as_number(args[(i)]))
|
||||
#define moduleBaseArgInt(i) ((int32_t)jerry_value_as_number(args[(i)]))
|
||||
#define moduleBaseArgBool(i) (jerry_value_is_true(args[(i)]))
|
||||
|
||||
#define moduleBaseOptFloat(i, def) \
|
||||
((jerry_length_t)(i) < argc && jerry_value_is_number(args[(i)]) \
|
||||
? (float_t)jerry_value_as_number(args[(i)]) : (def))
|
||||
|
||||
#define moduleBaseOptInt(i, def) \
|
||||
((jerry_length_t)(i) < argc && jerry_value_is_number(args[(i)]) \
|
||||
? (int32_t)jerry_value_as_number(args[(i)]) : (def))
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/asset/moduleasset.h"
|
||||
#include "script/module/console/moduleconsole.h"
|
||||
#include "script/module/display/modulecolor.h"
|
||||
#include "script/module/display/modulescreen.h"
|
||||
#include "script/module/engine/moduleengine.h"
|
||||
#include "script/module/entity/component/modulecomponentlist.h"
|
||||
#include "script/module/entity/modulecomponent.h"
|
||||
#include "script/module/entity/moduleentity.h"
|
||||
#include "script/module/input/moduleinput.h"
|
||||
#include "script/module/math/modulevec3.h"
|
||||
#include "script/module/scene/modulescene.h"
|
||||
#include "script/module/system/modulesystem.h"
|
||||
|
||||
static void moduleListInit(void) {
|
||||
moduleTextureInit();
|
||||
moduleColorInit();
|
||||
moduleAssetInit();
|
||||
moduleConsoleInit();
|
||||
moduleScreenInit();
|
||||
moduleEngineInit();
|
||||
moduleVec3Init();
|
||||
moduleComponentInit();
|
||||
moduleEntityInit();
|
||||
moduleComponentListInit();
|
||||
moduleInputInit();
|
||||
moduleSceneInit();
|
||||
moduleSystemInit();
|
||||
}
|
||||
|
||||
static void moduleListDispose(void) {
|
||||
moduleSystemDispose();
|
||||
moduleSceneDispose();
|
||||
moduleInputDispose();
|
||||
moduleComponentListDispose();
|
||||
moduleEntityDispose();
|
||||
moduleComponentDispose();
|
||||
moduleVec3Dispose();
|
||||
moduleEngineDispose();
|
||||
moduleScreenDispose();
|
||||
moduleConsoleDispose();
|
||||
moduleAssetDispose();
|
||||
moduleColorDispose();
|
||||
moduleTextureDispose();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "scene/scene.h"
|
||||
|
||||
static scriptproto_t MODULE_SCENE_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleSceneGetCurrent) {
|
||||
return jerry_number((double)SCENE.type);
|
||||
}
|
||||
|
||||
moduleBaseFunction(moduleSceneSet) {
|
||||
moduleBaseRequireArgs(1);
|
||||
moduleBaseRequireNumber(0);
|
||||
const scenetype_t type = (scenetype_t)moduleBaseArgInt(0);
|
||||
if(type <= SCENE_TYPE_NULL || type >= SCENE_TYPE_COUNT) {
|
||||
return moduleBaseThrow("Scene.set: invalid scene type");
|
||||
}
|
||||
sceneSet(type);
|
||||
return jerry_undefined();
|
||||
}
|
||||
|
||||
static void moduleSceneInit(void) {
|
||||
scriptProtoInit(&MODULE_SCENE_PROTO, "Scene", sizeof(uint8_t), NULL);
|
||||
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_SCENE_PROTO, "current", moduleSceneGetCurrent, NULL
|
||||
);
|
||||
scriptProtoDefineStaticFunc(
|
||||
&MODULE_SCENE_PROTO, "set", moduleSceneSet
|
||||
);
|
||||
|
||||
/* Scene.INITIAL, Scene.TEST, Scene.OVERWORLD, ... */
|
||||
jerry_value_t global = MODULE_SCENE_PROTO.prototype;
|
||||
#define X(structName, varName, varNameUpper, initFunc, updateFunc, disposeFunc) \
|
||||
do { \
|
||||
jerry_value_t _key = jerry_string_sz(#varNameUpper); \
|
||||
jerry_value_t _val = jerry_number((double)SCENE_TYPE_##varNameUpper); \
|
||||
jerry_object_set(global, _key, _val); \
|
||||
jerry_value_free(_val); \
|
||||
jerry_value_free(_key); \
|
||||
} while(0);
|
||||
#include "scene/scenelist.h"
|
||||
#undef X
|
||||
}
|
||||
|
||||
static void moduleSceneDispose(void) {
|
||||
scriptProtoDispose(&MODULE_SCENE_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "script/module/modulebase.h"
|
||||
#include "script/scriptproto.h"
|
||||
#include "system/system.h"
|
||||
|
||||
static scriptproto_t MODULE_SYSTEM_PROTO;
|
||||
|
||||
moduleBaseFunction(moduleSystemGetPlatform) {
|
||||
return jerry_number((double)systemGetPlatform());
|
||||
}
|
||||
|
||||
static void moduleSystemInit(void) {
|
||||
scriptProtoInit(&MODULE_SYSTEM_PROTO, "System", sizeof(uint8_t), NULL);
|
||||
|
||||
scriptProtoDefineStaticProp(
|
||||
&MODULE_SYSTEM_PROTO, "platform",
|
||||
moduleSystemGetPlatform, NULL
|
||||
);
|
||||
|
||||
/* Register PLATFORM_* integer constants on the System global. */
|
||||
jerry_value_t target = MODULE_SYSTEM_PROTO.prototype;
|
||||
|
||||
#define SYSTEM_PLATFORM(name, value) \
|
||||
do { \
|
||||
jerry_value_t key = jerry_string_sz("PLATFORM_" #name); \
|
||||
jerry_value_t val = jerry_number((double)(value)); \
|
||||
jerry_object_set(target, key, val); \
|
||||
jerry_value_free(val); \
|
||||
jerry_value_free(key); \
|
||||
} while(0);
|
||||
SYSTEM_PLATFORM_LIST
|
||||
#undef SYSTEM_PLATFORM
|
||||
}
|
||||
|
||||
static void moduleSystemDispose(void) {
|
||||
scriptProtoDispose(&MODULE_SYSTEM_PROTO);
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "script.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "asset/asset.h"
|
||||
#include "asset/loader/assetloader.h"
|
||||
#include "asset/loader/assetentry.h"
|
||||
#include "script/module/modulelist.h"
|
||||
#include <jerryscript.h>
|
||||
|
||||
script_t SCRIPT;
|
||||
|
||||
errorret_t scriptInit(void) {
|
||||
memoryZero(&SCRIPT, sizeof(script_t));
|
||||
|
||||
jerry_init(JERRY_INIT_EMPTY);
|
||||
SCRIPT.initialized = true;
|
||||
|
||||
moduleListInit();
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t scriptExecString(const char_t *source) {
|
||||
assertNotNull(source, "Source cannot be NULL");
|
||||
assertTrue(SCRIPT.initialized, "Script system not initialized");
|
||||
|
||||
jerry_value_t result = jerry_eval(
|
||||
(const jerry_char_t *)source,
|
||||
strlen(source),
|
||||
JERRY_PARSE_NO_OPTS
|
||||
);
|
||||
|
||||
if(jerry_value_is_exception(result)) {
|
||||
char_t buf[256];
|
||||
jerry_value_t errVal = jerry_exception_value(result, false);
|
||||
jerry_value_t errStr = jerry_value_to_string(errVal);
|
||||
jerry_size_t len = jerry_string_to_buffer(
|
||||
errStr, JERRY_ENCODING_UTF8, (jerry_char_t *)buf, sizeof(buf) - 1
|
||||
);
|
||||
buf[len] = '\0';
|
||||
jerry_value_free(errStr);
|
||||
jerry_value_free(errVal);
|
||||
jerry_value_free(result);
|
||||
errorThrow("Script error: %s", buf);
|
||||
}
|
||||
|
||||
jerry_value_free(result);
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t scriptExecFile(const char_t *path) {
|
||||
assertNotNull(path, "Path cannot be NULL");
|
||||
assertTrue(SCRIPT.initialized, "Script system not initialized");
|
||||
|
||||
assetentry_t *entry = assetLock(path, ASSET_LOADER_TYPE_SCRIPT, NULL);
|
||||
assertNotNull(entry, "Failed to get asset entry for script");
|
||||
errorChain(assetRequireLoaded(entry));
|
||||
assetUnlockEntry(entry);
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t scriptDispose(void) {
|
||||
if(!SCRIPT.initialized) errorOk();
|
||||
moduleListDispose();
|
||||
jerry_cleanup();
|
||||
SCRIPT.initialized = false;
|
||||
errorOk();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) 2026 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
|
||||
typedef struct {
|
||||
bool_t initialized;
|
||||
} script_t;
|
||||
|
||||
extern script_t SCRIPT;
|
||||
|
||||
/**
|
||||
* Initializes the script system, starting up JerryScript and registering all
|
||||
* built-in modules.
|
||||
*
|
||||
* @return Any error that occurred.
|
||||
*/
|
||||
errorret_t scriptInit(void);
|
||||
|
||||
/**
|
||||
* Evaluates a JS source string in the global scope.
|
||||
*
|
||||
* @param source Null-terminated JS source to evaluate.
|
||||
* @return Any error that occurred.
|
||||
*/
|
||||
errorret_t scriptExecString(const char_t *source);
|
||||
|
||||
/**
|
||||
* Loads and evaluates a script asset from the archive. The result is cached
|
||||
* by the asset system; repeated calls with the same path do not re-execute.
|
||||
*
|
||||
* @param path Path of the script inside the asset archive.
|
||||
* @return Any error that occurred.
|
||||
*/
|
||||
errorret_t scriptExecFile(const char_t *path);
|
||||
|
||||
/**
|
||||
* Disposes of the script system and shuts down JerryScript.
|
||||
*
|
||||
* @return Any error that occurred.
|
||||
*/
|
||||
errorret_t scriptDispose(void);
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user