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)))
88 "Calculate interpolation factor in the range [0, 1] for the
89 leftover frame time LAG."
90 (clamp 0 1 (/ lag tick-interval)))
92 (define (frame-sleep time)
93 "Sleep for the remainder of the frame that started at TIME."
94 (let ((t (- (+ time frame-interval)
96 (usleep (max 0 (* t 1000)))))
98 (define (process-frame previous-time lag)
99 "Render and/or update the game as needed, integrating from the
100 PREVIOUS-TIME to the current time, and updating using a game tick
101 accumulator initialized to LAG. Returns a timestamp to be used as the
102 starting point of the next delta time calculation and the leftover
103 time in the game tick accumulator."
104 (let* ((current-time (sdl2:sdl-ticks))
105 (dt (- current-time previous-time))
106 (lag (update (+ lag dt))))
107 (draw dt (alpha lag))
108 (frame-sleep current-time)
109 (values current-time lag)))
111 (define (game-loop previous-time lag)
112 "Update game state, and render. PREVIOUS-TIME is the time in
113 milliseconds of the last iteration of the game loop."
114 (let-values (((time lag)
115 (process-frame previous-time lag)))
116 (game-loop time lag)))
121 ;; Catch SIGINT and kill the loop
125 (set! %root-scene scene)
126 (game-loop (sdl2:sdl-ticks) 0))
127 (lambda (cont callback)
128 (when (procedure? callback)
131 (define (stop-game-loop)
132 "Abort the game loop"
133 (abort-to-prompt 'game-loop-prompt #f))
135 (define %sdl-window #f)
136 (define %sdl-renderer #f)
137 (define %gl-context #f)
139 (define (init-window)
141 (set! %sdl-window (sdl2:make-window #:opengl? #t #:show? #t))
142 (set! %sdl-renderer (sdl2:make-renderer %sdl-window))
143 (sdl2:set-gl-attribute! 'context-major-version 3)
144 (sdl2:set-gl-attribute! 'context-minor-version 2)
145 (sdl2:set-gl-attribute! 'double-buffer 1)
146 (sdl2:set-gl-attribute! 'depth-size 24)
147 (set! %gl-context (sdl2:make-gl-context %sdl-window))
148 (sdl2:set-gl-swap-interval! 'vsync))
150 (define (open-window title resolution fullscreen?)
151 (sdl2:set-window-title! %sdl-window title)
152 (sdl2:set-window-size! %sdl-window resolution)
153 (sdl2:set-window-fullscreen! %sdl-window fullscreen?)
154 (sdl2:show-window! %sdl-window))
156 (define (close-window)
157 (sdl2:hide-window! %sdl-window)
160 (define* (start-game scene #:key
162 (resolution '(640 480))
164 (when-quit (lambda () (stop-game))))
166 (open-window title resolution fullscreen?)
167 (run-game-loop scene #:when-quit when-quit)