pxl8/src/lua/pxl8.lua

394 lines
9.9 KiB
Lua
Raw Normal View History

2025-08-13 15:04:49 -05:00
local ffi = require("ffi")
local C = ffi.C
2025-10-04 04:13:48 -05:00
local gfx = _pxl8_gfx
local input = _pxl8_input
2025-08-13 15:04:49 -05:00
-- 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
2025-10-04 04:13:48 -05:00
function pxl8.get_width()
return C.pxl8_gfx_get_width(gfx)
end
function pxl8.get_height()
return C.pxl8_gfx_get_height(gfx)
2025-08-13 15:04:49 -05:00
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
2025-10-05 16:25:17 -05:00
function pxl8.create_texture(pixels, width, height)
local pixel_data = ffi.new("u8[?]", width * height)
for i = 0, width * height - 1 do
pixel_data[i] = pixels[i + 1] or 0
end
local result = C.pxl8_gfx_create_texture(gfx, pixel_data, width, height)
if result < 0 then
return nil
end
return result
end
function pxl8.upload_atlas()
C.pxl8_gfx_upload_atlas(gfx)
end
2025-08-13 15:04:49 -05:00
-- 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
2025-09-28 13:10:29 -05:00
function pxl8.vfx_raster_bars(bars, time)
local c_bars = ffi.new("pxl8_raster_bar[?]", #bars)
2025-08-13 15:04:49 -05:00
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
2025-09-28 13:10:29 -05:00
C.pxl8_vfx_raster_bars(gfx, c_bars, #bars, time)
2025-08-13 15:04:49 -05:00
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)
2025-10-04 04:13:48 -05:00
C.pxl8_vfx_rotozoom(gfx, angle, zoom, cx or pxl8.get_width()/2, cy or pxl8.get_height()/2)
2025-08-13 15:04:49 -05:00
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)
2025-10-04 04:13:48 -05:00
return C.pxl8_particles_create(max_count or 1000)
2025-08-13 15:04:49 -05:00
end
function pxl8.particles_destroy(ps)
2025-10-04 04:13:48 -05:00
C.pxl8_particles_destroy(ps)
2025-08-13 15:04:49 -05:00
end
function pxl8.particles_clear(ps)
2025-10-04 04:13:48 -05:00
C.pxl8_particles_clear(ps)
2025-08-13 15:04:49 -05:00
end
function pxl8.particles_emit(ps, count)
2025-10-04 04:13:48 -05:00
C.pxl8_particles_emit(ps, count or 1)
2025-08-13 15:04:49 -05:00
end
function pxl8.particles_update(ps, dt)
2025-10-04 04:13:48 -05:00
C.pxl8_particles_update(ps, dt)
2025-08-13 15:04:49 -05:00
end
function pxl8.particles_render(ps)
2025-10-04 04:13:48 -05:00
C.pxl8_particles_render(ps, gfx)
2025-08-13 15:04:49 -05:00
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)
2025-10-04 04:13:48 -05:00
C.pxl8_vfx_rain(ps, width or pxl8.get_width(), wind or 0.0)
2025-08-13 15:04:49 -05:00
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)
2025-10-04 04:13:48 -05:00
C.pxl8_vfx_snow(ps, width or pxl8.get_width(), wind or 10.0)
2025-08-13 15:04:49 -05:00
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
2025-09-24 00:39:44 -05:00
function pxl8.tilesheet_new(tile_size)
2025-10-04 04:13:48 -05:00
return C.pxl8_tilesheet_create(tile_size or 16)
2025-09-24 00:39:44 -05:00
end
function pxl8.tilesheet_destroy(tilesheet)
C.pxl8_tilesheet_destroy(tilesheet)
end
function pxl8.tilesheet_load(tilesheet, filepath)
return C.pxl8_tilesheet_load(tilesheet, filepath, gfx)
end
function pxl8.tilemap_new(width, height, tile_size)
2025-10-04 04:13:48 -05:00
return C.pxl8_tilemap_create(width, height, tile_size or 16)
2025-09-24 00:39:44 -05:00
end
function pxl8.tilemap_destroy(tilemap)
C.pxl8_tilemap_destroy(tilemap)
end
function pxl8.tilemap_set_tilesheet(tilemap, tilesheet)
return C.pxl8_tilemap_set_tilesheet(tilemap, tilesheet)
end
function pxl8.tilemap_set_tile(tilemap, layer, x, y, tile_id, flags)
C.pxl8_tilemap_set_tile(tilemap, layer or 0, x, y, tile_id or 0, flags or 0)
end
function pxl8.tilemap_get_tile_id(tilemap, layer, x, y)
return C.pxl8_tilemap_get_tile_id(tilemap, layer or 0, x, y)
end
function pxl8.tilemap_set_camera(tilemap, x, y)
C.pxl8_tilemap_set_camera(tilemap, x, y)
end
function pxl8.tilemap_render(tilemap)
C.pxl8_tilemap_render(tilemap, gfx)
end
function pxl8.tilemap_render_layer(tilemap, layer)
C.pxl8_tilemap_render_layer(tilemap, gfx, layer)
end
function pxl8.tilemap_is_solid(tilemap, x, y)
return C.pxl8_tilemap_is_solid(tilemap, x, y)
end
function pxl8.tilemap_check_collision(tilemap, x, y, w, h)
return C.pxl8_tilemap_check_collision(tilemap, x, y, w, h)
end
pxl8.TILE_FLIP_X = 1
pxl8.TILE_FLIP_Y = 2
pxl8.TILE_SOLID = 4
pxl8.TILE_TRIGGER = 8
2025-10-04 04:13:48 -05:00
function pxl8.clear_zbuffer()
C.pxl8_3d_clear_zbuffer(gfx)
end
function pxl8.set_model(mat)
C.pxl8_3d_set_model(gfx, mat)
end
function pxl8.set_view(mat)
C.pxl8_3d_set_view(gfx, mat)
end
function pxl8.set_projection(mat)
C.pxl8_3d_set_projection(gfx, mat)
end
function pxl8.set_wireframe(wireframe)
C.pxl8_3d_set_wireframe(gfx, wireframe)
end
2025-10-05 16:25:17 -05:00
function pxl8.set_affine_textures(affine)
C.pxl8_3d_set_affine_textures(gfx, affine)
end
2025-10-04 04:13:48 -05:00
function pxl8.set_backface_culling(culling)
C.pxl8_3d_set_backface_culling(gfx, culling)
end
function pxl8.draw_triangle_3d(v0, v1, v2, color)
local vec0 = ffi.new("pxl8_vec3", {x = v0[1], y = v0[2], z = v0[3]})
local vec1 = ffi.new("pxl8_vec3", {x = v1[1], y = v1[2], z = v1[3]})
local vec2 = ffi.new("pxl8_vec3", {x = v2[1], y = v2[2], z = v2[3]})
C.pxl8_3d_draw_triangle_raw(gfx, vec0, vec1, vec2, color)
end
2025-10-05 16:25:17 -05:00
function pxl8.draw_triangle_3d_textured(v0, v1, v2, uv0, uv1, uv2, texture_id)
local vec0 = ffi.new("pxl8_vec3", {x = v0[1], y = v0[2], z = v0[3]})
local vec1 = ffi.new("pxl8_vec3", {x = v1[1], y = v1[2], z = v1[3]})
local vec2 = ffi.new("pxl8_vec3", {x = v2[1], y = v2[2], z = v2[3]})
C.pxl8_3d_draw_triangle_textured(gfx, vec0, vec1, vec2,
uv0[1], uv0[2], uv1[1], uv1[2], uv2[1], uv2[2], texture_id)
end
2025-10-04 04:13:48 -05:00
function pxl8.draw_line_3d(p0, p1, color)
local vec0 = ffi.new("pxl8_vec3", {x = p0[1], y = p0[2], z = p0[3]})
local vec1 = ffi.new("pxl8_vec3", {x = p1[1], y = p1[2], z = p1[3]})
C.pxl8_3d_draw_line_3d(gfx, vec0, vec1, color)
end
function pxl8.mat4_identity()
return C.pxl8_mat4_identity()
end
function pxl8.mat4_multiply(a, b)
return C.pxl8_mat4_multiply(a, b)
end
function pxl8.mat4_translate(x, y, z)
return C.pxl8_mat4_translate(x, y, z)
end
function pxl8.mat4_rotate_x(angle)
return C.pxl8_mat4_rotate_x(angle)
end
function pxl8.mat4_rotate_y(angle)
return C.pxl8_mat4_rotate_y(angle)
end
function pxl8.mat4_rotate_z(angle)
return C.pxl8_mat4_rotate_z(angle)
end
function pxl8.mat4_scale(x, y, z)
return C.pxl8_mat4_scale(x, y, z)
end
function pxl8.mat4_ortho(left, right, bottom, top, near, far)
return C.pxl8_mat4_ortho(left, right, bottom, top, near, far)
end
function pxl8.mat4_perspective(fov, aspect, near, far)
return C.pxl8_mat4_perspective(fov, aspect, near, far)
end
function pxl8.mat4_lookat(eye, center, up)
local eye_vec = ffi.new("pxl8_vec3", {x = eye[1], y = eye[2], z = eye[3]})
local center_vec = ffi.new("pxl8_vec3", {x = center[1], y = center[2], z = center[3]})
local up_vec = ffi.new("pxl8_vec3", {x = up[1], y = up[2], z = up[3]})
return C.pxl8_mat4_lookat(eye_vec, center_vec, up_vec)
end
2025-09-24 00:39:44 -05:00
pxl8.gfx = gfx
pxl8.input = input
2025-10-04 11:55:04 -05:00
pxl8.ui = _pxl8_ui
function pxl8.bounds(x, y, w, h)
return ffi.new("pxl8_bounds", {x = x, y = y, w = w, h = h})
end
function pxl8.ui_window_begin(title, x, y, w, h, options)
local rect = ffi.new("pxl8_bounds", {x = x, y = y, w = w, h = h})
return C.pxl8_ui_window_begin(_pxl8_ui, title, rect, options or 0)
end
function pxl8.ui_window_end()
C.pxl8_ui_window_end(_pxl8_ui)
end
function pxl8.ui_button(label)
return C.pxl8_ui_button(_pxl8_ui, label)
end
function pxl8.ui_label(text)
C.pxl8_ui_label(_pxl8_ui, text)
end
function pxl8.ui_layout_row(item_count, widths, height)
local widths_array = widths
if type(widths) == "table" then
widths_array = ffi.new("int[?]", #widths, widths)
elseif type(widths) == "number" then
widths_array = ffi.new("int[1]", widths)
end
C.pxl8_ui_layout_row(_pxl8_ui, item_count, widths_array, height)
end
2025-09-24 00:39:44 -05:00
2025-08-13 15:04:49 -05:00
return pxl8