From: Javier Sancho Date: Tue, 14 Feb 2017 19:16:21 +0000 (+0100) Subject: Merge branch 'release/0.2' X-Git-Url: https://git.jsancho.org/?p=gacela.git;a=commitdiff_plain;h=12725e0c889f28354fc75cfb34683f9323dd04dd;hp=2f85dc81c49441bfd939c268e1ccc125c9d1d399 Merge branch 'release/0.2' --- diff --git a/examples/01-hello-world/01-hello-world.scm b/examples/01-hello-world/01-hello-world.scm new file mode 100644 index 0000000..61263f1 --- /dev/null +++ b/examples/01-hello-world/01-hello-world.scm @@ -0,0 +1,24 @@ +#!/usr/bin/env guile +!# + +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2017 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + +(use-modules (gacela)) + +(display-scene + (window ((resolution '(640 480))) + (bitmap "hello-world.bmp"))) diff --git a/examples/01-hello-world/hello-world.bmp b/examples/01-hello-world/hello-world.bmp new file mode 100644 index 0000000..def2531 Binary files /dev/null and b/examples/01-hello-world/hello-world.bmp differ diff --git a/examples/02-event-driven-programming/02-event-driven-programming.scm b/examples/02-event-driven-programming/02-event-driven-programming.scm new file mode 100644 index 0000000..b151233 --- /dev/null +++ b/examples/02-event-driven-programming/02-event-driven-programming.scm @@ -0,0 +1,34 @@ +#!/usr/bin/env guile +!# + +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2017 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + +(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 counter-quit)) + (bitmap "x.bmp"))) diff --git a/examples/02-event-driven-programming/x.bmp b/examples/02-event-driven-programming/x.bmp new file mode 100755 index 0000000..9633a7e Binary files /dev/null and b/examples/02-event-driven-programming/x.bmp differ diff --git a/examples/fran.scm b/examples/fran.scm index ea016f3..79c0055 100644 --- a/examples/fran.scm +++ b/examples/fran.scm @@ -28,12 +28,10 @@ ;;; First example -(run-scene (import-bitmap "hello_world.bmp")) - -(define red (import-bitmap "red.bmp")) +(define red (bitmap "red.bmp")) (define left-right-red (move-xy wiggle 0 red)) (run-scene left-right-red) -(define blue (import-bitmap "blue.bmp")) +(define blue (bitmap "blue.bmp")) (define up-down-blue (move-xy 0 waggle blue)) (run-scene up-down-blue) diff --git a/examples/hello_world.bmp b/examples/hello_world.bmp deleted file mode 100644 index def2531..0000000 Binary files a/examples/hello_world.bmp and /dev/null differ diff --git a/gacela.scm b/gacela.scm index 5b31c64..90840bf 100644 --- a/gacela.scm +++ b/gacela.scm @@ -23,8 +23,10 @@ (begin (define %public-modules '((gacela image) + (gacela game) (gacela math) - (gacela scene))) + (gacela scene) + (gacela window))) (for-each (let ((i (module-public-interface (current-module)))) (lambda (m) diff --git a/gacela/event.scm b/gacela/event.scm new file mode 100644 index 0000000..9efd8e1 --- /dev/null +++ b/gacela/event.scm @@ -0,0 +1,37 @@ +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2017 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + + +(define-module (gacela event) + #:use-module ((sdl2 events) #:prefix sdl2:) + #:export (process-events + quit?)) + + +(define *current-events* '()) + +(define (poll-events) + (let ((event (sdl2:poll-event))) + (cond (event + (cons event (poll-events))) + (else + '())))) + +(define (process-events) + (set! *current-events* (poll-events))) + +(define (quit?) + (not (null? (filter (lambda (e) (sdl2:quit-event? e)) *current-events*)))) diff --git a/gacela/game.scm b/gacela/game.scm index dad0412..48bf129 100644 --- a/gacela/game.scm +++ b/gacela/game.scm @@ -17,13 +17,15 @@ (define-module (gacela game) #:use-module (gacela math) + #:use-module (gacela event) #:use-module ((sdl2) #:prefix sdl2:) #:use-module ((sdl2 render) #:prefix sdl2:) #:use-module ((sdl2 surface) #:prefix sdl2:) #:use-module ((sdl2 video) #:prefix sdl2:) #:use-module (gl) #:use-module (srfi srfi-11) - #:export (play-game + #:export (start-game + stop-game %sdl-renderer)) @@ -37,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 @@ -72,7 +75,9 @@ unused accumulator time." (cond ((>= ticks max-ticks-per-frame) lag) ((>= lag tick-interval) - ;(process-events) + (process-events) + (if (and (quit?) (procedure? when-quit)) + (when-quit)) ;(agenda-tick!) (iter (- lag tick-interval) (1+ ticks))) (else @@ -142,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?) @@ -152,8 +157,15 @@ milliseconds of the last iteration of the game loop." (sdl2:hide-window! %sdl-window) (sdl2:sdl-quit)) -(define (play-game scene) +(define* (start-game scene #:key + (title "Untitled") + (resolution '(640 480)) + (fullscreen? #f) + (when-quit (lambda () (stop-game)))) (init-window) - (open-window) - (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/image.scm b/gacela/image.scm index 370bbd1..b1ee674 100644 --- a/gacela/image.scm +++ b/gacela/image.scm @@ -22,10 +22,10 @@ #:use-module ((sdl2 image) #:prefix sdl2:) #:use-module ((sdl2 render) #:prefix sdl2:) #:use-module ((sdl2 surface) #:prefix sdl2:) - #:export (import-bitmap + #:export (bitmap move-xy)) -(define (import-bitmap filename) +(define (bitmap filename) (make-scene "bitmap" (let ((image (sdl2:load-image filename)) diff --git a/gacela/scene.scm b/gacela/scene.scm index e620480..ccdc23e 100644 --- a/gacela/scene.scm +++ b/gacela/scene.scm @@ -42,6 +42,6 @@ (define (display-scene scene . args) (apply (scene-procedure scene) args)) -(define (run-scene scene) - (play-game - (scene-procedure scene))) +(define (run-scene scene . args) + (apply start-game + (cons (scene-procedure scene) args))) diff --git a/gacela/window.scm b/gacela/window.scm new file mode 100644 index 0000000..6d43ce6 --- /dev/null +++ b/gacela/window.scm @@ -0,0 +1,32 @@ +;;; Gacela, a GNU Guile extension for fast games development +;;; Copyright (C) 2016 by Javier Sancho Fernandez +;;; +;;; This program is free software: you can redistribute it and/or modify +;;; it under the terms of the GNU General Public License as published by +;;; the Free Software Foundation, either version 3 of the License, or +;;; (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;;; GNU General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program. If not, see . + + +(define-module (gacela window) + #:use-module (gacela scene) + #:export (window)) + + +(define-syntax window + (lambda (x) + (syntax-case x () + ((_ ((property-name property-value) ...) scene) + #'(make-scene + "window" + (lambda () + (apply run-scene (cons scene + (append (list (symbol->keyword 'property-name) property-value) + ...)))))))))