From 5a8e5314cceaa73d438c0b3f7a2fdb26ea9b3a2c Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Tue, 14 Feb 2017 20:10:33 +0100 Subject: [PATCH] Customizing quit event behaviour --- .../02-event-driven-programming.scm | 11 ++++++++- gacela.scm | 1 + gacela/game.scm | 23 ++++++++++++++----- gacela/scene.scm | 2 +- 4 files changed, 29 insertions(+), 8 deletions(-) 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))) -- 2.39.2