summaryrefslogtreecommitdiff
path: root/2d/game-loop.scm
blob: 9e743c7f1e352b40a2e1e8b8056f102650f5247a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
;;; guile-2d
;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu>
;;;
;;; Guile-2d is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-2d is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Game loop.
;;
;;; Code:

(define-module (2d game-loop)
  #:use-module ((sdl sdl) #:prefix SDL:)
  #:use-module (figl gl)
  #:use-module (2d agenda)
  #:export (set-render-callback
            set-update-callback
            set-key-up-callback
            set-key-down-callback
            set-mouse-motion-callback
            set-mouse-button-down-callback
            set-mouse-button-up-callback
            run-game-loop))

(define target-fps 60)
(define frame-interval (/ 1000 target-fps))

;;;
;;; Callbacks
;;;

(define render-callback (lambda () #t))
(define update-callback (lambda () #t))
(define key-up-callback (lambda (key mod unicode) #t))
(define key-down-callback (lambda (key mod unicode) #t))
(define mouse-motion-callback (lambda (buttons x y xrel yrel) #t))
(define mouse-button-down-callback (lambda (button x y) #t))
(define mouse-button-up-callback (lambda (button x y) #t))

(define (set-render-callback callback)
  "Sets the render callback procedure."
  (set! render-callback callback))

(define (set-update-callback callback)
  "Sets the update callback procedure."
  (set! update-callback callback))

(define (set-key-up-callback callback)
  "Sets the key up callback procedure."
  (set! key-up-callback callback))

(define (set-key-down-callback callback)
  "Sets the key down callback procedure."
  (set! key-down-callback callback))

(define (set-mouse-motion-callback callback)
  "Sets the mouse motion callback procedure."
  (set! mouse-motion-callback callback))

(define (set-mouse-button-down-callback callback)
  "Sets the mouse button down callback procedure."
  (set! mouse-button-down-callback callback))

(define (set-mouse-button-up-callback callback)
  "Sets the mouse button up callback procedure."
  (set! mouse-button-up-callback callback))

;;;
;;; Event Handling
;;;

(define handle-events
  (let ((e (SDL:make-event)))
    (lambda ()
      "Handles all events in the SDL event queue."
      (while (SDL:poll-event e)
        (handle-event e)))))

(define (handle-event e)
  "Calls the relevant callback for the event."
  (case (SDL:event:type e)
    ((SDL_KEYDOWN)
     (key-down-callback (event-keycode e)
                        (event-keymods e)
                        (SDL:event:key:keysym:unicode e)))
    ((SDL_KEYUP)
     (key-up-callback (event-keycode e)
                      (event-keymods e)
                      (SDL:event:key:keysym:unicode e)))
    ((SDL_MOUSEMOTION)
     (mouse-motion-callback (SDL:event:motion:state e)
                            (SDL:event:motion:x e)
                            (SDL:event:motion:y e)
                            (SDL:event:motion:xrel e)
                            (SDL:event:motion:yrel e)))
    ((SDL_MOUSEBUTTONDOWN)
     (mouse-button-down-callback (SDL:event:button:button e)
                                 (SDL:event:button:x e)
                                 (SDL:event:button:y e)))
    ((SDL_MOUSEBUTTONUP)
     (mouse-button-up-callback (SDL:event:button:button e)
                               (SDL:event:button:x e)
                               (SDL:event:button:y e)))))

(define (event-keycode e)
  "Returns an integer keycode from an SDL event."
  (SDL:enum->number SDL:event-keys (SDL:event:key:keysym:sym e)))

(define (event-keymods e)
  "Returns an integer bitmask of keymods from an SDL event"
  (SDL:flags->number (SDL:flagstash:event-mod) (SDL:event:key:keysym:mod e)))

;;;
;;; Update and Render
;;;

(define (render)
  "Renders a frame."
  (set-gl-matrix-mode (matrix-mode modelview))
  (gl-load-identity)
  (gl-clear (clear-buffer-mask color-buffer depth-buffer))
  (render-callback)
  (SDL:gl-swap-buffers))

(define accumulate-fps
  (let ((last-time (delay (SDL:get-ticks)))
        (fps 0))
    (lambda ()
      "Calculates frames per second."
      (let ((time (SDL:get-ticks)))
        (set! fps (1+ fps))
        (when (>= time (+ (force last-time) 1000))
          (pk 'FPS fps)
          (set! last-time (delay time))
          (set! fps 0))))))

(define (update accumulator)
  "Call the update callback. The update callback will be called as
many times as frame-interval can divide accumulator. The return value
is the unused accumulator time."
  (if (>= accumulator frame-interval)
      (begin
        (update-callback)
        (update-agenda)
        (update (- accumulator frame-interval)))
      accumulator))

(define update-and-render
  (let ((remainder 0)
        (last-time (delay (SDL:get-ticks))))
    (lambda ()
      "Calls update and draw callback when enough time has passed
since the last tick."
      (let* ((time (SDL:get-ticks))
             (elapsed (- time (force last-time)))
             (accumulator (+ remainder elapsed)))
        (when (>= accumulator frame-interval)
          (set! last-time (delay time))
          (set! remainder (update accumulator))
          (accumulate-fps)
          (render))))))

;;;
;;; Game Loop
;;;

(define (run-game-loop)
  "Runs event handling, update, and render loop."
  (handle-events)
  (update-and-render)
  (run-game-loop))