Files
retro3d/examples/sprite_test.ce

118 lines
2.5 KiB
Plaintext

// Sprite Test for retro3d
// Usage: cell run examples/sprite_test.ce <sprite_path>
// sprite_path should be without extension, e.g. "assets/goblin"
// Fills screen with sprites, hue-shifted based on normalized screen position
var time_mod = use('time')
var retro3d = use('core')
var sprite_path = args[0]
if (!sprite_path) {
log.console("Usage: cell run examples/sprite_test.ce <sprite_path>")
log.console(" sprite_path: path without extension (e.g. 'assets/goblin')")
$_.stop()
}
var sprite = null
var last_time = 0
function hsv_to_rgb(h, s, v) {
var c = v * s
var x = c * (1 - Math.abs((h / 60) % 2 - 1))
var m = v - c
var r = 0, g = 0, b = 0
if (h < 60) { r = c; g = x; b = 0 }
else if (h < 120) { r = x; g = c; b = 0 }
else if (h < 180) { r = 0; g = c; b = x }
else if (h < 240) { r = 0; g = x; b = c }
else if (h < 300) { r = x; g = 0; b = c }
else { r = c; g = 0; b = x }
return [r + m, g + m, b + m, 1]
}
function _init() {
log.console("retro3d Sprite Test")
log.console("Loading sprite: " + sprite_path)
retro3d.set_style("ps1")
sprite = retro3d.load_sprite(sprite_path)
if (!sprite) {
log.console("Error: Could not load sprite: " + sprite_path)
$_.stop()
return
}
log.console("Sprite loaded: " + text(sprite.width) + "x" + text(sprite.height))
log.console("Press ESC to exit")
last_time = time_mod.number()
frame()
}
function _update(dt) {
if (retro3d._state.keys_held['escape']) {
$_.stop()
}
}
function _draw() {
retro3d.clear(0.1, 0.1, 0.15, 1.0)
if (!sprite) return
var w = sprite.width
var h = sprite.height
// Fill from bottom to top with sprites
// Hue shifts based on normalized screen position
var y = 0
while (y < 240) {
var x = 0
while (x < 320) {
// Normalized position (0-1)
var nx = x / 320
var ny = y / 240
// Hue based on position (0-360 degrees)
var hue = (nx + ny) * 180 // Combined X and Y influence
hue = hue % 360
var color = hsv_to_rgb(hue, 0.8, 1.0)
retro3d.draw_sprite(sprite, x, y, {
color: color,
mode: "cutout"
})
x = x + w
}
y = y + h
}
}
function frame() {
retro3d._begin_frame()
if (!retro3d._process_events()) {
log.console("Exiting...")
$_.stop()
return
}
var now = time_mod.number()
var dt = now - last_time
last_time = now
_update(dt)
_draw()
retro3d._end_frame()
$_.delay(frame, 1/60)
}
_init()