implement our own gui module, drop microui
This commit is contained in:
parent
2555bec8eb
commit
8baf5f06ea
25 changed files with 495 additions and 507 deletions
|
|
@ -1,5 +1,5 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local cube3d (require :mod.cube3d))
|
||||
(local menu (require :mod.menu))
|
||||
(local worldgen (require :mod.worldgen))
|
||||
|
||||
(var time 0)
|
||||
|
|
@ -29,53 +29,59 @@
|
|||
(pxl8.load_palette "res/sprites/pxl8_logo.ase")
|
||||
(set logo-sprite (pxl8.load_sprite "res/sprites/pxl8_logo.ase"))
|
||||
(set particles (pxl8.particles_new 1000))
|
||||
(cube3d.init)
|
||||
(worldgen.init)))
|
||||
|
||||
(global update (fn [dt]
|
||||
(set time (+ time dt))
|
||||
(when (pxl8.key_pressed "escape")
|
||||
(menu.toggle)
|
||||
(when (= active-demo :worldgen)
|
||||
(pxl8.set_relative_mouse_mode (not (menu.is-paused)))))
|
||||
|
||||
(when transition
|
||||
(pxl8.transition_update transition dt)
|
||||
(when (pxl8.transition_is_complete transition)
|
||||
(when transition-pending
|
||||
(when (and (= active-demo :worldgen) (not= transition-pending :worldgen))
|
||||
(pxl8.set_relative_mouse_mode false))
|
||||
(set active-demo transition-pending)
|
||||
(set transition-pending nil)
|
||||
(when (= active-demo :fire) (set fire-init? false))
|
||||
(when (= active-demo :rain) (set rain-init? false))
|
||||
(when (= active-demo :snow) (set snow-init? false)))
|
||||
(pxl8.transition_destroy transition)
|
||||
(set transition nil)))
|
||||
(when (not (menu.is-paused))
|
||||
(set time (+ time dt))
|
||||
|
||||
(when (pxl8.key_pressed "1") (switch-demo :logo))
|
||||
(when (pxl8.key_pressed "2") (switch-demo :plasma))
|
||||
(when (pxl8.key_pressed "3") (switch-demo :tunnel))
|
||||
(when (pxl8.key_pressed "4") (switch-demo :raster))
|
||||
(when (pxl8.key_pressed "5") (switch-demo :fire))
|
||||
(when (pxl8.key_pressed "6") (switch-demo :rain))
|
||||
(when (pxl8.key_pressed "7") (switch-demo :snow))
|
||||
(when (pxl8.key_pressed "8") (switch-demo :cube3d))
|
||||
(when (pxl8.key_pressed "9") (switch-demo :worldgen))
|
||||
(when (pxl8.key_pressed "=")
|
||||
(set use-famicube-palette? (not use-famicube-palette?))
|
||||
(local palette-path (if use-famicube-palette? "res/palettes/famicube.ase" "res/sprites/pxl8_logo.ase"))
|
||||
(pxl8.load_palette palette-path))
|
||||
(when transition
|
||||
(pxl8.transition_update transition dt)
|
||||
(when (pxl8.transition_is_complete transition)
|
||||
(when transition-pending
|
||||
(when (and (= active-demo :worldgen) (not= transition-pending :worldgen))
|
||||
(pxl8.set_relative_mouse_mode false))
|
||||
(set active-demo transition-pending)
|
||||
(set transition-pending nil)
|
||||
(when (= active-demo :fire) (set fire-init? false))
|
||||
(when (= active-demo :rain) (set rain-init? false))
|
||||
(when (= active-demo :snow) (set snow-init? false)))
|
||||
(pxl8.transition_destroy transition)
|
||||
(set transition nil)))
|
||||
|
||||
(case active-demo
|
||||
:logo (do
|
||||
(set logo-x (+ logo-x (* logo-dx dt)))
|
||||
(set logo-y (+ logo-y (* logo-dy dt)))
|
||||
(when (or (< logo-x 0) (> logo-x 512))
|
||||
(set logo-dx (- logo-dx)))
|
||||
(when (or (< logo-y 0) (> logo-y 296))
|
||||
(set logo-dy (- logo-dy))))
|
||||
:cube3d (cube3d.update dt)
|
||||
:worldgen (worldgen.update dt))
|
||||
(when (pxl8.key_pressed "1") (switch-demo :logo))
|
||||
(when (pxl8.key_pressed "2") (switch-demo :plasma))
|
||||
(when (pxl8.key_pressed "3") (switch-demo :tunnel))
|
||||
(when (pxl8.key_pressed "4") (switch-demo :raster))
|
||||
(when (pxl8.key_pressed "5") (switch-demo :fire))
|
||||
(when (pxl8.key_pressed "6") (switch-demo :rain))
|
||||
(when (pxl8.key_pressed "7") (switch-demo :snow))
|
||||
(when (pxl8.key_pressed "8") (switch-demo :worldgen))
|
||||
(when (pxl8.key_pressed "=")
|
||||
(set use-famicube-palette? (not use-famicube-palette?))
|
||||
(local palette-path (if use-famicube-palette? "res/palettes/famicube.ase" "res/sprites/pxl8_logo.ase"))
|
||||
(pxl8.load_palette palette-path))
|
||||
|
||||
(when particles
|
||||
(pxl8.particles_update particles dt))))
|
||||
(case active-demo
|
||||
:logo (do
|
||||
(set logo-x (+ logo-x (* logo-dx dt)))
|
||||
(set logo-y (+ logo-y (* logo-dy dt)))
|
||||
(when (or (< logo-x 0) (> logo-x 512))
|
||||
(set logo-dx (- logo-dx)))
|
||||
(when (or (< logo-y 0) (> logo-y 296))
|
||||
(set logo-dy (- logo-dy))))
|
||||
:worldgen (worldgen.update dt))
|
||||
|
||||
(when particles
|
||||
(pxl8.particles_update particles dt)))
|
||||
|
||||
(when (menu.is-paused)
|
||||
(menu.update))))
|
||||
|
||||
(global frame (fn []
|
||||
(case active-demo
|
||||
|
|
@ -122,11 +128,12 @@
|
|||
(set snow-init? true))
|
||||
(pxl8.particles_render particles)))
|
||||
|
||||
:cube3d (cube3d.frame)
|
||||
|
||||
:worldgen (worldgen.frame)
|
||||
|
||||
_ (pxl8.clear 0))
|
||||
|
||||
(when transition
|
||||
(pxl8.transition_render transition))))
|
||||
(pxl8.transition_render transition))
|
||||
|
||||
(when (menu.is-paused)
|
||||
(menu.draw))))
|
||||
|
|
|
|||
|
|
@ -1,251 +0,0 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
(local debug-ui (require :mod.debug_ui))
|
||||
|
||||
(var angle-x 0)
|
||||
(var angle-y 0)
|
||||
(var angle-z 0)
|
||||
(var auto-rotate? true)
|
||||
(var orthographic? false)
|
||||
(var wireframe? true)
|
||||
(var time 0)
|
||||
(var zoom 5.0)
|
||||
(var texture-id nil)
|
||||
(var use-texture? false)
|
||||
(var affine? false)
|
||||
(var cam-x 0)
|
||||
(var cam-y 2)
|
||||
(var cam-z 12)
|
||||
(var cam-yaw 0)
|
||||
(var cam-pitch -0.2)
|
||||
(var show-debug-ui? false)
|
||||
(var fps 0)
|
||||
(var fps-accumulator 0)
|
||||
(var fps-frame-count 0)
|
||||
|
||||
(fn init []
|
||||
(set angle-x 0)
|
||||
(set angle-y 0)
|
||||
(set angle-z 0)
|
||||
(set auto-rotate? true)
|
||||
(set orthographic? false)
|
||||
(set wireframe? true)
|
||||
(set time 0)
|
||||
(set zoom 5.0)
|
||||
(set use-texture? false)
|
||||
(set affine? false)
|
||||
(set cam-x 0)
|
||||
(set cam-y 2)
|
||||
(set cam-z 12)
|
||||
(set cam-yaw 0)
|
||||
(set cam-pitch -0.2)
|
||||
(set show-debug-ui? false)
|
||||
(set fps 0)
|
||||
(set fps-accumulator 0)
|
||||
(set fps-frame-count 0)
|
||||
(set texture-id (pxl8.load_sprite "res/sprites/pxl8_logo.ase"))
|
||||
(pxl8.upload_atlas))
|
||||
|
||||
(fn make-cube-vertices []
|
||||
[[-1 -1 -1] [1 -1 -1] [1 1 -1] [-1 1 -1]
|
||||
[-1 -1 1] [1 -1 1] [1 1 1] [-1 1 1]])
|
||||
|
||||
(fn make-cube-faces []
|
||||
[[0 1 2] [0 2 3]
|
||||
[1 5 6] [1 6 2]
|
||||
[5 4 7] [5 7 6]
|
||||
[4 0 3] [4 3 7]
|
||||
[3 2 6] [3 6 7]
|
||||
[4 5 1] [4 1 0]])
|
||||
|
||||
(fn make-cube-faces-with-uvs []
|
||||
[{:tri [0 1 2] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [0 2 3] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [1 5 6] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [1 6 2] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [5 4 7] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [5 7 6] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [4 0 3] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [4 3 7] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [3 2 6] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [3 6 7] :uvs [[0 0] [1 1] [0 1]]}
|
||||
{:tri [4 5 1] :uvs [[0 0] [1 0] [1 1]]}
|
||||
{:tri [4 1 0] :uvs [[0 0] [1 1] [0 1]]}])
|
||||
|
||||
(fn get-face-color [face-idx]
|
||||
(let [colors [12 22 30 16 28 20]]
|
||||
(. colors (+ 1 (% face-idx 6)))))
|
||||
|
||||
(fn update [dt]
|
||||
(set time (+ time 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))
|
||||
|
||||
(let [wheel-y (pxl8.mouse_wheel_y)]
|
||||
(when (and (not= wheel-y 0) (not (pxl8.ui_has_mouse_focus)))
|
||||
(if orthographic?
|
||||
(set zoom (math.max 0.5 (math.min (- zoom (* wheel-y 0.2)) 10.0)))
|
||||
(let [zoom-speed 0.5
|
||||
forward-x (* (math.sin cam-yaw) wheel-y zoom-speed)
|
||||
forward-z (* (math.cos cam-yaw) wheel-y zoom-speed)]
|
||||
(set cam-x (+ cam-x forward-x))
|
||||
(set cam-z (- cam-z forward-z))))))
|
||||
|
||||
(let [move-speed 5.0
|
||||
rot-speed 2.0
|
||||
zoom-speed 2.0
|
||||
forward-x (* (math.sin cam-yaw) move-speed dt)
|
||||
forward-z (* (math.cos cam-yaw) move-speed dt)
|
||||
right-x (* (math.cos cam-yaw) move-speed dt)
|
||||
right-z (* (- (math.sin cam-yaw)) move-speed dt)]
|
||||
|
||||
(if orthographic?
|
||||
(do
|
||||
(when (pxl8.key_down "w")
|
||||
(set zoom (math.max 0.5 (- zoom (* zoom-speed dt)))))
|
||||
(when (pxl8.key_down "s")
|
||||
(set zoom (math.min 10.0 (+ zoom (* zoom-speed dt)))))
|
||||
(when (pxl8.key_down "a")
|
||||
(set cam-x (- cam-x right-x))
|
||||
(set cam-z (- cam-z right-z)))
|
||||
(when (pxl8.key_down "d")
|
||||
(set cam-x (+ cam-x right-x))
|
||||
(set cam-z (+ cam-z right-z)))
|
||||
(when (pxl8.key_down "q")
|
||||
(set cam-y (- cam-y (* move-speed dt))))
|
||||
(when (pxl8.key_down "e")
|
||||
(set cam-y (+ cam-y (* move-speed dt)))))
|
||||
(do
|
||||
(when (pxl8.key_down "w")
|
||||
(set cam-x (+ cam-x forward-x))
|
||||
(set cam-z (- cam-z forward-z)))
|
||||
(when (pxl8.key_down "s")
|
||||
(set cam-x (- cam-x forward-x))
|
||||
(set cam-z (+ cam-z forward-z)))
|
||||
(when (pxl8.key_down "a")
|
||||
(set cam-x (- cam-x right-x))
|
||||
(set cam-z (- cam-z right-z)))
|
||||
(when (pxl8.key_down "d")
|
||||
(set cam-x (+ cam-x right-x))
|
||||
(set cam-z (+ cam-z right-z)))
|
||||
(when (pxl8.key_down "q")
|
||||
(set cam-y (- cam-y (* move-speed dt))))
|
||||
(when (pxl8.key_down "e")
|
||||
(set cam-y (+ cam-y (* move-speed dt))))))
|
||||
|
||||
(when (pxl8.key_down "left")
|
||||
(set cam-yaw (- cam-yaw (* rot-speed dt))))
|
||||
(when (pxl8.key_down "right")
|
||||
(set cam-yaw (+ cam-yaw (* rot-speed dt))))
|
||||
(when (pxl8.key_down "up")
|
||||
(set cam-pitch (+ cam-pitch (* rot-speed dt))))
|
||||
(when (pxl8.key_down "down")
|
||||
(set cam-pitch (- cam-pitch (* rot-speed dt))))
|
||||
|
||||
(set cam-pitch (math.max -1.5 (math.min cam-pitch 1.5))))
|
||||
|
||||
(when (pxl8.key_pressed " ")
|
||||
(set wireframe? (not wireframe?)))
|
||||
(when (pxl8.key_pressed "f")
|
||||
(set affine? (not affine?)))
|
||||
(when (pxl8.key_pressed "p")
|
||||
(set orthographic? (not orthographic?)))
|
||||
(when (pxl8.key_pressed "r")
|
||||
(set auto-rotate? (not auto-rotate?)))
|
||||
(when (pxl8.key_pressed "t")
|
||||
(set use-texture? (not use-texture?)))
|
||||
(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 auto-rotate?
|
||||
(set angle-x (+ angle-x (* dt 0.7)))
|
||||
(set angle-y (+ angle-y (* dt 0.5)))
|
||||
(set angle-z (+ angle-z (* dt 0.3)))))
|
||||
|
||||
(fn draw-cube [pos scale rotation-offset]
|
||||
(let [[x y z] pos
|
||||
model (-> (pxl8.mat4_translate x y z)
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_z (+ angle-z (* rotation-offset 0.7))))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_y (+ angle-y (* rotation-offset 1.3))))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_rotate_x (+ angle-x rotation-offset)))
|
||||
(pxl8.mat4_multiply (pxl8.mat4_scale scale scale scale)))]
|
||||
(pxl8.set_model model))
|
||||
|
||||
(let [vertices (make-cube-vertices)]
|
||||
(if (and use-texture? texture-id)
|
||||
(let [faces (make-cube-faces-with-uvs)]
|
||||
(each [_i face-data (ipairs faces)]
|
||||
(let [tri-indices face-data.tri
|
||||
tri-uvs face-data.uvs
|
||||
v0 (. vertices (+ 1 (. tri-indices 1)))
|
||||
v1 (. vertices (+ 1 (. tri-indices 2)))
|
||||
v2 (. vertices (+ 1 (. tri-indices 3)))
|
||||
uv0 (. tri-uvs 1)
|
||||
uv1 (. tri-uvs 2)
|
||||
uv2 (. tri-uvs 3)]
|
||||
(pxl8.draw_triangle_3d_textured
|
||||
v0 v1 v2
|
||||
uv0 uv1 uv2
|
||||
texture-id))))
|
||||
(let [faces (make-cube-faces)]
|
||||
(each [i face (ipairs faces)]
|
||||
(let [[i0 i1 i2] face
|
||||
v0 (. vertices (+ 1 i0))
|
||||
v1 (. vertices (+ 1 i1))
|
||||
v2 (. vertices (+ 1 i2))
|
||||
color (get-face-color (math.floor (/ (- i 1) 2)))]
|
||||
(pxl8.draw_triangle_3d v0 v1 v2 color)))))))
|
||||
|
||||
(fn frame []
|
||||
(pxl8.clear 0)
|
||||
|
||||
(pxl8.clear_zbuffer)
|
||||
(pxl8.set_affine_textures affine?)
|
||||
(pxl8.set_backface_culling true)
|
||||
(pxl8.set_wireframe wireframe?)
|
||||
|
||||
(if orthographic?
|
||||
(let [size zoom
|
||||
aspect (/ (pxl8.get_width) (pxl8.get_height))
|
||||
w (* size aspect)
|
||||
h size]
|
||||
(pxl8.set_projection (pxl8.mat4_ortho (- w) w (- h) h 1.0 100.0)))
|
||||
(let [aspect (/ (pxl8.get_width) (pxl8.get_height))
|
||||
fov (* (/ 60.0 180.0) 3.14159)]
|
||||
(pxl8.set_projection (pxl8.mat4_perspective fov aspect 0.1 100.0))))
|
||||
|
||||
(let [target-x (* (math.sin cam-yaw) (math.cos cam-pitch))
|
||||
target-y (math.sin cam-pitch)
|
||||
target-z (* (- (math.cos cam-yaw)) (math.cos cam-pitch))
|
||||
look-x (+ cam-x target-x)
|
||||
look-y (+ cam-y target-y)
|
||||
look-z (+ cam-z target-z)]
|
||||
(pxl8.set_view (pxl8.mat4_lookat [cam-x cam-y cam-z] [look-x look-y look-z] [0 1 0])))
|
||||
|
||||
(draw-cube [0 0 0] 1.0 0)
|
||||
(draw-cube [-3 0 -4] 0.8 0.5)
|
||||
(draw-cube [3 1 -7] 0.9 1.2)
|
||||
(draw-cube [0 -2 -10] 1.1 -0.7)
|
||||
|
||||
(let [new-state (debug-ui.render {:show-debug-ui show-debug-ui?
|
||||
:fps fps
|
||||
:wireframe wireframe?
|
||||
:auto-rotate auto-rotate?
|
||||
:orthographic orthographic?
|
||||
:use-texture use-texture?
|
||||
:affine affine?})]
|
||||
(when (not= new-state.show-debug-ui nil) (set show-debug-ui? new-state.show-debug-ui))
|
||||
(when (not= new-state.wireframe nil) (set wireframe? new-state.wireframe))
|
||||
(when (not= new-state.auto-rotate nil) (set auto-rotate? new-state.auto-rotate))
|
||||
(when (not= new-state.orthographic nil) (set orthographic? new-state.orthographic))
|
||||
(when (not= new-state.use-texture nil) (set use-texture? new-state.use-texture))
|
||||
(when (not= new-state.affine nil) (set affine? new-state.affine))))
|
||||
|
||||
{:init init
|
||||
:update update
|
||||
:frame frame}
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
|
||||
(fn render [state]
|
||||
(var new-state {})
|
||||
(when state.show-debug-ui
|
||||
(let [window-open (pxl8.ui_window_begin "Debug Menu (F8)" 10 10 256 128)]
|
||||
(when window-open
|
||||
(pxl8.ui_layout_row 1 0 0)
|
||||
(pxl8.ui_label (string.format "FPS: %.0f" (or state.fps 0)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Wireframe" state.wireframe)]
|
||||
(when changed (set new-state.wireframe new-val)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Auto-rotate" state.auto-rotate)]
|
||||
(when changed (set new-state.auto-rotate new-val)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Orthographic" state.orthographic)]
|
||||
(when changed (set new-state.orthographic new-val)))
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Texture" state.use-texture)]
|
||||
(when changed (set new-state.use-texture new-val)))
|
||||
(when state.use-texture
|
||||
(pxl8.ui_indent 20)
|
||||
(let [(changed new-val) (pxl8.ui_checkbox "Affine mapping" state.affine)]
|
||||
(when changed (set new-state.affine new-val)))
|
||||
(pxl8.ui_indent -20))
|
||||
(pxl8.ui_window_end))
|
||||
(when (not window-open)
|
||||
(set new-state.show-debug-ui false))))
|
||||
new-state)
|
||||
|
||||
{: render}
|
||||
51
demo/mod/menu.fnl
Normal file
51
demo/mod/menu.fnl
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
(local pxl8 (require :pxl8))
|
||||
|
||||
(var paused false)
|
||||
|
||||
(fn show []
|
||||
(set paused true)
|
||||
(pxl8.set_relative_mouse_mode false)
|
||||
(pxl8.center_cursor))
|
||||
|
||||
(fn hide []
|
||||
(set paused false)
|
||||
(pxl8.set_relative_mouse_mode true))
|
||||
|
||||
(fn toggle []
|
||||
(if paused
|
||||
(hide)
|
||||
(show)))
|
||||
|
||||
(fn update []
|
||||
(let [(mx my) (pxl8.get_mouse_pos)]
|
||||
(pxl8.gui_cursor_move mx my))
|
||||
|
||||
(when (pxl8.mouse_pressed 1)
|
||||
(pxl8.gui_cursor_down))
|
||||
|
||||
(when (pxl8.mouse_released 1)
|
||||
(pxl8.gui_cursor_up)))
|
||||
|
||||
(fn draw []
|
||||
(pxl8.gui_begin_frame)
|
||||
|
||||
(pxl8.gui_window 200 100 240 140 "pxl8 demo")
|
||||
|
||||
(when (pxl8.gui_button 1 215 145 210 32 "Resume")
|
||||
(hide))
|
||||
|
||||
(when (pxl8.gui_button 2 215 185 210 32 "Quit")
|
||||
(pxl8.quit))
|
||||
|
||||
(if (pxl8.gui_is_hovering)
|
||||
(pxl8.set_cursor :hand)
|
||||
(pxl8.set_cursor :arrow))
|
||||
|
||||
(pxl8.gui_end_frame))
|
||||
|
||||
{:is-paused (fn [] paused)
|
||||
:toggle toggle
|
||||
:show show
|
||||
:hide hide
|
||||
:update update
|
||||
:draw draw}
|
||||
|
|
@ -70,11 +70,6 @@
|
|||
(pxl8.error (.. "Failed to apply textures - result: " result))))))))
|
||||
|
||||
(fn update [dt]
|
||||
(pxl8.set_relative_mouse_mode mouse-look?)
|
||||
|
||||
(when (pxl8.key_pressed "escape")
|
||||
(set mouse-look? (not mouse-look?)))
|
||||
|
||||
(when (pxl8.key_pressed "`")
|
||||
(set auto-run? (not auto-run?)))
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue