summaryrefslogtreecommitdiff
path: root/src/main.odin
blob: 11cea8ec3bd144fed86cf162e92d3eb7fc78551c (plain)
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
package main

import rl "vendor:raylib"
import "core:fmt"
import "core:math"
import "core:os"
import "core:strconv"
import "core:reflect"
import "core:strings"

MONO_FONT :: #load("../res/RedHatMono.ttf")
font : rl.Font

BLACK  : rl.Color = {16,  16,  16, 255}
YELLOW : rl.Color = {254, 175, 1,  255}
ORANGE : rl.Color = {238, 123, 26, 255}
RED    : rl.Color = {222, 73,  50, 255}

LERP_SPEED :: 0.10
HIGHLIGHT_THICKNESS :: 4

layout :: struct {
	wave_a_start : rl.Vector2,
	wave_a_end   : rl.Vector2,
	wave_a_amp : f32,
	
	wave_b_start : rl.Vector2,
	wave_b_end   : rl.Vector2,
	wave_b_amp : f32,
	
	grid_opacity : f32,
	grid_data_opacity : f32,
	highlight_opacity : f32,
	highlight_index : int,
	highlight_position : rl.Vector2,
	highlight_dimensions : rl.Vector2,
	arrow_thickness : f32,
	arrow_chopping : f32,
	heatmap_opacity : f32,
	
	billing : f32,
	
	camera_position : rl.Vector2,
	camera_zoom : f32,
	
	grid_start : rl.Vector2
	grid_end   : rl.Vector2
}

waveform_a := [?]f32{0.0, 0.1, 0.5, 0.3, 0.1, -0.1,  0.0, 0.0, 0.0, 0.1, 0.3, 0.0}
waveform_b := [?]f32{0.0, 0.0, 0.0, 0.2, 0.4,  0.2, -0.1, 0.0, 0.1, 0.3, 0.1, 0.0}

data_frames : [dynamic][len(waveform_a)*len(waveform_b)]cell



slides : [dynamic]layout
state : layout
target : layout
data_state : [len(waveform_a)*len(waveform_b)]cell
data_target : [len(waveform_a)*len(waveform_b)]cell
delta : f32




