aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/11-canvas-geometry.lisp
blob: 8c8082b27f9dddb09c94fef76b63c2184922c4ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;;;; examples/11-canvas-geometry.lisp

(defpackage #:ww.examples/11
  (:use #:cl)
  (:export #:start))

(in-package :ww.examples/11)

(defclass geo-demo (ww:application) ())

(ww:defhandler quit
    (ww::on-keydown (app scancode)
      (when (eql :scancode-q scancode)
        (ww::stop))))

(defmethod ww:boot ((app geo-demo))
  (let ((canvas
          (make-instance 'ww:canvas
                         :pixel-width 200
                         :pixel-height 200)))
    ;; stretch canvas over the whole app
    (setf (ww:width canvas) (ww::application-width app)
          (ww:height canvas) (ww::application-height app))

    ;; add it to the display tree
    (ww:add-unit app canvas)

    ;; draw a circle
    (ww::with-grid-circle (x y) (150 50 30 :interiorp t)
      (ww::with-pixel (r g b a) (ww::pixel canvas x y)
        (setf r (mod (* 3 x y) 256)
              g (mod (+ x x) 256)
              b (mod (* y y) 256))))

    ;; draw a bunch of circles
    (loop
      for cx from 0 to 50 by 5 
      for cy from 0 to 50 by 5 do
        (ww::with-grid-circle (x y) ((+ 100 cx) (+ 100 cy) 10)
          (ww::with-pixel (r g b a) (ww::pixel canvas x y)
            (setf r (mod (* x y) 256)
                  g (mod (* x x) 256)
                  b (mod (* y x) 256)))))

    ;; draw a random pentagonal thing
    (let ((path
            (loop repeat 7
                  collect (list (+ 10 (random 80))
                                (+ 10 (random 80))))))
      (ww::with-grid-path (x y) (path :autoclosep t :interiorp t)
        (ww::with-pixel (r g b a) (ww::pixel canvas x y)
          (setf r (mod (* 2 y) 256)
                g (mod (* 3 x) 256)
                b (mod (* x y) 256)))))

        (let ((path
            (loop repeat 20
                  collect (list (+ 10 (random 80))
                                (+ 110 (random 80))))))
      (ww::with-grid-path (x y) (path :autoclosep t)
        (ww::with-pixel (r g b a) (ww::pixel canvas x y)
          (setf r (mod (* 2 y) 256)
                g (mod (* 3 x) 256)
                b (mod (* x y) 256)))))
    
    ;; blit the canvas
    (ww::blit canvas)

    ;; quit handler
    (ww:add-handler app #'quit)
    (ww:add-handler canvas #'quit)))

(defun start (&optional (side 500))
  (ww::start
   (make-instance
    'geo-demo
    :fps 10
    :width side
    :height side
    :title "Pixels Geometry")))