main.scm 10.1 KB
Newer Older
Hugo Hörnquist's avatar
Hugo Hörnquist committed
1 2 3 4 5 6 7 8 9
(add-to-load-path "/usr/local/share/guile/site/2.2/")

(use-modules (srfi srfi-1)
             (srfi srfi-9)

             (ice-9 threads)
             (oop goops)

             (sdl2)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
10
             (sdl2 ttf)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
             (sdl2 video)
             (sdl2 rect)
             (sdl2 render)
             (sdl2 surface)
             (sdl2 events)
             )

(define-public *unspecified* ((@ (guile) if) #f #f))

(define-syntax set!
  (syntax-rules (=)
    ((_) *unspecified*)

    ((_ field = (op args ...) rest ...)
     (begin ((@ (guile) set!) field (op field args ...))
            (set! rest ...)))

    ((_ field proc)
     ((@ (guile) set!) field proc))

    ((_ field proc rest ...)
     (begin ((@ (guile) set!) field proc) (set! rest ...)))))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
34 35 36 37 38
(define (signum x)
  (cond ((zero? x) 0)
        ((positive? x) 1)
        (else -1)))

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

(define (parse-map spec)
  (let ((arr (make-array #f (length spec) (string-length (car spec)))))
    (array-index-map! arr
                      (lambda (i j)
                        (case (string-ref (list-ref spec i) j)
                          ((#\space) 'space)
                          ((#\#) 'wall))))
    arr))

(define game-map
  (parse-map
   '("########"
     "#      #"
     "#  ### #"
     "#  #   #"
     "########")))

(define-values (board-height board-width)
  (apply values (array-dimensions game-map)))

(define (cross-product l1 l2)
  (concatenate
   (map (lambda (a)
          (map (lambda (b) (list a b))
               l2))
        l1)))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
67 68
(define pi 3.141592653589793)
(define tau (* 2 pi))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
69

Hugo Hörnquist's avatar
Hugo Hörnquist committed
70 71 72 73
(define-class <v3> ()
  (x #:accessor x #:init-keyword #:x)
  (y #:accessor y #:init-keyword #:y)
  (z #:accessor z #:init-keyword #:z))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
74

Hugo Hörnquist's avatar
Hugo Hörnquist committed
75 76
(define* (v3 #:optional (x 0) (y 0) (z 0))
  (make <v3> #:x x #:y y #:z z))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
77

Hugo Hörnquist's avatar
Hugo Hörnquist committed
78 79 80 81 82 83 84 85 86
(define-method (+ (v <v3>) (u <v3>))
  (v3 (+ (x v) (x u))
      (+ (y v) (y u))
      (+ (z v) (z u))))

(define-method (* (a <number>) (v <v3>))
  (v3 (* a (x v))
      (* a (y v))
      (* a (z v))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
87

Hugo Hörnquist's avatar
Hugo Hörnquist committed
88 89 90 91 92
(define (vector-length v)
  (sqrt (+ (* (x v) (x v))
           (* (y v) (y v))
           (* (z v) (z v)))))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
93 94
(define-class <player> ()
  (pos #:accessor p #:init-form (v3 1 1))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
95 96
  (angle #:accessor a #:init-value 0)
  (feild-of-view #:accessor fov #:init-value (/ tau 6)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
97 98 99 100 101 102

(define (get-t)
  (define t (gettimeofday))
  (+ (cdr t) (* 1000000 (car t))))

(define current-tile-size (make-parameter 50))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
103

Hugo Hörnquist's avatar
Hugo Hörnquist committed
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
(define current-renderer (make-parameter #f))

(define int (compose inexact->exact floor))

(define (draw-line x1 y1 x2 y2)
  (render-draw-line
   (current-renderer)
   (int (* (current-tile-size) x1)) (int (* (current-tile-size) y1))
   (int (* (current-tile-size) x2)) (int (* (current-tile-size) y2))))

(define (fill-rect x y w h)
 (render-fill-rect
  (current-renderer)
  (make-rect (int (* (current-tile-size) x))
             (int (* (current-tile-size) y))
             (int (* (current-tile-size) w))
             (int (* (current-tile-size) h)))))

(define (draw-line x1 y1 x2 y2)
  (render-draw-line
   (current-renderer)
   (int (* x1 (current-tile-size))) (int (* y1 (current-tile-size)))
   (int (* x2 (current-tile-size))) (int (* y2 (current-tile-size)))))

(define (make-rect* x y w h)
  (make-rect (int (* x (current-tile-size)))
             (int (* y (current-tile-size)))
             (int (* w (current-tile-size)))
             (int (* h (current-tile-size)))))

(define (fill-rects rects)
  (render-fill-rects (current-renderer) rects))

(define* (set-draw-color r g b #:optional (a #xFF))
  (set-render-draw-color (current-renderer) r g b a))

(define (clear)
  (clear-renderer (current-renderer)))

(define (present)
  (present-renderer (current-renderer)))

(define player (make <player> #:x 1 #:y 1 #:a 0))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
147 148 149

(define ray-count 50)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
150 151
(define keys-down (make-parameter '()))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
152 153
(define current-font (make-parameter #f))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
(define (update dt)

    (let ((ev (poll-event)))
      (cond #; [(keyboard-event-repeat? ev) #f] ;; TODO implement in bindings
       [(keyboard-down-event? ev)
        (keys-down (cons (keyboard-event-scancode ev)
                         (keys-down)))]
       [(keyboard-up-event? ev)
        (keys-down (delv (keyboard-event-scancode ev)
                         (keys-down)))]))


    (fold (lambda (key ret)
            (case key
              ((x q) 'game-end)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
169 170 171 172 173 174 175 176 177 178 179 180 181
              ((w) (set! (p player) = (+ (* dt 0.000003
                                            (v3 (cos (a player))
                                                (sin (a player))))))
               ret)
              ((s) (set! (p player) = (+ (* dt 0.000003 -1
                                            (v3 (cos (a player))
                                                (sin (a player)))))) ret)
              ;; ((a) (set! (p player) = (+ (* dt 0.000003 -1
              ;;                               (v3 (sin (a player))
              ;;                                   (cos (a player)))))) ret)
              ;; ((d) (set! (p player) = (+ (* dt 0.000003
              ;;                               (v3 (sin (a player))
              ;;                                   (cos (a player)))))) ret)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
182 183
              ((j) (set! (a player) = (- (* dt 0.000003))) ret)
              ((p) (set! (a player) = (+ (* dt 0.000003))) ret)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
184 185 186 187 188 189
              (else ret)
              ))
          #f (keys-down))

  )

Hugo Hörnquist's avatar
Hugo Hörnquist committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
(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)))
                     (eq? 'wall (array-ref game-map iy ix)))
                 (v3 dx dy)
                 (loop (+ dx (* 0.1 (cos a))) (+ dy (* 0.1 (sin a)))))))))
     (iota ray-count (- (a player) (/ (fov player) 2))
           (/ (fov player) ray-count)))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
208

Hugo Hörnquist's avatar
Hugo Hörnquist committed
209
(define fps-time (get-t))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
210

Hugo Hörnquist's avatar
Hugo Hörnquist committed
211
(define (draw-map)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
212 213 214 215 216 217 218 219 220 221 222 223
  (set-draw-color #xFF #xFF #xFF)
  (clear)

  (set-draw-color 0 0 #xFF)

  (fill-rects
   (filter-map
    (lambda (pt)
      (if (eq? 'wall (array-ref game-map (y pt) (x pt)))
          (make-rect* (x pt) (y pt)
                      1 1)
          #f))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
224
    (map (lambda (p) (apply v3 p))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
         (cross-product (iota board-width) (iota board-height)))))

  (set-draw-color 0 0 0)

  (for-each (lambda (i) (draw-line
                    0 i
                    board-width i))
            (iota board-height))

  (for-each (lambda (i) (draw-line
                    i 0
                    i board-height))
            (iota board-width))


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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
241 242 243
  (set-draw-color #xFF 0 0)

  (fill-rect
Hugo Hörnquist's avatar
Hugo Hörnquist committed
244 245
   (- (x (p player)) 0.1)
   (- (y (p player)) 0.1)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
246 247
   0.2 0.2)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
248 249 250 251 252 253
  (let ((pp (p player)))
   (for-each (lambda (p)
               (let ((v (+ pp p)))
                 (draw-line (x pp) (y pp)
                            (x v)  (y v))))
             (ray-trace player ray-count)))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
254 255 256 257

  (set-draw-color 0 #xFF 0)

  (draw-line
Hugo Hörnquist's avatar
Hugo Hörnquist committed
258 259 260
   (x (p player)) (y (p player))
   (+ (cos (a player)) (x (p player)))
   (+ (sin (a player)) (y (p player))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
261

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

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

Hugo Hörnquist's avatar
Hugo Hörnquist committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
(define (draw-first-person-perspective)
  (set-draw-color #xEE #xEE #xEE)
  (clear)

  (set-draw-color #x33 #x33 #x33)
  (render-fill-rect (current-renderer)
                    (make-rect 0 240 640 240))

  (set-draw-color 0 #xEE #xEE)

  (for-each (lambda (i p)
              (let ((segment-height (- 480 (* 70 (vector-length p)))))
                (set-draw-color 0
                                (int (min #xFF (- #x100 (* #x100 (/ (vector-length p) 7)))))
                                (int (min #xFF (- #x100 (* #x100 (/ (vector-length p) 7))))))
                (render-fill-rect
                 (current-renderer)
                 (make-rect (int (* i (/ 640 ray-count)))
                            (int (/ (- 480 segment-height) 2))
                            (int (1+ (/ 640 ray-count)))
                            (int segment-height)))))
            (iota ray-count)
            (ray-trace player ray-count))

  )

Hugo Hörnquist's avatar
Hugo Hörnquist committed
292 293
(define texture #f)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
(define (draw window rend)
  (parameterize ((current-renderer rend))
    (set-draw-color #xFF #xFF #xFF)
    (clear)

    (let ((texture (make-texture rend 'rgba8888 'target
                                   (* 8 (current-tile-size))
                                   (* 5 (current-tile-size)))))
      (set-render-target! rend texture)

      (set-draw-color #xFF #xFF #xFF)

      (draw-map)
      #;
      (parameterize ((current-tile-size 16))
        )

      (set-render-target! rend #f)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
312 313 314 315

      (draw-first-person-perspective)

      ;; (render-copy rend texture #:dstrect (list 0 0 (* 16 (current-tile-size)) (* 10 (current-tile-size))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
316
      (render-copy rend texture #:dstrect (list 0 0 (* 2 (current-tile-size)) (* 2 (current-tile-size))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
317 318 319 320 321

      (delete-texture! texture)


      )
Hugo Hörnquist's avatar
Hugo Hörnquist committed
322 323 324 325 326 327 328 329 330 331 332

    (let ((nt (get-t)))
      (let ((surf (render-font-solid (current-font) (format #f "FPS: ~,2f" (/ 1000000 (- nt fps-time)))
                                     (make-color 0 0 0 #xFF))))
        (render-copy (current-renderer) (surface->texture (current-renderer) surf)
                     #:dstrect (list 1 1
                                     (surface-width surf)
                                     (surface-height surf))))

      (set! fps-time nt))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
333
    (present)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
334

Hugo Hörnquist's avatar
Hugo Hörnquist committed
335
    ))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
336 337

(define (main-loop window)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
338 339 340
  (define rend (make-renderer window '(accelerated #; vsync texture
                                                           )
                              ))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
341 342 343

  (define last-t (get-t))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
344
  (let loop ((counter 0))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
345
    ;; (usleep (int (/ 1000000 (* 60 60))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
346 347
    ;; (sigaction SIGINT (lambda (sig) (set! looping #f)))

Hugo Hörnquist's avatar
Hugo Hörnquist committed
348
    (if (= counter 10)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
349 350 351 352 353 354 355 356
        (begin (draw window rend) (loop 0))
        (begin (let ((dt (let ((t (get-t)))
                           (let ((dt (- t last-t)))
                             (set! last-t t)
                             dt))))
                 (if (update dt)
                     'return
                     (loop (1+ counter))))))))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
357 358

(sdl-init)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
359 360 361 362 363
(ttf-init)

(current-font (load-font "/usr/share/fonts/OTF/FiraMono-Regular.otf" 12))

(format #t "Loaded font ~a~%" (current-font))
Hugo Hörnquist's avatar
Hugo Hörnquist committed
364 365 366 367 368 369 370

(call-with-window
    (make-window
     #:title "Wolf 3D"
     #:size '(640 480))
  main-loop)

Hugo Hörnquist's avatar
Hugo Hörnquist committed
371
(ttf-quit)
Hugo Hörnquist's avatar
Hugo Hörnquist committed
372
(sdl-quit)