~lthms/fairy.lisp

48e0cb1fd1f8331ab92979dcc732a8bcbaf35ccb — Thomas Letan 6 years ago 0c28301
refactor: A more lispy implementation
2 files changed, 30 insertions(+), 45 deletions(-)

M example.lisp
M fairy.lisp
M example.lisp => example.lisp +7 -3
@@ 9,13 9,17 @@
  (gamekit:bind-button :mouse-left :released
                       (lambda () (gamekit:stop))))

(defvar *root* (fairy:init-root))

(defmethod gamekit:draw ((app app))
  (fairy:draw *root*))

(gamekit:start 'app)

; first, we create the element of our scene, starting with the fairy root
(fairy:defroot state app)
(defvar *test-layer* (fairy:new-layer 70 0))
(defvar *test-rec* (fairy:new-rectangle 5 5 100 150 fairy:*black*))
(defvar *test-rec* (fairy:new-rectangle 5 5 100 150 (gamekit:vec4 0 0 0 1)))

; then, we put everything together
(fairy:layer-add-child *test-layer* *test-rec*)
(fairy:root-add-child state *test-layer*)
(fairy:root-add-child *root* *test-layer*)

M fairy.lisp => fairy.lisp +23 -42
@@ 1,14 1,14 @@
(defpackage :fairy
  (:use :cl)
  (:export run
  (:export draw

           defroot
           init-root
           root-add-child
           root-delete-child

           rectangle
           new-rectangle
           rectange-origin
           rectangle-origin
           rectangle-width
           rectangle-height
           rectangle-color


@@ 25,15 25,10 @@
           new-surface
           surface-origin
           surface-width
           surface-height

           *black*))
           surface-height))

(cl:in-package :fairy)

(defvar *black* (gamekit:vec4 0 0 0 1))
(defvar *elements* '())

(defgeneric draw (element)
  (:documentation "Draw a new element on the screen"))



@@ 51,17 46,14 @@
    (gamekit:translate-canvas (gamekit:x (surface-origin element))
                              (gamekit:y (surface-origin element)))
    (dolist (el (surface-elements element))
      (draw (symbol-value el)))))
      (draw el))))

(defmacro surface-add-child (surface element)
  `(setf (surface-elements ,surface)
         (cons (if (symbolp ,element) ,element ',element)
               (surface-elements ,surface))))
(defun surface-add-child (surface element)
  (setf (surface-elements surface)
        (cons element (surface-elements surface))))

(defmacro surface-delete-child (surface element)
  `(setf (surface-elements ,surface)
         (delete (if (symbolp ,element) ,element ',element)
                 (surface-elements ,surface))))
(defun surface-delete-child (surface element)
  (setf (surface-elements surface) (delete element (surface-elements surface))))

(defstruct layer origin elements)



@@ 69,23 61,18 @@
  (make-layer :origin (gamekit:vec2 x y)
              :elements nil))


(defmethod draw ((element layer))
  (gamekit:with-pushed-canvas ()
    (gamekit:translate-canvas (gamekit:x (layer-origin element))
                              (gamekit:y (layer-origin element)))
    (dolist (el (layer-elements element))
      (draw (symbol-value el)))))
      (draw el))))

(defmacro layer-add-child (layer element)
  `(setf (layer-elements ,layer)
         (cons (if (symbolp ,element) ,element ',element)
               (layer-elements ,layer))))
(defun layer-add-child (layer element)
  (setf (layer-elements layer) (cons element (layer-elements layer))))

(defmacro layer-delete-child (layer element)
  `(setf (layer-elements ,layer)
         (delete (if (symbolp ,element) ,element ',element)
                 (layer-elements ,layer))))
(defun layer-delete-child (layer element)
  (setf (layer-elements layer) (delete element (layer-elements layer))))

(defstruct rectangle origin width height color)



@@ 103,22 90,16 @@

(defstruct root elements)

(defmacro root-add-child (root element)
  `(setf (root-elements ,root)
         (cons (if (symbolp ,element) ,element ',element)
               (root-elements ,root))))
(defun root-add-child (root element)
  (setf (root-elements root)
        (cons element (root-elements root))))

(defmacro root-delete-child (root element)
  `(setf (root-elements ,root)
         (delete (if (symbolp ,element) ,element ',element)
                 (root-elements ,root))))
(defun root-delete-child (root element)
  (setf (root-elements layer) (delete element (root-elements layer))))

(defmethod draw ((element root))
  (dolist (el (root-elements element))
    (draw (symbol-value el))))
    (draw el)))

(defmacro defroot (var gamekit)
  `(progn
     (defvar ,var (make-root :elements nil))
     (defmethod gamekit:draw ((app ,gamekit))
       (draw ,var))))
(defun init-root ()
  (make-root :elements nil))