~lthms/fairy.lisp

b702365257bb17247c03372b051efc7921b977d2 — Thomas Letan 6 years ago 796a49e
feature: Add a proper root element
1 files changed, 26 insertions(+), 31 deletions(-)

M fairy.lisp
M fairy.lisp => fairy.lisp +26 -31
@@ 1,6 1,8 @@
(defpackage :fairy
  (:use :cl)
  (:export run

           defroot
           root-add-child
           root-delete-child



@@ 12,32 14,26 @@
           rectangle-color

           layer
           layer-add-child
           layer-delete-child
           new-layer
           layer-origin

           surface
           surface-add-child
           surface-delete-child
           new-surface
           surface-origin
           surface-width
           surface-height))
           surface-height

           *black*))

(cl:in-package :fairy)

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

(define-condition not-a-symbol (error) (arg))

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

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

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



@@ 105,25 101,24 @@
                     (rectangle-height element)
                     :fill-paint (rectangle-color element)))

(gamekit:defgame app () ()
                 (:viewport-width 800)
                 (:viewport-height 600))
(defstruct root elements)

(defmethod gamekit:draw ((app app))
  (dolist (el *elements*)
    (draw (symbol-value el))))

(defmethod gamekit:post-initialize ((app app))
  (gamekit:bind-button :mouse-left :released
                       (lambda () (gamekit:stop))))

(defun run ()
  (gamekit:start 'app))
(defmacro root-add-child (root element)
  `(setf (root-elements ,root)
         (cons (if (symbolp ,element) ,element ',element)
               (root-elements ,root))))

;; TEST
(defvar *test-layer* (new-layer 70 0))
(defmacro root-delete-child (root element)
  `(setf (root-elements ,root)
         (delete (if (symbolp ,element) ,element ',element)
                 (root-elements ,root))))

(defvar *test-rec* (new-rectangle 5 5 100 150 *black*))
(defmethod draw ((element root))
  (dolist (el (root-elements element))
    (draw (symbol-value el))))

(root-add-child *test-layer*)
(layer-add-child *test-layer* *test-rec*)
(defmacro defroot (var gamekit)
  `(progn
     (defvar ,var (make-root :elements nil))
     (defmethod gamekit:draw ((app ,gamekit))
       (draw ,var))))