1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2016 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (gacela game)
19 #:use-module (gacela math)
20 #:use-module (gacela event)
21 #:use-module ((sdl2) #:prefix sdl2:)
22 #:use-module ((sdl2 render) #:prefix sdl2:)
23 #:use-module ((sdl2 surface) #:prefix sdl2:)
24 #:use-module ((sdl2 video) #:prefix sdl2:)
26 #:use-module (srfi srfi-11)
32 ;;; Based on Sly code. Thank you so much!!
34 (define %root-scene #f)
36 (define (interval rate)
37 (floor (/ 1000 rate)))
39 (define* (run-game-loop scene #:key
42 (max-ticks-per-frame 4)
44 "Run the game loop. SCENE is a signal which contains the current
45 scene renderer procedure. FRAME-RATE specifies the optimal number of
46 frames to draw SCENE per second. TICK-RATE specifies the optimal game
47 logic updates per second. Both FRAME-RATE and TICK-RATE are 60 by
48 default. MAX-TICKS-PER-FRAME is the maximum number of times the game
49 loop will update game state in a single frame. When this upper bound
50 is reached due to poor performance, the game will start to slow down
51 instead of becoming completely unresponsive and possibly crashing."
53 (let ((tick-interval (interval tick-rate))
54 (frame-interval (interval frame-rate)))
56 (define (draw dt alpha)
58 (let ((size (sdl2:window-size %sdl-window)))
59 (gl-viewport 0 0 (car size) (cadr size)))
60 (gl-clear (clear-buffer-mask color-buffer depth-buffer))
63 ;;(run-hook draw-hook dt alpha)
65 ;(set-graphics-alpha! gfx alpha)
66 ;((signal-ref scene) gfx)
68 (sdl2:swap-gl-window %sdl-window))
71 "Call the update callback. The update callback will be called as
72 many times as tick-interval can divide LAG. The return value is the
73 unused accumulator time."
74 (define (iter lag ticks)
75 (cond ((>= ticks max-ticks-per-frame)
77 ((>= lag tick-interval)
79 (if (and (quit-event?) (procedure? when-quit))
82 (iter (- lag tick-interval) (1+ ticks)))
89 "Calculate interpolation factor in the range [0, 1] for the
90 leftover frame time LAG."
91 (clamp 0 1 (/ lag tick-interval)))
93 (define (frame-sleep time)
94 "Sleep for the remainder of the frame that started at TIME."
95 (let ((t (- (+ time frame-interval)
97 (usleep (max 0 (* t 1000)))))
99 (define (process-frame previous-time lag)
100 "Render and/or update the game as needed, integrating from the
101 PREVIOUS-TIME to the current time, and updating using a game tick
102 accumulator initialized to LAG. Returns a timestamp to be used as the
103 starting point of the next delta time calculation and the leftover
104 time in the game tick accumulator."
105 (let* ((current-time (sdl2:sdl-ticks))
106 (dt (- current-time previous-time))
107 (lag (update (+ lag dt))))
108 (draw dt (alpha lag))
109 (frame-sleep current-time)
110 (values current-time lag)))
112 (define (game-loop previous-time lag)
113 "Update game state, and render. PREVIOUS-TIME is the time in
114 milliseconds of the last iteration of the game loop."
115 (let-values (((time lag)
116 (process-frame previous-time lag)))
117 (game-loop time lag)))
122 ;; Catch SIGINT and kill the loop
126 (set! %root-scene scene)
127 (game-loop (sdl2:sdl-ticks) 0))
128 (lambda (cont callback)
129 (when (procedure? callback)
132 (define (stop-game-loop)
133 "Abort the game loop"
134 (abort-to-prompt 'game-loop-prompt #f))
136 (define %sdl-window #f)
137 (define %sdl-renderer #f)
138 (define %gl-context #f)
140 (define (init-window)
142 (set! %sdl-window (sdl2:make-window #:opengl? #t #:show? #t))
143 (set! %sdl-renderer (sdl2:make-renderer %sdl-window))
144 (sdl2:set-gl-attribute! 'context-major-version 3)
145 (sdl2:set-gl-attribute! 'context-minor-version 2)
146 (sdl2:set-gl-attribute! 'double-buffer 1)
147 (sdl2:set-gl-attribute! 'depth-size 24)
148 (set! %gl-context (sdl2:make-gl-context %sdl-window))
149 (sdl2:set-gl-swap-interval! 'vsync))
151 (define (open-window title resolution fullscreen?)
152 (sdl2:set-window-title! %sdl-window title)
153 (sdl2:set-window-size! %sdl-window resolution)
154 (sdl2:set-window-fullscreen! %sdl-window fullscreen?)
155 (sdl2:show-window! %sdl-window))
157 (define (close-window)
158 (sdl2:hide-window! %sdl-window)
161 (define* (start-game scene #:key
163 (resolution '(640 480))
165 (when-quit (lambda () (stop-game))))
167 (open-window title resolution fullscreen?)
168 (run-game-loop scene #:when-quit when-quit)