init_slides :: proc() {
	// ---------------------------- Waveforms
	c : layout = {
		wave_a_start = {-600, -200},
		wave_a_end   = {600,  -200},
		wave_a_amp = 260,
		
		wave_b_start = {-600, 200},
		wave_b_end   = {600,  200},
		wave_b_amp = 260,
		
		grid_opacity = 0,
		grid_data_opacity = 0,
		highlight_opacity = 0,
		highlight_position = 0,
		arrow_thickness = 3,
		arrow_chopping = 0.3,
		
		heatmap_opacity = 0.5,
		
		billing = f32(len(waveform_a)),
		
		grid_start = {-400,  350},
		grid_end   = { 400, -450},
	}
	d : [12*12]cell
	highlight_rect := rect_from_index(0, HIGHLIGHT_THICKNESS)
	c.highlight_dimensions = {highlight_rect.width, highlight_rect.height}
	c.highlight_position = {highlight_rect.x, highlight_rect.y}
	
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Move waveforms into place
	c.wave_a_start = {grid_start.x,  grid_start.y+120}
	c.wave_a_end   = {grid_end.x  ,  grid_start.y+120}
	c.wave_b_start = {grid_start.x-120, grid_start.y}
	c.wave_b_end   = {grid_start.x-120, grid_end.y}
	c.wave_a_amp = 120
	c.wave_b_amp = 130
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Show grid
	c.grid_opacity = 1
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Show data numbers
	c.grid_data_opacity = 1
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Infinity coating
	for _, i in waveform_a {
		if i==0 do continue
		d[i_from_xy(i, 0)].cost = math.INF_F32
		d[i_from_xy(0, i)].cost = math.INF_F32
	}
	append(&slides, c)
	append(&data_frames, d)
	
	
	
	// ---------------------------- FIRST CELL
	d[i_from_xy(1,1)].cost = distance(waveform_a[1], waveform_b[1])
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Detecting minimum direction for first cost calculation
	d[i_from_xy(1,1)].direction = .DIAGONAL
	append(&slides, c)
	append(&data_frames, d)
	
	
	
	// ---------------------------- SECOND CELL
	d[i_from_xy(2,1)].cost = distance(waveform_a[2], waveform_b[1])
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Detecting minimum direction for second cost calculation
	
	d[i_from_xy(2,1)].direction = .LEFT
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Adding previous minimum to second cost calculation
	d[i_from_xy(2,1)].cost += d[i_from_xy(1,1)].cost
	append(&slides, c)
	append(&data_frames, d)
	
	
	
	// ---------------------------- THIRD CELL
	d[i_from_xy(1,2)].cost += distance(waveform_a[1], waveform_b[2])
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Detect minimum
	d[i_from_xy(1,2)].direction = .DOWN
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Add minimum
	d[i_from_xy(1,2)].cost += d[i_from_xy(1,1)].cost
	append(&slides, c)
	append(&data_frames, d)
	
	
	
	// ---------------------------- FOURTH CELL
	d[i_from_xy(2,2)].cost += distance(waveform_a[2], waveform_b[2])
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Detect minimum
	a_cost, a_direction := three_min(d[i_from_xy(1,2)],
	                                                d[i_from_xy(1,1)],
	                                                d[i_from_xy(2,1)])
	d[i_from_xy(2,2)].cost += a_cost
	d[i_from_xy(2,2)].direction = a_direction
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- FIFTH CELL
	full_calc(d[:], 1, 3)
	append(&slides, c)
	append(&data_frames, d)
	
	
	// ---------------------------- SIXTH CELL
	full_calc(d[:], 3, 1)
	append(&slides, c)
	append(&data_frames, d)
	
	
	// ---------------------------- WAVEFRONT first half
	for i in 4..<len(waveform_a) {
		coord : [2]int = {i, 1}
		for {
			full_calc(d[:], coord.x, coord.y)
			if coord.x==1 do break
			coord += {-1, 1}
		}
		append(&slides, c)
		append(&data_frames, d)
	}
	// ---------------------------- WAVEFRONT second half
	for i in 2..<len(waveform_a) {
		coord : [2]int = {len(waveform_a)-1, i}
		for {
			full_calc(d[:], coord.x, coord.y)
			if coord.y==len(waveform_b)-1 do break
			coord += {-1, 1}
		}
		append(&slides, c)
		append(&data_frames, d)
	}
	
	// ---------------------------- Disregard the numbers
	c.grid_data_opacity = 0
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Follow the arrows
	c.arrow_thickness = 6
	c.arrow_chopping = 0.25
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Pathfollowing
	coord : [2]int = {len(waveform_a)-1, len(waveform_b)-1}
	for {
		d[i_from_xy(coord.x, coord.y)].stepped_in = 1
		#partial switch d[i_from_xy(coord.x, coord.y)].direction {
			case .LEFT:
				coord -= {1, 0}
			case .DIAGONAL:
				coord -= {1, 1}
			case .DOWN:
				coord -= {0, 1}
		}
		append(&slides, c)
		append(&data_frames, d)
		if coord == {0,0} do break
	}
	
	// ---------------------------- Final step
	d[0].stepped_in = 1
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- Look at the pattern
	c.arrow_thickness = 0
	c.arrow_chopping = 0.49
	c.heatmap_opacity = 1
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- 
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- 
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- 
	append(&slides, c)
	append(&data_frames, d)
	
	// ---------------------------- 
	append(&slides, c)
	append(&data_frames, d)
}

rect_from_index :: proc(i : int, thickness : f32 = 0) -> rl.Rectangle {
	// rect is top left corener and dimensions
	width  := abs(grid_end.x - grid_start.x)
	height := abs(grid_end.y - grid_start.y)
	intergrid_dimensions : rl.Vector2 = {width, height}
	cell_dimensions := intergrid_dimensions/f32(len(waveform_a)-1)
	position_zero := grid_start-(cell_dimensions/2)
	output := position_zero + cell_dimensions * {f32(i%len(waveform_a)), -f32(i/len(waveform_b))}
	thickness_offset := thickness/2
	return {output.x-thickness_offset, output.y-thickness_offset, cell_dimensions.x+thickness_offset, cell_dimensions.y+thickness_offset}
}

i_from_xy :: proc(x : int, y : int) -> int {
	return y * len(waveform_a) + x
}

