/
Reprocessing_Common.re
191 lines (167 loc) · 4.14 KB
/
Reprocessing_Common.re
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
open Reasongl;
type strokeCapT =
| Round
| Square
| Project;
type rectModeT =
| Corner
| Center
| Radius;
type glState = Gl.Window.t;
type glCamera = {projectionMatrix: Gl.Mat4.t};
type colorT = {
r: float,
g: float,
b: float,
a: float
};
type styleT = {
strokeColor: option(colorT),
strokeWeight: int,
strokeCap: strokeCapT,
fillColor: option(colorT),
tintColor: option(colorT),
rectMode: rectModeT
};
type mouseT = {
mutable pos: (int, int),
mutable prevPos: (int, int),
mutable pressed: bool
};
module KeySet =
Set.Make(
{
type t = Reprocessing_Events.keycodeT;
let compare = compare;
}
);
type keyboardT = {
mutable keyCode: Reprocessing_Events.keycodeT,
mutable pressed: KeySet.t,
mutable released: KeySet.t,
mutable down: KeySet.t
};
type frameT = {
count: int,
rate: int,
deltaTime: float
};
type sizeT = {
mutable height: int,
mutable width: int,
mutable resizeable: bool
};
let circularBufferSize = 6 * 10000;
let vertexSize = 8;
type _imageT = {
framebuffer: option(Gl.framebufferT),
texture: Gl.textureT,
/* both redundant with tgls, maybe make Gl expose functions to get the width/height */
height: int,
width: int
};
type imageT = {
mutable glData: option(_imageT),
mutable drawnTo: bool,
};
type soundLoadStatusT =
| Loading
| ShouldPlay(float, bool)
| Loaded(Gl.Audio.t);
type soundT = ref(soundLoadStatusT);
type batchT = {
vertexArray: Gl.Bigarray.t(float, Gl.Bigarray.float32_elt),
elementArray: Gl.Bigarray.t(int, Gl.Bigarray.int16_unsigned_elt),
mutable vertexPtr: int,
mutable elementPtr: int,
mutable currTex: option(Gl.textureT),
nullTex: Gl.textureT
};
type glEnv = {
camera: glCamera,
window: Gl.Window.t,
gl: Gl.contextT,
vertexBuffer: Gl.bufferT,
elementBuffer: Gl.bufferT,
aVertexColor: Gl.attributeT,
aTextureCoord: Gl.attributeT,
aVertexPosition: Gl.attributeT,
pMatrixUniform: Gl.uniformT,
uSampler: Gl.uniformT,
batch: batchT,
keyboard: keyboardT,
mouse: mouseT,
mutable style: styleT,
mutable styleStack: list(styleT),
mutable frame: frameT,
mutable matrix: array(float),
mutable matrixStack: list(array(float)),
size: sizeT
};
module type ReProcessorT = {
type t;
let run:
(
~setup: glEnv => 'a,
~draw: ('a, glEnv) => 'a=?,
~mouseMove: ('a, glEnv) => 'a=?,
~mouseDragged: ('a, glEnv) => 'a=?,
~mouseDown: ('a, glEnv) => 'a=?,
~mouseUp: ('a, glEnv) => 'a=?,
~keyPressed: ('a, glEnv) => 'a=?,
~keyReleased: ('a, glEnv) => 'a=?,
~keyTyped: ('a, glEnv) => 'a=?,
unit
) =>
unit;
};
module Stream = {
type t = (string, int);
let empty = [];
let peekch = ((str, i): t) : option(char) =>
if (i < String.length(str)) {
Some(str.[i])
} else {
None
};
let popch = ((str, i): t) : t => (str, i + 1);
let peekn = ((str, i), len) =>
if (i + len < String.length(str)) {
Some(String.sub(str, i, len))
} else {
None
};
let skipWhite = ((str, i): t): t => {
let len = String.length(str);
let rec loop = n => {
if (n >= len) {
(str, n)
} else if (str.[n] === ' ') {
loop(n + 1)
} else {
(str, n)
}
};
loop(i);
};
let popn = ((str, i), len) => (str, i + len);
let match = (stream, matchstr) => {
let len = String.length(matchstr);
switch (peekn(stream, len)) {
| Some(peek) when peek == matchstr => popn(stream, len)
| Some(peek) =>
failwith("Could not match '" ++ (matchstr ++ ("', got '" ++ (peek ++ "' instead."))))
| None => failwith("Could not match " ++ matchstr)
}
};
let charsRemaining = ((str, i)) => String.length(str) - i;
let create = (str: string) : t => (str, 0);
};
let append_char = (s: string, c: char) : string => s ++ String.make(1, c);
let rec split = (stream, sep, accstr, acc) =>
switch (Stream.peekch(stream)) {
| Some(c) when c == sep => split(Stream.popch(stream), sep, "", [accstr, ...acc])
| Some(c) => split(Stream.popch(stream), sep, append_char(accstr, c), acc)
| None => List.rev([accstr, ...acc])
};
let split = (str, ~sep) => split(Stream.create(str), sep, "", []);