-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample.cpp
More file actions
271 lines (203 loc) · 7.34 KB
/
example.cpp
File metadata and controls
271 lines (203 loc) · 7.34 KB
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
#include "core/application.h"
#include <vector>
#include <cmath>
#include "hardware/display.h"
#include "hardware/battery.h"
constexpr size_t initial_balls = 25;
struct ball
{
lv_obj_t *obj_handle;
struct
{
float x;
float y;
} position;
struct
{
float x;
float y;
} velocity;
};
class example : public application
{
public:
example() : m_width(hardware::display::get().width()),
m_height(hardware::display::get().height()),
m_group(lv_group_create()),
m_screen(lv_scr_act())
{
lv_indev_t *indev = nullptr;
while ((indev = lv_indev_get_next(indev)))
if (lv_indev_get_type(indev) == LV_INDEV_TYPE_KEYPAD)
lv_indev_set_group(indev, m_group);
lv_group_add_obj(m_group, m_screen);
auto on_key = [](lv_event_t *e)
{
auto app = static_cast<example *>(lv_event_get_user_data(e));
auto key = lv_event_get_key(e);
switch (key)
{
case LV_KEY_UP:
app->add_ball();
break;
case LV_KEY_DOWN:
app->remove_ball();
break;
case LV_KEY_ENTER:
app->reset_balls();
break;
default:
break;
}
};
lv_obj_add_event_cb(m_screen, on_key, LV_EVENT_KEY, this);
}
~example()
{
while (m_balls.size())
remove_ball();
lv_group_delete(m_group);
}
private:
void on_create() override
{
lv_obj_remove_flag(m_screen, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_style_bg_color(m_screen, lv_color_black(), LV_STATE_DEFAULT);
m_battery_voltage = lv_label_create(lv_layer_top());
lv_obj_set_style_text_color(m_battery_voltage, lv_color_white(), LV_STATE_DEFAULT);
lv_obj_align(m_battery_voltage, LV_ALIGN_BOTTOM_LEFT, 4, -22);
m_ball_count = lv_label_create(lv_layer_top());
lv_obj_set_style_text_color(m_ball_count, lv_color_white(), LV_STATE_DEFAULT);
lv_obj_align(m_ball_count, LV_ALIGN_BOTTOM_LEFT, 4, -4);
m_balls.reserve(initial_balls);
reset_balls();
for (size_t i = 0; i < 10; i++)
m_voltage_level = 0.9f * m_voltage_level + 0.1f * hardware::battery::get().voltage_level();
auto hud_update = [](lv_timer_t *timer)
{
static_cast<example *>(lv_timer_get_user_data(timer))->update_hud();
};
lv_timer_create(hud_update, 200, this);
}
void on_update(float timestep) override
{
const auto min_x = 15;
const auto min_y = 15;
const auto max_x = m_width - 15;
const auto max_y = m_height - 15;
for (const auto ball : m_balls)
{
const auto flip_vx = (ball->position.x < min_x && ball->velocity.x < 0) || (ball->position.x > max_x && ball->velocity.x > 0);
const auto flip_vy = (ball->position.y < min_y && ball->velocity.y < 0) || (ball->position.y > max_y && ball->velocity.y > 0);
ball->velocity.x = flip_vx ? -ball->velocity.x : ball->velocity.x;
ball->velocity.y = flip_vy ? -ball->velocity.y : ball->velocity.y;
for (const auto b : m_balls)
{
if (b == ball)
continue;
const auto dx = ball->position.x - b->position.x;
const auto dy = ball->position.y - b->position.y;
if ((dx * dx + dy * dy) < 900)
resolve_collision(*b, *ball);
}
ball->position.x += ball->velocity.x * timestep;
ball->position.y += ball->velocity.y * timestep;
lv_obj_set_pos(ball->obj_handle, ball->position.x - 15, ball->position.y - 15);
}
}
void add_ball()
{
auto b = static_cast<ball *>(lv_malloc(sizeof(ball)));
b->obj_handle = lv_image_create(m_screen);
b->position.x = (m_width / 2);
b->position.y = (m_height / 2);
b->velocity.x = lv_rand(50, 150);
b->velocity.y = lv_rand(50, 150);
if (lv_rand(0, 1))
b->velocity.x = -b->velocity.x;
if (lv_rand(0, 1))
b->velocity.y = -b->velocity.y;
lv_obj_set_size(b->obj_handle, 30, 30);
lv_obj_set_pos(b->obj_handle, b->position.x - 15, b->position.y - 15);
lv_obj_set_style_radius(b->obj_handle, 15, LV_STATE_DEFAULT);
lv_obj_set_style_border_width(b->obj_handle, 0, LV_STATE_DEFAULT);
char path[] = "F:/balls/ball_0.png";
path[14] = '0' + lv_rand(0, 7);
lv_image_set_src(b->obj_handle, path);
m_balls.push_back(b);
}
void remove_ball()
{
if (!m_balls.size())
return;
auto b = m_balls.back();
m_balls.pop_back();
lv_obj_delete(b->obj_handle);
lv_free(b);
}
void reset_balls()
{
if (m_timer)
return;
auto timer_cb = [](lv_timer_t *timer)
{
auto app = static_cast<example *>(lv_timer_get_user_data(timer));
if (app->m_balls.size() == initial_balls)
{
lv_timer_delete(app->m_timer);
app->m_timer = nullptr;
return;
}
if (app->m_balls.size() < initial_balls)
app->add_ball();
else
app->remove_ball();
};
m_timer = lv_timer_create(timer_cb, 100, this);
}
void update_hud()
{
m_voltage_level = 0.9f * m_voltage_level + 0.1f * hardware::battery::get().voltage_level();
lv_label_set_text_fmt(m_battery_voltage, "Battery: %lumv", m_voltage_level);
lv_label_set_text_fmt(m_ball_count, "Balls: %zu", m_balls.size());
}
void resolve_collision(ball &b1, ball &b2)
{
const float dx = b2.position.x - b1.position.x;
const float dy = b2.position.y - b1.position.y;
const float distance = std::sqrt(dx * dx + dy * dy);
const float penetration_depth = (15 + 15) - distance;
const float normal_x = dx / distance;
const float normal_y = dy / distance;
const float resolution_distance = penetration_depth / 2;
b1.position.x -= normal_x * resolution_distance;
b1.position.y -= normal_y * resolution_distance;
b2.position.x += normal_x * resolution_distance;
b2.position.y += normal_y * resolution_distance;
const float relative_vx = b2.velocity.x - b1.velocity.x;
const float relative_vy = b2.velocity.y - b1.velocity.y;
const float v_along_normal = relative_vx * normal_x + relative_vy * normal_y;
if (v_along_normal > 0)
return;
const float j = -(1 + 0.99f) * v_along_normal / 2;
const float impulse_x = j * normal_x;
const float impulse_y = j * normal_y;
b1.velocity.x -= impulse_x;
b1.velocity.y -= impulse_y;
b2.velocity.x += impulse_x;
b2.velocity.y += impulse_y;
}
const uint16_t m_width;
const uint16_t m_height;
lv_group_t *m_group;
lv_obj_t *m_screen;
lv_obj_t *m_battery_voltage;
lv_obj_t *m_ball_count;
lv_timer_t *m_timer = nullptr;
std::vector<ball *> m_balls;
uint32_t m_voltage_level = 0;
};
application *create_application()
{
return new example();
}