main.scm 9.96 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 96 97
(define (find-next-wall x y a callback)
  (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)
          (callback (+ x dx) (+ y dy))
          (let* ((dy (* 1.01 dy-ampl (sgn (sin a))))
                 (dx (* dy (cot a))))
            (callback (+ x dx) (+ y dy)))))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
98

Hugo Hörnquist's avatar
Hugo Hörnquist committed
99
(define (ray-trace player ray-count)
100
  (let ((p (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
101 102
    (let ((px (x p))
          (py (y p)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
103 104 105 106 107 108 109
      (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
Hugo Hörnquist's avatar
Hugo Hörnquist committed
110 111 112
                      (list (make-ray #:a a #:type #f
                                      #:v (v3 (- x px) (- y py))
                                      #:hitf (v3 x y))) ]
Hugo Hörnquist's avatar
Hugo Hörnquist committed
113 114 115

                     ;; hit wall
                     [(array-ref (board-data game-map) (int y) (int x))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
116 117 118 119 120 121 122 123 124
                      (lambda (tile) (memv tile '(wall)))
                      => (lambda (tile)
                           (list (make-ray #:a a #:type tile
                                           #:v (v3 (- x px) (- y py))
                                           #:hitf (v3 x y))))]

                     ;; hit window
                     [(array-ref (board-data game-map) (int y) (int x))
                      (lambda (tile) (memv tile '(window)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
125
                      => (lambda (tile)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
126 127 128 129
                           (cons (make-ray #:a a #:type tile
                                           #:v (v3 (- x px) (- y py))
                                           #:hitf (v3 x y))
                                 (find-next-wall x y a loop)))]
Hugo Hörnquist's avatar
Hugo Hörnquist committed
130 131

                     ;; follow ray
Hugo Hörnquist's avatar
Hugo Hörnquist committed
132
                     [else (find-next-wall x y a loop)])))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
133 134 135

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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
138
(define (draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
139
  (set-draw-color #xFF #xFF #xFF)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
140 141
  (clear)

142
  ;; draw level
Hugo Hörnquist's avatar
Hugo Hörnquist committed
143 144
  (for-each
   (lambda (pt)
145
     (apply set-draw-color
146
      (case (array-ref (board-data game-map) (y pt) (x pt))
147 148 149 150 151
        ((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
152
   (map (lambda (p) (apply v3 p))
153
        (cross-product (iota (board-width game-map)) (iota (board-height game-map)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
154

155 156
  ;; draw raycast
  (set-draw-color #xFF 0 0)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
157

Hugo Hörnquist's avatar
Hugo Hörnquist committed
158
  (let ((pp (p player)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
159 160 161 162 163 164 165 166 167
    (for-each
     (lambda (rays)
       (for-each
        (lambda (ray)
          (let ((v (+ pp (vec ray))))
            (draw-line (x pp) (y pp)
                       (x v)  (y v))))
        rays))
     (atomic-box-ref rays)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
168
  )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
169

Hugo Hörnquist's avatar
Hugo Hörnquist committed
170
(define (draw-first-person-perspective)
171
  (set-draw-color #xBB #xBB #xFF)       ; light blue
Hugo Hörnquist's avatar
Hugo Hörnquist committed
172 173
  (clear)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
174
  ;; floor
Hugo Hörnquist's avatar
Hugo Hörnquist committed
175 176 177 178
  (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
179
  (let ((rays (atomic-box-ref rays)))
180 181
    (unless (null? rays)
      (for-each
Hugo Hörnquist's avatar
Hugo Hörnquist committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
       (lambda (i rays)
         (for-each
          (lambda (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)))))))
          rays))
206 207 208
       (iota ray-count)
       rays
       ))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
209 210


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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
212 213 214

(define color (make-parameter (make-color 0 0 0 #xFF)))
(define* (render-text str #:key (line 0))
215
  (let ((surf (render-font-blended (current-font) str (color))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
216 217
    (render-copy (current-renderer)
                 (surface->texture (current-renderer) surf)
218
                 #:dstrect (list 5 (+ 1 (* line (+ 1 (font-height (current-font)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
219
                                 (surface-width surf)
220 221
                                 (surface-height surf)))
    (delete-surface! surf)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
222

223 224


Hugo Hörnquist's avatar
Hugo Hörnquist committed
225 226 227 228 229
(define (with-render-target target thunk)
  (dynamic-wind (lambda () (set-render-target! (current-renderer) target))
                thunk
                (lambda () (set-render-target! (current-renderer) #f))))

230
(define (make-fps-counter)
231
  (let ((last-time (sdl-ticks)))
232
    (lambda ()
233 234
      ;; TODO this only ticks when sdl is active, which is NOT when
      ;; the window is unmapped
235
      (let ((new-time (sdl-ticks)))
236 237 238 239 240 241 242
        (let ((return (- new-time last-time)))
          (set! last-time new-time)
          return)))))

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

246 247
        ;; minimap
        (let ((texture (make-texture rend 'rgba8888 'target
Hugo Hörnquist's avatar
Hugo Hörnquist committed
248
                                     (int (* (board-width game-map)  (current-tile-size)))
249
                                     (* (board-height game-map) (current-tile-size)))))
250
          (with-render-target texture draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
251

252
          ;; Camera
Hugo Hörnquist's avatar
Hugo Hörnquist committed
253
          (draw-first-person-perspective)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
254

255
          (render-copy rend texture
256
                       #:dstrect
Hugo Hörnquist's avatar
Hugo Hörnquist committed
257
                       (let ((map-size 100))
258 259
                         (list 0 (- 480 map-size)
                               map-size map-size)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
260

261
          (delete-texture! texture))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
262

263 264
        ;; Text overlay
        (parameterize ((color (make-color 0 0 0 #xFF)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
265

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

269 270 271 272
          (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
273

274
        (present)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
275 276

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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
280 281
  (begin-thread
   (while
282
       #t (let ((rays-next (ray-trace player ray-count)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
283 284
            (atomic-box-set! rays rays-next)
            (wait))))
285

286
  ;; load textures
Hugo Hörnquist's avatar
Hugo Hörnquist committed
287 288 289 290 291
  (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
292

293 294
  (let ((counter 0))
    (while #t
295
           (sigaction SIGINT (lambda (sig) (break)))
296 297 298

           (if (= counter 1)
               (begin (draw window rend) (set! counter 0))
299
               (let ((dt (fps-counter)))
300
                 (update dt)
301
                 (usleep (max 0 (floor/ (- 1000 dt) 360)))
302
                 (set! counter = (+ 1)))))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
303

304 305 306 307 308
(define current-font (make-parameter #f))

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

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

312 313 314 315
  ;; (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
316

317 318 319 320 321
  (call-with-window
   (make-window
    #:title "Wolf 3D"
    #:size '(640 480))
   main-loop)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
322

323 324
  (ttf-quit)
  (sdl-quit))