diff --git a/archive/scene/scene/scenemap.h b/archive/scene/scene/scenemap.h index 86f8766..8e28b7c 100644 --- a/archive/scene/scene/scenemap.h +++ b/archive/scene/scene/scenemap.h @@ -8,7 +8,7 @@ #pragma once #include "scene/scene.h" #include "rpg/entity/entity.h" -#include "display/camera.h" +#include "display/camera/camera.h" #include "asset/asset.h" typedef struct { diff --git a/assets/scene/initial.lua b/assets/scene/initial.lua index edee85d..b17f4b4 100644 --- a/assets/scene/initial.lua +++ b/assets/scene/initial.lua @@ -1,8 +1,14 @@ module('spritebatch') module('time') +module('camera') +module('glm') -x = 0.0 -y = 0.0 +camera = cameraCreate(CAMERA_PROJECTION_TYPE_PERSPECTIVE) + +print('Camera position') +print(camera.position.x) +print(camera.position.y) +print(camera.position.z) function sceneDispose() -- print('Disposing initial scene') @@ -10,19 +16,18 @@ end function sceneUpdate() -- print('Updating initial scene') - x = x + TIME.delta * 10.0 - y = y + TIME.delta * 10.0 end function sceneRender() - -- print('Rendering initial scene') - x1 = x + 32 - y1 = y + 32 + cameraPushMatrix(camera) + spriteBatchPush( nil, - x, y, - x1, y1, + 0, 0, + 1, 1, nil ) spriteBatchFlush() + + cameraPopMatrix() end \ No newline at end of file diff --git a/src/display/CMakeLists.txt b/src/display/CMakeLists.txt index a579281..5f321d6 100644 --- a/src/display/CMakeLists.txt +++ b/src/display/CMakeLists.txt @@ -8,13 +8,13 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC display.c framebuffer.c - camera.c screen.c texture.c spritebatch.c ) # Subdirectories +add_subdirectory(camera) add_subdirectory(mesh) add_subdirectory(palette) add_subdirectory(tileset) diff --git a/src/display/camera/CMakeLists.txt b/src/display/camera/CMakeLists.txt new file mode 100644 index 0000000..d8c90ab --- /dev/null +++ b/src/display/camera/CMakeLists.txt @@ -0,0 +1,10 @@ +# 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 + camera.c +) \ No newline at end of file diff --git a/src/display/camera.c b/src/display/camera/camera.c similarity index 100% rename from src/display/camera.c rename to src/display/camera/camera.c diff --git a/src/display/camera.h b/src/display/camera/camera.h similarity index 88% rename from src/display/camera.h rename to src/display/camera/camera.h index 6a7ae88..c266304 100644 --- a/src/display/camera.h +++ b/src/display/camera/camera.h @@ -9,8 +9,6 @@ #include "dusk.h" #include "display/color.h" -#define CAMERA_COUNT_MAX 4 - typedef enum { CAMERA_PROJECTION_TYPE_PERSPECTIVE, CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED, @@ -32,20 +30,20 @@ typedef struct { mat4 view; struct { - float_t position[3]; - float_t target[3]; - float_t up[3]; + vec3 position; + vec3 target; + vec3 up; } lookat; struct { - float_t offset[3]; - float_t target[3]; - float_t up[3]; + vec3 offset; + vec3 target; + vec3 up; float_t pixelsPerUnit; } lookatPixelPerfect; struct { - float_t position[2]; + vec2 position; float_t zoom; } _2d; }; diff --git a/src/display/display.h b/src/display/display.h index 34d1397..24c203b 100644 --- a/src/display/display.h +++ b/src/display/display.h @@ -8,7 +8,7 @@ #pragma once #include "displaydefs.h" #include "error/error.h" -#include "display/camera.h" +#include "display/camera/camera.h" #include "display/framebuffer.h" typedef struct { diff --git a/src/display/screen.h b/src/display/screen.h index ff9ace7..b9f5f8b 100644 --- a/src/display/screen.h +++ b/src/display/screen.h @@ -8,7 +8,7 @@ #pragma once #include "dusk.h" #include "display/framebuffer.h" -#include "display/camera.h" +#include "display/camera/camera.h" #include "display/mesh/quad.h" #if DISPLAY_SIZE_DYNAMIC == 1 diff --git a/src/event/event.c b/src/event/event.c index cc3fdf5..a109a63 100644 --- a/src/event/event.c +++ b/src/event/event.c @@ -8,7 +8,7 @@ #include "event.h" #include "assert/assert.h" #include "util/memory.h" -#include "script/scriptstruct.h" +#include "script/struct/scriptstruct.h" void eventInit( event_t *event, diff --git a/src/scene/scene.c b/src/scene/scene.c index 2b49b83..5003a15 100644 --- a/src/scene/scene.c +++ b/src/scene/scene.c @@ -7,26 +7,20 @@ #include "assert/assert.h" #include "util/memory.h" -#include "display/camera.h" +#include "display/camera/camera.h" #include "display/screen.h" scene_t SCENE; -camera_t cam; - errorret_t sceneInit(void) { memoryZero(&SCENE, sizeof(scene_t)); - cameraInitOrthographic(&cam); - errorChain(scriptContextInit(&SCENE.scriptContext)); errorOk(); } void sceneUpdate(void) { - if(!scriptContextHasFunc(&SCENE.scriptContext, "sceneUpdate")) { - return; - } + if(!scriptContextHasFunc(&SCENE.scriptContext, "sceneUpdate")) return; errorret_t err = scriptContextCallFunc( &SCENE.scriptContext, "sceneUpdate", NULL, 0, NULL @@ -35,20 +29,11 @@ void sceneUpdate(void) { } void sceneRender(void) { - if(!scriptContextHasFunc(&SCENE.scriptContext, "sceneRender")) { - return; - } - - cam.orthographic.right = SCREEN.width; - cam.orthographic.bottom = SCREEN.height; - cam.orthographic.left = 0.0f; - cam.orthographic.top = 0.0f; - cameraPushMatrix(&cam); + if(!scriptContextHasFunc(&SCENE.scriptContext, "sceneRender")) return; errorret_t err = scriptContextCallFunc( &SCENE.scriptContext, "sceneRender", NULL, 0, NULL ); - cameraPopMatrix(); errorCatch(errorPrint(err)); } diff --git a/src/script/CMakeLists.txt b/src/script/CMakeLists.txt index da4ed3c..3ad00e9 100644 --- a/src/script/CMakeLists.txt +++ b/src/script/CMakeLists.txt @@ -9,8 +9,8 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} scriptmanager.c scriptcontext.c scriptmodule.c - scriptstruct.c ) # Subdirectories -add_subdirectory(module) \ No newline at end of file +add_subdirectory(module) +add_subdirectory(struct) \ No newline at end of file diff --git a/src/script/module/display/CMakeLists.txt b/src/script/module/display/CMakeLists.txt index 5bdb936..b316bae 100644 --- a/src/script/module/display/CMakeLists.txt +++ b/src/script/module/display/CMakeLists.txt @@ -6,5 +6,8 @@ # Sources target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC + moduleglm.c + modulecamera.c modulespritebatch.c + moduleglm.c ) \ No newline at end of file diff --git a/src/script/module/display/modulecamera.c b/src/script/module/display/modulecamera.c new file mode 100644 index 0000000..2669492 --- /dev/null +++ b/src/script/module/display/modulecamera.c @@ -0,0 +1,300 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "modulecamera.h" +#include "assert/assert.h" +#include "display/camera/camera.h" +#include "util/memory.h" +#include "script/struct/scriptstruct.h" +#include "util/string.h" + +void moduleCamera(scriptcontext_t *context) { + assertNotNull(context, "Context cannot be NULL."); + + // Structure + scriptStructRegister( + context, + "camera_mt", + moduleCameraGetter, + moduleCameraSetter + ); + + // Definitions + #define MODULE_CAMERA_SCRIPT_LEN 64 + char_t buffer[MODULE_CAMERA_SCRIPT_LEN]; + #define MODULE_CAMERA_DEF(str, val) \ + snprintf( \ + buffer, \ + MODULE_CAMERA_SCRIPT_LEN, \ + "%s = %d", \ + str, \ + val \ + ); \ + scriptContextExec(context, buffer); + + MODULE_CAMERA_DEF( + "CAMERA_PROJECTION_TYPE_PERSPECTIVE", + CAMERA_PROJECTION_TYPE_PERSPECTIVE + ); + MODULE_CAMERA_DEF( + "CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED", + CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED + ); + MODULE_CAMERA_DEF( + "CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC", + CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC + ); + + MODULE_CAMERA_DEF( + "CAMERA_VIEW_TYPE_MATRIX", + CAMERA_VIEW_TYPE_MATRIX + ); + MODULE_CAMERA_DEF( + "CAMERA_VIEW_TYPE_LOOKAT", + CAMERA_VIEW_TYPE_LOOKAT + ); + MODULE_CAMERA_DEF( + "CAMERA_VIEW_TYPE_2D", + CAMERA_VIEW_TYPE_2D + ); + MODULE_CAMERA_DEF( + "CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT", + CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT + ); + + // Methods + scriptContextRegFunc(context, "cameraCreate", moduleCameraCreate); + scriptContextRegFunc(context, "cameraPushMatrix", moduleCameraPushMatrix); + scriptContextRegFunc(context, "cameraPopMatrix", moduleCameraPopMatrix); +} + +int moduleCameraCreate(lua_State *L) { + assertNotNull(L, "Lua state cannot be NULL."); + + scriptcontext_t *context = *(scriptcontext_t **)lua_getextraspace(L); + assertNotNull(context, "Script context cannot be NULL."); + + // If we are provided a projection type, use it. + cameraprojectiontype_t projType = CAMERA_PROJECTION_TYPE_PERSPECTIVE; + if(lua_gettop(L) >= 1) { + projType = (cameraprojectiontype_t)luaL_checkinteger(L, 1); + } + + // Create camera. + camera_t *cam = (camera_t *)lua_newuserdata(L, sizeof(camera_t)); + memoryZero(cam, sizeof(camera_t)); + + // Push metatable. + scriptStructPushMetatable(context, "camera_mt", cam); + + // Init camera + switch(projType) { + case CAMERA_PROJECTION_TYPE_PERSPECTIVE: + case CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED: + cameraInitPerspective(cam); + cam->projType = projType; + break; + + case CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC: + cameraInitOrthographic(cam); + cam->projType = projType; + break; + + default: + luaL_error(L, "Invalid camera projection type specified."); + break; + } + + return 1; +} + +int moduleCameraPushMatrix(lua_State *L) { + assertNotNull(L, "Lua state cannot be NULL."); + + // Camera should be provided (pointer to camera_t). + camera_t *cam = *(camera_t **)lua_touserdata(L, 1); + assertNotNull(cam, "Camera pointer cannot be NULL."); + + cameraPushMatrix(cam); + return 0; +} + +int moduleCameraPopMatrix(lua_State *L) { + assertNotNull(L, "Lua state cannot be NULL."); + cameraPopMatrix(); + return 0; +} + +void moduleCameraGetter( + const scriptcontext_t *context, + const char_t *key, + const void *structPtr, + scriptvalue_t *outValue +) { + assertNotNull(context, "Script context cannot be NULL."); + assertNotNull(key, "Key cannot be NULL."); + assertNotNull(structPtr, "Structure pointer cannot be NULL."); + assertNotNull(outValue, "Output value cannot be NULL."); + + camera_t *cam = (camera_t *)structPtr; + + if(stringCompare(key, "near") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->nearClip; + return; + } else if(stringCompare(key, "far") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->farClip; + return; + } else if(stringCompare(key, "nearClip") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->nearClip; + return; + } else if(stringCompare(key, "farClip") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->farClip; + return; + } + + // Perspective relative values + if(cam->projType == CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) { + if(stringCompare(key, "left") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->orthographic.left; + return; + } else if(stringCompare(key, "right") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->orthographic.right; + return; + } else if(stringCompare(key, "top") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->orthographic.top; + return; + } else if(stringCompare(key, "bottom") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->orthographic.bottom; + return; + } + } else if( + cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE || + cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED + ) { + if(stringCompare(key, "fov") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = cam->perspective.fov; + return; + } + } + + // View type relative values. + if(cam->viewType == CAMERA_VIEW_TYPE_MATRIX) { + + } else if(cam->viewType == CAMERA_VIEW_TYPE_LOOKAT) { + if(stringCompare(key, "position") == 0) { + scriptStructPushMetatable( + context, + "vec3_mt", + (void*)&cam->lookat.position + ); + outValue->type = SCRIPT_VALUE_TYPE_USERDATA; + return; + } + } else if(cam->viewType == CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT) { + + } else if(cam->viewType == CAMERA_VIEW_TYPE_2D) { + + } +} + +void moduleCameraSetter( + const scriptcontext_t *context, + const char_t *key, + void *structPtr, + const scriptvalue_t *inValue +) { + assertNotNull(context, "Script context cannot be NULL."); + assertNotNull(key, "Key cannot be NULL."); + assertNotNull(structPtr, "Structure pointer cannot be NULL."); + assertNotNull(inValue, "Input value cannot be NULL."); + + camera_t *cam = (camera_t *)structPtr; + + if(stringCompare(key, "near") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->nearClip = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->nearClip = (float_t)inValue->value.intValue; + } + return; + } else if(stringCompare(key, "far") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->farClip = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->farClip = (float_t)inValue->value.intValue; + } + return; + } else if(stringCompare(key, "nearClip") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->nearClip = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->nearClip = (float_t)inValue->value.intValue; + } + return; + } else if(stringCompare(key, "farClip") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->farClip = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->farClip = (float_t)inValue->value.intValue; + } + return; + } + + // Perspective relative values + if(cam->projType == CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) { + if(stringCompare(key, "left") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->orthographic.left = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->orthographic.left = (float_t)inValue->value.intValue; + } + return; + } else if(stringCompare(key, "right") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->orthographic.right = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->orthographic.right = (float_t)inValue->value.intValue; + } + return; + } else if(stringCompare(key, "top") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->orthographic.top = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->orthographic.top = (float_t)inValue->value.intValue; + } + return; + } else if(stringCompare(key, "bottom") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->orthographic.bottom = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->orthographic.bottom = (float_t)inValue->value.intValue; + } + return; + } + } else if( + cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE || + cam->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED + ) { + if(stringCompare(key, "fov") == 0) { + if(inValue->type == SCRIPT_VALUE_TYPE_FLOAT) { + cam->perspective.fov = inValue->value.floatValue; + } else if(inValue->type == SCRIPT_VALUE_TYPE_INT) { + cam->perspective.fov = (float_t)inValue->value.intValue; + } + return; + } + } +} \ No newline at end of file diff --git a/src/script/module/display/modulecamera.h b/src/script/module/display/modulecamera.h new file mode 100644 index 0000000..77855b9 --- /dev/null +++ b/src/script/module/display/modulecamera.h @@ -0,0 +1,70 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "script/scriptcontext.h" + +/** + * Register camera functions to the given script context. + * + * @param context The script context to register camera functions to. + */ +void moduleCamera(scriptcontext_t *context); + +/** + * Script binding for creating a new camera. + * + * @param L The Lua state. + * @return Number of return values on the Lua stack. + */ +int moduleCameraCreate(lua_State *L); + +/** + * Script binding for pushing the camera matrix onto the matrix stack. + * + * @param L The Lua state. + * @return Number of return values on the Lua stack. + */ +int moduleCameraPushMatrix(lua_State *L); + +/** + * Script binding for popping the camera matrix from the matrix stack. + * + * @param L The Lua state. + * @return Number of return values on the Lua stack. + */ +int moduleCameraPopMatrix(lua_State *L); + +/** + * Getter for camera structure fields. + * + * @param context The script context. + * @param key The field key. + * @param structPtr Pointer to the camera structure. + * @param outValue Output script value. + */ +void moduleCameraGetter( + const scriptcontext_t *context, + const char_t *key, + const void *structPtr, + scriptvalue_t *outValue +); + +/** + * Setter for camera structure fields. + * + * @param context The script context. + * @param key The field key. + * @param structPtr Pointer to the camera structure. + * @param inValue Input script value. + */ +void moduleCameraSetter( + const scriptcontext_t *context, + const char_t *key, + void *structPtr, + const scriptvalue_t *inValue +); \ No newline at end of file diff --git a/src/script/module/display/moduleglm.c b/src/script/module/display/moduleglm.c new file mode 100644 index 0000000..74a544c --- /dev/null +++ b/src/script/module/display/moduleglm.c @@ -0,0 +1,49 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "moduleglm.h" +#include "assert/assert.h" +#include "script/struct/scriptstruct.h" +#include "util/string.h" + +void moduleGLM(scriptcontext_t *context) { + assertNotNull(context, "Context cannot be NULL."); + + scriptStructRegister( + context, + "vec3_mt", + moduleGLMVec3Getter, + NULL + ); +} + +void moduleGLMVec3Getter( + const scriptcontext_t *context, + const char_t *key, + const void *structPtr, + scriptvalue_t *outValue +) { + assertNotNull(context, "Script context cannot be NULL."); + assertNotNull(key, "Key cannot be NULL."); + assertNotNull(structPtr, "Structure pointer cannot be NULL."); + assertNotNull(outValue, "Output value cannot be NULL."); + + vec3 *v = (vec3 *)structPtr; + if(stringCompare(key, "x") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = (*v)[0]; + return; + } else if(stringCompare(key, "y") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = (*v)[1]; + return; + } else if(stringCompare(key, "z") == 0) { + outValue->type = SCRIPT_VALUE_TYPE_FLOAT; + outValue->value.floatValue = (*v)[2]; + return; + } +} \ No newline at end of file diff --git a/src/script/module/display/moduleglm.h b/src/script/module/display/moduleglm.h new file mode 100644 index 0000000..26965dc --- /dev/null +++ b/src/script/module/display/moduleglm.h @@ -0,0 +1,31 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "script/scriptcontext.h" + +/** + * Registers the GLM module with the given script context. + * + * @param context The script context to register the module with. + */ +void moduleGLM(scriptcontext_t *context); + +/** + * Getter function for the vec3 structure. + * + * @param context The script context. + * @param key The key to get. + * @param structPtr Pointer to the vec3 structure. + * @param outValue Pointer to store the output value. + */ +void moduleGLMVec3Getter( + const scriptcontext_t *context, + const char_t *key, + const void *structPtr, + scriptvalue_t *outValue +); \ No newline at end of file diff --git a/src/script/module/input/moduleinput.c b/src/script/module/input/moduleinput.c index e441d85..34f13bd 100644 --- a/src/script/module/input/moduleinput.c +++ b/src/script/module/input/moduleinput.c @@ -9,6 +9,7 @@ #include "input/input.h" #include "assert/assert.h" #include "util/string.h" +#include "script/struct/scriptstruct.h" void moduleInput(scriptcontext_t *context) { assertNotNull(context, "Script context cannot be NULL"); diff --git a/src/script/module/input/moduleinput.h b/src/script/module/input/moduleinput.h index bacedb1..9547c16 100644 --- a/src/script/module/input/moduleinput.h +++ b/src/script/module/input/moduleinput.h @@ -7,7 +7,6 @@ #pragma once #include "script/scriptcontext.h" -#include "script/scriptstruct.h" /** * Register input functions to the given script context. diff --git a/src/script/module/time/moduletime.c b/src/script/module/time/moduletime.c index a739d25..d8b8efc 100644 --- a/src/script/module/time/moduletime.c +++ b/src/script/module/time/moduletime.c @@ -7,7 +7,7 @@ #include "moduletime.h" #include "assert/assert.h" -#include "script/scriptstruct.h" +#include "script/struct/scriptstruct.h" void moduleTime(scriptcontext_t *ctx) { assertNotNull(ctx, "Script context cannot be NULL"); diff --git a/src/script/scriptmodule.c b/src/script/scriptmodule.c index 03e8ab9..897afc4 100644 --- a/src/script/scriptmodule.c +++ b/src/script/scriptmodule.c @@ -15,6 +15,8 @@ #include "script/module/time/moduletime.h" #include "script/module/event/moduleevent.h" #include "script/module/display/modulespritebatch.h" +#include "script/module/display/modulecamera.h" +#include "script/module/display/moduleglm.h" #include "util/string.h" const scriptmodule_t SCRIPT_MODULE_LIST[] = { @@ -27,6 +29,8 @@ const scriptmodule_t SCRIPT_MODULE_LIST[] = { { .name = "time", .callback = moduleTime }, { .name = "event", .callback = moduleEvent }, { .name = "spritebatch", .callback = moduleSpriteBatch }, + { .name = "camera", .callback = moduleCamera }, + { .name = "glm", .callback = moduleGLM }, }; #define SCRIPT_MODULE_COUNT ( \ diff --git a/src/script/scriptvalue.h b/src/script/scriptvalue.h index 3431574..05e38d4 100644 --- a/src/script/scriptvalue.h +++ b/src/script/scriptvalue.h @@ -13,14 +13,15 @@ #define SCRIPT_VALUE_TYPE_FLOAT 2 #define SCRIPT_VALUE_TYPE_STRING 3 #define SCRIPT_VALUE_TYPE_BOOL 4 +#define SCRIPT_VALUE_TYPE_USERDATA 5 typedef struct scriptvalue_s { uint8_t type; union { int32_t intValue; - float floatValue; + float_t floatValue; const char_t *strValue; - bool boolValue; + bool_t boolValue; } value; } scriptvalue_t; \ No newline at end of file diff --git a/src/script/struct/CMakeLists.txt b/src/script/struct/CMakeLists.txt new file mode 100644 index 0000000..0be342d --- /dev/null +++ b/src/script/struct/CMakeLists.txt @@ -0,0 +1,10 @@ +# 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 + scriptstruct.c +) \ No newline at end of file diff --git a/src/script/scriptstruct.c b/src/script/struct/scriptstruct.c similarity index 96% rename from src/script/scriptstruct.c rename to src/script/struct/scriptstruct.c index 401d8a7..bca3883 100644 --- a/src/script/scriptstruct.c +++ b/src/script/struct/scriptstruct.c @@ -73,9 +73,16 @@ int scriptStructIndex(lua_State *l) { lua_pushboolean(l, outValue.value.boolValue); break; - default: + case SCRIPT_VALUE_TYPE_NIL: lua_pushnil(l); break; + + case SCRIPT_VALUE_TYPE_USERDATA: + break; + + default: + assertUnreachable("Unsupported value type for struct field retrieval"); + break; } return 1; @@ -147,7 +154,7 @@ structmetatablecontext_t * scriptStructGetMetatableContext(lua_State *L) { } void scriptStructPushMetatable( - scriptcontext_t *context, + const scriptcontext_t *context, const char_t *metatableName, void *structPtr ) { diff --git a/src/script/scriptstruct.h b/src/script/struct/scriptstruct.h similarity index 88% rename from src/script/scriptstruct.h rename to src/script/struct/scriptstruct.h index ea3adbf..7d0200b 100644 --- a/src/script/scriptstruct.h +++ b/src/script/struct/scriptstruct.h @@ -6,21 +6,8 @@ */ #pragma once -#include "scriptcontext.h" - -typedef void (*scriptstructgetter_t)( - const scriptcontext_t *context, - const char_t *key, - const void *structPtr, - scriptvalue_t *outValue -); - -typedef void (*scriptstructsetter_t)( - const scriptcontext_t *context, - const char_t *key, - void *structPtr, - const scriptvalue_t *inValue -); +#include "scriptstructgetter.h" +#include "scriptstructsetter.h" typedef struct { scriptcontext_t *context; @@ -78,7 +65,7 @@ structmetatablecontext_t *scriptStructGetMetatableContext(lua_State *l); * @param structPtr Pointer to the structure to push. */ void scriptStructPushMetatable( - scriptcontext_t *context, + const scriptcontext_t *context, const char_t *metatableName, void *structPtr ); diff --git a/src/script/struct/scriptstructgetter.h b/src/script/struct/scriptstructgetter.h new file mode 100644 index 0000000..b70190e --- /dev/null +++ b/src/script/struct/scriptstructgetter.h @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "script/scriptcontext.h" + +typedef void (*scriptstructgetter_t)( + const scriptcontext_t *context, + const char_t *key, + const void *structPtr, + scriptvalue_t *outValue +); \ No newline at end of file diff --git a/src/script/struct/scriptstructsetter.h b/src/script/struct/scriptstructsetter.h new file mode 100644 index 0000000..852ee19 --- /dev/null +++ b/src/script/struct/scriptstructsetter.h @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2026 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "script/scriptcontext.h" + +typedef void (*scriptstructsetter_t)( + const scriptcontext_t *context, + const char_t *key, + void *structPtr, + const scriptvalue_t *inValue +); \ No newline at end of file diff --git a/src/ui/ui.h b/src/ui/ui.h index 93a364d..956fe45 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -9,7 +9,7 @@ #include "asset/asset.h" #include "display/texture.h" #include "display/tileset/tileset.h" -#include "display/camera.h" +#include "display/camera/camera.h" typedef struct { camera_t camera;