home *** CD-ROM | disk | FTP | other *** search
- ;;; Copyright (C) 1995 Cygnus Support, Inc.
- ;;;
- ;;; This program is free software; you can redistribute it and/or modify
- ;;; it under the terms of the GNU General Public License as published by
- ;;; the Free Software Foundation; either version 2, or (at your option)
- ;;; any later version.
- ;;;
- ;;; This program is distributed in the hope that it will be useful,
- ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
- ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ;;; GNU General Public License for more details.
- ;;;
- ;;; You should have received a copy of the GNU General Public License
- ;;; along with this software; see the file COPYING. If not, write to
- ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
- ;;;
- ;;; As a special exception, Cygnus Support gives permission
- ;;; for additional uses of the text contained in its release of this library.
- ;;;
- ;;; The exception is that, if you link this library with other files
- ;;; to produce an executable, this does not by itself cause the
- ;;; resulting executable to be covered by the GNU General Public License.
- ;;; Your use of that executable is in no way restricted on account of
- ;;; linking this library code into it.
- ;;;
- ;;; This exception does not however invalidate any other reasons why
- ;;; the executable file might be covered by the GNU General Public License.
- ;;;
- ;;; This exception applies only to the code released by
- ;;; Cygnus Support as part of this library. If you copy
- ;;; code from other releases distributed under the terms of the GPL into a copy of
- ;;; this library, as the General Public License permits, the exception does
- ;;; not apply to the code that you add in this way. To avoid misleading
- ;;; anyone as to the status of such modified files, you must delete
- ;;; this exception notice from such code.
- ;;;
- ;;; If you write modifications of your own for this library, it is your choice
- ;;; whether to permit this exception to apply to your modifications.
- ;;; If you do not wish that, delete this exception notice.
-
-
-
- (require 'random)
- (define (coin-toss . from)
- (list-ref from (random (length from))))
-
-
-
- (define (glut-just-once thunk)
- (thunk))
-
-
-
-
- (define (glutReshapeFunc-callout w h)
- (glut-just-once (lambda () (glutReshapeFunc w h))))
-
- (define (glutDefaultReshape w h)
- (glMatrixMode GL_PROJECTION)
- (glLoadIdentity)
- (glFrustum -1 1 -1 1 1 200)
- (glMatrixMode GL_MODELVIEW)
- (glViewport 0 0 w h))
-
- (define glutReshapeFunc glutDefaultReshape)
-
-
-
- (define (glutDisplayFunc-callout)
- (glut-just-once (lambda () (glutDisplayFunc))))
- (define (glutDefaultDisplay) #t)
- (define glutDisplayFunc glutDefaultDisplay)
-
-
-
- (define (glutKeyboardFunc-callout char mouse-x mouse-y)
- (glut-just-once (lambda () (glutKeyboardFunc char mouse-x mouse-y))))
- (define (glutDefaultKeyboardFunc char mouse-x mouse-y) #t)
- (define glutKeyboardFunc glutDefaultKeyboardFunc)
-
-
-
- (define (glutMouseFunc-callout button type x y)
- (glut-just-once (lambda () (glutMouseFunc button type x y))))
- (define (glutDefaultMouseFunc button type x y) #f)
- (define glutMouseFunc glutDefaultMouseFunc)
-
-
-
-
- (define (glutPassiveMotionFunc-callout x y)
- (glut-just-once (lambda () (glutPassiveMotionFunc x y))))
- (define (glutDefaultPassiveMotionFunc button type x y) #f)
- (define glutPassiveMotionFunc glutDefaultPassiveMotionFunc)
-
-
-
-
- (define (glutIdleFunc-callout)
- (glut-just-once (lambda () (glutIdleFunc))))
- (define (glutDefaultIdleFunc) #f)
- (define glutIdleFunc glutDefaultIdleFunc)
-
-
- (define doubling #t)
- (define filling #f)
- (glutInitDisplayMode (logior (if doubling GLUT_DOUBLE 0) GLUT_RGB GLUT_DEPTH))
- (glutCreateWindow "the thing")
- (use-glutDisplayFunc #t)
- (use-glutIdleFunc #t)
- (use-glutReshapeFunc #t)
- (use-glutIdleFunc #t)
- (use-glutKeyboardFunc #t)
- (use-glutMouseFunc #t)
- (glClearDepth 1.0)
- (glClearColor 0.0 0.0 0.0 0.0)
-
-
-
- (define fogColor #s(0.0 0.0 0.0 1.0))
-
- (glEnable GL_FOG)
- (glFogi GL_FOG_MODE GL_LINEAR)
- (glHint GL_FOG_HINT GL_NICEST)
- (glFogf GL_FOG_START 3.0)
- (glFogf GL_FOG_END 5.0)
- (glFogfv GL_FOG_COLOR fogColor)
- (glClearColor 0.0 0.0 0.0 1.0)
- (glDepthFunc GL_LESS)
- (glEnable GL_DEPTH_TEST)
- (glShadeModel GL_FLAT)
-
-
-
- (define rotation '(0 0 0 0 0 0))
- (define v-rotation '(0 0 0 0 0 0))
- (define a-rotation '(0 0 0 0 0 0))
- (define dur-rotation '(0 0 0 0 0 0))
-
- (define (clip mag val)
- (if (>= mag (abs val))
- val
- (if (<= 0 val)
- mag
- (- mag))))
-
- (define (rotation-tick)
- (set! rotation
- (map + rotation v-rotation))
- (set! v-rotation
- (map (lambda (a b)
- (clip .005 (+ a b)))
- v-rotation a-rotation))
- (set! a-rotation
- (map (lambda (a d)
- (if (= d 0)
- ((coin-toss + -)
- (random 0.0006))
- a))
- a-rotation
- dur-rotation))
- (set! dur-rotation
- (map (lambda (n)
- (if (= n 0)
- (+ 19 (random 10))
- (+ -1 n)))
- dur-rotation)))
-
-
- (define (glutDisplayFunc)
- (glClear (logior GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT))
- (glColor3f (abs (- (car rotation) (floor (car rotation))))
- (abs (- (cadr rotation) (floor (car rotation))))
- (abs (- (caddr rotation) (floor (car rotation)))))
- (glLineWidth 4)
- (map
- (lambda (a ax)
- (apply glRotatef a ax))
- (cdddr rotation)
- '((1 0 0) (0 1 0) (0 0 1)))
- ((if filling glutSolidIcosahedron glutWireIcosahedron))
- (if doubling
- (glutSwapBuffers))
- (glFlush))
-
-
- (define attention (list 0.0 0.0 -4.0))
-
- (define (glutReshapeFunc w h)
- (glViewport 0 0 w h)
- (glMatrixMode GL_PROJECTION)
- (glLoadIdentity)
- (gluPerspective 45.0 (/ w h) 3.0 5.0)
- (glMatrixMode GL_MODELVIEW)
- (glLoadIdentity)
- (apply glTranslatef attention))
-
- (define zooming #f)
- (define (glutKeyboardFunc c x y)
- (cond
- ((eq? c #\q) (exit))
- ((eq? c #\z) (set! zooming #t) (set! zoomed 0))
- (else #f)))
-
- (define (glutMain)
- (glutMainLoop))
-
-
-
- (define zoomed 0)
- (define (glutIdleFunc)
- (rotation-tick)
- (if zooming
- (if (< zoomed 2.9)
- (begin
- (glLoadIdentity)
- (apply glTranslatef attention)
- (glTranslatef 0 0 zoomed)
- (set! zoomed (+ zoomed .038)))
- (begin
- (set! zooming #f)
- (set! zoomed 0)
- (goto-break))))
- (glutPostRedisplay))
-
-
- (define (glutMouseFunc type x y z)
- (set! zooming #t)
- (set! zoomed 0))
-
-
-
- ;; How big a canvas?
- ;;
- (define play-w 520)
- (define play-h 520)
-
- ;; Where does the play area start
- ;;
- (define bounds-x 4)
- (define bounds-y 4)
-
- ;; Where is the paddle ul cornder?
- ;;
- (define paddle-x 0)
- (define paddle-y 375)
-
- ;; Where is the puck center?
- ;;
- (define puck-x 0)
- (define puck-r 6)
- (define puck-y (- paddle-y puck-r 1))
-
- ;; Paddle size:
- ;;
- (define paddle-height 10)
- (define paddle-width 64)
-
- ;; How big is the in-bounds area for the puck?
- ;;
- (define bounds-w 512)
- (define bounds-h puck-y)
-
- ;; How many blocks per row?
- ;;
- (define n-row 16)
-
- ;; Row y positions
- ;;
- (define row0-y 32)
- (define row1-y 64)
-
- ;; Individual block size
- ;;
- (define row-height 16)
- (define row-width (/ bounds-w n-row))
-
- ;; Each entry either the name of a canvas
- ;; item for the block or #f if the block
- ;; has been eliminated:
- ;;
- (define row0 (make-vector n-row #f))
- (define row1 (make-vector n-row #f))
- (define row0-ref (make-vector n-row #f))
- (define row1-ref (make-vector n-row #f))
-
-
- ;; Puck dynamic
- ;;
- (define puck-max-vx puck-r)
- (define puck-max-vy puck-r)
- (define puck-init-init-vx 2)
- (define puck-init-init-vy -2)
- (define puck-init-vx 2)
- (define puck-init-vy -2)
- (define puck-vx puck-init-vx)
- (define puck-vy puck-init-vy)
- (define (coin-toss . from)
- (list-ref from (random (length from))))
- (define (puck-tick)
- (let ((old-x puck-x)
- (old-y puck-y)
- (new-puck-x 0)
- (new-puck-y 0))
- (set! new-puck-x (+ puck-x puck-vx))
- (set! new-puck-y (+ puck-y puck-vy))
- (cond
-
- ((or (and (< new-puck-y (+ row1-y row-height))
- (>= new-puck-y row1-y)
- (hit-puck-at-game-x!? row1 new-puck-x)
- row1-y)
- (and (< new-puck-y (+ row0-y row-height))
- (>= new-puck-y row0-y)
- (hit-puck-at-game-x!? row0 new-puck-x)
- row0-y)
- (and (< new-puck-y 0)
- 0))
- => (lambda (yref)
- (report-score)
- (set! puck-vy (- puck-vy))
- (set! new-puck-y (+ yref (- new-puck-y yref)))
- (if (= 0 n-blocks)
- (begin
- (win-level)
- (set! old-y new-puck-y)
- (set! old-x new-puck-x)))))
-
- ((<= bounds-h new-puck-y)
- (cond
- ((paddle-sweet? new-puck-x)
- (begin
- (set! puck-vy (- puck-vy))
- (set! new-puck-y (+ bounds-h (- bounds-h new-puck-y)))))
- ((paddle-sour? new-puck-x)
- (begin
- (set! puck-vy (- puck-vy))
- (let ((total (+ (* puck-vx puck-vx) (* puck-vy puck-vy))))
- (set! puck-vx ((coin-toss + -) (random puck-init-vx)))
- (set! puck-vy (- (sqrt (- total (* puck-vx puck-vx))))))
- (set! new-puck-y (+ bounds-h (- bounds-h new-puck-y)))))
- ((<= (+ bounds-h 90) new-puck-y)
- (lose-level)
- (set! old-y new-puck-y)
- (set! old-x new-puck-x))))
-
-
- ((< new-puck-x 0)
- (begin
- (set! puck-vx (- puck-vx))
- (set! new-puck-x (- new-puck-x))))
-
- ((<= bounds-w new-puck-x)
- (begin
- (set! puck-vx (- puck-vx))
- (set! new-puck-x (+ bounds-w (- bounds-w new-puck-x))))))
-
- (move-puck (- new-puck-x old-x) (- new-puck-y old-y))))
-
-
- (define (paddle-sweet? x)
- (and (> (abs puck-vx) .00001)
- (let ((r (/ paddle-width 2)))
- (< (abs (- x (+ r paddle-x)))
- r))))
-
- (define (paddle-sour? x)
- (let ((r (/ paddle-width 2)))
- (< (abs (- x (+ r paddle-x)))
- (+ (* 3 puck-r) r))))
-
-
- (define game-playable #t)
- (define game-playing #f)
- (define score 0)
- (define pucks-per-game 3)
- (define n-pucks pucks-per-game)
- (define n-blocks #f)
-
-
-
- (define (new-level)
- ; (.game.c 'delete 'all)
- ; (set! score-report #f)
- ; (set! puck-report #f)
- ; (set! game-over-report #f)
- (make-row! row0 row0-y)
- (make-row! row1 row1-y)
- (make-row! row0-ref row0-y)
- (make-row! row1-ref row1-y)
- (set! n-blocks (* 2 n-row))
- (new-puck)
- (new-paddle)
- (report-game-state))
-
- (define (new-game)
- (set! game-playable #t)
- (set! game-playing #f)
- (set! puck-init-vx puck-init-init-vx)
- (set! puck-init-vy puck-init-init-vy)
- (new-level)
- (set! n-pucks pucks-per-game)
- (set! score 0)
- (report-game-state))
-
-
-
- (define (projection)
- (glMatrixMode GL_PROJECTION)
- (glLoadIdentity)
- (glFrustum (- (/ play-w 2)) (/ play-h 2)
- (/ play-w 2) (- (/ play-h 2))
- 100 5000)
- (glMatrixMode GL_MODELVIEW)
- (glLoadIdentity))
-
- (define extra-angle 0)
- (define extra-dist 0)
- (define angle-v 0)
- (define dist-v 13)
- (define tumbling #f)
- (define (view)
- (glLoadIdentity)
- (glTranslatef 0 0 (- (+ 200 extra-dist)))
- (glRotatef (+ 15 extra-angle) 1 0 0)
- (glTranslatef (- (/ play-w 2)) (- (/ play-h 2)) 0))
-
- (define (tumble)
- (set! extra-angle (+ extra-angle angle-v))
- (set! extra-dist (+ extra-dist dist-v))
- (if (> extra-dist 6000)
- (quit))
- (glutPostRedisplay))
-
-
- (define screen-w 0)
- (define screen-h 0)
- (define (reshape w h)
- (glViewport 0 0 w h)
- (set! screen-w w)
- (set! screen-h h)
- (projection))
-
- (define (break-glutKeyboardFunc c x y)
- (cond
- ((eq? c #\q) (exit))
- ((eq? c #\t)
- (set! angle-v 4)
- (set! dist-v 13)
- (set! tumbling #t)
- (set! glutIdleFunc (lambda () (if tumbling (tumble)))))
- ((eq? c #\p)
- (if (eq? x-glutIdleFunc glutIdleFunc)
- (set! glutIdleFunc (lambda () (if tumbling (tumble))))
- (set! glutIdleFunc x-glutIdleFunc)))
- (else #f)))
-
-
- ;; Make the two rows:
- ;;
- (define (make-row! v y)
- (let loop ((n 0))
- (if (= n 16)
- v
- (begin
- (vector-set! v n (cons (+ (/ row-width 2) bounds-x (* n row-width))
- (+ (/ row-height 2) bounds-y y)))
- (loop (+ n 1))))))
-
-
- (define (hit-puck-at-game-x!? row x-game)
- (let* ((x (- x-game bounds-x))
- (i (inexact->exact (floor (/ x row-width)))))
- (and (>= i 0)
- (< i (vector-length row))
- (vector-ref row i)
- (begin
- (draw-without-clearing (lambda () #t) #t)
- (vector-set! row i #f)
- (set! n-blocks (- n-blocks 1))
- (set! score (+ 1 score))
- #t))))
-
- (define (draw-row row ref y color)
- (glPushMatrix)
- (array-for-each
- (lambda (on off)
- (if on
- (glColor3fv color)
- (glColor3fv #s(0.0 0.0 0.0)))
- (glPushMatrix)
- (let ((coord (or on off)))
- (glTranslatef (car coord) (cdr coord) 0)
- (glScalef (/ row-width 4) (/ row-height 4) 1)
- (glLineWidth 1)
- (glutWireCube 4.0)
- (glLineWidth 0))
- (glPopMatrix))
- row ref)
- (glPopMatrix))
-
-
-
- ;; Drawing the paddle:
- ;;
- (define paddle-color '#s(0.2 1.0 0.6))
- (define paddle #f)
- (define (new-paddle)
- (set! paddle
- (cons (+ bounds-x paddle-x (/ paddle-width 2))
- (+ bounds-y paddle-y (/ paddle-height 2)))))
-
-
- (define (draw-paddle color)
- (if paddle
- (begin
- (glPushMatrix)
- (glColor3fv color)
- (glTranslatef (car paddle) (cdr paddle) 0)
- (glScalef paddle-width paddle-height 8)
- (glutWireCube 1.0)
- (glPopMatrix))))
-
- (define (draw-puck color)
- (if paddle
- (begin
- (glPushMatrix)
- (glColor3fv color)
- (glTranslatef (car puck) (cdr puck) 0)
- (glScalef puck-r puck-r puck-r)
- (glutWireCube 1.0)
- (glPopMatrix))))
-
- (define puck-color #s(1.0 0.3 0.3))
-
- (define draw-without-clearing
- (let* ((cap-max 5)
- (cap 0)
- (discharge (lambda (th)
- (if (not (= 0 cap))
- (begin
- (set! cap 0)
- (th)))))
- (more-likely (lambda (th)
- (set! cap (+ 1 cap))
- (if (= cap cap-max)
- (discharge th))))
- (cap-2-max 5)
- (cap-2 0)
- (discharge-2 (lambda (th)
- (if (not (= 0 cap-2))
- (begin
- (set! cap-2 0)
- (th)))))
- (more-likely-2 (lambda (th)
- (set! cap-2 (+ 1 cap-2))
- (if (= cap-2 cap-2-max)
- (discharge-2 th)))))
- (lambda (thunk . opt)
- (begin
- (glPushMatrix)
- (view)
- (thunk)
- (draw-paddle paddle-color)
- (draw-puck puck-color)
- (glFlush)
- (glPushMatrix)
- (glTranslatef (/ play-w 2) (/ play-h 2) (/ play-h 2))
- (glColor3fv #s(1.0 1.0 1.0))
- (glutWireCube play-h)
- (glPopMatrix)
- (if (<= (- puck-y puck-r) (+ row1-y row-height))
- (begin
- (more-likely (lambda () (draw-row row1 row1-ref row1-y '#s(1.0 0.3 1.0))))
- (if (<= (- puck-y puck-r) (+ row0-y row-height))
- (more-likely-2 (lambda () (draw-row row0 row0-ref row0-y '#s(1.0 1.0 0.3))))
- (discharge-2 (lambda () (draw-row row0 row0-ref row0-y '#s(1.0 1.0 0.3))))))
- (discharge (lambda () (draw-row row1 row1-ref row1-y '#s(1.0 0.3 1.0)))))
- (if (not (eq? '() opt))
- (begin
- (set! cap 1)
- (set! cap-2 1)
- (discharge-2 (lambda () (draw-row row0 row0-ref row0-y '#s(1.0 1.0 0.3))))
- (discharge (lambda () (draw-row row1 row1-ref row1-y '#s(1.0 0.3 1.0))))))
- (glPopMatrix)))))
-
- (define (center-paddle-at-canvas-coord x)
- (draw-without-clearing
- (lambda ()
- (draw-paddle #s(0.0 0.0 0.0))
- (set! paddle-x (- x bounds-x (/ paddle-width 2)))
- (new-paddle))))
-
-
- ;; Drawing the puck:
- ;;
-
- (define puck #f)
- (define (new-puck)
- (set! puck-y (- paddle-y puck-r 1))
- (set! puck-x 0)
- (set! puck-vx puck-init-vx)
- (set! puck-vy puck-init-vy)
- (set! puck (cons (+ bounds-x puck-x)
- (+ bounds-y puck-y))))
-
- (define (move-puck dx dy)
- (draw-without-clearing
- (lambda ()
- (draw-puck #s(0.0 0.0 0.0))
- (set! puck-x (+ puck-x dx))
- (set! puck-y (+ puck-y dy))
- (set! puck (cons (+ bounds-x puck-x)
- (+ bounds-y puck-y))))))
-
- (define (remove-puck)
- (new-puck)
- (glutPostRedisplay))
-
-
- (define (x-glutIdleFunc)
- (if tumbling
- (tumble)
- (if game-playing
- (puck-tick)
- (set! game-playing #t))))
-
-
-
- (define score-report #f)
- (define puck-report #f)
- (define game-over-report #f)
-
- (define (lose-level)
- (remove-puck)
- (set! glutIdleFunc (lambda () #f))
- (glutPostRedisplay))
-
- (define (win-level)
- (new-game)
- (set! glutIdleFunc (lambda () #f))
- (glutPostRedisplay))
- ; (set! n-pucks (+ 1 n-pucks))
- ; (remove-puck)
- ; (set! game-playing #f)
- ; (set! score (+ 25 score))
- ; (set! puck-init-vy (* puck-init-vy 2))
- ; (set! puck-init-vx (* puck-init-vx 2))
- ; (if (< puck-init-vx puck-max-vx)
- ; (new-level)
- ; (set! game-playable #f))
- ; (report-game-state)
-
-
- (define (report-score)
- ; (and score-report (.game.c 'delete score-report))
- ; (set! score-report
- ; (.game.c 'create 'text 10 (+ 64 paddle-y)
- ; :font "-adobe-helvetica-bold-r-normal-*-24-*-*-*-*-*-*-*"
- ; :anchor 'w))
- ; (.game.c 'insert score-report 0
- ; (string-append "Score: " (number->string score)))
- #t
- )
-
-
- (define (report-game-state)
- (report-score)
- ; (and puck-report (.game.c 'delete puck-report))
- ; (set! puck-report
- ; (.game.c 'create 'text 200 (+ 64 paddle-y)
- ; :font "-adobe-helvetica-bold-r-normal-*-24-*-*-*-*-*-*-*"
- ; :fill (cond
- ; ((not game-playable) 'thistle4)
- ; ((eq? n-pucks 0) 'red)
- ; (else 'navy))
- ; :anchor 'w))
- ; (.game.c 'insert puck-report 0
- ; (if (not game-playable)
- ; "GAME OVER"
- ; (string-append "Pucks remaining: " (number->string n-pucks))))
- ; (and game-over-report (.game.c 'delete game-over-report))
- ; (set! game-over-report
- ; (.game.c 'create 'text 10 (+ 96 paddle-y)
- ; :font "-adobe-helvetica-bold-o-normal-*-18-*-*-*-*-*-*-*"
- ; :fill (if (not game-playable) 'red 'ForestGreen)
- ; :anchor 'w))
- ; (.game.c 'insert game-over-report 0
- ; (cond
- ; ((not game-playable) "`P' to start a new game; `Q' to quit")
- ; (game-playing "`P' to pause; `Q' to quit this game")
- ; (else "`P' to play; `Q' to quit")))
- #t
- )
-
-
- (define (play-game)
- (if (not game-playable) (new-game))
- (set! game-playing #t)
- (report-game-state)
- (loop)
- (report-game-state))
-
- (define (break-glutPassiveMotionFunc x y)
- (center-paddle-at-canvas-coord
- (+ bounds-x (* bounds-w (/ x screen-w)))))
-
-
-
- (define (break-glutReshapeFunc w h)
- (if (or (not (= w 550))
- (not (= h 550)))
- (glutReshapeWindow 550 550)
- (reshape w h)))
-
- (define (break-glutDisplayFunc)
- (glClear (logior GL_COLOR_BUFFER_BIT 0)); ((GL_DEPTH_BUFFER_BIT))
- (view)
- (draw-without-clearing
- (lambda () #f)
- #t))
-
- (define glutPassiveMotionFunc #f)
- (define (goto-break)
- (glDrawBuffer GL_FRONT)
- (set! glutDisplayFunc break-glutDisplayFunc)
- (use-glutPassiveMotionFunc #t)
- (set! glutPassiveMotionFunc break-glutPassiveMotionFunc)
- (set! glutReshapeFunc break-glutReshapeFunc)
- (set! glutKeyboardFunc break-glutKeyboardFunc)
- (set! glutIdleFunc (lambda () (lambda () (if tumbling (tumble)))))
- (use-glutIdleFunc #t)
- (glDisable GL_DEPTH_TEST)
- (glDisable GL_FOG)
- (new-game)
- (use-glutPassiveMotionFunc #t)
- (glutReshapeFunc 0 0)
- (glutPostRedisplay))
-
- (glutMainLoop)
-
-
-