add sprite flipping, cleanup some lua things

This commit is contained in:
asrael 2026-01-08 12:17:40 -06:00
parent 15041984f1
commit b659523d69
8 changed files with 48 additions and 27 deletions

19
.luarc.json Normal file
View file

@ -0,0 +1,19 @@
{
"runtime": {
"version": "LuaJIT"
},
"diagnostics": {
"globals": [
"pxl8",
"pxl8_gfx",
"pxl8_input",
"pxl8_rng",
"pxl8_sfx",
"pxl8_sys"
],
"disable": [
"lowercase-global",
"unbalanced-assignments"
]
}
}

View file

@ -95,7 +95,7 @@
:logo (do :logo (do
(pxl8.clear 0) (pxl8.clear 0)
(when logo-sprite (when logo-sprite
(pxl8.sprite logo-sprite logo-x logo-y 128 64))) (pxl8.sprite logo-sprite logo-x logo-y 128 64 (< logo-dx 0) (< logo-dy 0))))
:plasma (pxl8.vfx_plasma time 0.10 0.04 1) :plasma (pxl8.vfx_plasma time 0.10 0.04 1)

View file

@ -1,5 +1,3 @@
local ffi = require("ffi")
local core = require("pxl8.core") local core = require("pxl8.core")
local gfx2d = require("pxl8.gfx2d") local gfx2d = require("pxl8.gfx2d")
local input = require("pxl8.input") local input = require("pxl8.input")
@ -13,11 +11,10 @@ local world = require("pxl8.world")
local transition = require("pxl8.transition") local transition = require("pxl8.transition")
local anim = require("pxl8.anim") local anim = require("pxl8.anim")
local sfx = require("pxl8.sfx") local sfx = require("pxl8.sfx")
core.init(_pxl8_gfx, _pxl8_input, _pxl8_rng, _pxl8_sfx_mixer, _pxl8_sys)
local pxl8 = {} local pxl8 = {}
core.init(pxl8_gfx, pxl8_input, pxl8_rng, pxl8_sfx, pxl8_sys)
pxl8.get_fps = core.get_fps pxl8.get_fps = core.get_fps
pxl8.get_height = core.get_height pxl8.get_height = core.get_height
pxl8.get_title = core.get_title pxl8.get_title = core.get_title

View file

@ -3,11 +3,11 @@ local C = ffi.C
local core = {} local core = {}
function core.init(gfx, input, rng, sfx_mixer, sys) function core.init(gfx, input, rng, sfx, sys)
core.gfx = gfx core.gfx = gfx
core.input = input core.input = input
core.rng = rng core.rng = rng
core.sfx_mixer = sfx_mixer core.sfx = sfx
core.sys = sys core.sys = sys
end end

View file

@ -105,15 +105,15 @@ function sfx.delay_set_time(node, time_l, time_r)
end end
function sfx.get_master_volume() function sfx.get_master_volume()
return C.pxl8_sfx_mixer_get_master_volume(core.sfx_mixer) return C.pxl8_sfx_mixer_get_master_volume(core.sfx)
end end
function sfx.mixer_attach(ctx) function sfx.mixer_attach(ctx)
C.pxl8_sfx_mixer_attach(core.sfx_mixer, ctx) C.pxl8_sfx_mixer_attach(core.sfx, ctx)
end end
function sfx.mixer_detach(ctx) function sfx.mixer_detach(ctx)
C.pxl8_sfx_mixer_detach(core.sfx_mixer, ctx) C.pxl8_sfx_mixer_detach(core.sfx, ctx)
end end
function sfx.node_destroy(node) function sfx.node_destroy(node)
@ -154,7 +154,7 @@ function sfx.reverb_set_room(node, room)
end end
function sfx.set_master_volume(volume) function sfx.set_master_volume(volume)
C.pxl8_sfx_mixer_set_master_volume(core.sfx_mixer, volume) C.pxl8_sfx_mixer_set_master_volume(core.sfx, volume)
end end
function sfx.stop_all(ctx) function sfx.stop_all(ctx)

