-
Notifications
You must be signed in to change notification settings - Fork 0
/
tween.jai
495 lines (392 loc) · 13 KB
/
tween.jai
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/// Immediate mode tweening
/*
Tweens are created by calling 'tween' in the update
loop. It returns true (if the tween has finished) and
a pointer to the Tween.
The first time 'tween' is called, a new tween will be
created and registered in the system.
Example:
x := 10;
y := 0;
while running {
dt := delta_time_in_seconds();
// Creates or updates a 3 second tween
// from x (10) to x + 10 (20) using
// the default easing (Quad, InOut).
if tween(dt, 3, *x, x + 10) {
print("X tween has finished!\n");
}
// Creates or updates a 10 second tween
// from y (0) to y + 10 (y) using Linear
// easing.
if tween(dt, 10, *y, y + 10, .Linear, .In) {
print("Y tween has finished!\n");
}
}
Tweens can be reset like so. Looping is just resetting
the tween after it has finished.
// ...
done, t := tween(dt, 2, *x, x + 10, .Quart, .In_Out);
if done {
// Reset and replay the tween next frame
reset(t);
}
To properly loop tweens without snapping the value back
to its initial position, 'reverse' can be called:
// ...
x := 10;
loops := 0;
done, t := tween(dt, 8, *x, x + 50);
if done {
loops += 1;
if loops < 10 {
reset(t); // Resets are explicit
reverse(t);
}
}
The above example will tween 'x' from its initial
position (10) to 60 after 8 seconds, then will tween
its final position (60) back to 10 (again after 8
seconds). This will smoothly loop the position back
and forth.
Tweens can be delayed with the 'delay' parameter:
// ...
if tween(dt, 1, *x, x + 10, delay = 5) {
print("This tween took 6 seconds to finish\n");
}
We can also tween structures. Any values in 'to' will
be applied to 'from'.
pos := Vector2.{ 10, 20 };
if tween(dt, *pos, .{ 100, 50 }) {
print("We've reached the right position!\n");
}
The underlying algorithm for will check the delta
between 'from' and 'to', and tween any that are
non-zero.
pos := Vector3.{ x = 50, y = 20, z = 80 };
if tween(dt, *pos, .{ x = 100, y = 200, z = pos.z }) {
// Only 'x' and 'y' were modified in 'pos'.
// 'z' was ignored because it had the same
// value in 'from' and 'to'
}
To sequence tweens:
pos := Vector2.{ 10, 20 };
if tween(dt, *pos, .{ x = 100 }) {
print("First move to the right\n");
if tween(dt, *pos, .{ y = 50 }) {
print("Then move down\n");
if tween(dt, *pos, .{ x = 0, y = 0 }) {
print("Then move back to 0, 0\n");
}
}
}
Because the library is meant to be lightweight,
sequencing works like this out of the box. However, we
can very easily improve things:
pos := Vector2.{ 10, 20 };
state: Tween_State;
if state == {
case .First;
if tween(dt, *pos, .{ x = 100 }) {
state = .Second;
}
case .Second;
if tween(dt, *pos, .{ y = 50 }) {
state = .Third;
}
case .Third;
if tween(dt, *pos, .{ x = 0, y = 0 }) {
print("All tweens have finished!\n");
}
}
To reset the entire sequence, we can cache the pointers
and reset them once our final tween has finished:
// ...
tweens: [Tween_State.Total_States]*Tween;
if state == {
// ...
case .Third;
done, t := tween(dt, *pos, .{ x = 0, y = 0, z = pos.z });
tweens[cast(int)state] = t;
if done {
print("All tweens have finished!\n");
state = .First;
for tweens reset(it);
}
}
A more advanced use case is to create tweens ahead of
time and update them based on some logic. We can do
this with 'make_tween' and 'update_tween':
// ...
sequence: [..]*Tween;
array_add(*sequence, make_tween(5, *pos, .{ x = 10, y = 20, z = pos.z }));
array_add(*sequence, make_tween(3, *pos, .{ x = 50, y = 80, z = pos.z }));
array_add(*sequence, make_tween(2, *pos, .{ x = 90, y = pos.y, z = pos.z }));
i := 0;
if state == {
case .First;
if update_tween(sequence[i], dt) {
i = i + 1 % sequence.count;
state = .Second;
}
// ...
case .Third;
if update_tween(sequence[i], dt) {
print("All tweens have finished!\n");
for tween: sequence {
reset(tween);
reverse(tween);
}
i = 0;
state = .First;
}
}
*/
Ease :: enum {
Linear;
Quad;
Cubic;
Quart;
Quint;
Expo;
Sine;
Circ;
Back;
Elastic;
}
Transition :: enum {
In;
Out;
In_Out;
}
Tween :: struct {
ease: Ease;
transition: Transition;
rate: float;
progress: float;
delay: float;
done: bool;
}
tween :: (dt: float, time_sec: float, from: *$T, to: T, ease := Ease.Quart, transition := Transition.In_Out, delay: float = 0, id: s64 = 0, loc := #caller_location) -> bool, *Tween {
hash := get_hash_for_tween(loc, id);
state := make_tween(time_sec, from, to, ease, transition, delay, id, loc);
return update_tween(state, dt), state;
}
make_tween :: (time_sec: float, from: *$T, to: T, ease := Ease.Quart, transition := Transition.In_Out, delay: float = 0, id: s64 = 0, loc := #caller_location) -> *Tween {
if !context.tween_manager.initialized init_tween_manager();
hash := get_hash_for_tween(loc, id);
state := find_or_create_tween(hash, time_sec, from, to, ease, transition, delay);
return state;
}
update_tween :: (tween: *Tween, dt: float) -> (done: bool) {
// Early out for null/deleted tweens
if !tween || !tween_is_managed(tween) return false;
if tween.done return true;
state := cast(*Tween_State)tween;
if state.delay > 0 {
state.delay -= dt;
return false;
}
state.progress += state.rate * dt;
if state.progress >= 1 {
state.done = true;
return true;
}
// The tween is still in progress
p := do_tween_step(state.ease, state.transition, state.progress);
for state.tweenable_values {
<<it.value = it.from + p * it.delta;
}
return false;
}
reset :: (tween: *Tween) {
if !tween return;
state := cast(*Tween_State)tween;
tween.done = false;
tween.delay = state.initial_delay;
tween.progress = 0;
}
reverse :: (tween: *Tween) {
if !tween return;
state := cast(*Tween_State)tween;
for * state.tweenable_values {
tmp := it.from;
it.from = it.to;
it.to = tmp;
it.delta = -it.delta;
}
}
delete :: (tween: *Tween) {
if !tween return;
removed: bool;
state := <<cast(*Tween_State)tween;
removed, state = table_remove(*context.tween_manager.tweens, state.hash);
if removed array_free(state.tweenable_values);
}
init_tween_manager :: (allocator := context.allocator) {
manager := *context.tween_manager;
manager.initialized = true;
manager.tweens.allocator = allocator;
}
deinit_tween_manager :: () {
manager := *context.tween_manager;
for manager.tweens array_free(it.tweenable_values);
deinit(*manager.tweens);
manager.initialized = false;
}
// Generate 'do_tween_step' from the Ease enum and EASING_FUNCTIONS array.
#insert -> string {
info := cast(*Type_Info_Enum)Ease;
b: String_Builder;
print_to_builder(*b, "do_tween_step :: (ease: Ease, transition: Transition, progress: float) -> float {\n");
append(*b, "\tp := progress;\n");
append(*b, "\tif ease == {\n");
for info.names {
func := EASING_FUNCTIONS[it_index];
print_to_builder(*b, #string END
case .%1; if transition == {
case .In;
return %2;
case .Out;
p = 1 - p;
return 1 - (%2);
case .In_Out;
p *= 2;
if p < 1 {
return 0.5 * (%2);
}
p = 2 - p;
return 0.5 * (1 - (%2)) + 0.5;
}
END, it, func);
}
append(*b, "\t}\n\treturn p;\n}\n");
return builder_to_string(*b);
}
#scope_file;
Tween_State :: struct {
#as using tween: Tween;
hash: u64;
initial_delay: float;
value: *void;
tweenable_values: [..]Tweenable;
Tweenable :: struct {
to: float;
from: float;
delta: float;
value: *float;
}
}
#add_context tween_manager: struct {
tweens: Table(u64, Tween_State);
initialized: bool;
};
tween_is_managed :: (tween: *Tween) -> bool {
state := cast(*Tween_State)tween;
_, exists := table_find(*context.tween_manager.tweens, state.hash);
return exists;
}
find_or_create_tween :: (hash: u64, time_sec: float, from: *$T, to: T, ease: Ease, transition: Transition, delay: float) -> *Tween_State
#modify {
info := cast(*Type_Info)T;
if info.type != .STRUCT && info.type != .FLOAT {
compiler_report(tprint("% is not a tweenable type!", T));
return false;
}
return true;
}
{
manager := *context.tween_manager.tweens;
tween := table_find_pointer(manager, hash);
if !tween {
rate: float;
if time_sec > 0 {
rate = 1 / time_sec;
}
tween = table_add(manager, hash, .{
hash = hash,
ease = ease,
transition = transition,
rate = rate,
value = from,
delay = delay,
initial_delay = delay,
});
// Calculate deltas and add any non-zero to tweenable_values.
// This is an array of values we should update every frame.
// @Todo(Judah): Pre-calculate deltas and call array_reserve?
tween.tweenable_values.allocator = context.tween_manager.tweens.allocator;
#insert -> string {
member_is_tweenable :: (info: Type_Info_Struct_Member) -> bool {
return info.type.type == .FLOAT && info.offset_into_constant_storage == -1;
}
b: String_Builder;
info := cast(*Type_Info)T;
current_offset := -1;
if info.type == .STRUCT {
sinfo := cast(*Type_Info_Struct)info;
for sinfo.members if member_is_tweenable(it) {
// Don't try to tween any #place'd members.
if it.offset_in_bytes <= current_offset continue;
current_offset = it.offset_in_bytes;
print_to_builder(*b, #string END
delta_%1 := to.%1 - from.%1;
if delta_%1 != 0 {
array_add(*tween.tweenable_values, .{
to = to.%1,
from = from.%1,
delta = delta_%1,
value = *from.%1,
});
}
END, it.name);
}
}
else if info.type == .FLOAT {
print_to_builder(*b, #string END
delta := to - <<from;
if delta != 0 {
array_add(*tween.tweenable_values, .{
to = to,
from = <<from,
delta = delta,
value = from,
});
}
END);
}
return builder_to_string(*b);
}
}
return tween;
}
EASING_FUNCTIONS :: string.[
"p", // Linear
"p * p", // Quad
"p * p * p", // Cubic
"p * p * p * p", // Quart
"p * p * p * p * p", // Quint
"pow(2, 10 * (p - 1))", // Expo
"-cos(p * (PI * 0.5)) + 1", // Sine
"-(sqrt(1 - (p * p)) - 1)", // Circ
"p * p * (2.7 * p - 1.7)", // Back
"-pow(2, 10 * p - 10) * sin((p * 10 - 10.75) * ((2 * PI) / 3))", // Elastic
];
// Yoinked from GetRect
KNUTH_GOLDEN_RATIO_64 :: 11400714819323198485;
get_hash_for_tween :: (loc: Source_Code_Location, identifier: s64) -> u64 {
Hash :: #import "Hash";
// Maybe we should hash the filename pointer in order to better prevent collisions.
name_hash := Hash.djb2_hash(loc.fully_pathed_filename);
line_hash := Hash.knuth_hash(cast,no_check(u64) loc.line_number);
identifier_hash := Hash.knuth_hash(cast,no_check(u64) identifier);
result := combine_hashes(name_hash, combine_hashes(line_hash, identifier_hash));
return result;
}
combine_hashes :: (a: u64, b: u64) -> u64 {
return a * KNUTH_GOLDEN_RATIO_64 + b;
}
#import "Math";
#import "Basic";
#import "Compiler";
#import "Hash_Table";