-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
UIcode.cpp
129 lines (113 loc) · 4.16 KB
/
UIcode.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#define GUILITE_ON //Do not define this macro once more!!!
#include "GuiLite.h"
#include <math.h>
#define UI_WIDTH 240
#define UI_HEIGHT 320
#define SQUARE_SIZE_1 10
#define SQUARE_SIZE_2 14
#define PI 3.14159
static c_surface* s_surface;
static c_display* s_display;
static int s_mode;
namespace pendulum
{
int r1 = 90;
int r2 = 90;
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
float m1 = 25;
float m2 = 50;
float a1 = PI / 2;
float a2 = PI / 2;
float a1_v = 0;
float a2_v = 0;
float g = 0.5;
void draw(int x, int y, const char* text1 = 0, const char* text2 = 0)
{
//caculate acceleration
float num1 = -g * (2 * m1 + m2) * sin(a1);
float num2 = -m2 * g * sin(a1 - 2 * a2);
float num3 = -2 * sin(a1 - a2) * m2;
float num4 = a2_v * a2_v * r2 + a1_v * a1_v * r1 * cos(a1 - a2);
float den = r1 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2));
float a1_a = (num1 + num2 + num3 * num4) / den;
num1 = 2 * sin(a1 - a2);
num2 = (a1_v * a1_v * r1 * (m1 + m2));
num3 = g * (m1 + m2) * cos(a1);
num4 = a2_v * a2_v * r2 * m2 * cos(a1 - a2);
den = r2 * (2 * m1 + m2 - m2 * cos(2 * a1 - 2 * a2));
float a2_a = (num1 * (num2 + num3 + num4)) / den;
a1_v += a1_a;
a2_v += a2_a;
a1 += a1_v;
a2 += a2_v;
//earse footprint
s_surface->draw_line(x, y, x1 + x, y1 + y, 0, Z_ORDER_LEVEL_0);
s_surface->draw_line(x1 + x, y1 + y, x2 + x, y2 + y, 0, Z_ORDER_LEVEL_0);
if (text1 && text2)
{
c_word::draw_string(s_surface, Z_ORDER_LEVEL_0, text1, x1 + x - 14, y1 + y - 10, c_theme::get_font(FONT_DEFAULT), 0, 0);
c_word::draw_string(s_surface, Z_ORDER_LEVEL_0, text2, x2 + x - 18, y2 + y - 10, c_theme::get_font(FONT_DEFAULT), 0, 0);
}
else
{
s_surface->draw_rect(c_rect(x1 + x - SQUARE_SIZE_1 / 2, y1 + y - SQUARE_SIZE_1 / 2, SQUARE_SIZE_1, SQUARE_SIZE_1), 0, 1, Z_ORDER_LEVEL_0);
s_surface->draw_rect(c_rect(x2 + x - SQUARE_SIZE_2 / 2, y2 + y - SQUARE_SIZE_2 / 2, SQUARE_SIZE_2, SQUARE_SIZE_2), 0, 1, Z_ORDER_LEVEL_0);
}
//draw new picture
x1 = r1 * sin(a1);
y1 = r1 * cos(a1);
x2 = r2 * sin(a2) + x1;
y2 = r2 * cos(a2) + y1;
s_surface->draw_line(x, y, x1 + x, y1 + y, GL_RGB(0, 162, 232), Z_ORDER_LEVEL_0);
s_surface->draw_line(x1 + x, y1 + y, x2 + x, y2 + y, GL_RGB(0, 162, 232), Z_ORDER_LEVEL_0);
if (text1 && text2)
{
c_word::draw_string(s_surface, Z_ORDER_LEVEL_0, text1, x1 + x - 14, y1 + y - 10, c_theme::get_font(FONT_DEFAULT), GL_RGB(255, 255, 255), 0);
c_word::draw_string(s_surface, Z_ORDER_LEVEL_0, text2, x2 + x - 18, y2 + y - 10, c_theme::get_font(FONT_DEFAULT), GL_RGB(255, 255, 255), 0);
}
else
{
s_surface->draw_rect(c_rect(x1 + x - SQUARE_SIZE_1 / 2, y1 + y - SQUARE_SIZE_1 / 2, SQUARE_SIZE_1, SQUARE_SIZE_1), GL_RGB(255, 255, 255), 1, Z_ORDER_LEVEL_0);
s_surface->draw_rect(c_rect(x2 + x - SQUARE_SIZE_2 / 2, y2 + y - SQUARE_SIZE_2 / 2, SQUARE_SIZE_2, SQUARE_SIZE_2), GL_RGB(255, 255, 255), 1, Z_ORDER_LEVEL_0);
}
}
}
extern const LATTICE_FONT_INFO Consolas_19;
void load_resource()
{
c_theme::add_font(FONT_DEFAULT, &Consolas_19);
}
//////////////////////// start UI ////////////////////////
void create_ui(void* phy_fb, int screen_width, int screen_height, int color_bytes, struct DISPLAY_DRIVER* driver) {
static c_surface surface(UI_WIDTH, UI_HEIGHT, color_bytes, Z_ORDER_LEVEL_0);
static c_display display(phy_fb, screen_width, screen_height, &surface, driver);
s_surface = &surface;
s_display = &display;
load_resource();
s_surface->fill_rect(c_rect(0, 0, UI_WIDTH, UI_HEIGHT), 0, Z_ORDER_LEVEL_0);
while (true)
{
s_mode ? pendulum::draw(120, 100, "Gui", "Lite") : pendulum::draw(120, 100);
thread_sleep(30);
}
}
//////////////////////// interface for all platform ////////////////////////
extern "C" void startHelloPendulum(void* phy_fb, int width, int height, int color_bytes, struct DISPLAY_DRIVER* driver) {
create_ui(phy_fb, width, height, color_bytes, driver);
}
extern "C" void sendKey2HelloPendulum(unsigned int key)
{
s_surface->fill_rect(c_rect(0, 0, UI_WIDTH, UI_HEIGHT), 0, Z_ORDER_LEVEL_0);
s_mode = s_mode ? 0 : 1;
}
void* getUiOfHelloPendulum(int* width, int* height, bool force_update)
{
if (s_display)
{
return s_display->get_updated_fb(width, height, force_update);
}
return NULL;
}