Test lua rgb rainbow
This commit is contained in:
@@ -51,5 +51,5 @@ else
|
||||
end
|
||||
end
|
||||
|
||||
-- localeSet(DUSK_LOCALE_EN_US)
|
||||
localeSet(DUSK_LOCALE_EN_US)
|
||||
sceneSet('scene/initial.dsf')
|
||||
@@ -1,15 +1,12 @@
|
||||
module('spritebatch')
|
||||
module('camera')
|
||||
module('color')
|
||||
-- module('ui')
|
||||
module('text')
|
||||
module('screen')
|
||||
module('time')
|
||||
-- module('map')
|
||||
module('glm')
|
||||
|
||||
screenSetBackground(colorBlack())
|
||||
-- mapLoad('map/testmap/testmap.dmf')
|
||||
camera = cameraCreate(CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC)
|
||||
|
||||
text = "Hello World"
|
||||
@@ -21,11 +18,6 @@ function sceneUpdate()
|
||||
end
|
||||
|
||||
function sceneRender()
|
||||
-- Map Test
|
||||
-- cameraPushMatrix(mapCamera)
|
||||
-- mapRender()
|
||||
-- cameraPopMatrix()
|
||||
|
||||
-- UI Test
|
||||
cameraPushMatrix(camera)
|
||||
camera.bottom = screenGetHeight()
|
||||
@@ -36,7 +28,16 @@ function sceneRender()
|
||||
x = math.sin(TIME.time * 2) * (x / 2) + (x / 2)
|
||||
y = (screenGetHeight() - height) / 2
|
||||
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()
|
||||
end
|
||||
@@ -151,7 +151,6 @@ errorret_t assetInit(void) {
|
||||
fclose(ASSET.pbpFile);
|
||||
errorThrow("Failed to read PBP header", pbpPath);
|
||||
}
|
||||
|
||||
if(memoryCompare(
|
||||
ASSET.pbpHeader.signature,
|
||||
ASSET_PBP_SIGNATURE,
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Subdirectories
|
||||
add_subdirectory(asset)
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(event)
|
||||
add_subdirectory(input)
|
||||
|
||||
10
src/script/module/asset/CMakeLists.txt
Normal file
10
src/script/module/asset/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
|
||||
moduleasset.c
|
||||
)
|
||||
59
src/script/module/asset/moduleasset.c
Normal file
59
src/script/module/asset/moduleasset.c
Normal 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;
|
||||
}
|
||||
20
src/script/module/asset/moduleasset.h
Normal file
20
src/script/module/asset/moduleasset.h
Normal 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);
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "display/color.h"
|
||||
#include "assert/assert.h"
|
||||
#include "util/string.h"
|
||||
#include "time/time.h"
|
||||
|
||||
void moduleColor(scriptcontext_t *context) {
|
||||
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, "colorRainbow", moduleColorRainbow);
|
||||
|
||||
scriptContextExec(context, COLOR_SCRIPT);
|
||||
}
|
||||
@@ -149,3 +151,37 @@ int moduleColorToString(lua_State *L) {
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -45,3 +45,11 @@ int moduleColorNewIndex(lua_State *L);
|
||||
* @return Number of return values.
|
||||
*/
|
||||
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);
|
||||
@@ -28,8 +28,8 @@ int moduleSceneSet(lua_State *L) {
|
||||
|
||||
errorret_t err = sceneSet(script);
|
||||
if(err.code != ERROR_OK) {
|
||||
luaL_error(L, "Failed to set scene");
|
||||
errorCatch(errorPrint(err));
|
||||
luaL_error(L, "Failed to set scene");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "script/module/display/modulescreen.h"
|
||||
#include "script/module/story/modulestoryflag.h"
|
||||
#include "script/module/map/modulemap.h"
|
||||
#include "script/module/asset/moduleasset.h"
|
||||
#include "util/string.h"
|
||||
|
||||
const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
||||
@@ -43,6 +44,7 @@ const scriptmodule_t SCRIPT_MODULE_LIST[] = {
|
||||
{ .name = "screen", .callback = moduleScreen },
|
||||
{ .name = "storyflag", .callback = moduleStoryFlag },
|
||||
{ .name = "map", .callback = moduleMap },
|
||||
{ .name = "asset", .callback = moduleAsset },
|
||||
};
|
||||
|
||||
#define SCRIPT_MODULE_COUNT ( \
|
||||
|
||||
Reference in New Issue
Block a user