]> git.jsancho.org Git - gacela.git/blob - gacela/game.scm
dd7886cc150e5f230bf3ed5c3c97318c0aa8f4da
[gacela.git] / gacela / game.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2016 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
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:)
25   #:use-module (gl)
26   #:use-module (srfi srfi-11)
27   #:export (play-game
28             %sdl-renderer))
29
30
31 ;;; Based on Sly code. Thank you so much!!
32
33 (define %root-scene #f)
34
35 (define (interval rate)
36   (floor (/ 1000 rate)))
37
38 (define* (run-game-loop scene #:key
39                         (frame-rate 60)
40                         (tick-rate 60)
41                         (max-ticks-per-frame 4))
42   "Run the game loop.  SCENE is a signal which contains the current
43 scene renderer procedure.  FRAME-RATE specifies the optimal number of
44 frames to draw SCENE per second.  TICK-RATE specifies the optimal game
45 logic updates per second.  Both FRAME-RATE and TICK-RATE are 60 by
46 default.  MAX-TICKS-PER-FRAME is the maximum number of times the game
47 loop will update game state in a single frame.  When this upper bound
48 is reached due to poor performance, the game will start to slow down
49 instead of becoming completely unresponsive and possibly crashing."
50
51   (let ((tick-interval  (interval tick-rate))
52         (frame-interval (interval frame-rate)))
53
54     (define (draw dt alpha)
55       "Render a frame."
56       (let ((size (sdl2:window-size %sdl-window)))
57         (gl-viewport 0 0 (car size) (cadr size)))
58       (gl-clear (clear-buffer-mask color-buffer depth-buffer))
59       (if %root-scene
60           (%root-scene))
61       ;;(run-hook draw-hook dt alpha)
62       ;(with-graphics gfx
63         ;(set-graphics-alpha! gfx alpha)
64         ;((signal-ref scene) gfx)
65 ;       )
66       (sdl2:swap-gl-window %sdl-window))
67
68     (define (update lag)
69       "Call the update callback. The update callback will be called as
70 many times as tick-interval can divide LAG. The return value is the
71 unused accumulator time."
72       (define (iter lag ticks)
73         (cond ((>= ticks max-ticks-per-frame)
74                lag)
75               ((>= lag tick-interval)
76                (process-events)
77                                         ;(agenda-tick!)
78                (iter (- lag tick-interval) (1+ ticks)))
79               (else
80                lag)))
81       (iter lag 0))
82
83     (define (alpha lag)
84       "Calculate interpolation factor in the range [0, 1] for the
85 leftover frame time LAG."
86       (clamp 0 1 (/ lag tick-interval)))
87
88     (define (frame-sleep time)
89       "Sleep for the remainder of the frame that started at TIME."
90       (let ((t (- (+ time frame-interval)
91                   (sdl2:sdl-ticks))))
92         (usleep (max 0 (* t 1000)))))
93
94     (define (process-frame previous-time lag)
95       "Render and/or update the game as needed, integrating from the
96 PREVIOUS-TIME to the current time, and updating using a game tick
97 accumulator initialized to LAG.  Returns a timestamp to be used as the
98 starting point of the next delta time calculation and the leftover
99 time in the game tick accumulator."
100       (let* ((current-time (sdl2:sdl-ticks))
101              (dt (- current-time previous-time))
102              (lag (update (+ lag dt))))
103         (draw dt (alpha lag))
104         (frame-sleep current-time)
105         (values current-time lag)))
106   
107     (define (game-loop previous-time lag)
108       "Update game state, and render.  PREVIOUS-TIME is the time in
109 milliseconds of the last iteration of the game loop."
110       (let-values (((time lag)
111                     (process-frame previous-time lag)))
112         (game-loop time lag)))
113   
114     (call-with-prompt
115         'game-loop-prompt
116       (lambda ()
117         ;; Catch SIGINT and kill the loop
118         (sigaction SIGINT
119           (lambda (signum)
120             (stop-game-loop)))
121         (set! %root-scene scene)
122         (game-loop (sdl2:sdl-ticks) 0))
123       (lambda (cont callback)
124         (when (procedure? callback)
125           (callback cont))))))
126
127 (define (stop-game-loop)
128   "Abort the game loop"
129   (abort-to-prompt 'game-loop-prompt #f))
130
131 (define %sdl-window #f)
132 (define %sdl-renderer #f)
133 (define %gl-context #f)
134
135 (define (init-window)
136   (sdl2:sdl-init)
137   (set! %sdl-window (sdl2:make-window #:opengl? #t #:show? #t))
138   (set! %sdl-renderer (sdl2:make-renderer %sdl-window))
139   (sdl2:set-gl-attribute! 'context-major-version 3)
140   (sdl2:set-gl-attribute! 'context-minor-version 2)
141   (sdl2:set-gl-attribute! 'double-buffer 1)
142   (sdl2:set-gl-attribute! 'depth-size 24)
143   (set! %gl-context (sdl2:make-gl-context %sdl-window))
144   (sdl2:set-gl-swap-interval! 'vsync))
145
146 (define* (open-window #:key (title "Untitled") (resolution '(640 480)) (fullscreen? #f))
147   (sdl2:set-window-title! %sdl-window title)
148   (sdl2:set-window-size! %sdl-window resolution)
149   (sdl2:set-window-fullscreen! %sdl-window fullscreen?)
150   (sdl2:show-window! %sdl-window))
151
152 (define (close-window)
153   (sdl2:hide-window! %sdl-window)
154   (sdl2:sdl-quit))
155
156 (define (play-game scene)
157   (init-window)
158   (open-window)
159   (run-game-loop scene)
160   (close-window))