main.scm 13.6 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
huh?  
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

46 47 48 49 50 51
(define game-map (call-with-input-file "simple.map" (compose parse-map read-map)))

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

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 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
;; (define (ray-trace player ray-count)
;;   (let ((x (x (p player)))
;;         (y (y (p player))))
;;     (map
;;      (lambda (a)
;;        (let loop ((dx 0)
;;                   (dy 0))
;;          (let ((nx (+ x dx))
;;                (ny (+ y dy)))
;;            (let ((ix (int nx))
;;                  (iy (int ny)))
;;              (if (or (not (and (<= 0 nx board-width)
;;                                (<= 0 ny board-height)))
;;                      (memv (array-ref game-map iy ix)
;;                            '(wall window)))
;;                  (make-ray #:a a
;;                            #:v (v3 dx dy)
;;                            #:type (if (not (and (<= 0 nx board-width)
;;                                                 (<= 0 ny board-height)))
;;                                       #f (array-ref game-map iy ix))
;;                            #:hitf (v3 nx ny)
;;                            #:hit (v3 ix iy))
;;                  (loop (+ dx (* 0.1 (cos a))) (+ dy (* 0.1 (sin a)))))))))
;;      (iota ray-count (- (a player) (/ (fov player) 2))
;;            (/ (fov player) ray-count)))))


(define (pow x y)
  (let ((xd (abs (decimals x)))
        (yd (abs (decimals y))))
    (min (max xd yd)
         (max (- 1 xd) (- 1 yd)))))

(define (decimals x)
  (- x (truncate x)))

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

(define sgn signum)

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
(define (x-from-edge x a)
  "distance-from-y-wall"
  (mod (* (sgn (sin a))
          (decimals x))
       1))

(define (y-from-edge y a)
  "distance-from-x-wall"
  (mod (* (sgn (cos a))
          (decimals y))
       1))

(define distance-from-x-wall y-from-edge)
(define distance-from-y-wall x-from-edge)

(define (hit-x? x y a)
  (and (< (x-from-edge x a) 0.01)
       (< (x-from-edge x a) (y-from-edge y a))))

(define (hit-y? x y a)
  (and (< (y-from-edge y a) 0.01)
       (< (y-from-edge y a) (x-from-edge x a))))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
152
(define (ray-trace player ray-count)
153
  (let ((p (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
154 155 156
    (let ((px (x p))
          (py (y p)))
     (map (lambda (a)
157 158
            (let loop ((x px) (y py))
              (format #t "a = ~,3f  x = ~,6f  y = ~,6f~%" a x y)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
159

160 161
              (cond [(or (not (and (<= 0 x) (< x (board-width game-map))
                                   (<= 0 y) (< y (board-height game-map))))
162 163
                         (< 15 (sqrt (+ (expt (- x px) 2)
                                        (expt (- y py) 2)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
164
                     ;; outside board
165
                     (make-ray #:a a #:type #f
Hugo Hörnquist's avatar
Hugo Hörnquist committed
166 167 168
                               #:v (v3 (- x px) (- y py))
                               #:hitf (v3 x y)) ]

169
                    [(array-ref (board-data game-map) (int y) (int x))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
170 171
                     (lambda (s) (memv s '(wall window)))
                     => (lambda (tile)
172 173 174

                          (format #t "hit ~a~%" tile)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
175
                          ;; hit wall
176
                          (make-ray #:a a #:type tile
Hugo Hörnquist's avatar
Hugo Hörnquist committed
177 178
                                    #:v (v3 (- x px) (- y py))
                                    #:hitf (v3 x y)))]
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

                    ;; ================= follow ray ===============================
                    [(hit-x? x y a)
                     (let ((possible-dy (* (sgn (sin a))
                                           (tan a))))
                       (if (> 1 (abs (+ (* (sgn (sin a))
                                           (distance-from-x-wall y a))
                                        possible-dy)))
                           ;; enter x, leave x
                           (loop (+ x (sgn (cos a)))
                                 (+ y possible-dy))

                           ;; else, enter x, leave y
                           (let ((dy (* (sgn (sin a))
                                        (distance-from-x-wall y a))))
                             (format #t "xxx: a = ~,2f  x = ~,2f y  = ~,2f  dy = ~,2f~%" a x y dy)
                             (loop (+ x (* dy (cot a)))
                                   (+ y dy)))))]

                    ;; hit wall from top or bottom
                    [(hit-y? x y a)

                     
                     (loop (+ x (* 0.01 (cos a)))
                           (+ y (* 0.01 (sin a))))

                     #;
                     (let ((b (- a (/ tau 4))))
                       (let ((possible-dx (* (sgn (sin b))
                                             (tan b))))
                         (if (> 1 (abs (+ (* (sgn (cos a))
                                             (mod (* (sgn (cos b))
                                                     (pow x y))
                                                  1))
                                          possible-dx)))
                             (begin
                               (format #t "yy: dx = ~a~%" possible-dx )
                               (loop (+ x possible-dx)
                                     (+ y (sgn (sin b)))))
                             (let ((dx (* (sgn (sin b))
                                          (mod (* (sgn (sin b))
                                                  (pow x y))
                                               1))))
                               (format #t "yyy: a = ~,2f  x = ~,2f  y = ~,2f  dx = ~,2f~%" a x y dx)
                               (loop (+ x dx)
                                     (+ y (* dx (tan b))))))))]

                    ;; Middle of square

                    ;; moving horizontally
                    [(> (abs (cos a))
                        (abs (sin a)))
                     (let  ((dx (* 1.01 (sgn (cos a))
                                   (mod (* x -1 (sgn (cos a)))
                                        1))))
                       (loop (+ x dx)
                             (+ y (* dx (tan a) (sgn (cos a))))))

                     ]

                    ;; moving vertically
Hugo Hörnquist's avatar
Hugo Hörnquist committed
240
                    [else
241 242 243 244 245
                     (let ((dy (* 1.01 (sgn (sin a))
                                  (mod (* y -1 (sgn (sin a)))
                                       1))))
                       (loop (+ x (* dy (cot a)))
                             (+ y dy)))])))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
246 247 248
          (iota ray-count (- (a player) (/ (fov player) 2))
                (/ (fov player) ray-count)))))
  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
249

Hugo Hörnquist's avatar
Hugo Hörnquist committed
250
(define (draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
251
  (set-draw-color #xFF #xFF #xFF)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
252 253
  (clear)

254
  ;; draw level
Hugo Hörnquist's avatar
Hugo Hörnquist committed
255 256
  (for-each
   (lambda (pt)
257
     (apply set-draw-color
258
      (case (array-ref (board-data game-map) (y pt) (x pt))
259 260 261 262 263
        ((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
264
   (map (lambda (p) (apply v3 p))
265
        (cross-product (iota (board-width game-map)) (iota (board-height game-map)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
266

267 268
  ;; draw raycast
  (set-draw-color #xFF 0 0)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
269

Hugo Hörnquist's avatar
Hugo Hörnquist committed
270
  (let ((pp (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
271 272
   (for-each (lambda (ray)
               (let ((v (+ pp (vec ray))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
273 274
                 (draw-line (x pp) (y pp)
                            (x v)  (y v))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
275
             (atomic-box-ref rays)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
276
  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
277

Hugo Hörnquist's avatar
Hugo Hörnquist committed
278
(define (draw-first-person-perspective)
279
  (set-draw-color #xBB #xBB #xFF)       ; light blue
Hugo Hörnquist's avatar
Hugo Hörnquist committed
280 281
  (clear)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
282
  ;; floor
Hugo Hörnquist's avatar
Hugo Hörnquist committed
283 284 285 286
  (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
287
  (let ((rays (atomic-box-ref rays)))
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
    (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
314 315


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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
317 318 319

(define color (make-parameter (make-color 0 0 0 #xFF)))
(define* (render-text str #:key (line 0))
320
  (let ((surf (render-font-blended (current-font) str (color))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
321 322
    (render-copy (current-renderer)
                 (surface->texture (current-renderer) surf)
323
                 #:dstrect (list 5 (+ 1 (* line (+ 1 (font-height (current-font)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
324 325 326
                                 (surface-width surf)
                                 (surface-height surf)))))

327 328


Hugo Hörnquist's avatar
Hugo Hörnquist committed
329 330 331 332 333
(define (with-render-target target thunk)
  (dynamic-wind (lambda () (set-render-target! (current-renderer) target))
                thunk
                (lambda () (set-render-target! (current-renderer) #f))))

334
(define (make-fps-counter)
335
  (let ((last-time (sdl-ticks)))
336
    (lambda ()
337
      (let ((new-time (sdl-ticks)))
338 339 340 341 342 343 344 345
        (let ((return (- new-time last-time)))
          (set! last-time new-time)
          return)))))

(define draw
  (let ((fps-counter (make-fps-counter)))
    (lambda ( window rend)
      (parameterize ((current-renderer rend))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
346

347 348
        ;; minimap
        (let ((texture (make-texture rend 'rgba8888 'target
349 350
                                     (* (board-width game-map)  (current-tile-size))
                                     (* (board-height game-map) (current-tile-size)))))
351
          (with-render-target texture draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
352

353 354
          ;; Camera
          (draw-first-person-perspective)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
355

356
          (render-copy rend texture
357 358 359 360
                       #:dstrect
                       (let ((map-size 200))
                         (list 0 (- 480 map-size)
                               map-size map-size)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
361

362
          (delete-texture! texture))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
363

364 365
        ;; Text overlay
        (parameterize ((color (make-color 0 0 0 #xFF)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
366

367
          ;; FPS counter
368
          (render-text (format #f "FPS: ~,2f" (/ 1000 (fps-counter))) #:line 0)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
369

370 371 372 373
          (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
374

375
        (present)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
376 377

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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
381 382 383 384 385
  (begin-thread
   (while
       #t (let ((rays-next (ray-trace player (1+ ray-count))))
            (atomic-box-set! rays rays-next)
            (wait))))
386

387
  ;; load textures
Hugo Hörnquist's avatar
Hugo Hörnquist committed
388 389 390 391 392
  (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
393

394 395
  (let ((counter 0))
    (while #t
396
           (sigaction SIGINT (lambda (sig) (break)))
397 398 399

           (if (= counter 1)
               (begin (draw window rend) (set! counter 0))
400
               (let ((dt (fps-counter)))
401
                 (update dt)
402
                 (usleep (max 0 (floor/ (- 1000 dt) 360)))
403
                 (set! counter = (+ 1)))))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
404

405 406 407 408 409
(define current-font (make-parameter #f))

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

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

413 414 415 416
  ;; (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
417

418 419 420 421 422
  (call-with-window
   (make-window
    #:title "Wolf 3D"
    #:size '(640 480))
   main-loop)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
423

424 425
  (ttf-quit)
  (sdl-quit))