/
th.um
391 lines (337 loc) · 6.73 KB
/
th.um
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
//~~
// Module with useful variables and types.
// Variables: time, delta, platform
// Constants: black, white, red, green, blue, yellow, magenta, cyan.
//~~
import (
"std.um"
)
//~~Tophat type aliases
// standard type for real values
type fu* = real32
// standard type for integer values
type iu* = int32
// standard type for unsigned values
type uu* = uint32
//~~
//~~enum ErrCode
type ErrCode* = enum (int32) {
ok = 0
failure = 1
io
bad_enum
bad_action
bad_input
alloc
already
out_of_bounds
}
//~~
fn (e: ^ErrCode) toStr*(): str {
switch e^ {
case .ok:
return "ok"
case .io:
return "input/output error"
case .bad_enum:
return "invalid enum value"
case .bad_action:
return "invalid action"
case .bad_input:
return "invalid input value"
case .alloc:
return "failed allocation"
case .already:
return "action already performet"
case .out_of_bounds:
return "index out of bounds"
}
return "unknown error"
}
fn __errFromCode*(ec: ErrCode, msg: str = ""): std::Err {
if msg != "" {
return std::error(int(ec), ec.toStr() + ": " + msg, "tophat")
}
return std::error(int(ec), ec.toStr(), "tophat")
}
//~~struct Vf2
// vector 2
type Vf2* = struct {
x, y: fu
}
//~~
//~~fn mkVf2
// Vf2 constructor
fn mkVf2*(x: fu = 0, y: fu = 0): Vf2 {
return Vf2{x, y}
}
//~~
//~~fn Vf2.rotated
// rotates `p` around `origin` with `rot` in degrees
fn (p: ^Vf2) rotated*(origin: Vf2, rot: fu): Vf2 {
//~~
angle := (rot * std::pi) / 180
pos := p^
pos.x -= origin.x
pos.y -= origin.y
x := pos.x * cos(angle) - pos.y * sin(angle)
y := pos.x * sin(angle) + pos.y * cos(angle)
pos.x = x + origin.x
pos.y = y + origin.y
return pos
}
//~~fn Vf2.distanceTo
// distance between p1 and p2
fn (p1: ^Vf2) distanceTo*(p2: Vf2): fu {
//~~
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y))
}
//~~fn Vf2.angleTo
// Angle between p1 and p2
fn (p1: ^Vf2) angleTo*(p2: Vf2): real {
//~~
if p1.x == p2.x && p1.y == p2.y { return 0 }
return atan2(p2.y - p1.y, p2.x - p1.x) * 180 / std::pi + 180
}
//~~fn Vf2.abs
// Absolute value of a vector.
fn (p: ^Vf2) abs*(): Vf2 {
//~~
return Vf2{fabs(p.x), fabs(p.y)}
}
//~~fn Vf2.round
// Rounds a vector.
fn (p: ^Vf2) round*(): Vf2 {
//~~
return Vf2{round(p.x), round(p.y)}
}
//~~fn Vf2.trunc
// Truncates a vector.
fn (p: ^Vf2) trunc*(): Vf2 {
//~~
return Vf2{trunc(p.x), trunc(p.y)}
}
//~~fn Vf2.floor
// Floors a vector.
fn (p: ^Vf2) floor*(): Vf2 {
//~~
return Vf2{floor(p.x), floor(p.y)}
}
//~~fn Vf2.ceil
// Ceils a vector.
fn (p: ^Vf2) ceil*(): Vf2 {
//~~
return Vf2{ceil(p.x), ceil(p.y)}
}
//~~fn vf2f
// Creates a vector with both x and y set to f
fn vf2f*(f: fu): Vf2 {
//~~
return Vf2{f, f}
}
//~~fn Vf2.sub
// Subtracts a vector from another one.
fn (p: ^Vf2) sub*(p2: Vf2): Vf2 {
//~~
return Vf2{p.x - p2.x, p.y - p2.y}
}
//~~fn Vf2.subf
// Subtracts a fu from a vector.
fn (p: ^Vf2) subf*(f: fu): Vf2 {
//~~
return Vf2{p.x - f, p.y - f}
}
//~~fn Vf2.add
// Adds a vector to another one.
fn (p: ^Vf2) add*(p2: Vf2): Vf2 {
//~~
return Vf2{p.x + p2.x, p.y + p2.y}
}
//~~fn Vf2.addf
// Adds a fu to a vector.
fn (p: ^Vf2) addf*(f: fu): Vf2 {
//~~
return Vf2{p.x + f, p.y + f}
}
//~~fn Vf2.div
// Divides a vector by another one.
fn (p: ^Vf2) div*(p2: Vf2): Vf2 {
//~~
return Vf2{p.x / p2.x, p.y / p2.y}
}
//~~fn Vf2.divf
// Divides a vector by a fu.
fn (p: ^Vf2) divf*(f: fu): Vf2 {
//~~
return Vf2{p.x / f, p.y / f}
}
//~~fn Vf2.mul
// Multiplies a vector by another one.
fn (p: ^Vf2) mul*(p2: Vf2): Vf2 {
//~~
return Vf2{p.x * p2.x, p.y * p2.y}
}
//~~fn Vf2.mulf
// Multiplies a vector by a fu.
fn (p: ^Vf2) mulf*(f: fu): Vf2 {
//~~
return Vf2{p.x * f, p.y * f}
}
//~~fn Vf2.mag
// Returns the magnitude of a vector p.
fn (p: ^Vf2) mag*(): fu {
//~~
return sqrt(p.x * p.x + p.y * p.y)
}
//~~fn Vf2.norm
// Normalizes a vector.
fn (p: ^Vf2) norm*(): Vf2 {
//~~
if p.x == 0 && p.y == 0 {
return p^
}
return p.divf(p.mag())
}
//~~fn Vf2.dot
// Calculates dot product between 2 vectors.
fn (p: ^Vf2) dot*(q: Vf2): fu {
//~~
return p.x*q.x + p.y*q.y
}
//~~struct Transform
// Struct defining transformation. Used for example by entities.
type Transform* = struct {
p: Vf2 // position
s: Vf2 // scale
o: Vf2 // origin
r: fu // rotation
}
//~~
//~~fn mkTransform
// Transform constructor
fn mkTransform*(p: Vf2, s: Vf2 = Vf2{1, 1}, o: Vf2 = Vf2{0, 0}, r: fu = 0.0): Transform {
//~~
return Transform{p: p, s: s, o: o, r: r}
}
fn umth_transform_transform(o, t: ^Transform)
//~~fn Transform.transformed
// Transforms a transform with another transform.
fn (o: ^Transform) transformed*(t: Transform): Transform {
//~~
ret := o^
umth_transform_transform(&ret, &t)
return ret
}
fn umth_transform_vf2(v: ^Vf2, t: ^Transform)
//~~fn Vf2.transformed
// Transforms a vf2 to another vf2.
// Order:
// 1. scale
// 2. rotation
// 3. position
//
// This allows conversion from a relative to an absolute vf2.
fn (v: ^Vf2) transformed*(t: Transform): Vf2 {
//~~
ret := v^
umth_transform_vf2(&ret, &t)
return ret
}
//~~type Quad
type Quad* = [4]Vf2
//~~
fn umth_quad_max(o: ^Vf2, q: ^Quad)
//~~fn Quad.getMax
// Gets the maximum coordinate.
fn (q: ^Quad) getMax*(): Vf2 {
//~~
o := Vf2{}
umth_quad_max(&o, q)
return o
}
fn umth_quad_min(o: ^Vf2, q: ^Quad)
//~~fn Quad.getMin
// Gets the minimum coordinate.
fn (q: ^Quad) getMin*(): Vf2 {
//~~
o := Vf2{}
umth_quad_min(&o, q)
return o
}
//~~fn Quad.getDims
// Returns the dimensions of the quad's bounding box
fn (q: ^Quad) getDims*(): Vf2 {
//~~
max := q[0]
min := q[0]
maxmag := max.mag()
minmag := min.mag()
for i:=1; i < len(q^); i++ {
qmag := q[i].mag()
if maxmag < qmag {
max = q[i]
maxmag = qmag
}
if minmag > qmag {
min = q[i]
minmag = qmag
}
}
return max.sub(min).abs()
}
fn umth_th_getglobal(): ^struct{}
//~~fn getGlobal
// returns a pointer to the th_global. Set this as your extensions thg.
fn getGlobal*(): ^struct{} {
//~~
return umth_th_getglobal()
}
fn umth_th_getfuncs(): ^struct{}
//~~fn getFuncs
// returns pointer to tophat functions. Pass this to th_ext_set.
fn getFuncs*(): ^struct{} {
//~~
return umth_th_getfuncs()
}
//~~var enableErrrors
// If true, errors will result in a call to exit(255), otherwise printf is used.
var enableErrors*: bool = true
//~~
fn __error*(msg: str) {
if enableErrors {
exit(255, msg)
} else {
printf("error: %s\n", msg)
}
}
//~~Color constants
const (
black* = 0xff
white* = 0xffffffff
red* = 0xff0000ff
green* = 0x00ff00ff
blue* = 0x0000ffff
yellow* = 0xffff00ff
magenta* = 0xff00ffff
cyan* = 0x00ffffff
)
//~~
//~~enum Platform
type Platform* = enum {
unknown
linux
windows
macOs
web
}
//~~
//~~Misc variables
var (
// time in ms from start of the game
time*: uint
// length of the last frame in ms
delta*: int
// platform tophat is running on
platform*: Platform
)
//~~