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