Getting shaders working with lua.
This commit is contained in:
@@ -9,6 +9,7 @@ module('text')
|
|||||||
module('tileset')
|
module('tileset')
|
||||||
module('texture')
|
module('texture')
|
||||||
module('input')
|
module('input')
|
||||||
|
module('shader')
|
||||||
|
|
||||||
CELL_STATE_DEFAULT = 0
|
CELL_STATE_DEFAULT = 0
|
||||||
CELL_STATE_HOVER = 1
|
CELL_STATE_HOVER = 1
|
||||||
@@ -179,19 +180,23 @@ end
|
|||||||
|
|
||||||
function sceneUpdate()
|
function sceneUpdate()
|
||||||
x = x + inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT)
|
x = x + inputAxis(INPUT_ACTION_LEFT, INPUT_ACTION_RIGHT)
|
||||||
y = y + inputAxis(INPUT_ACTION_UP, INPUT_ACTION_DOWN)
|
y = y - inputAxis(INPUT_ACTION_UP, INPUT_ACTION_DOWN)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function sceneRender()
|
function sceneRender()
|
||||||
-- Update camera
|
-- Update camera
|
||||||
cameraPushMatrix(camera)
|
|
||||||
camera.bottom = screenGetHeight()
|
camera.bottom = screenGetHeight()
|
||||||
camera.right = screenGetWidth()
|
camera.right = screenGetWidth()
|
||||||
|
|
||||||
|
shaderBind(SHADER_UNLIT)
|
||||||
|
proj = cameraGetProjectionMatrix(camera)
|
||||||
|
shaderSetMatrix(SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj)
|
||||||
|
view = cameraGetViewMatrix(camera)
|
||||||
|
shaderSetMatrix(SHADER_UNLIT, SHADER_UNLIT_VIEW, view)
|
||||||
|
|
||||||
spriteBatchPush(
|
spriteBatchPush(
|
||||||
nil,
|
x, y,
|
||||||
x, y, x + 32, y + 32,
|
x + 32, y + 32,
|
||||||
colorWhite()
|
colorWhite()
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -251,6 +256,4 @@ function sceneRender()
|
|||||||
-- end
|
-- end
|
||||||
-- end
|
-- end
|
||||||
spriteBatchFlush()
|
spriteBatchFlush()
|
||||||
|
|
||||||
cameraPopMatrix()
|
|
||||||
end
|
end
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
# https://opensource.org/licenses/MIT
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
add_subdirectory(dusk)
|
add_subdirectory(dusk)
|
||||||
|
add_subdirectory(duskrpg)
|
||||||
|
|
||||||
if(DUSK_TARGET_SYSTEM STREQUAL "linux" OR DUSK_TARGET_SYSTEM STREQUAL "knulli")
|
if(DUSK_TARGET_SYSTEM STREQUAL "linux" OR DUSK_TARGET_SYSTEM STREQUAL "knulli")
|
||||||
add_subdirectory(dusklinux)
|
add_subdirectory(dusklinux)
|
||||||
|
|||||||
@@ -52,12 +52,9 @@ add_subdirectory(engine)
|
|||||||
add_subdirectory(error)
|
add_subdirectory(error)
|
||||||
add_subdirectory(event)
|
add_subdirectory(event)
|
||||||
add_subdirectory(input)
|
add_subdirectory(input)
|
||||||
add_subdirectory(item)
|
|
||||||
add_subdirectory(locale)
|
add_subdirectory(locale)
|
||||||
add_subdirectory(map)
|
|
||||||
add_subdirectory(scene)
|
add_subdirectory(scene)
|
||||||
add_subdirectory(script)
|
add_subdirectory(script)
|
||||||
add_subdirectory(story)
|
|
||||||
add_subdirectory(time)
|
add_subdirectory(time)
|
||||||
add_subdirectory(ui)
|
add_subdirectory(ui)
|
||||||
add_subdirectory(util)
|
add_subdirectory(util)
|
||||||
|
|||||||
@@ -20,11 +20,9 @@
|
|||||||
#include "display/shader/shaderunlit.h"
|
#include "display/shader/shaderunlit.h"
|
||||||
#include "time/time.h"
|
#include "time/time.h"
|
||||||
|
|
||||||
display_t DISPLAY = { 0 };
|
#include "script/module/display/moduleshader.h"
|
||||||
|
|
||||||
texture_t PALETTE_TEXTURE;
|
display_t DISPLAY = { 0 };
|
||||||
texture_t UNCOMPRESSED_TEXTURE;
|
|
||||||
texture_t COMPRESSED_TEXTURE;
|
|
||||||
|
|
||||||
errorret_t displayInit(void) {
|
errorret_t displayInit(void) {
|
||||||
memoryZero(&DISPLAY, sizeof(DISPLAY));
|
memoryZero(&DISPLAY, sizeof(DISPLAY));
|
||||||
@@ -33,68 +31,33 @@ errorret_t displayInit(void) {
|
|||||||
errorChain(displayPlatformInit());
|
errorChain(displayPlatformInit());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Setup initial shader with default values
|
||||||
errorChain(shaderInit(&SHADER_UNLIT, &SHADER_UNLIT_DEFINITION));
|
errorChain(shaderInit(&SHADER_UNLIT, &SHADER_UNLIT_DEFINITION));
|
||||||
|
camera_t cam;
|
||||||
|
cameraInit(&cam);
|
||||||
|
mat4 mat;
|
||||||
|
cameraGetProjectionMatrix(&cam, mat);
|
||||||
|
errorChain(shaderBind(&SHADER_UNLIT));
|
||||||
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, mat));
|
||||||
|
cameraGetViewMatrix(&cam, mat);
|
||||||
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, mat));
|
||||||
|
glm_mat4_identity(mat);
|
||||||
|
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, mat));
|
||||||
|
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, NULL));
|
||||||
|
|
||||||
errorChain(quadInit());
|
errorChain(quadInit());
|
||||||
errorChain(frameBufferInitBackBuffer());
|
errorChain(frameBufferInitBackBuffer());
|
||||||
errorChain(spriteBatchInit());
|
errorChain(spriteBatchInit());
|
||||||
errorChain(textInit());
|
errorChain(textInit());
|
||||||
errorChain(screenInit());
|
errorChain(screenInit());
|
||||||
|
|
||||||
// PALETTES[0].colors[0] = COLOR_RED;
|
assertTrue(SHADER_UNLIT.shaderProgramId != 0, "Unlit shader must be initialized before display init.");
|
||||||
// PALETTES[0].colors[1] = COLOR_GREEN;
|
|
||||||
// PALETTES[0].colors[2] = COLOR_BLUE;
|
|
||||||
// PALETTES[0].colors[3] = COLOR_WHITE;
|
|
||||||
// PALETTES[0].colors[4] = COLOR_MAGENTA;
|
|
||||||
// PALETTES[0].colors[5] = COLOR_CYAN;
|
|
||||||
// PALETTES[0].colors[6] = COLOR_YELLOW;
|
|
||||||
// PALETTES[0].colors[7] = COLOR_BLACK;
|
|
||||||
// PALETTES[0].count = 8;
|
|
||||||
|
|
||||||
// uint8_t indices[64] = {
|
|
||||||
// 0,0,0,0,0,0,0,0,
|
|
||||||
// 1,1,1,1,1,1,1,1,
|
|
||||||
// 2,2,2,2,2,2,2,2,
|
|
||||||
// 3,3,3,3,3,3,3,3,
|
|
||||||
// 4,4,4,4,4,4,4,4,
|
|
||||||
// 5,5,5,5,5,5,5,5,
|
|
||||||
// 6,6,6,6,6,6,6,6,
|
|
||||||
// 7,7,7,7,7,7,7,7
|
|
||||||
// };
|
|
||||||
|
|
||||||
// errorChain(textureInit(
|
|
||||||
// &PALETTE_TEXTURE,
|
|
||||||
// 8, 8,
|
|
||||||
// TEXTURE_FORMAT_PALETTE,
|
|
||||||
// (texturedata_t){
|
|
||||||
// .paletted = {
|
|
||||||
// .indices = indices,
|
|
||||||
// .palette = &PALETTES[0]
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// ));
|
|
||||||
|
|
||||||
errorChain(textureInit(
|
|
||||||
&UNCOMPRESSED_TEXTURE,
|
|
||||||
8, 8,
|
|
||||||
TEXTURE_FORMAT_RGBA,
|
|
||||||
(texturedata_t){
|
|
||||||
.rgbaColors = (color_t[]){
|
|
||||||
COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, COLOR_MAGENTA, COLOR_CYAN, COLOR_YELLOW, COLOR_BLACK,
|
|
||||||
COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, COLOR_MAGENTA, COLOR_CYAN, COLOR_YELLOW, COLOR_BLACK, COLOR_RED,
|
|
||||||
COLOR_BLUE, COLOR_WHITE, COLOR_MAGENTA, COLOR_CYAN, COLOR_YELLOW, COLOR_BLACK, COLOR_RED, COLOR_GREEN,
|
|
||||||
COLOR_WHITE, COLOR_MAGENTA, COLOR_CYAN, COLOR_YELLOW, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE,
|
|
||||||
COLOR_MAGENTA, COLOR_CYAN, COLOR_YELLOW, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE,
|
|
||||||
COLOR_CYAN, COLOR_YELLOW, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, COLOR_MAGENTA,
|
|
||||||
COLOR_YELLOW, COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, COLOR_MAGENTA, COLOR_CYAN,
|
|
||||||
COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, COLOR_MAGENTA, COLOR_CYAN, COLOR_YELLOW
|
|
||||||
}
|
|
||||||
}
|
|
||||||
));
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t displayUpdate(void) {
|
errorret_t displayUpdate(void) {
|
||||||
|
assertTrue(SHADER_UNLIT.shaderProgramId != 0, "Unlit shader must be initialized before display update.");
|
||||||
#ifdef displayPlatformUpdate
|
#ifdef displayPlatformUpdate
|
||||||
errorChain(displayPlatformUpdate());
|
errorChain(displayPlatformUpdate());
|
||||||
#endif
|
#endif
|
||||||
@@ -110,38 +73,7 @@ errorret_t displayUpdate(void) {
|
|||||||
SCREEN.background
|
SCREEN.background
|
||||||
);
|
);
|
||||||
|
|
||||||
camera_t camera;
|
errorChain(sceneRender());
|
||||||
// cameraInitOrthographic(&camera);
|
|
||||||
// camera.orthographic.left = 0.0f;
|
|
||||||
// camera.orthographic.right = SCREEN.width;
|
|
||||||
// camera.orthographic.top = SCREEN.height;
|
|
||||||
// camera.orthographic.bottom = 0.0f;
|
|
||||||
cameraInitPerspective(&camera);
|
|
||||||
camera.lookat.position[0] = 3.0f;
|
|
||||||
camera.lookat.position[1] = 3.0f;
|
|
||||||
camera.lookat.position[2] = 3.0f;
|
|
||||||
|
|
||||||
mat4 proj, view, model;
|
|
||||||
cameraGetProjectionMatrix(&camera, proj);
|
|
||||||
cameraGetViewMatrix(&camera, view);
|
|
||||||
glm_mat4_identity(model);
|
|
||||||
|
|
||||||
errorChain(shaderBind(&SHADER_UNLIT));
|
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj));
|
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
|
|
||||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model));
|
|
||||||
// errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &PALETTE_TEXTURE));
|
|
||||||
errorChain(shaderSetTexture(&SHADER_UNLIT, SHADER_UNLIT_TEXTURE, &UNCOMPRESSED_TEXTURE));
|
|
||||||
errorChain(spriteBatchPush(
|
|
||||||
0.0f, 0.0f,
|
|
||||||
1.0f, 1.0f,
|
|
||||||
COLOR_WHITE,
|
|
||||||
0.0f, 0.0f,
|
|
||||||
1.0f, 1.0f
|
|
||||||
));
|
|
||||||
errorChain(spriteBatchFlush());
|
|
||||||
|
|
||||||
// errorCatch(errorPrint(sceneRender()));
|
|
||||||
|
|
||||||
// Render UI
|
// Render UI
|
||||||
// uiRender();
|
// uiRender();
|
||||||
|
|||||||
@@ -7,4 +7,5 @@
|
|||||||
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
PUBLIC
|
PUBLIC
|
||||||
shader.c
|
shader.c
|
||||||
|
shaderunlit.c
|
||||||
)
|
)
|
||||||
@@ -8,15 +8,19 @@
|
|||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
#include "assert/assert.h"
|
#include "assert/assert.h"
|
||||||
|
|
||||||
|
shader_t *bound = NULL;
|
||||||
|
|
||||||
errorret_t shaderInit(shader_t *shader, const shaderdefinition_t *def) {
|
errorret_t shaderInit(shader_t *shader, const shaderdefinition_t *def) {
|
||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
errorChain(shaderInitPlatform(shader, def));
|
errorChain(shaderInitPlatform(shader, def));
|
||||||
|
bound = NULL;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
errorret_t shaderBind(shader_t *shader) {
|
errorret_t shaderBind(shader_t *shader) {
|
||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
errorChain(shaderBindPlatform(shader));
|
errorChain(shaderBindPlatform(shader));
|
||||||
|
bound = shader;
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +32,7 @@ errorret_t shaderSetMatrix(
|
|||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
assertNotNull(matrix, "Matrix cannot be null");
|
assertNotNull(matrix, "Matrix cannot be null");
|
||||||
|
assertTrue(bound == shader, "Shader must be bound.");
|
||||||
errorChain(shaderSetMatrixPlatform(shader, name, matrix));
|
errorChain(shaderSetMatrixPlatform(shader, name, matrix));
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -39,6 +44,7 @@ errorret_t shaderSetTexture(
|
|||||||
) {
|
) {
|
||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
|
assertTrue(bound == shader, "Shader must be bound.");
|
||||||
errorChain(shaderSetTexturePlatform(shader, name, texture));
|
errorChain(shaderSetTexturePlatform(shader, name, texture));
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -56,6 +62,7 @@ errorret_t shaderSetTexture(
|
|||||||
|
|
||||||
errorret_t shaderDispose(shader_t *shader) {
|
errorret_t shaderDispose(shader_t *shader) {
|
||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
|
bound = NULL;
|
||||||
errorChain(shaderDisposePlatform(shader));
|
errorChain(shaderDisposePlatform(shader));
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
10
src/dusk/display/shader/shaderunlit.c
Normal file
10
src/dusk/display/shader/shaderunlit.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "shaderunlit.h"
|
||||||
|
|
||||||
|
shader_t SHADER_UNLIT = { 0 };
|
||||||
@@ -15,4 +15,4 @@
|
|||||||
// #define SHADER_UNLIT_COLOR "u_Color"
|
// #define SHADER_UNLIT_COLOR "u_Color"
|
||||||
|
|
||||||
extern shaderdefinition_t SHADER_UNLIT_DEFINITION;
|
extern shaderdefinition_t SHADER_UNLIT_DEFINITION;
|
||||||
static shader_t SHADER_UNLIT;
|
extern shader_t SHADER_UNLIT;
|
||||||
@@ -7,11 +7,8 @@
|
|||||||
add_subdirectory(display)
|
add_subdirectory(display)
|
||||||
add_subdirectory(event)
|
add_subdirectory(event)
|
||||||
add_subdirectory(input)
|
add_subdirectory(input)
|
||||||
add_subdirectory(item)
|
|
||||||
add_subdirectory(locale)
|
add_subdirectory(locale)
|
||||||
add_subdirectory(map)
|
|
||||||
add_subdirectory(system)
|
add_subdirectory(system)
|
||||||
add_subdirectory(scene)
|
add_subdirectory(scene)
|
||||||
add_subdirectory(story)
|
|
||||||
add_subdirectory(time)
|
add_subdirectory(time)
|
||||||
add_subdirectory(ui)
|
add_subdirectory(ui)
|
||||||
@@ -15,4 +15,5 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME}
|
|||||||
modulescreen.c
|
modulescreen.c
|
||||||
moduletileset.c
|
moduletileset.c
|
||||||
moduletexture.c
|
moduletexture.c
|
||||||
|
moduleshader.c
|
||||||
)
|
)
|
||||||
@@ -69,6 +69,16 @@ void moduleCamera(scriptcontext_t *context) {
|
|||||||
|
|
||||||
// Methods
|
// Methods
|
||||||
lua_register(context->luaState, "cameraCreate", moduleCameraCreate);
|
lua_register(context->luaState, "cameraCreate", moduleCameraCreate);
|
||||||
|
lua_register(
|
||||||
|
context->luaState,
|
||||||
|
"cameraGetProjectionMatrix",
|
||||||
|
moduleCameraGetProjectionMatrix
|
||||||
|
);
|
||||||
|
lua_register(
|
||||||
|
context->luaState,
|
||||||
|
"cameraGetViewMatrix",
|
||||||
|
moduleCameraGetViewMatrix
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
int moduleCameraCreate(lua_State *L) {
|
int moduleCameraCreate(lua_State *L) {
|
||||||
@@ -116,7 +126,7 @@ int moduleCameraCreate(lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int moduleCameraIndex(lua_State *l) {
|
int moduleCameraIndex(lua_State *l) {
|
||||||
assertNotNull(l, "Lua state cannot be NULL.");
|
assertNotNull(l, "Lua state cannot be NULL.");
|
||||||
|
|
||||||
const char_t *key = luaL_checkstring(l, 2);
|
const char_t *key = luaL_checkstring(l, 2);
|
||||||
@@ -289,3 +299,35 @@ int moduleCameraNewIndex(lua_State *l) {
|
|||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int moduleCameraGetProjectionMatrix(lua_State *L) {
|
||||||
|
assertNotNull(L, "Lua state cannot be NULL.");
|
||||||
|
|
||||||
|
camera_t *cam = (camera_t *)luaL_checkudata(L, 1, "camera_mt");
|
||||||
|
assertNotNull(cam, "Camera pointer cannot be NULL.");
|
||||||
|
|
||||||
|
// Create mat4
|
||||||
|
mat4 test;
|
||||||
|
cameraGetProjectionMatrix(cam, test);
|
||||||
|
|
||||||
|
// Lua needs to own this matrix now
|
||||||
|
mat4 *m = (mat4 *)lua_newuserdata(L, sizeof(mat4));
|
||||||
|
memoryCopy(m, test, sizeof(mat4));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int moduleCameraGetViewMatrix(lua_State *L) {
|
||||||
|
assertNotNull(L, "Lua state cannot be NULL.");
|
||||||
|
|
||||||
|
camera_t *cam = (camera_t *)luaL_checkudata(L, 1, "camera_mt");
|
||||||
|
assertNotNull(cam, "Camera pointer cannot be NULL.");
|
||||||
|
|
||||||
|
// Create mat4
|
||||||
|
mat4 test;
|
||||||
|
cameraGetViewMatrix(cam, test);
|
||||||
|
|
||||||
|
// Lua needs to own this matrix now
|
||||||
|
mat4 *m = (mat4 *)lua_newuserdata(L, sizeof(mat4));
|
||||||
|
memoryCopy(m, test, sizeof(mat4));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -36,3 +36,19 @@ int moduleCameraIndex(lua_State *l);
|
|||||||
* @param l The Lua state.
|
* @param l The Lua state.
|
||||||
*/
|
*/
|
||||||
int moduleCameraNewIndex(lua_State *l);
|
int moduleCameraNewIndex(lua_State *l);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script binding for getting a camera's projection matrix.
|
||||||
|
*
|
||||||
|
* @param L The Lua state.
|
||||||
|
* @return Number of return values on the Lua stack.
|
||||||
|
*/
|
||||||
|
int moduleCameraGetProjectionMatrix(lua_State *L);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script binding for getting a camera's view matrix.
|
||||||
|
*
|
||||||
|
* @param L The Lua state.
|
||||||
|
* @return Number of return values on the Lua stack.
|
||||||
|
*/
|
||||||
|
int moduleCameraGetViewMatrix(lua_State *L);
|
||||||
94
src/dusk/script/module/display/moduleshader.c
Normal file
94
src/dusk/script/module/display/moduleshader.c
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2026 Dominic Masters
|
||||||
|
*
|
||||||
|
* This software is released under the MIT License.
|
||||||
|
* https://opensource.org/licenses/MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "moduleshader.h"
|
||||||
|
#include "assert/assert.h"
|
||||||
|
#include "display/shader/shader.h"
|
||||||
|
#include "display/shader/shaderunlit.h"
|
||||||
|
#include "display/camera/camera.h"
|
||||||
|
|
||||||
|
void moduleShader(scriptcontext_t *context) {
|
||||||
|
assertNotNull(context, "Context cannot be NULL.");
|
||||||
|
|
||||||
|
// Shader unlit defs
|
||||||
|
lua_pushlightuserdata(context->luaState, &SHADER_UNLIT);
|
||||||
|
lua_setglobal(context->luaState, "SHADER_UNLIT");
|
||||||
|
|
||||||
|
lua_pushstring(context->luaState, SHADER_UNLIT_PROJECTION);
|
||||||
|
lua_setglobal(context->luaState, "SHADER_UNLIT_PROJECTION");
|
||||||
|
lua_pushstring(context->luaState, SHADER_UNLIT_VIEW);
|
||||||
|
lua_setglobal(context->luaState, "SHADER_UNLIT_VIEW");
|
||||||
|
lua_pushstring(context->luaState, SHADER_UNLIT_MODEL);
|
||||||
|
lua_setglobal(context->luaState, "SHADER_UNLIT_MODEL");
|
||||||
|
lua_pushstring(context->luaState, SHADER_UNLIT_TEXTURE);
|
||||||
|
lua_setglobal(context->luaState, "SHADER_UNLIT_TEXTURE");
|
||||||
|
|
||||||
|
// Shader methods
|
||||||
|
lua_register(context->luaState, "shaderBind", moduleShaderBind);
|
||||||
|
lua_register(context->luaState, "shaderSetMatrix", moduleShaderSetMatrix);
|
||||||
|
lua_register(context->luaState, "shaderSetTexture", moduleShaderSetTexture);
|
||||||
|
}
|
||||||
|
|
||||||
|
int moduleShaderBind(lua_State *l) {
|
||||||
|
assertNotNull(l, "Lua state cannot be NULL.");
|
||||||
|
|
||||||
|
// Should be passed a shader userdata pointer only.
|
||||||
|
shader_t *shader = (shader_t *)lua_touserdata(l, 1);
|
||||||
|
assertNotNull(shader, "Shader pointer cannot be NULL.");
|
||||||
|
|
||||||
|
errorret_t ret = shaderBind(shader);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
luaL_error(l, "Failed to bind shader: %s", ret.state->message);
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int moduleShaderSetMatrix(lua_State *l) {
|
||||||
|
assertNotNull(l, "Lua state cannot be NULL.");
|
||||||
|
|
||||||
|
// Expect shader, string and matrix.
|
||||||
|
if(!lua_isuserdata(l, 1)) {
|
||||||
|
luaL_error(l, "First argument must be a shader_mt userdata.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!lua_isstring(l, 2)) {
|
||||||
|
luaL_error(l, "Second argument must be a string.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!lua_isuserdata(l, 3)) {
|
||||||
|
luaL_error(l, "Third argument must be a mat4_mt userdata.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
shader_t *shader = (shader_t *)lua_touserdata(l, 1);
|
||||||
|
assertNotNull(shader, "Shader pointer cannot be NULL.");
|
||||||
|
|
||||||
|
const char_t *uniformName = luaL_checkstring(l, 2);
|
||||||
|
assertStrLenMin(uniformName, 1, "Uniform name cannot be empty.");
|
||||||
|
|
||||||
|
mat4 *mat = (mat4 *)lua_touserdata(l, 3);
|
||||||
|
assertNotNull(mat, "Matrix pointer cannot be NULL.");
|
||||||
|
|
||||||
|
errorret_t ret = shaderSetMatrix(shader, uniformName, *mat);
|
||||||
|
if(ret.code != ERROR_OK) {
|
||||||
|
luaL_error(l, "Failed to set shader matrix: %s", ret.state->message);
|
||||||
|
errorCatch(errorPrint(ret));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int moduleShaderSetTexture(lua_State *l) {
|
||||||
|
assertNotNull(l, "Lua state cannot be NULL.");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
42
src/dusk/script/module/display/moduleshader.h
Normal file
42
src/dusk/script/module/display/moduleshader.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
/**
|
||||||
|
* 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 shader functions to the given script context.
|
||||||
|
*
|
||||||
|
* @param context The script context to register shader functions to.
|
||||||
|
*/
|
||||||
|
void moduleShader(scriptcontext_t *context);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script binding for binding a shader.
|
||||||
|
*
|
||||||
|
* @param l The Lua state.
|
||||||
|
* @return Number of return values on the Lua stack.
|
||||||
|
*/
|
||||||
|
int moduleShaderBind(lua_State *l);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script binding for setting a matrix uniform in a shader.
|
||||||
|
*
|
||||||
|
* @param l The Lua state.
|
||||||
|
* @return Number of return values on the Lua stack.
|
||||||
|
*/
|
||||||
|
int moduleShaderSetMatrix(lua_State *l);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script binding for setting a texture uniform in a shader.
|
||||||
|
*
|
||||||
|
* @param l The Lua state.
|
||||||
|
* @return Number of return values on the Lua stack.
|
||||||
|
*/
|
||||||
|
int moduleShaderSetTexture(lua_State *l);
|
||||||
|
|
||||||
|
errorret_t doThing();
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "script/module/input/moduleinput.h"
|
#include "script/module/input/moduleinput.h"
|
||||||
#include "script/module/moduleplatform.h"
|
#include "script/module/moduleplatform.h"
|
||||||
#include "script/module/scene/modulescene.h"
|
#include "script/module/scene/modulescene.h"
|
||||||
#include "script/module/item/moduleitem.h"
|
|
||||||
#include "script/module/locale/modulelocale.h"
|
#include "script/module/locale/modulelocale.h"
|
||||||
#include "script/module/time/moduletime.h"
|
#include "script/module/time/moduletime.h"
|
||||||
#include "script/module/event/moduleevent.h"
|
#include "script/module/event/moduleevent.h"
|
||||||
@@ -18,13 +17,13 @@
|
|||||||
#include "script/module/display/modulespritebatch.h"
|
#include "script/module/display/modulespritebatch.h"
|
||||||
#include "script/module/display/modulecamera.h"
|
#include "script/module/display/modulecamera.h"
|
||||||
#include "script/module/display/moduleglm.h"
|
#include "script/module/display/moduleglm.h"
|
||||||
|
#include "script/module/display/moduleshader.h"
|
||||||
#include "script/module/ui/moduleui.h"
|
#include "script/module/ui/moduleui.h"
|
||||||
#include "script/module/display/moduletext.h"
|
#include "script/module/display/moduletext.h"
|
||||||
#include "script/module/display/modulescreen.h"
|
#include "script/module/display/modulescreen.h"
|
||||||
#include "script/module/story/modulestoryflag.h"
|
|
||||||
#include "script/module/map/modulemap.h"
|
|
||||||
#include "script/module/display/moduletexture.h"
|
#include "script/module/display/moduletexture.h"
|
||||||
#include "script/module/display/moduletileset.h"
|
#include "script/module/display/moduletileset.h"
|
||||||
|
#include "script/scriptgame.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
|
||||||
const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
||||||
@@ -33,7 +32,6 @@ const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
|||||||
{ .name = "platform", .callback = modulePlatform },
|
{ .name = "platform", .callback = modulePlatform },
|
||||||
{ .name = "color", .callback = moduleColor },
|
{ .name = "color", .callback = moduleColor },
|
||||||
{ .name = "scene", .callback = moduleScene },
|
{ .name = "scene", .callback = moduleScene },
|
||||||
{ .name = "item", .callback = moduleItem },
|
|
||||||
{ .name = "locale", .callback = moduleLocale },
|
{ .name = "locale", .callback = moduleLocale },
|
||||||
{ .name = "time", .callback = moduleTime },
|
{ .name = "time", .callback = moduleTime },
|
||||||
{ .name = "event", .callback = moduleEvent },
|
{ .name = "event", .callback = moduleEvent },
|
||||||
@@ -43,10 +41,13 @@ const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
|||||||
{ .name = "ui", .callback = moduleUi },
|
{ .name = "ui", .callback = moduleUi },
|
||||||
{ .name = "text", .callback = moduleText },
|
{ .name = "text", .callback = moduleText },
|
||||||
{ .name = "screen", .callback = moduleScreen },
|
{ .name = "screen", .callback = moduleScreen },
|
||||||
{ .name = "storyflag", .callback = moduleStoryFlag },
|
|
||||||
{ .name = "map", .callback = moduleMap },
|
|
||||||
{ .name = "texture", .callback = moduleTexture },
|
{ .name = "texture", .callback = moduleTexture },
|
||||||
{ .name = "tileset", .callback = moduleTileset },
|
{ .name = "tileset", .callback = moduleTileset },
|
||||||
|
{ .name = "shader", .callback = moduleShader },
|
||||||
|
|
||||||
|
#ifdef SCRIPT_GAME_LIST
|
||||||
|
SCRIPT_GAME_LIST
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#define SCRIPT_MODULE_COUNT ( \
|
#define SCRIPT_MODULE_COUNT ( \
|
||||||
|
|||||||
@@ -157,11 +157,10 @@ errorret_t shaderParamGetLocationGL(
|
|||||||
#ifdef DUSK_OPENGL_LEGACY
|
#ifdef DUSK_OPENGL_LEGACY
|
||||||
assertUnreachable("Cannot get uniform locations on legacy opengl.");
|
assertUnreachable("Cannot get uniform locations on legacy opengl.");
|
||||||
#else
|
#else
|
||||||
shadergl_t *shaderGL = (shadergl_t *)shader;
|
*location = glGetUniformLocation(shader->shaderProgramId, name);
|
||||||
*location = glGetUniformLocation(shaderGL->shaderProgramId, name);
|
errorChain(errorGLCheck());
|
||||||
errorret_t err = errorGLCheck();
|
if(*location == -1) {
|
||||||
if(err.code != ERROR_OK) {
|
errorThrow("Uniform '%s' not found in shader.", name);
|
||||||
errorChain(err);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -174,8 +173,8 @@ errorret_t shaderSetMatrixGL(
|
|||||||
mat4 mat
|
mat4 mat
|
||||||
) {
|
) {
|
||||||
assertNotNull(shader, "Shader cannot be null");
|
assertNotNull(shader, "Shader cannot be null");
|
||||||
assertNotNull(mat, "Matrix data cannot be null");
|
|
||||||
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
assertStrLenMin(name, 1, "Uniform name cannot be empty");
|
||||||
|
assertNotNull(mat, "Matrix data cannot be null");
|
||||||
|
|
||||||
#ifdef DUSK_OPENGL_LEGACY
|
#ifdef DUSK_OPENGL_LEGACY
|
||||||
assertTrue(
|
assertTrue(
|
||||||
|
|||||||
16
src/duskrpg/CMakeLists.txt
Normal file
16
src/duskrpg/CMakeLists.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Includes
|
||||||
|
target_include_directories(${DUSK_LIBRARY_TARGET_NAME}
|
||||||
|
PUBLIC
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Subdirs
|
||||||
|
add_subdirectory(item)
|
||||||
|
add_subdirectory(map)
|
||||||
|
add_subdirectory(story)
|
||||||
|
add_subdirectory(script)
|
||||||
7
src/duskrpg/script/CMakeLists.txt
Normal file
7
src/duskrpg/script/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Subdirs
|
||||||
|
add_subdirectory(module)
|
||||||
9
src/duskrpg/script/module/CMakeLists.txt
Normal file
9
src/duskrpg/script/module/CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Copyright (c) 2026 Dominic Masters
|
||||||
|
#
|
||||||
|
# This software is released under the MIT License.
|
||||||
|
# https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
# Subdirectories
|
||||||
|
add_subdirectory(item)
|
||||||
|
add_subdirectory(map)
|
||||||
|
add_subdirectory(story)
|
||||||
16
src/duskrpg/script/scriptgame.h
Normal file
16
src/duskrpg/script/scriptgame.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/module/item/moduleitem.h"
|
||||||
|
#include "script/module/story/modulestoryflag.h"
|
||||||
|
#include "script/module/map/modulemap.h"
|
||||||
|
|
||||||
|
#define SCRIPT_GAME_LIST \
|
||||||
|
{ .name = "item", .callback = moduleItem }, \
|
||||||
|
{ .name = "storyflag", .callback = moduleStoryFlag }, \
|
||||||
|
{ .name = "map", .callback = moduleMap },
|
||||||
Reference in New Issue
Block a user