-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_graph.cpp
110 lines (90 loc) · 2.91 KB
/
function_graph.cpp
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
#include <fan/pch.h>
static constexpr f32_t zoom_sensitivity = 1.2;
static constexpr f32_t amplitude = 1;
static constexpr f32_t wavelength = 10;
struct global_t {
loco_t loco;
static constexpr f32_t samples = 100;
f32_t zoom = 1;
bool move = false;
fan::vec2 pos = gloco->camera_get_position(gloco->orthographic_camera.camera);
fan::vec2 offset = gloco->get_mouse_position();
}global;
f64_t f(f64_t x) {
return exp(x);
}
f64_t f2(f64_t x) {
return cos(x);
}
void handle_zoom_and_move() {
gloco->window.add_buttons_callback([&](const auto& d) {
auto update_zoom = [] {
auto window_size = gloco->window.get_size();
gloco->camera_set_ortho(gloco->orthographic_camera.camera,
fan::vec2(-window_size.x, window_size.x) / (global.zoom),
fan::vec2(-window_size.y, window_size.y) / (global.zoom)
);
};
switch (d.button) {
case fan::mouse_middle: {
global.move = (bool)d.state;
global.pos = gloco->camera_get_position(gloco->orthographic_camera.camera);
global.offset = gloco->get_mouse_position();
break;
}
case fan::mouse_scroll_up: {
global.zoom *= zoom_sensitivity;
update_zoom();
break;
}
case fan::mouse_scroll_down: {
global.zoom /= zoom_sensitivity;
update_zoom();
break;
}
};
});
}
int main() {
fan::vec2 window_size = global.loco.window.get_size();
gloco->camera_set_ortho(gloco->orthographic_camera.camera,
fan::vec2(-window_size.x, window_size.x),
fan::vec2(-window_size.y, window_size.y)
);
handle_zoom_and_move();
global.loco.window.add_mouse_move_callback([&](const auto& d) {
if (global.move) {
gloco->camera_set_position(gloco->orthographic_camera.camera, global.pos - (d.position - global.offset) / global.zoom * 2);
}
});
std::vector<loco_t::shape_t> lines(global.samples, fan::graphics::line_t{{
.src = fan::vec2(),
.dst = fan::vec2(),
.color = fan::colors::white
}});
std::vector<loco_t::shape_t> lines2(global.samples, fan::graphics::line_t{{
.src = fan::vec2(),
.dst = fan::vec2(),
.color = fan::colors::white
}});
f32_t divider = global.samples / 10.f;
static auto generate_line = [&](auto& line, auto f, int line_idx, f32_t x) {
fan::vec2 src = f(x / divider) * amplitude * global.samples;
fan::vec2 dst = f((x + 1.f) / divider) * amplitude * global.samples;
src.x = line_idx * wavelength;
dst.x = (line_idx + 1) * wavelength;
line.set_line(src, dst);
};
//for (f32_t i = 0; i < global.samples; i += 1) {
// generate_line(lines[i], i, (i));
//}
f32_t animator_index = 0;
global.loco.loop([&] {
for (f32_t i = 0; i < global.samples; i += 1) {
generate_line(lines[i], f, i, (i + animator_index));
generate_line(lines2[i], f2, i, (i + animator_index));
}
animator_index -= global.loco.delta_time * 20;
});
return 0;
}