Test lua rgb rainbow
All checks were successful
Build Dusk / run-tests (push) Successful in 1m15s
Build Dusk / build-linux (push) Successful in 1m23s
Build Dusk / build-psp (push) Successful in 1m26s
Build Dusk / build-dolphin (push) Successful in 2m6s

This commit is contained in:
2026-02-08 22:30:53 -06:00
parent 3db7e6b1b9
commit 592edb90a0
11 changed files with 150 additions and 14 deletions

View File

@@ -51,5 +51,5 @@ else
end end
end end
-- localeSet(DUSK_LOCALE_EN_US) localeSet(DUSK_LOCALE_EN_US)
sceneSet('scene/initial.dsf') sceneSet('scene/initial.dsf')

View File

@@ -1,15 +1,12 @@
module('spritebatch') module('spritebatch')
module('camera') module('camera')
module('color') module('color')
-- module('ui')
module('text') module('text')
module('screen') module('screen')
module('time') module('time')
-- module('map')
module('glm') module('glm')
screenSetBackground(colorBlack()) screenSetBackground(colorBlack())
-- mapLoad('map/testmap/testmap.dmf')
camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC)
text = "Hello World" text = "Hello World"
@@ -21,11 +18,6 @@ function sceneUpdate()
end end
function sceneRender() function sceneRender()
-- Map Test
-- cameraPushMatrix(mapCamera)
-- mapRender()
-- cameraPopMatrix()
-- UI Test -- UI Test
cameraPushMatrix(camera) cameraPushMatrix(camera)
camera.bottom = screenGetHeight() camera.bottom = screenGetHeight()
@@ -36,7 +28,16 @@ function sceneRender()
x = math.sin(TIME.time * 2) * (x / 2) + (x / 2) x = math.sin(TIME.time * 2) * (x / 2) + (x / 2)
y = (screenGetHeight() - height) / 2 y = (screenGetHeight() - height) / 2
y = math.cos(TIME.time * 3) * (y) + (y) y = math.cos(TIME.time * 3) * (y) + (y)
textDraw(x, y, text, colorMagenta())
-- For each letter
for i = 1, #text do
letter = text:sub(i, i)
letterWidth, _ = textMeasure(letter)
-- Draw letter with rainbow color
textDraw(x, y, letter, colorRainbow((i - 1) * 0.1, 8))
x = x + letterWidth
end
cameraPopMatrix() cameraPopMatrix()
end end

View File

