File: goban.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 (220 lines) | stat: -rw-r--r-- 7,277 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
;;;; -*- Mode: Lisp -*-
;;;; Draw and maintain a board for the game of Go
;;;; Author: Matthew Danish <mrd@debian.org>

(defpackage #:goban-sdl
  (:use #:common-lisp)
  (:nicknames #:gbsdl)
  (:export #:start))

(in-package #:goban-sdl)

(defparameter *screen-width* 640)
(defparameter *screen-height* 480)

(defparameter *padding-factor* 48)
(defparameter *left-side-factor* 32)
(defparameter *top-side-factor* 24)
(defparameter *hspacing-factor* 28)
(defparameter *vspacing-factor* 20)


(defvar *padding-x*)
(defvar *padding-y*)
(defvar *x1*)
(defvar *y1*)
(defvar *x-spacer*)
(defvar *y-spacer*)
(defvar *w*)
(defvar *h*)
(defvar *x2*)
(defvar *y2*)
(defvar *radius*)
(defvar *text-x*)
(defvar *text-y*)

(defvar *board*)

(defvar *font*)
(defparameter *font-file* (sdl-data:data-file "bboron" "ttf"))
(defparameter *font-size* 18)

(defun set-video-mode ()
  (setf *padding-x* (truncate *screen-width* *padding-factor*)
        *padding-y* (truncate *screen-height* *padding-factor*)
        *x1* (truncate *screen-width* *left-side-factor*)
        *y1* (truncate *screen-height* *top-side-factor*)
        *x-spacer* (truncate *screen-width* *hspacing-factor*)
        *y-spacer* (truncate *screen-height* *vspacing-factor*)
        *w* (* 18 *x-spacer*)
        *h* (* 18 *y-spacer*)
        *x2* (+ *x1* *w*)
        *y2* (+ *y1* *h*)
        *radius* (1- (truncate (min *x-spacer* *y-spacer*) 2))
        *text-x* (+ (* 2 *padding-x*)
                    *x1*
                    (* 20 *x-spacer*))
        *text-y* *y1*)
  
  (sdl:set-video-mode *screen-width*
                      *screen-height*
                      16
                      (logior sdl:+resizable+
                              sdl:+swsurface+
                              sdl:+doublebuf+)))

(defun init-sdl (&key (title "Go Ban"))
  (sdl:init (logior sdl:+init-video+ sdl:+init-noparachute+))
  (let ((surface (set-video-mode)))
    (when (sgum:null-pointer-p surface)
      (error "Unable to set video mode"))
    (sdl:wm-set-caption title nil)
    surface))

(defun run-sdl-event-loop (surface update-fn mouse-fn)
  (sdl:event-loop
   (:key-down (scan-code key mod unicode)
              (format t "Key down: ~A~%"
                      (if (< key 256)
                          (code-char key)
                          key))
              (when (= (char-code #\q) key)
                (return-from run-sdl-event-loop nil)))
   (:key-up (scan-code key mod unicode)
            (format t "Key up: ~A~%"
                      (if (< key 256)
                          (code-char key)
                          key)))
   (:mouse-button-up (button x y)
                     (format t "Mouse button up: ~A (~A, ~A)~%" button x y)
                     (funcall mouse-fn button x y :up))
   (:mouse-button-down (button x y)
                       (format t "Mouse button dn: ~A (~A, ~A)~%" button x y))
   (:quit ()
          (return))
   (:resize (width height)
            (setf *screen-width* width
                  *screen-height* height
                  surface (set-video-mode))
            (format t "Resized width = ~A height = ~A~%" width height))
   (:idle ()
          (funcall update-fn surface))))

(defmacro with-game-vars (vars &body body)
  `(symbol-macrolet ,(loop for v in vars collect
                           `(,v ,(intern (format nil "*~A*" v))))
    ,@body))

(defun place-piece (x y color)
  (setf (aref *board* x y)
        color))

(defun piece-at (i j)
  (aref *board* i j))

(defvar *turn* :black)
(defun default-mouse-handler (button x y up-or-down)
  (when (and (eq up-or-down :up)
             (eq button :left))
    (with-game-vars (x1 y1 radius x-spacer y-spacer)
      (let (i j quot-x rem-x quot-y rem-y)
        (multiple-value-setq (quot-x rem-x)
          (truncate (- x x1) x-spacer))
        (multiple-value-setq (quot-y rem-y)
          (truncate (- y y1) y-spacer))
        (setf i (cond ((<= rem-x radius)
                       quot-x)
                      ((>= rem-x (- x-spacer radius))
                       (1+ quot-x)))
              j (cond ((<= rem-y radius)
                       quot-y)
                      ((>= rem-y (- y-spacer radius))
                       (1+ quot-y))))
        (when (and i j (null (piece-at i j)))
          (place-piece i j *turn*)
          (setf *turn*
                (if (eq *turn* :black)
                    :white
                    :black)))))))
        
          

(defun draw-board (surface)
  (with-game-vars (w h padding-x padding-y radius
                     x-spacer y-spacer x1 y1 x2 y2)
    (sdl:draw-filled-rectangle surface
                               (- x1 padding-x)
                               (- y1 padding-y)
                               (+ w (* 2 padding-x))
                               (+ h (* 2 padding-y))
                               255 200 0)
    (dotimes (i 19)
      (let ((x (+ x1 (* i x-spacer)))
            (y (+ y1 (* i y-spacer))))
        (sdl:draw-line surface
                       x
                       y1
                       x
                       y2
                       0 0 0)
        (sdl:draw-line surface
                       x1
                       y
                       x2
                       y
                       0 0 0)))
    (dotimes (i 19)
      (dotimes (j 19)
        (let ((x (+ x1 (* i x-spacer)))
              (y (+ y1 (* j y-spacer))))
          (case (aref *board* i j)
            ((black :black)
             (sdl:draw-filled-circle surface x y radius 0 0 0))
            ((white :white)
             (sdl:draw-filled-circle surface x y radius 255 255 255))))))
    #+nil
    (sdl:update-rect surface x1 y1 w h)))

(defun draw-game-status (surface)
  (cl-sdl-ttf:with-solid-text (s *font* (format nil "Turn: ~A" *turn*)
                                 255 255 255)
    (sgum:with-foreign-objects ((rect sdl:rect))
      (setf (sdl:rect-x rect) *text-x*
            (sdl:rect-y rect) *text-y*)
      (sdl:blit-surface s sgum:+null-pointer+ surface rect)
      #+nil
      (sdl:update-rect surface
                       *text-x*
                       *text-y*
                       (sdl:rect-w rect)
                       (sdl:rect-h rect)))))

(defun update-screen (surface)
  (sdl:draw-filled-rectangle surface
                             0 0 *screen-width* *screen-height*
                             0 0 0)
  (draw-board surface)
  (draw-game-status surface)
  (sdl:flip surface))

(defun start (&key (width 640) (height 480) (title "Go Ban"))
  (let ((*screen-width* width)
        (*screen-height* height)
        (surface nil)
        (*board* (make-array '(19 19) :initial-element nil)))
    (setf *turn* :black)
    (unwind-protect
         (progn
           (setf surface (init-sdl :title title))
           (when surface
             (sdl-ttf:init)
             (unwind-protect
                  (progn
                    (setf *font* (sdl-ttf:open-font *font-file* *font-size*))
                    (when *font*
                      (run-sdl-event-loop surface
                                          #'update-screen
                                          #'default-mouse-handler)))
               (when *font*
                 (sdl-ttf:close-font *font*)))))
      (sdl:quit))))