-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.rkt
65 lines (51 loc) · 1.67 KB
/
application.rkt
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
#lang racket/base
(require graphics-engine/canvas
graphics-engine/private
opengl
racket/class
racket/gui/base
racket/match
racket/stxparam
(for-syntax racket/base
syntax/transformer))
(provide (all-defined-out))
(define-the-things
[the-frame current-frame]
[the-application current-application]
[the-process current-process])
(struct application (frame canvas do-start do-draw do-idle)
#:name gfx:application
#:constructor-name application)
(struct process (quit-sema)
#:name gfx:process
#:constructor-name process)
(define (quit)
(semaphore-post (process-quit-sema the-process)))
(define (run app)
(match-define (gfx:application frame canvas do-start do-draw do-idle) app)
(define (info msg . args)
(when (get-field verbose? canvas)
(displayln (format "application: ~a" (apply format msg args)))))
(parameterize ([current-frame frame ]
[current-canvas canvas])
(info "starting")
(when do-start (GL> (do-start)))
(send frame show #t)
(send canvas focus)
(define quit-sema (make-semaphore))
(thread (λ () (semaphore-wait quit-sema) (info "exiting") (exit)))
(when do-idle
(thread
(λ ()
(let loop ([state null])
(sync (system-idle-evt))
(loop (call-with-values (λ () (GL> (apply do-idle state))) list))))))
(when do-draw
(thread
(λ ()
(collect-garbage)
(let loop ([state null])
(collect-garbage 'incremental)
(sleep)
(loop (call-with-values (λ () (GL> (apply do-draw state))) list))))))
(process quit-sema)))