-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds CANVAS - see sketch-examples:stars
- Loading branch information
Showing
7 changed files
with
133 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
;;;; stars.lisp | ||
|
||
(in-package #:sketch-examples) | ||
|
||
;;; ____ _____ _ ____ ____ | ||
;;; / ___|_ _|/ \ | _ \/ ___| | ||
;;; \___ \ | | / _ \ | |_) \___ \ | ||
;;; ___) || |/ ___ \| _ < ___) | | ||
;;; |____/ |_/_/ \_\_| \_\____/ | ||
|
||
(defsketch stars | ||
((stars (loop :for i :below 10 :collect (make-stars))) | ||
(positions (loop :for i :from 18 :downto 0 :by 2 :collect i))) | ||
(background +black+) | ||
(dotimes (i (length stars)) | ||
(incf (elt positions i) 0.03) | ||
(let ((zoom (get-zoom (elt positions i)))) | ||
(with-current-matrix | ||
(with-pen (make-pen :fill (canvas-image (elt stars i))) | ||
(translate 200 200) | ||
(scale zoom) | ||
(rect -50 -50 100 100))))) | ||
(with-font (make-font :color +white+ :size 48 | ||
:align :center) | ||
(text "s k e t c h" 200 160)) | ||
(when (>= (get-zoom (car positions)) 20) | ||
(setf (car positions) 0) | ||
(setf positions (rotate-list positions)) | ||
(setf stars (rotate-list stars)))) | ||
|
||
(defun make-stars () | ||
(let ((canvas (make-canvas 100 100))) | ||
(dotimes (i 20) | ||
(let ((x (random 100)) | ||
(y (random 100))) | ||
(unless (and (< 40 x 60) | ||
(< 40 y 60))) | ||
(canvas-paint canvas (gray-255 (+ 200 (random 55))) x y))) | ||
(canvas-lock canvas) | ||
canvas)) | ||
|
||
(defun rotate-list (list) | ||
(let ((el (pop list))) | ||
(reverse (cons el (reverse list))))) | ||
|
||
(defun get-zoom (position) | ||
(exp (/ position 6))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ | |
(:file "life") | ||
(:file "brownian") | ||
(:file "hello-world") | ||
(:file "stars") | ||
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
;;;; canvas.lisp | ||
|
||
(in-package #:sketch) | ||
|
||
;;; ____ _ _ ___ ___ ____ | ||
;;; / ___| / \ | \ | \ \ / / \ / ___| | ||
;;; | | / _ \ | \| |\ \ / / _ \ \___ \ | ||
;;; | |___ / ___ \| |\ | \ V / ___ \ ___) | | ||
;;; \____/_/ \_|_| \_| \_/_/ \_|____/ | ||
|
||
|
||
(defclass canvas () | ||
((width :initarg :width :reader canvas-width) | ||
(height :initarg :height :reader canvas-height) | ||
(%image :initform nil :accessor %canvas-image) | ||
(%vector :initform nil :accessor %canvas-vector) | ||
(%locked :initform nil :accessor %canvas-locked))) | ||
|
||
(defun make-canvas (width height) | ||
(let ((canvas (make-instance 'canvas :width width :height height))) | ||
(canvas-reset canvas) | ||
canvas)) | ||
|
||
(defmethod %canvas-vector-pointer ((canvas canvas)) | ||
(static-vectors:static-vector-pointer (%canvas-vector canvas))) | ||
|
||
(defmethod canvas-reset ((canvas canvas)) | ||
(setf (%canvas-vector canvas) | ||
(static-vectors:make-static-vector (* (canvas-width canvas) (canvas-height canvas) 4) :initial-element 0))) | ||
|
||
(defmethod canvas-paint ((canvas canvas) (color color) x y) | ||
(let ((ptr (%canvas-vector-pointer canvas)) | ||
(pos (+ (* x 4) (* y 4 (canvas-width canvas)))) | ||
(vec (color-bgra-255 color))) | ||
(dotimes (i 4) | ||
(setf (cffi:mem-aref ptr :uint8 (+ pos i)) (elt vec i))))) | ||
|
||
(defmethod canvas-image ((canvas canvas)) | ||
(if (%canvas-locked canvas) | ||
(%canvas-image canvas) | ||
(make-image-from-surface | ||
(sdl2:create-rgb-surface-with-format-from | ||
(%canvas-vector-pointer canvas) | ||
(canvas-width canvas) | ||
(canvas-height canvas) | ||
32 | ||
(* 4 (canvas-width canvas)) | ||
:format sdl2:+pixelformat-argb8888+)))) | ||
|
||
(defmethod canvas-lock ((canvas canvas)) | ||
(setf (%canvas-image canvas) (canvas-image canvas) | ||
(%canvas-locked canvas) t)) | ||
|
||
(defmethod canvas-unlock ((canvas canvas)) | ||
(setf (%canvas-locked canvas) nil)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters