This repository has been archived by the owner on Apr 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathrenderloop.jl
134 lines (110 loc) · 3.49 KB
/
renderloop.jl
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
export glscreen
function _is_alive(x::WeakRef)
if isa(x.value, Screen)
isopen(x.value) && return true
end
false
end
let screen_list = WeakRef[]
global current_screen, add_screen, get_screens, empty_screens!
clean!() = filter!(_is_alive, screen_list)
function current_screen()
clean!()
if isempty(screen_list)
error("No screen available. Consider creating one with the function glscreen()")
end
last(screen_list).value
end
add_screen(screen) = push!(screen_list, WeakRef(screen))
get_screens() = (clean!(); map(x->x.value, screen_list))
empty_screens!() = empty!(screen_list)
end
function cleanup()
for screen in get_screens()
destroy!(screen)
end
empty_screens!()
end
struct Millimeter
end
global const mm = Millimeter()
import Base: *
const pixel_per_mm = Ref(3.7)
function (*)(x::Millimeter, y::Number)
round(Int, y * pixel_per_mm[])
end
function (*)(x::Number, y::Millimeter)
round(Int, x * pixel_per_mm[])
end
function get_scaled_dpi(window)
monitor = GLFW.GetPrimaryMonitor()
props = GLWindow.MonitorProperties(monitor)
# it seems like small displays with high dpi make mm look quite big.
# so lets scale it a bit. 518 is a bit arbitrary, but the scale of my
# screen on which I test everything, hence it will make you see things as I do.
scaling = props.physicalsize[1] / 518
min(props.dpi...) * scaling # we do not start fiddling with differently scaled xy dpi's
end
function glscreen(name = "GLVisualize";
resolution = GLWindow.standard_screen_resolution(),
debugging = false,
clear = true,
color = RGBA(1,1,1,1),
stroke = (0f0, color),
hidden = false,
visible = true,
focus = true,
fullscreen = false
)
cleanup()
screen = Screen(
name,
resolution = resolution, debugging = debugging,
clear = clear, color = color, stroke = stroke,
hidden = hidden, visible = visible, focus = focus,
fullscreen = fullscreen
)
add_screen(screen)
GLWindow.add_complex_signals!(screen) #add the drag events and such
GLFW.MakeContextCurrent(GLWindow.nativewindow(screen))
pixel_per_mm[] = get_scaled_dpi(screen) / 25.4
screen
end
const timer_signal_dict = Dict{WeakRef, Dict{Int, Signal{Float64}}}()
"""
Creates a timer signal with `updates_per_second` while `window` is open.
It's reusing timer signals with the same update rate and registering the updates
with GLFW.
"""
function get_timer_signal(updates_per_second, window=current_screen())
dict = get!(timer_signal_dict, WeakRef(window), Dict{Int, Signal{Float64}}())
get!(dict, updates_per_second) do
fpswhen(window.inputs[:window_open], updates_per_second)
end
end
function fold_loop(v0, _)
val, range, index = v0
val = range[index]
index += 1
index > length(range) && (index = 1)
(val, range, index)
end
function loop(range::Range, rate=60)
t = get_timer_signal(rate)
map(first, foldp(fold_loop, (first(range), range, 1), t))
end
function fold_bounce(v0, _)
val, range, index, direction = v0
val = range[index]
index += direction
if index in (length(range)+1, 0)
direction = -direction
index += 2direction
end
(val, range, index, direction)
end
function bounce(range::Range{T}, rate=60) where T
t = get_timer_signal(rate)
map(first, foldp(fold_bounce, (first(range), range, 1, 1), t))
end
export bounce, loop