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