diff --git a/src/engine.lisp b/src/engine.lisp new file mode 100644 index 0000000..b27a285 --- /dev/null +++ b/src/engine.lisp @@ -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*)))) diff --git a/src/game.lisp b/src/game.lisp new file mode 100644 index 0000000..0b6df85 --- /dev/null +++ b/src/game.lisp @@ -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)