add fps util for pxl8 in lua

This commit is contained in:
asrael 2025-11-11 23:26:51 -06:00
parent 7dd32ec453
commit d40007aad6
6 changed files with 53 additions and 59 deletions

View file

@ -1,18 +1,12 @@
(local pxl8 (require :pxl8)) (local pxl8 (require :pxl8))
(local debug-ui (require :mod.debug_ui))
(var world nil) (var world nil)
(var cam-x 1000) (var cam-x 1000)
(var cam-y 64) (local cam-y 64)
(var cam-z 1000) (var cam-z 1000)
(var cam-yaw 0) (var cam-yaw 0)
(var cam-pitch 0) (var cam-pitch 0)
(var bob-time 0) (var bob-time 0)
(var show-debug-ui false)
(var affine false)
(var fps 0)
(var fps-accumulator 0)
(var fps-frame-count 0)
(local move-speed 200) (local move-speed 200)
(local turn-speed 2.0) (local turn-speed 2.0)
@ -33,51 +27,38 @@
:iterations 4})] :iterations 4})]
(if (< result 0) (if (< result 0)
(pxl8.error (.. "Failed to generate cave - result: " result)) (pxl8.error (.. "Failed to generate cave - result: " result))
(do (let [floor-tex (pxl8.procgen_tex {:name "floor"
(let [floor-tex (pxl8.procgen_tex {:name "floor" :seed 11111
:seed 11111 :width 64
:width 64 :height 64
:height 64 :base_color 19})
:base_color 20}) ceiling-tex (pxl8.procgen_tex {:name "ceiling"
ceiling-tex (pxl8.procgen_tex {:name "ceiling" :seed 22222
:seed 22222 :width 64
:width 64 :height 64
:height 64 :base_color 1})
:base_color 0}) wall-tex (pxl8.procgen_tex {:name "wall"
wall-tex (pxl8.procgen_tex {:name "wall" :seed 12345
:seed 12345 :width 64
:width 64 :height 64
:height 64 :base_color 4})]
:base_color 4})]
(pxl8.upload_atlas) (pxl8.upload_atlas)
(let [result (pxl8.world_apply_textures world [ (let [result (pxl8.world_apply_textures world [
{:name "floor" {:name "floor"
:texture_id floor-tex :texture_id floor-tex
:rule (fn [normal] (> normal.y 0.7))} :rule (fn [normal] (> normal.y 0.7))}
{:name "ceiling" {:name "ceiling"
:texture_id ceiling-tex :texture_id ceiling-tex
:rule (fn [normal] (< normal.y -0.7))} :rule (fn [normal] (< normal.y -0.7))}
{:name "wall" {:name "wall"
:texture_id wall-tex :texture_id wall-tex
:rule (fn [normal] (and (<= normal.y 0.7) (>= normal.y -0.7)))}])] :rule (fn [normal] (and (<= normal.y 0.7) (>= normal.y -0.7)))}])]
(when (< result 0) (when (< result 0)
(pxl8.error (.. "Failed to apply textures - result: " result))))))))) (pxl8.error (.. "Failed to apply textures - result: " result))))))))
(fn update [dt] (fn update [dt]
(set fps-accumulator (+ fps-accumulator dt))
(set fps-frame-count (+ fps-frame-count 1))
(when (>= fps-accumulator 0.25)
(set fps (/ fps-frame-count fps-accumulator))
(set fps-accumulator 0)
(set fps-frame-count 0))
(when (pxl8.key_pressed "F8")
(set show-debug-ui (not show-debug-ui))
(pxl8.ui_window_set_open "Debug Menu (F8)" show-debug-ui))
(when (pxl8.world_is_loaded world) (when (pxl8.world_is_loaded world)
(let [forward-x (- (math.sin cam-yaw)) (let [forward-x (- (math.sin cam-yaw))
forward-z (- (math.cos cam-yaw)) forward-z (- (math.cos cam-yaw))
@ -166,22 +147,12 @@
(pxl8.set_model (pxl8.mat4_identity)) (pxl8.set_model (pxl8.mat4_identity))
(pxl8.set_affine_textures affine)
(pxl8.world_render world [cam-x eye-y cam-z]) (pxl8.world_render world [cam-x eye-y cam-z])
(pxl8.text (.. "Pos: " (string.format "%.0f" cam-x) "," (pxl8.text (.. "Pos: " (string.format "%.0f" cam-x) ","
(string.format "%.0f" cam-y) "," (string.format "%.0f" cam-y) ","
(string.format "%.0f" cam-z)) 10 25 12) (string.format "%.0f" cam-z)) 10 25 12)
(pxl8.text (.. "FPS: " (string.format "%.1f" (pxl8.get_fps))) 10 40 12))))
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui
:fps fps
:wireframe false
:auto-rotate false
:orthographic false
:use-texture true
:affine affine})]
(when (not= new-state.show-debug-ui nil) (set show-debug-ui new-state.show-debug-ui))
(when (not= new-state.affine nil) (set affine new-state.affine))))))
{:init init {:init init
:update update :update update

View file

@ -1,5 +1,6 @@
local ffi = require("ffi") local ffi = require("ffi")
local C = ffi.C local C = ffi.C
local game = _pxl8_game
local gfx = _pxl8_gfx local gfx = _pxl8_gfx
local input = _pxl8_input local input = _pxl8_input
local ui = _pxl8_ui local ui = _pxl8_ui
@ -46,6 +47,10 @@ 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) 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 end
function pxl8.get_fps()
return C.pxl8_game_get_fps(game)
end
function pxl8.get_width() function pxl8.get_width()
return C.pxl8_gfx_get_width(gfx) return C.pxl8_gfx_get_width(gfx)
end end

View file

@ -119,6 +119,7 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]) {
pxl8_script_set_gfx(game->script, game->gfx); pxl8_script_set_gfx(game->script, game->gfx);
pxl8_script_set_input(game->script, &game->input); pxl8_script_set_input(game->script, &game->input);
pxl8_script_set_ui(game->script, game->ui); pxl8_script_set_ui(game->script, game->ui);
pxl8_script_set_game(game->script, game);
if (game->script_path[0] != '\0') { if (game->script_path[0] != '\0') {
pxl8_result result = pxl8_script_load_main(game->script, game->script_path); pxl8_result result = pxl8_script_load_main(game->script, game->script_path);
@ -307,3 +308,7 @@ void pxl8_quit(pxl8_game* game) {
pxl8_script_destroy(game->script); pxl8_script_destroy(game->script);
if (game->ui) pxl8_ui_destroy(game->ui); if (game->ui) pxl8_ui_destroy(game->ui);
} }
f32 pxl8_game_get_fps(const pxl8_game* game) {
return game ? game->fps : 0.0f;
}

View file

@ -52,3 +52,4 @@ pxl8_game_result pxl8_init(pxl8_game* game, i32 argc, char* argv[]);
pxl8_game_result pxl8_update(pxl8_game* game); pxl8_game_result pxl8_update(pxl8_game* game);
pxl8_game_result pxl8_frame(pxl8_game* game); pxl8_game_result pxl8_frame(pxl8_game* game);
void pxl8_quit(pxl8_game* game); void pxl8_quit(pxl8_game* game);
f32 pxl8_game_get_fps(const pxl8_game* game);

View file

@ -79,9 +79,12 @@ static const char* pxl8_ffi_cdefs =
"typedef float f32;\n" "typedef float f32;\n"
"typedef double f64;\n" "typedef double f64;\n"
"typedef struct pxl8_gfx pxl8_gfx;\n" "typedef struct pxl8_gfx pxl8_gfx;\n"
"typedef struct pxl8_game pxl8_game;\n"
"typedef struct { int x, y, w, h; } pxl8_bounds;\n" "typedef struct { int x, y, w, h; } pxl8_bounds;\n"
"typedef struct { int x, y; } pxl8_point;\n" "typedef struct { int x, y; } pxl8_point;\n"
"\n" "\n"
"f32 pxl8_game_get_fps(const pxl8_game* game);\n"
"\n"
"i32 pxl8_gfx_get_height(pxl8_gfx* ctx);\n" "i32 pxl8_gfx_get_height(pxl8_gfx* ctx);\n"
"i32 pxl8_gfx_get_width(pxl8_gfx* ctx);\n" "i32 pxl8_gfx_get_width(pxl8_gfx* ctx);\n"
"void pxl8_circle(pxl8_gfx* ctx, i32 x, i32 y, i32 r, u32 color);\n" "void pxl8_circle(pxl8_gfx* ctx, i32 x, i32 y, i32 r, u32 color);\n"
@ -408,6 +411,14 @@ void pxl8_script_set_ui(pxl8_script* script, pxl8_ui* ui) {
} }
} }
void pxl8_script_set_game(pxl8_script* script, void* game) {
if (!script) return;
if (script->L && game) {
lua_pushlightuserdata(script->L, game);
lua_setglobal(script->L, "_pxl8_game");
}
}
static pxl8_result pxl8_script_prepare_path(pxl8_script* script, const char* filename, char* out_basename, size_t basename_size) { static pxl8_result pxl8_script_prepare_path(pxl8_script* script, const char* filename, char* out_basename, size_t basename_size) {
char filename_copy[PATH_MAX]; char filename_copy[PATH_MAX];
strncpy(filename_copy, filename, sizeof(filename_copy) - 1); strncpy(filename_copy, filename, sizeof(filename_copy) - 1);

View file

@ -20,6 +20,7 @@ void pxl8_script_set_cart_path(pxl8_script* script, const char* cart_path, const
void pxl8_script_set_gfx(pxl8_script* script, pxl8_gfx* gfx); void pxl8_script_set_gfx(pxl8_script* script, pxl8_gfx* gfx);
void pxl8_script_set_input(pxl8_script* script, pxl8_input_state* input); void pxl8_script_set_input(pxl8_script* script, pxl8_input_state* input);
void pxl8_script_set_ui(pxl8_script* script, pxl8_ui* ui); void pxl8_script_set_ui(pxl8_script* script, pxl8_ui* ui);
void pxl8_script_set_game(pxl8_script* script, void* game);
pxl8_result pxl8_script_call_function(pxl8_script* script, const char* name); pxl8_result pxl8_script_call_function(pxl8_script* script, const char* name);
pxl8_result pxl8_script_call_function_f32(pxl8_script* script, const char* name, f32 arg); pxl8_result pxl8_script_call_function_f32(pxl8_script* script, const char* name, f32 arg);