initial commit

This commit is contained in:
asrael 2025-08-13 15:04:49 -05:00
commit c18896def0
No known key found for this signature in database
GPG key ID: 2786557804DFAE24
30 changed files with 4183 additions and 0 deletions

192
src/lua/pxl8.lua Normal file
View file

@ -0,0 +1,192 @@
local ffi = require("ffi")
local C = ffi.C
local gfx = _pxl8_gfx_ctx
local input = _pxl8_input_ctx
-- pxl8 lua api
--
local pxl8 = {}
function pxl8.clr(color)
C.pxl8_clr(gfx, color or 0)
end
function pxl8.pixel(x, y, color)
if color then
C.pxl8_pixel(gfx, x, y, color)
else
return C.pxl8_get_pixel(gfx, x, y)
end
end
function pxl8.line(x0, y0, x1, y1, color)
C.pxl8_line(gfx, x0, y0, x1, y1, color)
end
function pxl8.rect(x, y, w, h, color)
C.pxl8_rect(gfx, x, y, w, h, color)
end
function pxl8.rect_fill(x, y, w, h, color)
C.pxl8_rect_fill(gfx, x, y, w, h, color)
end
function pxl8.circle(x, y, r, color)
C.pxl8_circle(gfx, x, y, r, color)
end
function pxl8.circle_fill(x, y, r, color)
C.pxl8_circle_fill(gfx, x, y, r, color)
end
function pxl8.text(str, x, y, color)
C.pxl8_text(gfx, str, x or 0, y or 0, color or 15)
end
function pxl8.sprite(id, x, y, w, h, flip_x, flip_y)
C.pxl8_sprite(gfx, id or 0, x or 0, y or 0, w or 16, h or 16, flip_x or false, flip_y or false)
end
function pxl8.get_screen()
return C.pxl8_get_screen(gfx)
end
function pxl8.load_palette(filepath)
return C.pxl8_gfx_load_palette(gfx, filepath)
end
function pxl8.load_sprite(filepath)
local sprite_id = ffi.new("unsigned int[1]")
local result = C.pxl8_gfx_load_sprite(gfx, filepath, sprite_id)
if result == 0 then
return sprite_id[0]
else
return nil, result
end
end
-- log
function pxl8.info(msg)
C.pxl8_lua_info(msg)
end
function pxl8.warn(msg)
C.pxl8_lua_warn(msg)
end
function pxl8.error(msg)
C.pxl8_lua_error(msg)
end
function pxl8.debug(msg)
C.pxl8_lua_debug(msg)
end
function pxl8.trace(msg)
C.pxl8_lua_trace(msg)
end
function pxl8.key_down(key)
if type(key) == "string" then
key = string.byte(key)
end
return C.pxl8_key_down(input, key)
end
function pxl8.key_pressed(key)
if type(key) == "string" then
key = string.byte(key)
end
return C.pxl8_key_pressed(input, key)
end
function pxl8.vfx_copper_bars(bars, time)
local c_bars = ffi.new("pxl8_copper_bar[?]", #bars)
for i, bar in ipairs(bars) do
c_bars[i-1].base_y = bar.base_y or 0
c_bars[i-1].amplitude = bar.amplitude or 10
c_bars[i-1].height = bar.height or 5
c_bars[i-1].speed = bar.speed or 1.0
c_bars[i-1].phase = bar.phase or 0
c_bars[i-1].color = bar.color or 15
c_bars[i-1].fade_color = bar.fade_color or bar.color or 15
end
C.pxl8_vfx_copper_bars(gfx, c_bars, #bars, time)
end
function pxl8.vfx_plasma(time, scale1, scale2, palette_offset)
C.pxl8_vfx_plasma(gfx, time, scale1 or 0.05, scale2 or 0.03, palette_offset or 0)
end
function pxl8.vfx_rotozoom(angle, zoom, cx, cy)
local screen = pxl8.get_screen()
C.pxl8_vfx_rotozoom(gfx, angle, zoom, cx or screen.width/2, cy or screen.height/2)
end
function pxl8.vfx_tunnel(time, speed, twist)
C.pxl8_vfx_tunnel(gfx, time, speed or 2.0, twist or 0.5)
end
function pxl8.particles_new(max_count)
local ps = ffi.new("pxl8_particle_system")
C.pxl8_vfx_particles_init(ps, max_count or 1000)
return ps
end
function pxl8.particles_destroy(ps)
C.pxl8_vfx_particles_destroy(ps)
end
function pxl8.particles_clear(ps)
C.pxl8_vfx_particles_clear(ps)
end
function pxl8.particles_emit(ps, count)
C.pxl8_vfx_particles_emit(ps, count or 1)
end
function pxl8.particles_update(ps, dt)
C.pxl8_vfx_particles_update(ps, dt)
end
function pxl8.particles_render(ps)
C.pxl8_vfx_particles_render(ps, gfx)
end
function pxl8.vfx_explosion(ps, x, y, color, force)
C.pxl8_vfx_explosion(ps, x, y, color or 15, force or 200.0)
end
function pxl8.vfx_fire(ps, x, y, width, palette_start)
C.pxl8_vfx_fire(ps, x, y, width or 50, palette_start or 64)
end
function pxl8.vfx_rain(ps, width, wind)
C.pxl8_vfx_rain(ps, width or pxl8.get_screen().width, wind or 0.0)
end
function pxl8.vfx_smoke(ps, x, y, color)
C.pxl8_vfx_smoke(ps, x, y, color or 8)
end
function pxl8.vfx_snow(ps, width, wind)
C.pxl8_vfx_snow(ps, width or pxl8.get_screen().width, wind or 10.0)
end
function pxl8.vfx_sparks(ps, x, y, color)
C.pxl8_vfx_sparks(ps, x, y, color or 15)
end
function pxl8.vfx_starfield(ps, speed, spread)
C.pxl8_vfx_starfield(ps, speed or 5.0, spread or 500.0)
end
function pxl8.gfx_color_ramp(start, count, from_color, to_color)
C.pxl8_gfx_color_ramp(gfx, start, count, from_color, to_color)
end
function pxl8.gfx_fade_palette(start, count, amount, target_color)
C.pxl8_gfx_fade_palette(gfx, start, count, amount, target_color)
end
return pxl8