main.scm 9.54 KB
Newer Older
1 2 3 4
#!/usr/bin/guile \
-e main -s
!#

Hugo Hörnquist's avatar
Hugo Hörnquist committed
5
;; (add-to-load-path "/usr/local/share/guile/site/2.2/")
6
(add-to-load-path (dirname (current-filename)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
7

8 9 10
(define (media str)
  (string-append (dirname (current-filename)) str))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
11 12 13
(use-modules (srfi srfi-1)

             (sdl2)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
14
             (sdl2 ttf)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
15
             (sdl2 video)
16
             (sdl2 image)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
17 18 19 20
             (sdl2 rect)
             (sdl2 render)
             (sdl2 surface)
             (sdl2 events)
21

22
             (rnrs base)
23 24 25 26

             (ice-9 threads)
             (ice-9 atomic)

27 28 29 30
             (util)
             (map)
             (class)
             (draw)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
31 32
             )

33 34 35 36 37 38
(define-values (wait wake)
  (let ((mutex (make-mutex))
        (condvar (make-condition-variable)))
    (values
     (lambda () (with-mutex mutex (wait-condition-variable condvar mutex)))
     (lambda () (broadcast-condition-variable condvar)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
39 40

(define texture-map (make-hash-table))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
41

Hugo Hörnquist's avatar
Hugo Hörnquist committed
42
(define ray-count 64)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
43

44
(define rays (make-atomic-box '()))
Hugo Hörnquist's avatar
huh?  
Hugo Hörnquist committed
45

Hugo Hörnquist's avatar
Hugo Hörnquist committed
46
(define game-map (call-with-input-file "b-huset.map" (compose parse-map read-map)))
47 48 49

(define player (make-player (+ 1/2 (car (board-spawn game-map)))
                            (+ 1/2 (cadr (board-spawn game-map)))
50
                            0.1))
51

52 53
(define update
  (let ((keys-down '()))
54
    (lambda (dt)                             ; dt in ms
55

56 57
      (let ((ev (poll-event)))
        (cond #; [(keyboard-event-repeat? ev) #f] ;; TODO implement in bindings
58 59 60 61

         [(keyboard-down-event? ev)
          (set! keys-down (lset-adjoin eq? keys-down (keyboard-event-scancode ev)))]

62
         [(keyboard-up-event? ev)
63
          (set! keys-down (delv (keyboard-event-scancode ev) keys-down))]
64

65
         [(quit-event? ev) (raise SIGINT)]))
66

67 68 69 70
      (for-each
       (lambda (key)
         (case key
           ((x q) (raise SIGINT))
71
           ((w s) (set! (p player)
72
                        = (+ (* dt 6e-3 (if (eq? key 'w) 1 -1)
73 74 75
                                (v3 (cos (a player))
                                    (sin (a player))))))
            (wake))
76
           ((j) (set! (a player) = (- (* dt 4e-3)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
77 78
            (when (> 0 (a player))
              (set! (a player) tau))
79
            (wake))
80

81
           ((p) (set! (a player) = (+ (* dt 4e-3)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
82 83
            (when (< tau (a player))
              (set! (a player) 0))
84
            (wake))
85 86
           ))
       keys-down))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
87

Hugo Hörnquist's avatar
Hugo Hörnquist committed
88 89 90 91 92 93 94 95
(define (decimals x)
  (- x (truncate x)))

(define (cot a)
  (/ (tan a)))

(define sgn signum)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
96
(define (ray-trace player ray-count)
97
  (let ((p (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
98 99
    (let ((px (x p))
          (py (y p)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
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
      (map (lambda (a)
             (let loop ((x px) (y py))
               (cond [(or (not (and (<= 0 x) (< x (board-width game-map))
                                    (<= 0 y) (< y (board-height game-map))))
                          (< 15 (sqrt (+ (expt (- x px) 2)
                                         (expt (- y py) 2)))))
                      ;; outside board
                      (make-ray #:a a #:type #f
                                #:v (v3 (- x px) (- y py))
                                #:hitf (v3 x y)) ]

                     ;; hit wall
                     [(array-ref (board-data game-map) (int y) (int x))
                      (lambda (s) (memv s '(wall window)))
                      => (lambda (tile)
                           (make-ray #:a a #:type tile
                                     #:v (v3 (- x px) (- y py))
                                     #:hitf (v3 x y)))]

                     ;; follow ray
                     [else (let ((dx-ampl (mod (* -1 (sgn (cos a)) (decimals x)) 1))
                                 (dy-ampl (mod (* -1 (sgn (sin a)) (decimals y)) 1)))
                             (let* ((dx (* 1.01 dx-ampl (sgn (cos a))))
                                    (dy (* dx (tan a))))
                               (if (<= (abs dy) dy-ampl)
                                   (loop (+ x dx) (+ y dy))
                                   (let* ((dy (* 1.01 dy-ampl (sgn (sin a))))
                                          (dx (* dy (cot a))))
                                     (loop (+ x dx) (+ y dy))))))])))

           (iota ray-count (- (a player) (/ (fov player) 2))
                 (/ (fov player) ray-count)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
132
  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
133

Hugo Hörnquist's avatar
Hugo Hörnquist committed
134
(define (draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
135
  (set-draw-color #xFF #xFF #xFF)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
136 137
  (clear)

138
  ;; draw level
Hugo Hörnquist's avatar
Hugo Hörnquist committed
139 140
  (for-each
   (lambda (pt)
141
     (apply set-draw-color
142
      (case (array-ref (board-data game-map) (y pt) (x pt))
143 144 145 146 147
        ((wall)   '(#xBB #xBB #xBB))
        ((window) '(#x10 #x10 #xFF))
        ((grass)  '(0 #xCC 0))
        (else     '(#xFF #xFF #xFF))))
     (fill-rect (x pt) (y pt) 1 1))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
148
   (map (lambda (p) (apply v3 p))
149
        (cross-product (iota (board-width game-map)) (iota (board-height game-map)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
150

151 152
  ;; draw raycast
  (set-draw-color #xFF 0 0)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
153

Hugo Hörnquist's avatar
Hugo Hörnquist committed
154
  (let ((pp (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
155 156
   (for-each (lambda (ray)
               (let ((v (+ pp (vec ray))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
157 158
                 (draw-line (x pp) (y pp)
                            (x v)  (y v))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
159
             (atomic-box-ref rays)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
160
  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
161

Hugo Hörnquist's avatar
Hugo Hörnquist committed
162
(define (draw-first-person-perspective)
163
  (set-draw-color #xBB #xBB #xFF)       ; light blue
Hugo Hörnquist's avatar
Hugo Hörnquist committed
164 165
  (clear)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
166
  ;; floor
Hugo Hörnquist's avatar
Hugo Hörnquist committed
167 168 169 170
  (set-draw-color #x33 #x33 #x33)
  (render-fill-rect (current-renderer)
                    (make-rect 0 240 640 240))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
171
  (let ((rays (atomic-box-ref rays)))
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
    (unless (null? rays)
      (for-each
       (lambda (i r)
         (cond ((hashq-ref texture-map (type r))
                => (lambda (texture)
                     (let* ((l (length r))
                            (segment-height (int (- 480 (* 30 l) 40))))

                       (let ((c (max 0 (int (- #xFF (* l 20))))))
                         (set-texture-color-mod! texture c c c))

                       (render-copy
                        (current-renderer)
                        texture
                        #:dstrect (list (floor/ (* i 640) ray-count)
                                        (floor/ (- 480 segment-height) 2)
                                        (floor/ 640 ray-count)
                                        segment-height)


                        #:srcrect
                        (list (int (* 16 (pos-on-wall r))) 0
                              1 16)))))))
       (iota ray-count)
       rays
       ))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
198 199


Hugo Hörnquist's avatar
Hugo Hörnquist committed
200

Hugo Hörnquist's avatar
Hugo Hörnquist committed
201 202 203

(define color (make-parameter (make-color 0 0 0 #xFF)))
(define* (render-text str #:key (line 0))
204
  (let ((surf (render-font-blended (current-font) str (color))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
205 206
    (render-copy (current-renderer)
                 (surface->texture (current-renderer) surf)
207
                 #:dstrect (list 5 (+ 1 (* line (+ 1 (font-height (current-font)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
208
                                 (surface-width surf)
209 210
                                 (surface-height surf)))
    (delete-surface! surf)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
211

212 213


Hugo Hörnquist's avatar
Hugo Hörnquist committed
214 215 216 217 218
(define (with-render-target target thunk)
  (dynamic-wind (lambda () (set-render-target! (current-renderer) target))
                thunk
                (lambda () (set-render-target! (current-renderer) #f))))

219
(define (make-fps-counter)
220
  (let ((last-time (sdl-ticks)))
221
    (lambda ()
222 223
      ;; TODO this only ticks when sdl is active, which is NOT when
      ;; the window is unmapped
224
      (let ((new-time (sdl-ticks)))
225 226 227 228 229 230 231
        (let ((return (- new-time last-time)))
          (set! last-time new-time)
          return)))))

(define draw
  (let ((fps-counter (make-fps-counter)))
    (lambda ( window rend)
232 233
      (parameterize ((current-renderer rend)
                     (current-tile-size (/ 480 (board-height game-map))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
234

235 236
        ;; minimap
        (let ((texture (make-texture rend 'rgba8888 'target
Hugo Hörnquist's avatar
Hugo Hörnquist committed
237
                                     (int (* (board-width game-map)  (current-tile-size)))
238
                                     (* (board-height game-map) (current-tile-size)))))
239
          (with-render-target texture draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
240

241
          ;; Camera
Hugo Hörnquist's avatar
Hugo Hörnquist committed
242
          (draw-first-person-perspective)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
243

244
          (render-copy rend texture
245
                       #:dstrect
Hugo Hörnquist's avatar
Hugo Hörnquist committed
246
                       (let ((map-size 100))
247 248
                         (list 0 (- 480 map-size)
                               map-size map-size)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
249

250
          (delete-texture! texture))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
251

252 253
        ;; Text overlay
        (parameterize ((color (make-color 0 0 0 #xFF)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
254

255
          ;; FPS counter
Hugo Hörnquist's avatar
Hugo Hörnquist committed
256
          (render-text (format #f "FPS: ~,2f" (/ 1000 (1+ (fps-counter)))) #:line 0)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
257

258 259 260 261
          (render-text
           (format #f "x = ~,4f   y = ~,4f   a = ~,4f"
                   (x (p player)) (y (p player)) (a player))
           #:line 1))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
262

263
        (present)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
264 265

(define (main-loop window)
Hugo Hörnquist's avatar
huh?  
Hugo Hörnquist committed
266
  (define rend (make-renderer window '(accelerated #; vsync texture)))
267
  (define fps-counter (make-fps-counter))
268

Hugo Hörnquist's avatar
Hugo Hörnquist committed
269 270
  (begin-thread
   (while
271
       #t (let ((rays-next (ray-trace player ray-count)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
272 273
            (atomic-box-set! rays rays-next)
            (wait))))
274

275
  ;; load textures
Hugo Hörnquist's avatar
Hugo Hörnquist committed
276 277 278 279 280
  (for-each
   (lambda (type)
     (hashq-set! texture-map type
                 (surface->texture rend (load-image (media (format #f "/textures/~a.png" type))))))
   '(wall window))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
281

282 283
  (let ((counter 0))
    (while #t
284
           (sigaction SIGINT (lambda (sig) (break)))
285 286 287

           (if (= counter 1)
               (begin (draw window rend) (set! counter 0))
288
               (let ((dt (fps-counter)))
289
                 (update dt)
290
                 (usleep (max 0 (floor/ (- 1000 dt) 360)))
291
                 (set! counter = (+ 1)))))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
292

293 294 295 296 297
(define current-font (make-parameter #f))

(define (main args)
  (sdl-init)
  (ttf-init)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
298

299
  (current-font (load-font (media "/font.otf") 20))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
300

301 302 303 304
  ;; (let ((sock-path "/tmp/guile-socket"))
  ;;   (use-modules (system repl server))
  ;;   (delete-file sock-path)
  ;;   (spawn-server (make-unix-domain-server-socket #:path sock-path)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
305

306 307 308 309 310
  (call-with-window
   (make-window
    #:title "Wolf 3D"
    #:size '(640 480))
   main-loop)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
311

312 313
  (ttf-quit)
  (sdl-quit))