View file

@ -590,7 +590,7 @@ void pxl8_text(pxl8_gfx* gfx, const char* text, i32 x, i32 y, u32 color) {
} }
void pxl8_sprite(pxl8_gfx* gfx, u32 sprite_id, i32 x, i32 y, i32 w, i32 h) { void pxl8_sprite(pxl8_gfx* gfx, u32 sprite_id, i32 x, i32 y, i32 w, i32 h, bool flip_x, bool flip_y) {
if (!gfx || !gfx->atlas || !gfx->framebuffer) return; if (!gfx || !gfx->atlas || !gfx->framebuffer) return;
const pxl8_atlas_entry* entry = pxl8_atlas_get_entry(gfx->atlas, sprite_id); const pxl8_atlas_entry* entry = pxl8_atlas_get_entry(gfx->atlas, sprite_id);
@ -611,11 +611,12 @@ void pxl8_sprite(pxl8_gfx* gfx, u32 sprite_id, i32 x, i32 y, i32 w, i32 h) {
bool is_1to1_scale = (w == entry->w && h == entry->h); bool is_1to1_scale = (w == entry->w && h == entry->h);
bool is_unclipped = (clip_left == 0 && clip_top == 0 && clip_right == 0 && clip_bottom == 0); bool is_unclipped = (clip_left == 0 && clip_top == 0 && clip_right == 0 && clip_bottom == 0);
bool is_flipped = flip_x || flip_y;
u32 atlas_width = pxl8_atlas_get_width(gfx->atlas); u32 atlas_width = pxl8_atlas_get_width(gfx->atlas);
const u8* atlas_pixels = pxl8_atlas_get_pixels(gfx->atlas); const u8* atlas_pixels = pxl8_atlas_get_pixels(gfx->atlas);
if (is_1to1_scale && is_unclipped) { if (is_1to1_scale && is_unclipped && !is_flipped) {
if (gfx->pixel_mode == PXL8_PIXEL_HICOLOR) { if (gfx->pixel_mode == PXL8_PIXEL_HICOLOR) {
const u16* sprite_data = (const u16*)atlas_pixels + entry->y * atlas_width + entry->x; const u16* sprite_data = (const u16*)atlas_pixels + entry->y * atlas_width + entry->x;
pxl8_blit_hicolor( pxl8_blit_hicolor(
@ -638,8 +639,12 @@ void pxl8_sprite(pxl8_gfx* gfx, u32 sprite_id, i32 x, i32 y, i32 w, i32 h) {
} else { } else {
for (i32 py = 0; py < draw_height; py++) { for (i32 py = 0; py < draw_height; py++) {
for (i32 px = 0; px < draw_width; px++) { for (i32 px = 0; px < draw_width; px++) {
i32 src_x = entry->x + ((px + clip_left) * entry->w) / w; i32 local_x = (px + clip_left) * entry->w / w;
i32 src_y = entry->y + ((py + clip_top) * entry->h) / h; i32 local_y = (py + clip_top) * entry->h / h;
i32 src_x = flip_x ? entry->x + entry->w - 1 - local_x : entry->x + local_x;
i32 src_y = flip_y ? entry->y + entry->h - 1 - local_y : entry->y + local_y;
i32 src_idx = src_y * atlas_width + src_x; i32 src_idx = src_y * atlas_width + src_x;
i32 dest_idx = (dest_y + py) * gfx->framebuffer_width + (dest_x + px); i32 dest_idx = (dest_y + py) * gfx->framebuffer_width + (dest_x + px);

View file

@ -65,7 +65,7 @@ void pxl8_line(pxl8_gfx* gfx, i32 x0, i32 y0, i32 x1, i32 y1, u32 color);
void pxl8_pixel(pxl8_gfx* gfx, i32 x, i32 y, u32 color); void pxl8_pixel(pxl8_gfx* gfx, i32 x, i32 y, u32 color);
void pxl8_rect(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, u32 color); void pxl8_rect(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, u32 color);
void pxl8_rect_fill(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, u32 color); void pxl8_rect_fill(pxl8_gfx* gfx, i32 x, i32 y, i32 w, i32 h, u32 color);
void pxl8_sprite(pxl8_gfx* gfx, u32 sprite_id, i32 x, i32 y, i32 w, i32 h); void pxl8_sprite(pxl8_gfx* gfx, u32 sprite_id, i32 x, i32 y, i32 w, i32 h, bool flip_x, bool flip_y);
void pxl8_text(pxl8_gfx* gfx, const char* text, i32 x, i32 y, u32 color); void pxl8_text(pxl8_gfx* gfx, const char* text, i32 x, i32 y, u32 color);
void pxl8_3d_clear_zbuffer(pxl8_gfx* gfx); void pxl8_3d_clear_zbuffer(pxl8_gfx* gfx);

View file

@ -677,7 +677,7 @@ void pxl8_script_set_gfx(pxl8_script* script, pxl8_gfx* gfx) {
script->gfx = gfx; script->gfx = gfx;
if (script->L && gfx) { if (script->L && gfx) {
lua_pushlightuserdata(script->L, gfx); lua_pushlightuserdata(script->L, gfx);
lua_setglobal(script->L, "_pxl8_gfx"); lua_setglobal(script->L, "pxl8_gfx");
} }
} }
@ -686,7 +686,7 @@ void pxl8_script_set_input(pxl8_script* script, pxl8_input_state* input) {
script->input = input; script->input = input;
if (script->L && input) { if (script->L && input) {
lua_pushlightuserdata(script->L, input); lua_pushlightuserdata(script->L, input);
lua_setglobal(script->L, "_pxl8_input"); lua_setglobal(script->L, "pxl8_input");
} }
} }
@ -694,7 +694,7 @@ void pxl8_script_set_rng(pxl8_script* script, void* rng) {
if (!script) return; if (!script) return;
if (script->L && rng) { if (script->L && rng) {
lua_pushlightuserdata(script->L, rng); lua_pushlightuserdata(script->L, rng);
lua_setglobal(script->L, "_pxl8_rng"); lua_setglobal(script->L, "pxl8_rng");
} }
} }
@ -703,7 +703,7 @@ void pxl8_script_set_sfx(pxl8_script* script, pxl8_sfx_mixer* mixer) {
script->mixer = mixer; script->mixer = mixer;
if (script->L && mixer) { if (script->L && mixer) {
lua_pushlightuserdata(script->L, mixer); lua_pushlightuserdata(script->L, mixer);
lua_setglobal(script->L, "_pxl8_sfx_mixer"); lua_setglobal(script->L, "pxl8_sfx");
} }
} }
@ -711,7 +711,7 @@ void pxl8_script_set_sys(pxl8_script* script, void* sys) {
if (!script) return; if (!script) return;
if (script->L && sys) { if (script->L && sys) {
lua_pushlightuserdata(script->L, sys); lua_pushlightuserdata(script->L, sys);
lua_setglobal(script->L, "_pxl8_sys"); lua_setglobal(script->L, "pxl8_sys");
} }
} }
@ -1049,7 +1049,7 @@ static bool pxl8_script_is_builtin_global(const char* name) {
"print", "pxl8", "rawequal", "rawget", "rawset", "require", "print", "pxl8", "rawequal", "rawget", "rawset", "require",
"select", "setfenv", "setmetatable", "string", "table", "select", "setfenv", "setmetatable", "string", "table",
"tonumber", "tostring", "type", "unpack", "update", "frame", "tonumber", "tostring", "type", "unpack", "update", "frame",
"xpcall", "_pxl8_gfx", "_pxl8_input", "_pxl8_rng", "_pxl8_sfx_mixer", "_pxl8_sys", "xpcall", "pxl8_gfx", "pxl8_input", "pxl8_rng", "pxl8_sfx", "pxl8_sys",
NULL NULL
}; };
for (int i = 0; builtins[i]; i++) { for (int i = 0; builtins[i]; i++) {