@@ -151,8 +151,7 @@ errorret_t assetInit(void) {
fclose(ASSET.pbpFile); fclose(ASSET.pbpFile);
errorThrow("Failed to read PBP header", pbpPath); errorThrow("Failed to read PBP header", pbpPath);
} }
if(memoryCompare(
if(memoryCompare(
ASSET.pbpHeader.signature, ASSET.pbpHeader.signature,
ASSET_PBP_SIGNATURE, ASSET_PBP_SIGNATURE,
sizeof(ASSET_PBP_SIGNATURE) sizeof(ASSET_PBP_SIGNATURE)

View File

@@ -4,6 +4,7 @@
# https://opensource.org/licenses/MIT # https://opensource.org/licenses/MIT
# Subdirectories # Subdirectories
add_subdirectory(asset)
add_subdirectory(display) add_subdirectory(display)
add_subdirectory(event) add_subdirectory(event)
add_subdirectory(input) add_subdirectory(input)

View 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
moduleasset.c
)

View File

@@ -0,0 +1,59 @@
/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "moduleasset.h"
#include "asset/asset.h"
#include "display/texture.h"
#include "assert/assert.h"
#include "util/memory.h"
void moduleAsset(scriptcontext_t *context) {
assertNotNull(context, "Script context cannot be null");
lua_State *l = context->luaState;
assertNotNull(l, "Lua state cannot be null");
// Create metatable for texture structure.
lua_register(context->luaState, "assetTextureLoad", moduleAssetTextureLoad);
lua_register(context->luaState, "assetTextureDispose", moduleAssetTextureDispose);
}
int moduleAssetTextureLoad(lua_State *l) {
assertNotNull(l, "Lua state cannot be NULL.");
const char_t *filename = luaL_checkstring(l, 2);
assertStrLenMin(filename, 1, "Filename cannot be empty.");
// Create texture owned to lua
texture_t *tex = (texture_t *)lua_newuserdata(l, sizeof(texture_t));
memoryZero(tex, sizeof(texture_t));
errorret_t ret = assetLoad(filename, tex);
if(ret.code != ERROR_OK) {
errorCatch(errorPrint(ret));
luaL_error(l, "Failed to load texture asset: %s", filename);
return 0;
}
// Set metatable
luaL_getmetatable(l, "texture_mt");
lua_setmetatable(l, -2);
// Return the texture
return 1;
}
int moduleAssetTextureDispose(lua_State *l) {
assertNotNull(l, "Lua state cannot be NULL.");
texture_t *tex = (texture_t *)luaL_checkudata(l, 1, "texture_mt");
assertNotNull(tex, "Texture pointer cannot be NULL.");
textureDispose(tex);
return 0;
}

View File

@@ -0,0 +1,20 @@
/**
* 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 asset functions to the given script context.
*
* @param context The script context to register asset functions to.
*/
void moduleAsset(scriptcontext_t *context);
int moduleAssetTextureLoad(lua_State *l);
int moduleAssetTextureDispose(lua_State *l);

View File

@@ -9,6 +9,7 @@
#include "display/color.h" #include "display/color.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/string.h" #include "util/string.h"
#include "time/time.h"
void moduleColor(scriptcontext_t *context) { void moduleColor(scriptcontext_t *context) {
assertNotNull(context, "Context cannot be NULL."); assertNotNull(context, "Context cannot be NULL.");
@@ -31,6 +32,7 @@ void moduleColor(scriptcontext_t *context) {
} }
lua_register(context->luaState, "color", moduleColorFuncColor); lua_register(context->luaState, "color", moduleColorFuncColor);
lua_register(context->luaState, "colorRainbow", moduleColorRainbow);
scriptContextExec(context, COLOR_SCRIPT); scriptContextExec(context, COLOR_SCRIPT);
} }
@@ -147,5 +149,39 @@ int moduleColorToString(lua_State *L) {
color->r, color->g, color->b, color->a color->r, color->g, color->b, color->a
); );
return 1;
}
int moduleColorRainbow(lua_State *L) {
assertNotNull(L, "Lua state cannot be NULL.");
// Allow time offset
float_t t = TIME.time;
if(lua_gettop(L) >= 1) {
if(!lua_isnumber(L, 1)) {
return luaL_error(L, "Rainbow time offset must be a number.");
}
t += (float_t)lua_tonumber(L, 1);
}
// Allow speed multiplier
if(lua_gettop(L) >= 2) {
if(!lua_isnumber(L, 2)) {
return luaL_error(L, "Rainbow speed multiplier must be a number.");
}
t *= (float_t)lua_tonumber(L, 2);
}
// Generate rainbow based on time.
color_t *color = (color_t *)lua_newuserdata(L, sizeof(color_t));
color->r = (colorchannel8_t)((sinf(t) + 1.0f) * 0.5f * 255.0f);
color->g = (colorchannel8_t)((sinf(t + 2.0f) + 1.0f) * 0.5f * 255.0f);
color->b = (colorchannel8_t)((sinf(t + 4.0f) + 1.0f) * 0.5f * 255.0f);
color->a = 255;
// Set metatable
luaL_getmetatable(L, "color_mt");
lua_setmetatable(L, -2);
return 1; return 1;
} }

View File

@@ -44,4 +44,12 @@ int moduleColorNewIndex(lua_State *L);
* @param L The Lua state. * @param L The Lua state.
* @return Number of return values. * @return Number of return values.
*/ */
int moduleColorToString(lua_State *L); int moduleColorToString(lua_State *L);
/**
* Lua function to create a rainbow color based on time.
*
* @param L The Lua state.
* @return Number of return values.
*/
int moduleColorRainbow(lua_State *L);

View File

@@ -28,8 +28,8 @@ int moduleSceneSet(lua_State *L) {
errorret_t err = sceneSet(script); errorret_t err = sceneSet(script);
if(err.code != ERROR_OK) { if(err.code != ERROR_OK) {
luaL_error(L, "Failed to set scene");
errorCatch(errorPrint(err)); errorCatch(errorPrint(err));
luaL_error(L, "Failed to set scene");
return 0; return 0;
} }

View File

@@ -23,6 +23,7 @@
#include "script/module/display/modulescreen.h" #include "script/module/display/modulescreen.h"
#include "script/module/story/modulestoryflag.h" #include "script/module/story/modulestoryflag.h"
#include "script/module/map/modulemap.h" #include "script/module/map/modulemap.h"
#include "script/module/asset/moduleasset.h"
#include "util/string.h" #include "util/string.h"
const scriptmodule_t SCRIPT_MODULE_LIST[] = { const scriptmodule_t SCRIPT_MODULE_LIST[] = {
@@ -43,6 +44,7 @@ const scriptmodule_t SCRIPT_MODULE_LIST[] = {
{ .name = "screen", .callback = moduleScreen }, { .name = "screen", .callback = moduleScreen },
{ .name = "storyflag", .callback = moduleStoryFlag }, { .name = "storyflag", .callback = moduleStoryFlag },
{ .name = "map", .callback = moduleMap }, { .name = "map", .callback = moduleMap },
{ .name = "asset", .callback = moduleAsset },
}; };
#define SCRIPT_MODULE_COUNT ( \ #define SCRIPT_MODULE_COUNT ( \