This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.clj
317 lines (275 loc) · 9.28 KB
/
app.clj
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
;; Copyright (c) Zachary Tellman. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this
;; software.
(ns penumbra.app
(:use [clojure.contrib.core :only (-?>)]
[clojure.contrib.def :only [defmacro- defvar-]]
[penumbra.opengl]
[penumbra.opengl.core]
[clojure.walk :only (postwalk-replace)])
(:require [penumbra.opengl
[texture :as texture]
[context :as context]
[slate :as slate]]
[penumbra
[time :as time]]
[penumbra.app
[core :as app]
[window :as window]
[input :as input]
[controller :as controller]
[loop :as loop]
[event :as event]
[queue :as queue]]))
;;;
(defn- transform-extend-arglists [protocol name arglists template]
(list*
`fn
(map
(fn [args]
(let [template (postwalk-replace {'this (first args)} template)]
(list
(vec args)
(list*
(intern (symbol (namespace protocol)) name)
template
(next args)))))
arglists)))
(defmacro- auto-extend
"Lets the application, which contains an implementation of a protocol, automatically extend that protocol."
[type protocol template & explicit]
(let [sigs (eval `(vals (:sigs ~protocol)))]
(list
`extend
type
protocol
(merge
(->> (map
(fn [{name :name arglists :arglists}]
[(keyword name)
(transform-extend-arglists protocol name arglists template)])
sigs)
(apply concat)
(apply hash-map))
(apply hash-map explicit)))))
;;;
(defn- update-
"Updates the state of the application."
[app state f args]
(swap! state
(fn [state]
(let [state* (if (empty? args)
(f state)
(apply f (concat args [state])))]
(when state*
(controller/invalidated! app true))
(or state* state)))))
(defn- alter-callbacks
"Updates the update and display callbacks using timed-fn"
[clock callbacks]
(let [wrap (partial loop/timed-fn clock)
callbacks (if (:update callbacks)
(update-in callbacks [:update] wrap)
callbacks)
callbacks (if (:display callbacks)
(update-in callbacks [:display] wrap)
callbacks)]
callbacks))
(defrecord App
[state
clock
event-handler
queue
window
input-handler
controller
parent]
clojure.lang.IDeref
(deref [this] @(:state this)))
(auto-extend App penumbra.app.window/Window (deref (:window this)))
(auto-extend App penumbra.app.input/InputHandler (deref (:input-handler this)))
(auto-extend App penumbra.app.queue/QueueHash (deref (:queue this)))
(auto-extend App penumbra.app.event/EventHandler (:event-handler this))
(auto-extend App penumbra.app.controller/Controller (:controller this))
(extend
App
app/App
{:speed! (fn [app speed] (time/speed! (:clock app) speed))
:now (fn [app] @(:clock app))
:callback- (fn [app event args] ((-> app :callbacks event) args))
:init! (fn [app]
(window/init! app)
(input/init! app)
(queue/init! app)
(controller/resume! app)
(event/publish! app :init))
:destroy! (fn [app]
(event/publish! app :close)
(controller/stop! app)
(when-not (:parent app)
(window/destroy! app)
(input/destroy! app)))})
(defmethod print-method penumbra.app.App [app writer]
(.write writer "App"))
(defn create
"Creates an application."
[callbacks state]
(let [window (atom nil)
input (atom nil)
queue (atom nil)
event (event/create)
clock (time/clock)
state (atom state)
controller (controller/create)
app (App. state clock event queue window input controller app/*app*)]
(reset! window (window/create-fixed-window app))
(reset! input (input/create app))
(reset! queue (queue/create app))
(doseq [[event f] (alter-callbacks clock callbacks)]
(if (= event :display)
(event/subscribe! app :display (fn [& args] (f @state)))
(event/subscribe! app event (fn [& args] (update- app state f args)))))
app))
;;;
(defn app
"Returns the current application."
[]
app/*app*)
(defn- transform-import-arglists [protocol name doc arglists]
(list*
`defn name
doc
(map
(fn [args]
(list
(vec (next args))
(list*
(intern (symbol (namespace protocol)) name)
'penumbra.app.core/*app*
(next args))))
arglists)))
(defmacro- auto-import
"Creates an function which automatically fills in app with *app*"
[protocol & imports]
(let [sigs (eval `(vals (:sigs ~protocol)))]
(list*
'do
(map
(fn [{name :name arglists :arglists doc :doc}]
(transform-import-arglists protocol name doc arglists))
(let [imports (set imports)]
(filter #(imports (:name %)) sigs))))))
(auto-import penumbra.app.window/Window
title! size fullscreen! vsync! display-mode! display-modes)
(auto-import penumbra.app.controller/Controller
stop! pause!)
(auto-import penumbra.app.input/InputHandler
key-pressed? button-pressed? key-repeat! mouse-location)
;;
(defn clock
"Returns the application clock."
([] (clock app/*app*))
([app] (:clock app)))
(defn now
"Returns the elapsed clock time, in seconds, since the application began."
([] (now app/*app*))
([app] @(clock app)))
(defn speed!
"Sets the application clock speed."
([speed] (speed! app/*app* speed))
([app speed] (time/speed! (clock app) speed)))
(defn periodic-update!
"Starts a recurring update, which is called 'hz' times a second.
Time is governed by 'clock', which defaults to the application clock.
OpenGL calls cannot be made within this callback."
([hz f] (periodic-update! (clock) hz f))
([clock hz f] (periodic-update! app/*app* clock hz f))
([app clock hz f] (queue/periodic-enqueue! app clock hz #(update- app (:state app) f nil))))
(defn delay!
"Enqueues an update to be executed in 'delay' milliseconds.
Time is goverend by 'clock', which defaults to the application clock.
OpenGL calls cannot be made within this callback."
([delay f] (delay! (clock) delay f))
([clock delay f] (delay! app/*app* clock delay f))
([app clock delay f] (queue/enqueue! app clock delay #(update- app (:state app) f nil))))
(defn update!
"Enqueues an update to happen immediately.
OpenGL calls cannot be made within the callback."
([f] (update! app/*app* f))
([app f] (delay! (clock app) 0 f)))
(defn enqueue!
"Enqueues an update to happen before the next frame is rendered.
OpenGL calls in this callback are okay."
([f] (enqueue! app/*app* f))
([app f] (event/subscribe-once! app :enqueue #(update- app (:state app) f nil))))
(defn repaint!
"Forces the application to repaint."
([] (repaint! app/*app*))
([app] (controller/invalidated! app true)))
(defn frequency! [hz]
"Updates the update frequency of a periodic update.
This can only be called from within the periodic callback. A frequency of 0 or less will halt the update."
(reset! app/*hz* hz))
;;
(defmacro with-gl
"Creates a valid OpenGL context within the scope."
[& body]
`(slate/with-slate
(context/with-context nil
~@body)))
;;App state
(defn single-thread-main-loop
"Does everything in one pass."
([app]
(window/process! app)
(input/handle-keyboard! app)
(input/handle-mouse! app)
(window/handle-resize! app)
(if (or (window/invalidated? app) (controller/invalidated? app))
(do
(event/publish! app :enqueue)
(event/publish! app :update)
(controller/invalidated! app false)
(push-matrix
(clear 0 0 0)
(event/publish! app :display))
(window/update! app))
(Thread/sleep 1))
(if (window/close? app)
(controller/stop! app :requested-by-user))))
(defn start-single-thread
[app loop-fn]
(context/with-context nil
(loop-fn
app
(fn [inner-fn]
(app/init! app)
(inner-fn)
(app/speed! app 0))
(partial single-thread-main-loop app))
(when (controller/stopped? app)
(app/destroy! app)))
app)
(defn start
"Starts a window from scratch, or from a closed state.
Supported callbacks are:
:update [[delta time] state]
:display [[delta time] state]
:reshape [[x y width height] state]
:init [state]
:close [state]
:mouse-drag [[[dx dy] [x y]] button state]
:mouse-move [[[dx dy] [x y]] state]
:mouse-up [[x y] button state]
:mouse-click [[x y] button state]
:mouse-down [[x y] button state]
:key-type [key state]
:key-press [key state]
:key-release [key state]"
([callbacks state]
(start-single-thread (create callbacks state) loop/basic-loop)))