File: cannon.lisp

package info (click to toggle)
cl-sdl 0.2.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,188 kB
  • ctags: 1,610
  • sloc: lisp: 8,278; ansic: 516; sh: 177; makefile: 163
file content (372 lines) | stat: -rw-r--r-- 12,255 bytes parent folder | download | duplicates (3)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
;;;; -*- Mode: Lisp -*-

(defpackage #:cannon-game
  (:use #:cl)
  (:nicknames #:cannon)
  (:export #:start))

(in-package #:cannon)

(defvar *cannon*)

(defparameter *quad*
    '((0f0 0f0 1f0)
      ( 0.0f0 1.0f0 ) ( -1.0f0 -1.0f0 1.0f0 )
      ( 1.0f0 1.0f0 ) (  1.0f0 -1.0f0 1.0f0 )
      ( 1.0f0 0.0f0 ) (  1.0f0  1.0f0 1.0f0 )
      ( 0.0f0 0.0f0 ) ( -1.0f0  1.0f0 1.0f0 )

      (0f0 0f0 -1f0)
      ( 0.0f0 0.0f0 ) ( -1.0f0 -1.0f0 -1.0f0 )
      ( 0.0f0 1.0f0 ) ( -1.0f0  1.0f0 -1.0f0 )
      ( 1.0f0 1.0f0 ) (  1.0f0  1.0f0 -1.0f0 )
      ( 1.0f0 0.0f0 ) (  1.0f0 -1.0f0 -1.0f0 )

      (0f0 1f0 0f0)
      ( 1.0f0 1.0f0 ) ( -1.0f0  1.0f0 -1.0f0 )
      ( 1.0f0 0.0f0 ) ( -1.0f0  1.0f0  1.0f0 )
      ( 0.0f0 0.0f0 ) (  1.0f0  1.0f0  1.0f0 )
      ( 0.0f0 1.0f0 ) (  1.0f0  1.0f0 -1.0f0 )

      (0f0 -1f0 0f0)
      ( 0.0f0 1.0f0 ) ( -1.0f0 -1.0f0 -1.0f0 )
      ( 1.0f0 1.0f0 ) (  1.0f0 -1.0f0 -1.0f0 )
      ( 1.0f0 0.0f0 ) (  1.0f0 -1.0f0  1.0f0 )
      ( 0.0f0 0.0f0 ) ( -1.0f0 -1.0f0  1.0f0 )

      (1f0 0f0 0f0)
      ( 0.0f0 0.0f0 ) ( 1.0f0 -1.0f0 -1.0f0 )
      ( 0.0f0 1.0f0 ) ( 1.0f0  1.0f0 -1.0f0 )
      ( 1.0f0 1.0f0 ) ( 1.0f0  1.0f0  1.0f0 )
      ( 1.0f0 0.0f0 ) ( 1.0f0 -1.0f0  1.0f0 )

      (-1f0 0f0 0f0)
      ( 1.0f0 0.0f0 ) ( -1.0f0 -1.0f0 -1.0f0 )
      ( 0.0f0 0.0f0 ) ( -1.0f0 -1.0f0  1.0f0 )
      ( 0.0f0 1.0f0 ) ( -1.0f0  1.0f0  1.0f0 )
      ( 1.0f0 1.0f0 ) ( -1.0f0  1.0f0 -1.0f0 )))

(defun draw-boat ()
  #||
  (sgum:with-single-float-values (mat-spec 0.0f0 0.2f0 0.0f0 1f0)
    (gl:material-fv gl:+front+ gl:+specular+ mat-spec))
  (sgum:with-single-float-values (mat-shin 20f0)
    (gl:material-fv gl:+front+ gl:+shininess+ mat-shin))
  ||#
  (sgum:with-single-float-values (mat-amb-diff 0.1f0 0.1f0 0.1f0 1.0f0)
    (gl:material-fv gl:+front+ gl:+ambient-and-diffuse+ mat-amb-diff))
  (let ((ptr *quad*))
    (gl:begin gl:+quads+)
    (loop repeat 6 do
          (apply #'gl:normal-3f (first ptr))
          (setf ptr (rest ptr))
          (loop repeat 4 do
                ;;(apply #'gl:tex-coord-2f (first ptr))
                (apply #'gl:vertex-3f (second ptr))
                (setf ptr (cddr ptr))))
    (gl:end)))

(defun draw-cannon ()
  (let ((base 0.05d0)
        (top 0.05d0)
        (height 1.0d0))
    (sgum:with-single-float-values (mat-amb-diff 0.35f0 0.35f0 0.35f0 1.0f0)
      (gl:material-fv gl:+front+ gl:+ambient-and-diffuse+ mat-amb-diff))
    (glu:with-quadric (cyl)
      (glu:quadric-draw-style cyl glu:+fill+)
      (glu:quadric-normals cyl glu:+smooth+)
      (glu:cylinder cyl base top height 25 20))))


(defun draw-shot ()
  (sgum:with-single-float-values (mat-amb-diff 0.2f0 0.2f0 0.2f0 1.0f0)
    (gl:material-fv gl:+front+ gl:+ambient-and-diffuse+ mat-amb-diff))
  (glu:with-quadric (sphere)
    (glu:quadric-draw-style sphere glu:+fill+)
    (glu:quadric-normals sphere glu:+smooth+)
    (glu:sphere sphere 0.03d0 25 20)))

(let* ((size 60)
       (size-1 (1- size))
       (size/2 (truncate size 2))
       (segsize 0.2f0)
       (dampening 256)
       (current-array 0)
       (velocities (make-array (list 2 size size)
                               :element-type 'single-float
                               :initial-element 0f0))
       (array (make-array (list 2 size size) 
                          :element-type 'single-float
                          :initial-element 0f0))
       (prev-update 0)
       (update-interval 50)
       #+nil (unit-interval 10)
       (prev-draw 0)
       (prev-splash 0)
       (splash-interval 1000)
       (splash-depth 0.5f0))
  (declare (ignorable velocities))
  (defun init-water ()
    (setf (aref array current-array 20 28)
      0.5f0))
  (init-water)
  (defun update-water ()
    (declare (optimize (speed 3)))
    (let ((interval (- (sdl:get-ticks) prev-update)))
      (unless (>= interval update-interval)
        (return-from update-water nil))
      (loop for i from 1 below size-1 do
            (loop for j from 1 below size-1 do
                  (flet ((ref (dx dy)
                           (aref array current-array (+ i dx) (+ j dy))))
                    (let* ((t1 (+ (ref -1 0)
                                  (ref 1 0)
                                  (ref 0 -1)
                                  (ref 0 1)
                                  (ref -1 -1)
                                  (ref 1 -1)
                                  (ref -1 1)
                                  (ref 1 1)))
                           (t2 (- (/ t1 4)
                                  (aref array (- 1 current-array) i j)))
                           (new (- t2 (/ t2 dampening)))
                           #+nil
                           (old (aref array (- 1 current-array) i j)))
                      (setf (aref array (- 1 current-array) i j) new)))))
      
      (setf prev-update (sdl:get-ticks))
      (setf current-array (- 1 current-array)))
    
    (when (>= (- (sdl:get-ticks) prev-splash)
              splash-interval)
      (decf (aref array current-array 
                  (random size)
                  (random size))
            splash-depth)
      (setf prev-splash (sdl:get-ticks))))
  
  (defun draw-water ()
    (let (interval)
      (declare (ignorable interval))
      (when (zerop prev-draw)
        (setf prev-draw (sdl:get-ticks))
        (return-from draw-water nil))
      (setf interval (- (sdl:get-ticks)
                        prev-draw))
      (sgum:with-single-float-values (mat-amb-diff 0.0f0 0.3f0 0.6f0 1.0f0)
        (gl:material-fv gl:+front+ gl:+ambient-and-diffuse+ mat-amb-diff))
      (sgum:with-single-float-values (mat-shin 70f0)
        (gl:material-fv gl:+front+ gl:+shininess+ mat-shin))
      (gl:push-matrix)
      (gl:translate-f (* (- size/2) segsize) 0f0 0f0)
      (dotimes (i (1- size))
        (gl:push-matrix)
        (gl:translate-f 0f0 0f0 (* segsize (- i size/2)))
        (gl:begin gl:+triangle-strip+)
        (gl:normal-3f 0f0 1f0 0f0)
        (dotimes (j size)
          #+nil
          (when (zerop i)
            (incf (aref array current-array i j)
                  (* (aref velocities current-array i j)
                     (/ interval 500))))
          (gl:vertex-3f (* j segsize)
                        (aref array current-array i j)
                        0f0)
          #+nil
          (incf (aref array current-array (1+ i) j)
                  (* (aref velocities current-array (1+ i) j)
                     (/ interval 500)))
          (gl:vertex-3f (* j segsize)
                        (aref array current-array (1+ i) j)
                        segsize))
        (gl:end)
        (gl:pop-matrix))
      (gl:pop-matrix)
      (sgum:with-single-float-values (mat-shin 20f0)
        (gl:material-fv gl:+front+ gl:+shininess+ mat-shin)))))


(defun init-gl ()
  (gl:shade-model gl:+smooth+)
  (sdl:set-gl-attributes :red-size 4 :blue-size 4 :green-size 4
                         :doublebuffer 1 :depth-size 16)
  (gl:clear-color 0.0f0 0.0f0 0.0f0 0.0f0)
  (gl:clear-depth 1.0d0)
  (gl:enable gl:+depth-test+)
  (gl:depth-func gl:+lequal+)
  (gl:hint gl:+perspective-correction-hint+ gl:+nicest+)

  (gl:load-identity)

  (sgum:with-single-float-values (light-pos 1f0 1f0 1f0 1f0)
    (gl:light-fv gl:+light0+ gl:+position+ light-pos))
  (sgum:with-single-float-values (light-amb 0.8f0 0.7f0 0.5f0 1f0)
    (gl:light-fv gl:+light0+ gl:+ambient+ light-amb))
  (sgum:with-single-float-values (light-diff 1f0 0.8f0 1f0 1f0)
    (gl:light-fv gl:+light0+ gl:+diffuse+ light-diff))
  (sgum:with-single-float-values (light-spec 0.7f0 0.5f0 0f0 1f0)
    (gl:light-fv gl:+light0+ gl:+specular+ light-spec))
  
  (gl:enable gl:+lighting+)
  (gl:enable gl:+light0+)

  (gl:enable gl:+color-material+)
  (gl:color-material gl:+front+ gl:+specular+)
  (gl:enable gl:+fog+)
  (gl:hint gl:+fog-hint+ gl:+dont-care+)
  (gl:fog-f gl:+fog-start+ 1f0)
  (gl:fog-f gl:+fog-end+ 10f0)
  (gl:fog-f gl:+fog-density+ 0.30f0)
  t)

(defun run-sdl-event-loop ()
  (sdl:event-loop
   (:key-down (key)
	      (when (= key (char-code #\q))
		(return)))
   (:mouse-button-up (button x y)
		     (format t "Mouse button up: ~A (~A, ~A)~%" button x y))
   (:mouse-button-down (button x y)
		       (format t "Mouse button dn: ~A (~A, ~A)~%" button x y))
   (:quit ()
	  (return))
   (:resize (width height)
	    (format t "Resized width = ~A height = ~A~%" width height))
   (:idle ()
	  (update-gl))))

(defun update-gl (surface)
  (declare (ignorable surface))
  (gl:clear-color 0f0 0f0 0f0 0f0)
  (gl:clear (logior gl:+color-buffer-bit+
                    gl:+depth-buffer-bit+))
  (gl:load-identity)

  (gl:translate-f 0f0 -0.7f0 0.3f0)

  (progn
    (gl:push-matrix)
    (gl:rotate-f 190f0 1f0 0f0 0f0)
    (gl:translate-f 0f0 (- -0.1f0 0.05f0) 0f0)
    (gl:rotate-f 20f0 1f0 0f0 0f0)
    (gl:rotate-f 0f0 0f0 1f0 0f0)
    (gl:color-3f 0.35f0 0.35f0 0.35f0)
    (draw-cannon)
    (gl:pop-matrix))
  
  (progn
    (gl:push-matrix)
    (gl:translate-f 1f0 0f0 -5f0)
    (gl:color-3f 0f0 0f0 1f0)
    (gl:scale-f 0.2f0 0.1f0 0.2f0)
    (draw-boat)
    (gl:pop-matrix))

  (progn 
    (gl:push-matrix)
    (gl:translate-f 0f0 0.9f0 -1f0)
    (gl:color-3f 0.2f0 0.2f0 0.2f0)
    (draw-shot)
    (gl:pop-matrix))

  (progn
    (gl:push-matrix)
    (gl:color-3f 0f0 0.7f0 1f0)
    (update-water)
    (draw-water)
    (gl:pop-matrix))
  
  (sdl:gl-swap-buffers)
  t)

(defun resize (surface width height)
  (declare (ignorable surface))
  (when (zerop height)
    (setf height 1))
  (let ((ratio (coerce (/ width height) 'double-float)))
    (gl:viewport 0 0 width height)
    (gl:matrix-mode gl:+projection+)
    (gl:load-identity)
    (glu:perspective 45.0d0 ratio 0.1d0 100.0d0)
    (gl:matrix-mode gl:+modelview+)
    (gl:load-identity)

    t))

(defun event-loop (surface update-fn resize-fn 
                   handle-keypress-fn bpp video-flags)
  (sdl:event-loop
   (:key-down (key)
	      (if (= key (char-code #\q))
                  (return)
                (funcall handle-keypress-fn key)))
   (:mouse-button-up (button x y)
		     (format t "Mouse button up: ~A (~A, ~A)~%" button x y))
   (:mouse-button-down (button x y)
		       (format t "Mouse button dn: ~A (~A, ~A)~%" button x y))
   (:quit ()
	  (return))
   (:resize (width height)
	    (format t "Resized width = ~A height = ~A~%" width height)
            (setf surface
              (sdl:set-video-mode width height bpp video-flags))
            (funcall resize-fn surface width height))
   (:idle ()
	  (progn (funcall update-fn surface)
                 (incf-frames)))))


(defun handle-keypress (key)
  key)

(defvar *frames* 0)
(defvar *start-time*)

(defun incf-frames ()
  (incf *frames*)
  (let* ((time (get-universal-time))
         (delta (- time *start-time*)))
    (when (>= delta 5)
      (format t "~&~A frames in ~A seconds = ~A FPS~%"
              *frames* delta (float (/ *frames* delta)))
      (setf *frames* 0
            *start-time* time))))

(defun start (&key (width 640) (height 480) (bpp 16) (video-flags (logior sdl:+opengl+ sdl:+resizable+ sdl:+gl-doublebuffer+ sdl:+hwpalette+)) (title "Cannon"))
  (sdl:init sdl:+init-video+)

  (multiple-value-bind (hw-available-p blit-hw-p)
      (sdl:query-video-info :hw-available :blit-hw)
    (setf video-flags (logior video-flags
                              (if hw-available-p
                                  sdl:+hwsurface+
                                  sdl:+swsurface+)
                              (if blit-hw-p
                                  sdl:+hwaccel+
                                  0))))
  (setf *frames* 0
        *start-time* (get-universal-time))
  (sdl:gl-set-attribute sdl:+gl-doublebuffer+ 1)

  (let ((surface (sdl:set-video-mode width height bpp video-flags)))
    
    (when (sgum:null-pointer-p surface)
      (error "Failed to obtain surface"))
    
    (unless (init-gl)
      (error "Failed to initialize GL"))
    
    (resize surface width height)
    
    (sdl:wm-set-caption title nil)
    
    (unwind-protect (event-loop surface 
                                'update-gl
                                'resize
                                'handle-keypress
                                bpp
                                video-flags)
      
      (sdl:quit))
    (values)))