From de84217b6efe75c928c3fe58a6e0c15a2a4180fb Mon Sep 17 00:00:00 2001
From: vydd <vidovicd@gmail.com>
Date: Sat, 23 Jan 2016 04:03:25 +0100
Subject: [PATCH] Bug fixes

---
 sketch.asd           |  2 +-
 src/color.lisp       |  8 ++++----
 src/environment.lisp |  1 +
 src/figures.lisp     |  3 ++-
 src/package.lisp     |  5 ++++-
 src/sketch.lisp      | 43 ++++++++++++++++++++++++-------------------
 6 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/sketch.asd b/sketch.asd
index 46ba24d..1457a4c 100644
--- a/sketch.asd
+++ b/sketch.asd
@@ -28,8 +28,8 @@
 	       (:file "drawing")
 	       (:file "shapes")
 	       (:file "transforms")
-	       (:file "figures")
 	       (:file "resources")
 	       (:file "color")
                (:file "sketch")
+	       (:file "figures")
 	       (:file "controllers")))
diff --git a/src/color.lisp b/src/color.lisp
index df9a451..2467b85 100644
--- a/src/color.lisp
+++ b/src/color.lisp
@@ -167,10 +167,10 @@
        (color-blue color)
        (color-red color)))
 
-(defun color-filter-lighten (color &optional (amount 0.1))
-  (let ((hue (color-hue color))
-	(saturation (color-saturation color))
-	(brightness (clamp-1 (+ amount (color-brightness color))))
+(defun color-filter-hsb (color &key (hue 0.0) (saturation 0.0) (brightness 0.0))
+  (let ((hue (clamp-1 (+ hue (color-hue color))))
+	(saturation (clamp-1 (+ saturation (color-brightness color))))
+	(brightness (clamp-1 (+ brightness (color-brightness color))))
 	(alpha (color-alpha color)))
     (destructuring-bind (red green blue) (hsb-to-rgb hue saturation brightness)
       (make-instance 'color
diff --git a/src/environment.lisp b/src/environment.lisp
index 2a91300..7071608 100644
--- a/src/environment.lisp
+++ b/src/environment.lisp
@@ -71,6 +71,7 @@
     (gl:enable :blend :line-smooth :polygon-smooth)
     (gl:blend-func :src-alpha :one-minus-src-alpha)
     (gl:hint :line-smooth-hint :nicest)
+    (gl:hint :polygon-smooth-hint :nicest)
     (gl:clear-color 0.0 1.0 0.0 1.0)
     (gl:clear :color-buffer :depth-buffer)
     (gl:flush)))
diff --git a/src/figures.lisp b/src/figures.lisp
index 6e3f60b..497683d 100644
--- a/src/figures.lisp
+++ b/src/figures.lisp
@@ -37,7 +37,8 @@
   `(let ((*draw-sequence* nil))
      (let ((*env* (make-env))
 	   (*draw-mode* :figure))
-       ,@body)
+       (with-pen (make-default-pen)
+	 ,@body))
      (setf *draw-sequence* (nreverse *draw-sequence*))
      (let ((figure (make-instance 'figure :draws *draw-sequence*)))
        (defun ,name (x y)
diff --git a/src/package.lisp b/src/package.lisp
index 30bd633..3cf140a 100644
--- a/src/package.lisp
+++ b/src/package.lisp
@@ -38,6 +38,9 @@
 	   :radians
 	   :degrees
 
+	   ;; Utils
+	   :relative-path
+
 	   ;; Colors
 	   :color
 	   :make-color
@@ -68,7 +71,7 @@
 	   :color-filter-grayscale
 	   :color-filter-invert
 	   :color-filter-rotate
-	   :color-filter-lighten
+	   :color-filter-hsb
 	   :+red+
 	   :+green+
 	   :+blue+
diff --git a/src/sketch.lisp b/src/sketch.lisp
index 1762311..8eb4d40 100644
--- a/src/sketch.lisp
+++ b/src/sketch.lisp
@@ -62,8 +62,9 @@ used for drawing.")
        (progn
 	 (background ,error-color)
 	 (with-font (make-default-font)
-	   (text "ERROR" 20 20)
-	   (text "For restarts, press the debug key." 20 40))
+	   (with-identity-matrix
+	     (text "ERROR" 20 20)
+	     (text "For restarts, press the debug key." 20 40)))
 	 (setf restart-sketch t
 	       (env-red-screen *env*) t)))))
 
@@ -77,23 +78,24 @@ used for drawing.")
     (with-environment env
       (with-pen (make-default-pen)
 	(with-font (make-default-font)
-	  (unless copy-pixels
-	    (background (gray 0.4)))
-	  ;; Restart sketch on setup and when recovering from an error.
-	  (when restart-sketch
-	    (gl-catch (rgb 1 1 0)
-	      (setup sketch-window))
-	    (setf (slot-value sketch-window 'restart-sketch) nil))
-	  ;; If we're in the debug mode, we exit from it immediately,
-	  ;; so that the restarts are shown only once. Afterwards, we
-	  ;; continue presenting the user with the red screen, waiting for
-	  ;; the error to be fixed, or for the debug key to be pressed again.
-	  (if (debug-mode-p)
-	      (progn
-		(exit-debug-mode)
-		(draw-window sketch-window))
-	      (gl-catch (rgb 1 0 0)
-		(draw-window sketch-window)))))))
+	  (with-identity-matrix
+	    (unless copy-pixels
+	      (background (gray 0.4)))
+	    ;; Restart sketch on setup and when recovering from an error.
+	    (when restart-sketch
+	      (gl-catch (rgb 1 1 0)
+		(setup sketch-window))
+	      (setf (slot-value sketch-window 'restart-sketch) nil))
+	    ;; If we're in the debug mode, we exit from it immediately,
+	    ;; so that the restarts are shown only once. Afterwards, we
+	    ;; continue presenting the user with the red screen, waiting for
+	    ;; the error to be fixed, or for the debug key to be pressed again.
+	    (if (debug-mode-p)
+		(progn
+		  (exit-debug-mode)
+		  (draw-window sketch-window))
+		(gl-catch (rgb 1 0 0)
+		  (draw-window sketch-window))))))))
   (handle-sketch-event sketch-window :frame-draw))
 
 ;;; Macros
@@ -151,6 +153,9 @@ all slot names."
 	 (with-slots ,(gethash sketch-name *sketch-slot-hash-table*) sketch-window
 	   ,@body))
 
+       (defmethod setup :before ((sketch-window ,sketch-name))
+	 (background (gray 0.4)))
+
        (defmethod initialize-instance :after ((sketch-window ,sketch-name)
 					      &key &allow-other-keys)
 	 (let ((sdl-win (kit.sdl2:sdl-window sketch-window)))
