Building Games with egfx-game
egfx-game is the L1 game framework sitting atop egfx, egfx-wgpu 2.0, and egfx-audio. It handles the fixed 60 Hz simulation loop, keyboard/gamepad edge-triggered input, scene stack, PNG Sprite Atlas slicing, Tiled .tmj tilemap collisions, smooth camera with screen shake, pooled particles, JSON save slots, GPU Post-FX, and Emacs save-hook hot-reload.
1. Scaffold a Runnable Project
Section titled “1. Scaffold a Runnable Project”Generate a complete game directory with M-x egfx-new-game:
M-x egfx-new-game RET star-hopper RET ~/games/ RETThis creates:
star-hopper.el— runnableegfx-game-startentry point, fixed-timestep:on-update, batched:on-draw, and automaticegfx-arcade-register-gameregistration.assets/tuning.el— live-coding gameplay constants watched viaegfx-game-watch-file.MakefileandREADME.md— batch byte-compile target and quick-start instructions.
Load star-hopper.el and run M-x star-hopper to launch a live 60 FPS GPU window immediately.
2. Scene Lifecycle & Fixed Timestep
Section titled “2. Scene Lifecycle & Fixed Timestep”A scene is a plain plist with :on-enter, :on-update, :on-draw, and :on-exit callbacks managed by a push/pop stack (egfx-game-push-scene, egfx-game-pop-scene, egfx-game-replace-scene):
(require 'egfx-game)
(defun my-game-make-scene () (let ((x 140.0) (y 80.0)) (list :on-enter (lambda (app) (egfx-audio-start)) :on-update (lambda (app dt) (let* ((inp (egfx-game-app-input app)) (dx (egfx-game-axis inp 'left 'right)) (dy (egfx-game-axis inp 'up 'down))) (setq x (+ x (* dx 120.0 dt)) y (+ y (* dy 120.0 dt))) (when (egfx-game-action-pressed-p inp 'jump) (egfx-game-camera-shake (egfx-game-app-camera app) 0.4)))) :on-draw (lambda (app batch) (egfx-game-batch-begin batch 0.08 0.10 0.16) (egfx-game-draw-rect batch x y 16.0 16.0 0.35 0.85 1.0 1.0)))))
(egfx-game-start :id 'my-game :title "My GPU Game" :virtual-w 320 :virtual-h 180 :postfx 'crt :initial-scene (my-game-make-scene))3. Sprite Atlases, Tiled .tmj Maps & Collision
Section titled “3. Sprite Atlases, Tiled .tmj Maps & Collision”Load PNG sprite sheets directly onto the GPU (nearest for crisp pixel art, linear for smooth HD art) and pair them with Tiled JSON .tmj maps:
(let* ((atlas (egfx-game-atlas-load cv "/path/to/tiles.png" 18 18 'nearest)) (tmap (egfx-game-tilemap-load "/path/to/level1.tmj" "Solid")) (res (egfx-game-move-and-collide tmap px py 12.0 16.0 vx vy dt))) ;; `res' returns (:x X :y Y :vx VX :vy VY :grounded BOOL :hit-x BOOL :hit-y BOOL) (egfx-game-draw-tilemap batch tmap atlas cam "Solid") (egfx-game-draw-tile batch atlas 0 (plist-get res :x) (plist-get res :y)))All sprites in the frame are packed into a single flat float vector and submitted to egfx-wgpu in one draw call (up to 16,384 hardware-instanced quads per frame).
4. Audio, Particles, Saves & GPU Post-FX
Section titled “4. Audio, Particles, Saves & GPU Post-FX”- Audio (
egfx-audio): Load.wavclips (egfx-audio-load-wav) or synthesize retro chiptune tones (egfx-audio-make-tone) and play up to 32 concurrent voices withegfx-audio-play/egfx-audio-play-bgm. - Particles: Allocate a pool with
egfx-game-particles-make, spawn bursts withegfx-game-particles-burst, step withegfx-game-particles-update, and draw withegfx-game-draw-particles. - Persistent Saves: Store/load player progress as JSON with
(egfx-game-save-data "my-game" plist 1)and(egfx-game-load-data "my-game" 1). - Universal GPU Post-FX: Press
x(M-x egfx-game-cycle-postfx) at runtime to switch live betweennone,crt(scanlines + curvature),bloom, andvignette.
5. Live-Coding Hot-Reload in Emacs
Section titled “5. Live-Coding Hot-Reload in Emacs”Watch any tuning file, level .tmj, or WGSL shader with egfx-game-watch-file:
(egfx-game-watch-file app "/path/to/assets/tuning.el" (lambda (_app file) (load file nil t)))Edit gravity, jump impulse, or map layout in a side window and press C-x C-s: the live 60 FPS game updates instantly without restarting or losing state.
6. Register in egfx-arcade
Section titled “6. Register in egfx-arcade”Publish your game into the unified GPU cover wall with one call:
(when (fboundp 'egfx-arcade-register-game) (egfx-arcade-register-game (list :id "custom/my-game" :title "My GPU Game" :engine 'game :offline t :genre "2D Action" :installed t :goal "Custom 60 FPS Elisp GPU title" :summary "Built with egfx-game and registered into EGFX ARCADE" :key-profile 'game :launch-fn #'my-game)))