summaryrefslogtreecommitdiff
path: root/2d/game.scm
blob: 4c585211ac2bd4b96937580bdbc2e28c662c0852 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
;;; guile-2d
;;; Copyright (C) 2013 David Thompson <dthompson2@worcester.edu>
;;;
;;; Guile-2d is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU Lesser General Public License as
;;; published by the Free Software Foundation, either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-2d 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
;;; Lesser General Public License for more details.
;;;
;;; You should have received a copy of the GNU Lesser General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Game data structure.
;;
;;; Code:

(define-module (2d game)
  #:use-module (2d private game)
  #:use-module (2d game-loop)
  #:use-module (2d helpers)
  #:use-module (2d observer)
  #:use-module (2d vector2))

;;;
;;; Scenes
;;;

;; When no event callbacks are specified for a scene, these
;; (hopefully) convenient defaults will be used.
(define %default-scene-events
  `((quit     . ,(lambda (state) (quit-game)))
    (key-down . ,(lambda (state key mode unicode)
                   (when (any-equal? key 'escape 'q)
                     (quit-game))))))

(define (default-scene-events)
  (copy-tree %default-scene-events))

(define* (make-scene #:optional #:key
                     (title "A Guile-2D Scene")
                     (events (default-scene-events))
                     (update (lambda (s) #f))
                     (draw (lambda (s) #f))
                     (state #f))
  "Return a new scene. TITLE is a human readable name for the
scene. EVENTS is an alist of event handlers. UPDATE is a procedure
that updates the scene. DRAW is a procedure that renders the
scene. STATE is an object that encapsulates the scene state."
  (%make-scene title (alist->observer events) update draw state))

(define-syntax-rule (define-scene name kwargs ...)
  "Syntactic sugar over define and make-scene. Return a procedure that
creates a new scene."
  (define (name) (make-scene kwargs ...)))

(re-export <scene>
           scene?
           scene-title
           scene-observer
           scene-update-proc
           scene-draw-proc
           scene-state
           scene-trigger
           update-scene
           draw-scene
           push-scene
           replace-scene
           pop-scene)

(export make-scene
        define-scene
        default-scene-events)

;;;
;;; Games
;;;

(define* (make-game #:optional #:key
                    (title "A Guile-2D Game")
                    (resolution (vector2 640 480))
                    (fullscreen #f)
                    (first-scene #f))
  "Return a new game. All game properties have some reasonable default
value."
  (%make-game title resolution fullscreen first-scene))

(define-syntax-rule (define-game name kwargs ...)
  "Syntactic sugar over define and make-game."
  (define name (make-game kwargs ...)))

(re-export <game>
           game?
           game-title
           game-resolution
           game-fullscreen?
           game-first-scene
           run-game
           quit-game
           pause-game
           resume-game
           game-running?
           game-paused?
           current-fps)

(export make-game
        define-game)