From: Javier Sancho Date: Tue, 14 Feb 2017 19:10:33 +0000 (+0100) Subject: Customizing quit event behaviour X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=5a8e5314cceaa73d438c0b3f7a2fdb26ea9b3a2c;p=gacela.git Customizing quit event behaviour --- diff --git a/examples/02-event-driven-programming/02-event-driven-programming.scm b/examples/02-event-driven-programming/02-event-driven-programming.scm index 505f4b0..b151233 100644 --- a/examples/02-event-driven-programming/02-event-driven-programming.scm +++ b/examples/02-event-driven-programming/02-event-driven-programming.scm @@ -19,7 +19,16 @@ (use-modules (gacela)) +(define counter-quit #f) +(let ((n 0)) + (set! counter-quit + (lambda () + (set! n (+ n 1)) + (format #t "Quit clicked ~a times !!~%" n) + (if (> n 2) + (stop-game))))) + (display-scene (window ((resolution '(640 480)) - (when-quit (lambda () (format #t "Quit clicked!!~%")))) + (when-quit counter-quit)) (bitmap "x.bmp"))) diff --git a/gacela.scm b/gacela.scm index d49cbfa..90840bf 100644 --- a/gacela.scm +++ b/gacela.scm @@ -23,6 +23,7 @@ (begin (define %public-modules '((gacela image) + (gacela game) (gacela math) (gacela scene) (gacela window))) diff --git a/gacela/game.scm b/gacela/game.scm index 86136cd..48bf129 100644 --- a/gacela/game.scm +++ b/gacela/game.scm @@ -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)) diff --git a/gacela/scene.scm b/gacela/scene.scm index e969432..ccdc23e 100644 --- a/gacela/scene.scm +++ b/gacela/scene.scm @@ -43,5 +43,5 @@ (apply (scene-procedure scene) args)) (define (run-scene scene . args) - (apply play-game + (apply start-game (cons (scene-procedure scene) args)))