lerp :: proc() {
	layout_info := type_info_of(layout)
	
	// Handle the named type wrapper
	actual_info := layout_info
	if named_info, ok := layout_info.variant.(reflect.Type_Info_Named); ok {
		actual_info = named_info.base
	}
	
	// Now cast to struct
	struct_info := actual_info.variant.(reflect.Type_Info_Struct)
	
	for i in 0..<struct_info.field_count {
		field_type := struct_info.types[i]
		field_offset := struct_info.offsets[i]
		
		// Get pointers to the field in both global structs
		state_ptr := rawptr(uintptr(&state) + field_offset)
		target_ptr := rawptr(uintptr(&target) + field_offset)
		
		// Type switch to lerp and write directly back to state
		switch field_type.id {
			case typeid_of(f32):
				from_val := (cast(^f32)state_ptr)^
				to_val := (cast(^f32)target_ptr)^
				(cast(^f32)state_ptr)^ = from_val + (to_val - from_val) * (1.0 - math.pow(LERP_SPEED, delta))
			
			case typeid_of(rl.Vector2):
				from_val := (cast(^rl.Vector2)state_ptr)^
				to_val := (cast(^rl.Vector2)target_ptr)^
				(cast(^rl.Vector2)state_ptr)^ = from_val + (to_val - from_val) * (1.0 - math.pow(LERP_SPEED, delta))
		}
	}
	
	for cell, i in data_state {
		
		from_val := data_state[i].cost
		to_val  := data_target[i].cost
		if data_target[i].cost != math.INF_F32 && data_state[i].cost != math.nan_f32() {
			data_state[i].cost = from_val + (to_val - from_val) * (1.0 - math.pow(LERP_SPEED, delta)*0.9)
		} else {
			data_state[i].cost = data_target[i].cost
		}
		
		from_val = data_state[i].stepped_in
		to_val  = data_target[i].stepped_in
		data_state[i].stepped_in = from_val + (to_val - from_val) * (1.0 - math.pow(LERP_SPEED, delta))
		
		data_state[i].direction = data_target[i].direction
	}
}

normalize :: proc(v : rl.Vector2) -> rl.Vector2 {
	return v / math.sqrt((v.x*v.x) + (v.y*v.y))
}

draw_waveform :: proc(wave : []f32, start : rl.Vector2, end : rl.Vector2, amp : f32, name : cstring) {
	samples := len(wave)
	
	lines := samples-1
	rl.DrawLineEx(start, end, 2, rl.DARKGRAY)
	total_direction := end - start
	step := total_direction / f32(lines)
	perpendicular : rl.Vector2 = normalize({total_direction.y, -total_direction.x})
	
	end_pos : rl.Vector2
	for value, i in wave[:lines] {
		start_base := start + (step*f32(i))
		  end_base := start + (step*f32(i+1))
		start_pos : rl.Vector2 = start_base + (perpendicular*wave[i]*amp)
		  end_pos              =   end_base + (perpendicular*wave[i+1]*amp)
		rl.DrawLineEx(start_pos, end_pos, 4, ORANGE)
		rl.DrawCircleV(start_pos, 5, YELLOW)
		
		// Text on each point
		point_font_size :: 24
		end_value := wave[i+1]
		end_text := fmt.caprintf("%.1f", end_value, allocator = context.temp_allocator)
		value_pos := (end_base + (perpendicular*(wave[i+1]*amp-25))) - (rl.MeasureTextEx(font, end_text, point_font_size, 0)*0.5)
		rl.DrawTextEx(font, end_text, value_pos, point_font_size, 0, rl.GRAY)
	}
	rl.DrawCircleV(end_pos, 5, YELLOW)
	
	rl.DrawTextEx(font, name, start, 32, 0, rl.LIGHTGRAY)
}

