main.scm 13.3 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

42
(define player (make-player 5.5 13.5 0))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
43

Hugo Hörnquist's avatar
huh?  
Hugo Hörnquist committed
44
(define ray-count 64)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
45

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

48 49
(define update
  (let ((keys-down '()))
50
    (lambda (dt)                             ; dt in ms
51

52 53
      (let ((ev (poll-event)))
        (cond #; [(keyboard-event-repeat? ev) #f] ;; TODO implement in bindings
54 55 56 57

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

58
         [(keyboard-up-event? ev)
59
          (set! keys-down (delv (keyboard-event-scancode ev) keys-down))]
60

61
         [(quit-event? ev) (raise SIGINT)]))
62

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

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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
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
;; (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)

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
(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
148
(define (ray-trace player ray-count)
149
  (let ((p (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
150 151 152
    (let ((px (x p))
          (py (y p)))
     (map (lambda (a)
153 154
            (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
155

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

                    [(array-ref game-map (int y) (int x))
                     (lambda (s) (memv s '(wall window)))
                     => (lambda (tile)
168 169 170

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

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

                    ;; ================= 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
236
                    [else
237 238 239 240 241
                     (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
242 243 244
          (iota ray-count (- (a player) (/ (fov player) 2))
                (/ (fov player) ray-count)))))
  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
245

Hugo Hörnquist's avatar
Hugo Hörnquist committed
246
(define (draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
247
  (set-draw-color #xFF #xFF #xFF)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
248 249
  (clear)

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

263 264
  ;; draw raycast
  (set-draw-color #xFF 0 0)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
265

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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
274
(define (draw-first-person-perspective)
275
  (set-draw-color #xBB #xBB #xFF)       ; light blue
Hugo Hörnquist's avatar
Hugo Hörnquist committed
276 277
  (clear)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
278
  ;; floor
Hugo Hörnquist's avatar
Hugo Hörnquist committed
279 280 281 282
  (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
283
  (let ((rays (atomic-box-ref rays)))
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
    (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
310 311


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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
313 314 315

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

323 324


Hugo Hörnquist's avatar
Hugo Hörnquist committed
325 326 327 328 329
(define (with-render-target target thunk)
  (dynamic-wind (lambda () (set-render-target! (current-renderer) target))
                thunk
                (lambda () (set-render-target! (current-renderer) #f))))

330
(define (make-fps-counter)
331
  (let ((last-time (sdl-ticks)))
332
    (lambda ()
333
      (let ((new-time (sdl-ticks)))
334 335 336 337 338 339 340 341
        (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
342

343 344 345 346 347
        ;; minimap
        (let ((texture (make-texture rend 'rgba8888 'target
                                     (* board-width  (current-tile-size))
                                     (* board-height (current-tile-size)))))
          (with-render-target texture draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
348

349 350
          ;; Camera
          (draw-first-person-perspective)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
351

352
          (render-copy rend texture
353 354 355 356
                       #:dstrect
                       (let ((map-size 200))
                         (list 0 (- 480 map-size)
                               map-size map-size)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
357

358
          (delete-texture! texture))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
359

360 361
        ;; Text overlay
        (parameterize ((color (make-color 0 0 0 #xFF)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
362

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

366 367 368 369
          (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
370

371
        (present)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
372 373

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

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

383
  ;; load textures
Hugo Hörnquist's avatar
Hugo Hörnquist committed
384 385 386 387 388
  (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
389

390 391
  (let ((counter 0))
    (while #t
392
           (sigaction SIGINT (lambda (sig) (break)))
393 394 395

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

401 402 403 404 405
(define current-font (make-parameter #f))

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

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

409 410 411 412
  ;; (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
413

414 415 416 417 418
  (call-with-window
   (make-window
    #:title "Wolf 3D"
    #:size '(640 480))
   main-loop)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
419

420 421
  (ttf-quit)
  (sdl-quit))