Moved a few things around, definitely not clean but better.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
10
src/display/camera/CMakeLists.txt
Normal file
10
src/display/camera/CMakeLists.txt
Normal file
@@ -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
|
||||
)
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
scriptmanager.c
|
||||
scriptcontext.c
|
||||
scriptmodule.c
|
||||
scriptstruct.c
|
||||
)
|
||||
|
||||
# Subdirectories
|
||||
add_subdirectory(module)
|
||||
add_subdirectory(module)
|
||||
add_subdirectory(struct)
|
||||
@@ -6,5 +6,8 @@
|
||||
# Sources
|
||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
moduleglm.c
|
||||
modulecamera.c
|
||||
modulespritebatch.c
|
||||
moduleglm.c
|
||||
)
|
||||
300
src/script/module/display/modulecamera.c
Normal file
300
src/script/module/display/modulecamera.c
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
70
src/script/module/display/modulecamera.h
Normal file
70
src/script/module/display/modulecamera.h
Normal file
@@ -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
|
||||
);
|
||||
49
src/script/module/display/moduleglm.c
Normal file
49
src/script/module/display/moduleglm.c
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
31
src/script/module/display/moduleglm.h
Normal file
31
src/script/module/display/moduleglm.h
Normal file
@@ -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
|
||||
);
|
||||
@@ -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");
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#pragma once
|
||||
#include "script/scriptcontext.h"
|
||||
#include "script/scriptstruct.h"
|
||||
|
||||
/**
|
||||
* Register input functions to the given script context.
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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 ( \
|
||||
|
||||
@@ -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;
|
||||
10
src/script/struct/CMakeLists.txt
Normal file
10
src/script/struct/CMakeLists.txt
Normal file
@@ -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
|
||||
)
|
||||
@@ -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
|
||||
) {
|
||||
@@ -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
|
||||
);
|
||||
16
src/script/struct/scriptstructgetter.h
Normal file
16
src/script/struct/scriptstructgetter.h
Normal file
@@ -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
|
||||
);
|
||||
16
src/script/struct/scriptstructsetter.h
Normal file
16
src/script/struct/scriptstructsetter.h
Normal file
@@ -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
|
||||
);
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user