draw_grid :: proc(first : rl.Vector2, last : rl.Vector2, data : []cell) {
	highest_cost : f32 = 0
	for c in data {
		if c.cost > highest_cost && c.cost != math.INF_F32 {
			highest_cost = c.cost
		}
	}
	color := rl.DARKGRAY
	color.a = u8(255.0*state.grid_opacity)
	text_color := rl.WHITE
	text_color.a = u8(255.0*state.grid_data_opacity)
	cost_font_size :: 28
	for cell, i in data {
		
		// Cell bg color
		cell_bg := RED
		cell_bg.a = u8(255*cell.cost/highest_cost*state.heatmap_opacity)
		if cell.cost == math.INF_F32 do cell_bg = YELLOW
		rect := rect_from_index(i, 2)
		rl.DrawRectangleRec(rect, cell_bg)
		
		// Stepped in
		stepped_bg := rl.WHITE
		stepped_bg.a = u8(255*cell.stepped_in*0.8)
		rl.DrawRectangleRec(rect, stepped_bg)
		
		// Outline
		rl.DrawRectangleLinesEx(rect, 2*state.grid_opacity, color)
		
		// Cost value text
		cost_string := fmt.caprintf("%.2f", cell.cost, allocator = context.temp_allocator)
		text_dim := rl.MeasureTextEx(font, cost_string, cost_font_size, 0)
		text_draw_point := rl.Vector2{rect.x, rect.y}+(({rect.width, rect.height}-text_dim)*0.5)
		rl.DrawTextEx(font, fmt.ctprintf("%.2f", cell.cost), text_draw_point, cost_font_size, 0, text_color)
		
		if cell.direction != .NONE {
			target_point : rl.Vector2
			#partial switch cell.direction {
				case .LEFT:
					target_point = middle_of_rect(rect_from_index(i-1))
				case .DIAGONAL:
					target_point = middle_of_rect(rect_from_index(i-len(waveform_a)-1))
				case .DOWN:
					target_point = middle_of_rect(rect_from_index(i-len(waveform_a)))
			}
			arrow_start := middle_of_rect(rect)
			arrow_end := target_point
			diff := arrow_end - arrow_start
			arrow_start += diff*state.arrow_chopping
			arrow_end -= diff*state.arrow_chopping
			draw_arrow(arrow_start, arrow_end)
		}
	}
}

middle_of_rect :: proc(rect : rl.Rectangle) -> rl.Vector2 {
	return {rect.x+(rect.width*0.5), rect.y+(rect.height*0.5)}
}

draw_arrow :: proc(start, end : rl.Vector2) {
	thickness := state.arrow_thickness
	outline := thickness*0.8
	
	total_direction := end - start
	perpendicular : rl.Vector2 = normalize({total_direction.y, -total_direction.x})
	reverse : rl.Vector2 = normalize({-total_direction.x, -total_direction.y})
	
	right_hand : rl.Vector2 = end + (reverse*10) + (perpendicular*-10)
	left_hand  : rl.Vector2 = end + (reverse*10) + (perpendicular*10)
	
	rl.DrawLineEx(start, end, thickness+outline, BLACK)
	rl.DrawLineEx(end, right_hand, thickness+outline, BLACK)
	rl.DrawLineEx(end, left_hand, thickness+outline, BLACK)
	
	rl.DrawCircleV(start,      (thickness+outline)*0.5, BLACK)
	rl.DrawCircleV(end,        (thickness+outline)*0.5, BLACK)
	rl.DrawCircleV(right_hand, (thickness+outline)*0.5, BLACK)
	rl.DrawCircleV(left_hand,  (thickness+outline)*0.5, BLACK)
	
	
	rl.DrawLineEx(start, end, thickness, ORANGE)
	rl.DrawLineEx(end,   right_hand, thickness, ORANGE)
	rl.DrawLineEx(end,   left_hand, thickness, ORANGE)
	
	rl.DrawCircleV(start, thickness*0.5, ORANGE)
	rl.DrawCircleV(end, thickness*0.5, ORANGE)
	rl.DrawCircleV(right_hand, thickness*0.5, ORANGE)
	rl.DrawCircleV(left_hand,  thickness*0.5, ORANGE)
	
}

