]> git.jsancho.org Git - gacela.git/blobdiff - gacela/game.scm
Customizing quit event behaviour
[gacela.git] / gacela / game.scm
index 86136cd9a08fdf0070554dcf6a975efff108ff1d..48bf1297b2fce045905db6d40030fa10165fe81e 100644 (file)
@@ -24,7 +24,8 @@
   #:use-module ((sdl2 video) #:prefix sdl2:)
   #:use-module (gl)
   #:use-module (srfi srfi-11)
-  #:export (play-game
+  #:export (start-game
+           stop-game
            %sdl-renderer))
 
 
@@ -38,7 +39,8 @@
 (define* (run-game-loop scene #:key
                         (frame-rate 60)
                         (tick-rate 60)
-                        (max-ticks-per-frame 4))
+                        (max-ticks-per-frame 4)
+                       (when-quit #f))
   "Run the game loop.  SCENE is a signal which contains the current
 scene renderer procedure.  FRAME-RATE specifies the optimal number of
 frames to draw SCENE per second.  TICK-RATE specifies the optimal game
@@ -74,6 +76,8 @@ unused accumulator time."
               lag)
              ((>= lag tick-interval)
               (process-events)
+              (if (and (quit?) (procedure? when-quit))
+                  (when-quit))
                                        ;(agenda-tick!)
               (iter (- lag tick-interval) (1+ ticks)))
              (else
@@ -143,7 +147,7 @@ milliseconds of the last iteration of the game loop."
   (set! %gl-context (sdl2:make-gl-context %sdl-window))
   (sdl2:set-gl-swap-interval! 'vsync))
 
-(define* (open-window #:key (title "Untitled") (resolution '(640 480)) (fullscreen? #f))
+(define (open-window title resolution fullscreen?)
   (sdl2:set-window-title! %sdl-window title)
   (sdl2:set-window-size! %sdl-window resolution)
   (sdl2:set-window-fullscreen! %sdl-window fullscreen?)
@@ -153,8 +157,15 @@ milliseconds of the last iteration of the game loop."
   (sdl2:hide-window! %sdl-window)
   (sdl2:sdl-quit))
 
-(define (play-game scene . args)
+(define* (start-game scene #:key
+                   (title "Untitled")
+                   (resolution '(640 480))
+                   (fullscreen? #f)
+                   (when-quit (lambda () (stop-game))))
   (init-window)
-  (apply open-window args)
-  (run-game-loop scene)
+  (open-window title resolution fullscreen?)
+  (run-game-loop scene #:when-quit when-quit)
   (close-window))
+
+(define (stop-game)
+  (stop-game-loop))