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 debug-ui (require :mod.debug_ui))
(var world nil)
(var cam-x 1000)
(var cam-y 64)
(local cam-y 64)
(var cam-z 1000)
(var cam-yaw 0)
(var cam-pitch 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 turn-speed 2.0)
@ -33,51 +27,38 @@
:iterations 4})]
(if (< result 0)
(pxl8.error (.. "Failed to generate cave - result: " result))
(do
(let [floor-tex (pxl8.procgen_tex {:name "floor"
:seed 11111
:width 64
:height 64
:base_color 20})
ceiling-tex (pxl8.procgen_tex {:name "ceiling"
:seed 22222
:width 64
:height 64
:base_color 0})
wall-tex (pxl8.procgen_tex {:name "wall"
:seed 12345
:width 64
:height 64
:base_color 4})]
(let [floor-tex (pxl8.procgen_tex {:name "floor"
:seed 11111
:width 64
:height 64
:base_color 19})
ceiling-tex (pxl8.procgen_tex {:name "ceiling"
:seed 22222
:width 64
:height 64
:base_color 1})
wall-tex (pxl8.procgen_tex {:name "wall"
:seed 12345
:width 64
:height 64
:base_color 4})]
(pxl8.upload_atlas)
(pxl8.upload_atlas)
(let [result (pxl8.world_apply_textures world [
{:name "floor"
:texture_id floor-tex
:rule (fn [normal] (> normal.y 0.7))}
{:name "ceiling"
:texture_id ceiling-tex
:rule (fn [normal] (< normal.y -0.7))}
{:name "wall"
:texture_id wall-tex
:rule (fn [normal] (and (<= normal.y 0.7) (>= normal.y -0.7)))}])]
(when (< result 0)
(pxl8.error (.. "Failed to apply textures - result: " result)))))))))
(let [result (pxl8.world_apply_textures world [
{:name "floor"
:texture_id floor-tex
:rule (fn [normal] (> normal.y 0.7))}
{:name "ceiling"
:texture_id ceiling-tex
:rule (fn [normal] (< normal.y -0.7))}
{:name "wall"
:texture_id wall-tex
:rule (fn [normal] (and (<= normal.y 0.7) (>= normal.y -0.7)))}])]
(when (< result 0)
(pxl8.error (.. "Failed to apply textures - result: " result))))))))
(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)
(let [forward-x (- (math.sin cam-yaw))
forward-z (- (math.cos cam-yaw))
@ -166,22 +147,12 @@
(pxl8.set_model (pxl8.mat4_identity))
(pxl8.set_affine_textures affine)
(pxl8.world_render world [cam-x eye-y cam-z])
(pxl8.text (.. "Pos: " (string.format "%.0f" cam-x) ","
(string.format "%.0f" cam-y) ","
(string.format "%.0f" cam-z)) 10 25 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))))))
(pxl8.text (.. "FPS: " (string.format "%.1f" (pxl8.get_fps))) 10 40 12))))
{:init init
:update update