main :: proc() {
	
	// Initialization
	//--------------------------------------------------------------------------------------
	
	slide : int = 0
	if len(os.args) > 1 {
		slide, _ = strconv.parse_int(os.args[1])
	}

	rotation : f32 = 0.0

	cameraX : f32 = 0.0
	cameraY : f32 = 0.0
	camera : rl.Camera2D = {
		zoom=1
	}
	
	init_slides()
	
	rl.SetConfigFlags({.WINDOW_RESIZABLE})
	rl.InitWindow(1920, 1080, "BSC 2025 Presentation")
	rl.SetTargetFPS(60)
	rl.SetExitKey(nil)
	
	camera.offset = {f32(rl.GetScreenWidth()) / 2, f32(rl.GetScreenHeight()) / 2}
	camera.zoom = f32(rl.GetScreenHeight())/1080
	
	font = rl.LoadFontFromMemory(".ttf", raw_data(MONO_FONT), i32(len(MONO_FONT)), 128, nil, 0)
	//rl.ToggleBorderlessWindowed()
	
	for !rl.WindowShouldClose() {
		delta = rl.GetFrameTime()
		if rl.IsWindowResized() {
			height := f32(rl.GetScreenHeight())
			width  := f32(rl.GetScreenWidth())
			camera.offset = {width / 2, height / 2}
			camera.zoom = height/1080
		}
		// Input
		//----------------------------------------------------------------------------------
		go_forward := false
		go_back := false
		mousePosition := rl.GetMousePosition()
		left_clicked := rl.IsMouseButtonReleased(rl.MouseButton(0))
		right_clicked := rl.IsMouseButtonReleased(rl.MouseButton(1))
		
		go_forward = left_clicked || rl.IsKeyReleased(.RIGHT) || rl.IsKeyReleased(.PAGE_DOWN)
		go_back = right_clicked || rl.IsKeyReleased(.LEFT) || rl.IsKeyReleased(.PAGE_UP)
		
		// Process
		//----------------------------------------------------------------------------------
		
		if go_forward && slide < len(slides)-1 {
			slide += 1
			fmt.printfln("Forward! To slide #{}", slide)
			rect_from_index(1)
		} else if go_back && slide > 0 {
			slide -= 1
			fmt.printfln("Back! To slide #{}", slide)
		}
		
		target = slides[slide]
		data_target = data_frames[slide]
		lerp()
		
		// Draw
		//----------------------------------------------------------------------------------
		rl.BeginDrawing()
			rl.ClearBackground(BLACK)
			
			
			rl.BeginMode2D(camera)
			// World-space drawing
			
			
			draw_waveform(waveform_a[:], state.wave_a_start, state.wave_a_end, state.wave_a_amp, "Boom")
			draw_waveform(waveform_b[:], state.wave_b_start, state.wave_b_end, state.wave_b_amp, "Lav")
			draw_grid(grid_start, grid_end, data_state[:])
			highlight_color := YELLOW
			highlight_color.a = u8(255*state.highlight_opacity)
			rl.DrawRectangleLinesEx({state.highlight_position.x,
			                         state.highlight_position.y,
			                         state.highlight_dimensions.x,
			                         state.highlight_dimensions.y}, HIGHLIGHT_THICKNESS, highlight_color)
			
			
			rl.EndMode2D()
			// Screen-space drawing
			
			rl.DrawFPS(rl.GetScreenWidth() - 95, 10)
			
		rl.EndDrawing()
		//----------------------------------------------------------------------------------
		free_all(context.temp_allocator)
	}

	rl.CloseWindow()
}

distance :: proc(a, b : f32) -> f32 {
	//return (a - b)*(a - b);
	return math.abs(a - b);
}
three_min :: proc(left, diagonal, down : cell) -> (f32, DIRECTION) {
	if (left.cost <= diagonal.cost && left.cost <= down.cost) do return left.cost, .LEFT
	if (down.cost <= diagonal.cost && down.cost <= left.cost) do return down.cost, .DOWN
	return diagonal.cost, .DIAGONAL;
}
full_calc :: proc(d: []cell, x, y : int) {
	d[i_from_xy(x,y)].cost = distance(waveform_a[x], waveform_b[y])
	a_cost, a_direction := three_min(d[i_from_xy(x-1, y )],
	                                 d[i_from_xy(x-1, y-1)],
	                                 d[i_from_xy(x  , y-1)])
	d[i_from_xy(x,y)].cost += a_cost
	d[i_from_xy(x,y)].direction = a_direction
}

cell :: struct {
	cost : f32,
	direction : DIRECTION,
	stepped_in : f32,
}
DIRECTION :: enum {
	NONE,
	LEFT,
	DIAGONAL,
	DOWN,
}