This commit is contained in:
Joseph Ferano 2026-09-05 19:04:39 +07:00
parent 0732d473b7
commit 40f3ebf5d9
2 changed files with 113 additions and 0 deletions

56
src/engine.lisp Normal file
View File

@ -0,0 +1,56 @@
#-quicklisp
(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload '(:cl-raylib :livesupport) :silent t)
#+sbcl (sb-int:set-floating-point-modes :traps nil))
(defpackage #:engine
(:use #:cl)
(:local-nicknames (#:rl #:raylib)
(#:v #:3d-vectors))
(:export #:main)
(:export #:run-game))
(in-package #:engine)
(defvar *game-state* nil)
(defvar *game-config* nil)
(defvar *texture-cache* (make-hash-table :test #'equal))
;; (reset! texture-cache {})
(defvar *buffered-objs* nil)
(defun read-config (b))
(defun snapshot (b))
(defun restore (b))
(defun run-game (&key (title "Untitled") (width 800) (height 600)
config-path game-init game-update game-draw unload)
(if (rl:is-window-ready)
(warn "Existing raylib window open, ignoring!")
(progn
(rl:set-trace-log-level :log-warning)
(rl:with-window (width height title)
(rl:set-target-fps 30)
(setf *game-config* (when config-path (read-config config-path))
*game-state* (funcall game-init))
(flet ((frame ()
(livesupport:continuable
(let ((new-state (funcall game-update *game-state*)))
(rl:with-drawing (funcall game-draw new-state))
(setf *game-state* new-state)))))
(loop until (rl:window-should-close) do
(dolist (b *buffered-objs*) (snapshot b))
(restart-case (frame)
(retry-frame () :report "Restore grid and re-run frame"
(dolist (b *buffered-objs*) (restore b))
(frame))
(skip-frame () :report "Restore grid and continue"
(dolist (b *buffered-objs*) (restore b))))))
(loop for tex being the hash-values of *texture-cache*
do (rl:unload-texture tex))
(when unload (funcall unload *game-state*)))
(setf *buffered-objs* nil
*game-state* nil
*game-config* nil)
(clrhash *texture-cache*))))

57
src/game.lisp Normal file
View File

@ -0,0 +1,57 @@
#-quicklisp
(load (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname)))
(load (merge-pathnames "engine.lisp" *load-pathname*))
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload :cl-raylib :silent t)
#+sbcl (sb-int:set-floating-point-modes :traps nil))
(defpackage #:game
(:use #:cl)
(:local-nicknames (#:rl #:raylib)
(#:v #:3d-vectors)
(#:e #:engine))
(:export #:main))
(in-package #:game)
(defconstant +screen-width+ 1400)
(defconstant +screen-height+ 1000)
(defconstant +cell-size+ 5)
(defconstant +rows+ (floor +screen-height+ +cell-size+))
(defconstant +cols+ (floor +screen-width+ +cell-size+))
(defclass game ()
((layers :initarg :layers :accessor layers)
(tilesets :initarg :tilesets :accessor tilesets)
(tilemaps :initarg :tilemaps :accessor tilemaps)
(current-layer :initarg :current-layer :accessor current-layer)
(current-tilemap :initarg :current-tilemap :accessor current-tilemap)
(selected-tile :initarg :selected-tile :accessor selected-tile)))
(defun game-init ()
(make-instance 'game
:layers '()
:tilesets '()
:tilemaps '()
:current-layer 0
:current-tilemap 0
:selected-tile '(-1 -1 -1)))
(defun game-update (state)
state)
(defun game-draw (state)
(rl:clear-background :blue))
(defun main ()
(e:run-game
:title "Game"
:width 1200
:height 1000
:game-init 'game-init
:game-update 'game-update
:game-draw 'game-draw
:unload (lambda (state))))
(main)