This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
app.clj
399 lines (350 loc) · 9.84 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
;; 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]
[penumbra.app.core])
(:require [penumbra.opengl
[texture :as texture]
[context :as context]]
[penumbra
[slate :as slate]
[time :as time]]
[penumbra.app
[window :as window]
[input :as input]
[controller :as controller]
[loop :as loop]
[event :as event]
[queue :as queue]])
(:import [org.lwjgl.opengl Display]
[org.lwjgl.input Keyboard]))
;;;
(defn sync-update
([f]
(sync-update *app* f))
([app f]
(let [state* (dosync
(when-let [value (f @(:state app))]
(ref-set (:state app) value)
value))]
(when state*
(controller/repaint!)))))
(defn- subscribe-callback [app callback f]
(event/unique-subscribe!
(:event app)
callback
(fn [& args]
(sync-update
app
(if (empty? args)
f
(apply partial (list* f args)))))))
(defn- alter-callbacks [clock callbacks]
(let [callbacks (if-not (:update callbacks)
callbacks
(update-in callbacks [:update] #(loop/timed-fn clock %)))
callbacks (if-not (:display callbacks)
callbacks
(update-in callbacks [:display] #(loop/timed-fn clock %)))]
callbacks))
;;;
(defn publish!
([hook & args]
(apply event/publish! (list* (:event *app*) hook args))))
(defn subscribe!
([hook f]
(subscribe! *app* hook f))
([app hook f]
(event/subscribe! (:event app) hook f)))
(defn unsubscribe! []
(reset! *unsubscribe* true))
;;;
(defstruct app-struct
:window
:input
:controller
:queue
:clock
:callbacks
:state)
(defn create [callbacks state]
(let [clock (time/clock)
app (with-meta
(struct-map app-struct
:nested *app*
:window (window/create)
:input (input/create)
:controller (controller/create)
:queue (ref nil)
:event (event/create)
:clock clock
:state (ref state))
{:type ::app})]
(doseq [[c f] (alter-callbacks clock callbacks)]
(if (= :display c)
(event/unique-subscribe! (:event app) c #(f @(:state app)))
(subscribe-callback app c f)))
app))
(defmethod print-method ::app [app writer]
(let [{size :size active-percent :active-percent} (when (:texture-pool app) (texture/texture-pool-stats @(:texture-pool app)))
elapsed-time (time/now (:clock app))
state (cond
(controller/stopped? (:controller app)) "STOPPED"
(controller/paused? (:controller app)) "PAUSED"
:else "RUNNING")]
(.write
writer
(format "<<%s: %s\n %.2f seconds elapsed\n Allocated texture memory: %.2fM (%.2f%% in use)>>"
state
(Display/getTitle)
elapsed-time
(/ (or size 0.) 1e6) (* 100 (or active-percent 0.))))))
;;Clock
(defn clock
([]
(clock *app*))
([app]
(:clock app)))
(defn now
([]
(now *app*))
([app]
(time/now (clock app))))
(defn speed!
([clock-speed]
(speed! *app* clock-speed))
([app clock-speed]
(time/update-clock (:clock app) #(* % clock-speed))
nil))
;;Input
(defn key-pressed?
([key]
(key-pressed? *app* key))
([app key]
((-> app :input :keys deref vals set) key)))
(defn key-repeat!
([enabled]
(key-repeat! *app* enabled))
([app enabled]
(Keyboard/enableRepeatEvents enabled)))
;;Window
(defn title!
([title]
(title! *app* title))
([app title]
(when (-?> app :window :frame deref)
(.setTitle @(:frame *window*) title))
(Display/setTitle title)))
(defn display-modes
"Returns a list of available display modes."
[]
(window/display-modes))
(defn current-display-mode
"Returns the current display mode."
[]
(window/current-display-mode))
(defn display-mode!
"Sets the current display mode."
([width height] (window/display-mode! width height))
([mode] (window/display-mode! mode)))
(defn dimensions
"Returns dimensions of window as [width height]."
([] (dimensions *window*))
([w] (window/dimensions w)))
(defn vsync?
([]
(vsync? *app*))
([app]
(window/vsync?)))
(defn vsync!
([enabled]
(vsync! *app* enabled))
([app enabled]
(window/vsync! enabled)))
(defn resizable!
([enabled]
(resizable! *app* enabled))
([app enabled]
(if enabled
(window/enable-resizable! app)
(window/disable-resizable! app))))
(defn fullscreen!
([enabled]
(fullscreen! *app* enabled))
([app enabled]
(Display/setFullscreen enabled)))
;;Updates
(defn frequency!
"Update frequency of recurring update. Can only be called from inside recurring-update callback."
[hz]
(reset! *hz* hz))
(defn update
([f]
(update *app* f))
([app f]
(queue/update app #(sync-update app f))))
(defn periodic-update
([hz f]
(periodic-update *app* hz f))
([app hz f]
(queue/periodic-update app hz #(sync-update app f))))
(defn state
([] (state *app*))
([app] @(:state app)))
;;App state
(defn cleanup!
"Cleans up any active apps."
[]
(Display/destroy))
(defn repaint!
"Forces a new frame to be redrawn"
([]
(repaint! *app*))
([app]
(controller/repaint! (:controller app))))
(defn stop!
"Stops the application."
([]
(stop! *app*))
([app]
(controller/stop! (:controller app) true)))
(defn pause!
"Halts main loop, and yields control back to the REPL. Returns nil."
([]
(pause! *app*))
([app]
(speed! app 0)
(controller/pause! (:controller app))))
(defn- resume!
([]
(resume! *app*))
([app]
(loop/with-app app
(let [stopped? (controller/stopped?)]
(controller/resume!)
(when stopped?
(dosync (ref-set (:queue app) (queue/create)))
(publish! :init)
(publish! :reshape (concat [0 0] (dimensions))))
(input/resume!)
(speed! 1)))))
(defn- destroy
([]
(destroy *app*))
([app]
(let [nested? (:nested app)]
(try
(publish! :close)
(when-not nested?
(-> app
(update-in [:input] input/destroy)
(update-in [:window] window/destroy)))
(finally
(when-not nested?
(context/destroy)))))))
(defn- init
[app]
(if (controller/stopped? (:controller app))
(do
(title! "Penumbra")
(assoc app
:nested *app*
:window (window/init (:window app))
:input (input/init (:input app))))
app))
;;;
(defn single-thread-main-loop
"Does everything in one pass."
([]
(single-thread-main-loop *app*))
([app]
(Display/processMessages)
(input/handle-keyboard!)
(input/handle-mouse!)
(when (window/check-for-resize)
(repaint!))
(if (or (Display/isDirty) (controller/invalidated?))
(do
(publish! :update)
(controller/repainted!)
(push-matrix
(clear 0 0 0)
(publish! :display))
(Display/update))
(Thread/sleep 1))
(if (Display/isCloseRequested)
(controller/stop! :requested-by-user))))
(defn start-single-thread [f app]
(let [app (init app)]
(f
app
(fn [inner-fn]
(try
(resume!)
(inner-fn)
(catch Exception e
(.printStackTrace e)
(controller/stop! :exception)))
(speed! 0)
(if (controller/stopped?)
(destroy app)
(assoc app
:texture-pool *texture-pool*)))
single-thread-main-loop)
app))
(defn- try-async-resume [app]
(when (and (:async app) (controller/paused? (:controller app)))
(resume! app)
true))
(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 (create callbacks state)))
([app]
(if-not (try-async-resume app)
(let [app (start-single-thread loop/primary-loop app)]
(let [reason (-> app :controller :stopped? deref)]
(when (and *controller* (keyword? reason))
(controller/stop! reason)))
app)
app)))
(defn start*
"Same as start, but doesn't block until complete"
([callbacks state]
(start* (create callbacks state)))
([app]
(let [texture-pool (promise)]
(when-not (try-async-resume app)
(.start
(Thread.
(fn []
(context/with-context nil
(deliver texture-pool *texture-pool*)
(start-single-thread loop/secondary-loop app)))))
(controller/try-latch! (-> app :controller :latch deref))
(assoc app
:texture-pool @texture-pool
:async true)))))