simple-graphics
Check-in [85b7a9c108]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:First draft of the simple-graphics egg
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:85b7a9c108df6d30cad695142a78933dadc4e2fb
User & Date: alaric 2012-07-08 20:19:11
Context
2012-07-08
20:20
Added release-info file check-in: 6e19931395 user: alaric tags: trunk, 0.1
20:19
First draft of the simple-graphics egg check-in: 85b7a9c108 user: alaric tags: trunk
20:17
initial empty check-in check-in: a9ad9d35e0 user: alaric tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added simple-graphics.meta.













>
>
>
>
>
>
1
2
3
4
5
6
((egg "simple-graphics.egg")
 (files "simple-graphics.scm" "simple-graphics.setup")
 (license "BSD")
 (needs doodle miscmacros parley)
 (author "Alaric Snell-Pym")
 (synopsis "A simple graphics interface for education and fun"))

Added simple-graphics.scm.





























































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
(module simple-graphics
        (forward forwards turn right left backward backwards
         pen-up pen-down home clear
         (adventure begin-adventure! end-adventure!)
         save goto go-to heading
         red green blue orange yellow brown grey black
         faster slower speed
         lost)

;; TODO:
;; Display a turtle cursor sprite

(import chicken scheme)
(use doodle)
(use miscmacros)
(use parley)
(use posix)

(define +width+ 640)
(define +height+ 480)

(define *initialised* #f)

(define *x* #f)
(define *y* #f)

;; Standard unit-of-motion vector.
;; The magnitude of this vector represents the current scale factor.
;; The direction of this vector represents the current turtle direction.
;; The default sets the scale factor to 10 and the turtle pointing upwards.
(define *dx* #f)
(define *dy* #f)

(define *colour* #f)
(define *pen-down?* #f)

(define *saved-states* '())

(define *adventure-depth* 0)

(define *name* #f)

(define (reset-position!)
  (set! *x* (/ +width+ 2.0))
  (set! *y* (/ +height+ 2.0))
  (set! *dx* 0.0)
  (set! *dy* -10.0)
  (set! *colour* solid-black)
  (set! *pen-down?* #t)
  (set! *saved-states* '())
  (set! *adventure-depth* 0))

(define (reset!)
  (reset-position!)
  (clear-screen)
  (show!)
  (void))

(define (finish-operation!)
  (when (zero? *adventure-depth*)
   (when (or
          (< *x* 0)
          (< *y* 0)
          (> *x* +width+)
          (> *y* +height+))
         (printf "WARNING: The turtle is off the screen so you won't see much!\n")
         (printf "SUGGESTION: Type (home) to take the turtle back to the middle\n")))
  (void))

(define (ensure-initialised!)
  (unless *initialised*
          (new-doodle title: "Simple Graphics"
                      background: solid-white)
          (run-event-loop run-in-background: #t minimum-wait: 10)
          (reset!)
          (set! *initialised* #t))
  (void))

(define (home)
  (ensure-initialised!)
  (reset-position!)
  (finish-operation!))

(define (clear)
  (ensure-initialised!)
  (reset!)
  (finish-operation!))

(define (forward dist)
  (ensure-initialised!)
  (let ((new-x (+ *x* (* dist *dx*)))
        (new-y (+ *y* (* dist *dy*))))
    (when *pen-down?*
          (draw-line *x* *y* new-x new-y color: *colour*)
          (show!))
    (set! *x* new-x)
    (set! *y* new-y))
  (finish-operation!))

(define forwards forward)

(define (backward dist)
  (forward (- dist)))

(define backwards backward)

(define pi 3.1415926535897932384626433)

(define pi/180 (/ pi 180))

(define (degrees->radians angle)
  (* pi/180 angle))

(define (right #!optional (angle 90))
  ;; Rotate the *dx* / *dy* vector right by the given angle, in degrees
  (ensure-initialised!)
  (let ((cos-angle (cos (degrees->radians angle)))
        (sin-angle (sin (degrees->radians angle))))
   (let ((new-dx (- (* *dx* cos-angle) (* *dy* sin-angle)))
         (new-dy (+ (* *dx* sin-angle) (* *dy* cos-angle))))
     (set! *dx* new-dx)
     (set! *dy* new-dy)))
  (finish-operation!))

(define (left #!optional (angle 90))
  (right (- angle)))

(define (turn direction angle)
  (direction angle))

(define (pen-up)
  (ensure-initialised!)
  (set! *pen-down?* #f)
  (finish-operation!))

(define (pen-down)
  (ensure-initialised!)
  (set! *pen-down?* #t)
  (finish-operation!))

(define (lost)
  (printf "X: ~A Y: ~A\n" *x* *y*)
  (printf "DX: ~A DY: ~A\n" *dx* *dy*))

(define-record-type graphics-state
  (make-graphics-state x y dx dy colour pen-down?)
  graphics-state?
  (x graphics-state-x)
  (y graphics-state-y)
  (dx graphics-state-dx)
  (dy graphics-state-dy)
  (colour graphics-state-colour)
  (pen-down? graphics-state-pen-down?))

(define (current-state)
  (make-graphics-state *x* *y* *dx* *dy* *colour* *pen-down?*))

(define (set-state! s)
  (set! *x* (graphics-state-x s))
  (set! *y* (graphics-state-y s))
  (set! *dx* (graphics-state-dx s))
  (set! *dy* (graphics-state-dy s))
  (set! *colour* (graphics-state-colour s))
  (set! *pen-down?* (graphics-state-pen-down? s)))

(define (push-state!)
  (set! *saved-states*
        (cons (current-state) *saved-states*))
  (void))

(define (pop-state!)
  (set-state! (car *saved-states*))
  (set! *saved-states* (cdr *saved-states*))
  (void))

(define (begin-adventure!)
  (ensure-initialised!)
  (push-state!)
  (inc! *adventure-depth*))

(define (end-adventure!)
  (dec! *adventure-depth*)
  (pop-state!)
  (finish-operation!))

(define-syntax adventure
  (syntax-rules ()
    ((adventure body ...)
     (begin
       (begin-adventure!)
       body ...
       (end-adventure!)))))

(define (save)
  (ensure-initialised!)
  (unless *name*
          (set! *name* (parley "What's your name? ")))
  (let ((filename (sprintf "~A - ~A.png" *name* (seconds->string))))
    (save-screenshot filename)
    (printf "Saved to ~A\n" filename)))

(define-syntax define-colour
  (syntax-rules ()
    ((define-colour X R G B)
     (define (X)
       (ensure-initialised!)
       (set! *colour* (list R G B 1))
       (finish-operation!)))))

(define-colour red 1 0 0)
(define-colour green 0 1 0)
(define-colour blue 0 0 1)
(define-colour yellow 1 1 0)
(define-colour orange 1 0.5 0)
(define-colour brown 0.5 0.25 0)
(define-colour grey 0.5 0.5 0.5)
(define-colour black 0 0 0)

(define (heading angle)
  (ensure-initialised!)
  (let ((cos-angle (cos (degrees->radians (- angle 90))))
        (sin-angle (sin (degrees->radians (- angle 90))))
        (current-magnitude (sqrt (+ (* *dx* *dx*) (* *dy* *dy*)))))
    (set! *dx* (* cos-angle current-magnitude))
    (set! *dy* (* sin-angle current-magnitude)))
  (finish-operation!))

(define (goto x y)
  (ensure-initialised!)
  (set! *x* (+ (/ +width+ 2) x))
  (set! *y* (- (/ +height+ 2) y))
  (finish-operation!))

(define go-to goto)

(define (faster #!optional (factor 2))
  (ensure-initialised!)
  (set! *dx* (* *dx* factor))
  (set! *dy* (* *dy* factor))
  (finish-operation!))

(define (slower #!optional (factor 2))
  (faster (/ 1 factor)))

(define (speed s)
  (ensure-initialised!)
  (let* ((current-magnitude
          (sqrt (+ (* *dx* *dx*) (* *dy* *dy*))))
         (factor (/ (* 10 s) current-magnitude)))
    (set! *dx* (* *dx* factor))
    (set! *dy* (* *dy* factor)))
  (finish-operation!))

)

Added simple-graphics.setup.















>
>
>
>
>
>
>
1
2
3
4
5
6
7
(compile -s -O3 -d1 simple-graphics.scm -j simple-graphics)
(compile -s -O3 -d1 simple-graphics.import.scm)
(compile -c -O3 -d1 simple-graphics.scm -unit simple-graphics)

(install-extension 'simple-graphics '("simple-graphics.so" "simple-graphics.o" "simple-graphics.import.so")
                   '((version "1.0")
                     (static "simple-graphics.o")))

Added toys.scm.





































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
(use simple-graphics)
(use miscmacros)

(define (curve direction angle radius)
  (let ((delta (/ radius angle 0.63104805627394)))
   (dotimes (x angle)
            (forward delta)
            (direction 1))))

(define (spiral spins radius-per-spin)
  (let ((delta (/ radius-per-spin 205.7409)))
   (dotimes (x (* 36 spins))
            (forward (* x delta))
            (right 10))))


(dotimes (x 10) (spiral x 1))

(define (house)
  (adventure
   (forward 5)
   (left 90)
   (forward 1)
   (right 135)
   (forward (* (sqrt 2) 3.5))
   (right 90)
   (forward (* (sqrt 2) 3.5))
   (right 135)
   (forward 1)
   (left 90)
   (forward 5)
   (right 90)
   (forward 5)
))