-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
232 lines (198 loc) · 14.3 KB
/
main.cpp
File metadata and controls
232 lines (198 loc) · 14.3 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
#include "raylib.h"
#include "cmath"
#include "string"
#include "map"
#include "iostream"
class SpaceObject {
public:
Vector2 pos;
int speed;
int directionAngle;
int radius;
int sides;
Color color = {0, 0, 0, 0};
int angular_speed;
int angle=0;
void update(int W, int H, bool gravityBool, bool motionBool, bool rotationBool, float gravityAngle) {
//update pos/rotation
if (rotationBool) {
angle += angular_speed;
}
if (motionBool) {
pos.x += cos(directionAngle * PI / 180) * speed;
pos.y += sin(directionAngle * PI / 180) * speed;
}
if (gravityBool) {
pos.x += cos((gravityAngle-90) * PI / 180) * 9.8;
pos.y += sin((gravityAngle-90) * PI / 180) * 9.8;
}
// clamp pos
if (pos.x - radius > W) { pos.x = -radius; }
else if (pos.x + radius < 0) { pos.x = W + radius; }
if (pos.y - radius > H) { pos.y = -radius; }
else if (pos.y + radius < 0) { pos.y = H + radius; }
}
void draw() {
// draw object
DrawPoly(pos, sides, radius, angle, color);
}
};
//vars
const int objectsNumber = 40;
float gravityAngle = 0;
bool gravityBool = false;
bool motionBool = true;
bool rotationBool = true;
bool exitIt = false;
unsigned char COLORS[422][4] = {{240, 248, 255, 255}, {0, 255, 255, 255}, {127, 255, 212, 255}, {127, 255, 212, 255}, {118, 238, 198, 255}, {102, 205, 170, 255}, {69, 139, 116, 255}, {240, 255, 255, 255}, {240, 255, 255, 255}, {193, 205, 205, 255}, {224, 238, 238, 255}, {131, 139, 139, 255}, {245, 245, 220, 255}, {255, 228, 196, 255}, {255, 228, 196, 255}, {238, 213, 183, 255}, {205, 183, 158, 255}, {139, 125, 107, 255}, {0, 0, 0, 255}, {255, 235, 205, 255}, {0, 0, 255, 255}, {0, 0, 255, 255}, {0, 0, 238, 255}, {0, 0, 205, 255}, {0, 0, 139, 255}, {138, 43, 226, 255}, {165, 42, 42, 255}, {255, 64, 64, 255}, {238, 59, 59, 255}, {205, 51, 51, 255}, {139, 35, 35, 255}, {222, 184, 135, 255}, {255, 211, 155, 255}, {238, 197, 145, 255}, {205, 170, 125, 255}, {139, 115, 85, 255}, {95, 158, 160, 255}, {152, 245, 255, 255}, {142, 229, 238, 255}, {122, 197, 205, 255}, {83, 134, 139, 255}, {127, 255, 0, 255}, {127, 255, 0, 255}, {118, 238, 0, 255}, {102, 205, 0, 255}, {69, 139, 0, 255}, {210, 105, 30, 255}, {255, 127, 36, 255}, {238, 118, 33, 255}, {205, 102, 29, 255}, {139, 69, 19, 255}, {255, 127, 80, 255}, {255, 114, 86, 255}, {238, 106, 80, 255}, {205, 91, 69, 255}, {139, 62, 47, 255}, {100, 149, 237, 255}, {255, 248, 220, 255}, {255, 248, 220, 255}, {238, 232, 205, 255}, {205, 200, 177, 255}, {139, 136, 120, 255}, {220, 20, 60, 255}, {0, 255, 255, 255}, {0, 255, 255, 255}, {0, 238, 238, 255}, {0, 205, 205, 255}, {0, 139, 139, 255}, {0, 0, 139, 255}, {0, 139, 139, 255}, {184, 134, 11, 255}, {255, 185, 15, 255}, {238, 173, 14, 255}, {205, 149, 12, 255}, {139, 101, 8, 255}, {0, 100, 0, 255}, {189, 183, 107, 255}, {139, 0, 139, 255}, {85, 107, 47, 255}, {202, 255, 112, 255}, {188, 238, 104, 255}, {162, 205, 90, 255}, {110, 139, 61, 255}, {255, 140, 0, 255}, {255, 127, 0, 255}, {238, 118, 0, 255}, {205, 102, 0, 255}, {139, 69, 0, 255}, {153, 50, 204, 255}, {191, 62, 255, 255}, {178, 58, 238, 255}, {154, 50, 205, 255}, {104, 34, 139, 255}, {139, 0, 0, 255}, {233, 150, 122, 255}, {143, 188, 143, 255}, {193, 255, 193, 255}, {180, 238, 180, 255}, {155, 205, 155, 255}, {105, 139, 105, 255}, {72, 61, 139, 255}, {0, 206, 209, 255}, {148, 0, 211, 255}, {255, 20, 147, 255}, {255, 20, 147, 255}, {238, 18, 137, 255}, {205, 16, 118, 255}, {139, 10, 80, 255}, {0, 191, 255, 255}, {0, 191, 255, 255}, {0, 178, 238, 255}, {0, 154, 205, 255}, {0, 104, 139, 255}, {30, 144, 255, 255}, {30, 144, 255, 255}, {28, 134, 238, 255}, {24, 116, 205, 255}, {16, 78, 139, 255}, {178, 34, 34, 255}, {255, 48, 48, 255}, {238, 44, 44, 255}, {205, 38, 38, 255}, {139, 26, 26, 255}, {34, 139, 34, 255}, {255, 0, 255, 255}, {220, 220, 220, 255}, {255, 215, 0, 255}, {255, 215, 0, 255}, {238, 201, 0, 255}, {205, 173, 0, 255}, {139, 117, 0, 255}, {218, 165, 32, 255}, {255, 193, 37, 255}, {238, 180, 34, 255}, {205, 155, 29, 255}, {139, 105, 20, 255}, {0, 255, 0, 255}, {0, 255, 0, 255}, {0, 238, 0, 255}, {0, 205, 0, 255}, {0, 139, 0, 255}, {173, 255, 47, 255}, {240, 255, 240, 255}, {240, 255, 240, 255}, {224, 238, 224, 255}, {193, 205, 193, 255}, {131, 139, 131, 255}, {255, 105, 180, 255}, {255, 110, 180, 255}, {238, 106, 167, 255}, {205, 96, 144, 255}, {139, 58, 98, 255}, {205, 92, 92, 255}, {255, 106, 106, 255}, {238, 99, 99, 255}, {205, 85, 85, 255}, {139, 58, 58, 255}, {75, 0, 130, 255}, {255, 255, 240, 255}, {255, 255, 240, 255}, {238, 238, 224, 255}, {205, 205, 193, 255}, {139, 139, 131, 255}, {240, 230, 140, 255}, {255, 246, 143, 255}, {238, 230, 133, 255}, {205, 198, 115, 255}, {139, 134, 78, 255}, {230, 230, 250, 255}, {255, 240, 245, 255}, {255, 240, 245, 255}, {238, 224, 229, 255}, {205, 193, 197, 255}, {139, 131, 134, 255}, {124, 252, 0, 255}, {255, 250, 205, 255}, {255, 250, 205, 255}, {238, 233, 191, 255}, {205, 201, 165, 255}, {139, 137, 112, 255}, {173, 216, 230, 255}, {191, 239, 255, 255}, {178, 223, 238, 255}, {154, 192, 205, 255}, {104, 131, 139, 255}, {240, 128, 128, 255}, {224, 255, 255, 255}, {224, 255, 255, 255}, {209, 238, 238, 255}, {180, 205, 205, 255}, {122, 139, 139, 255}, {238, 221, 130, 255}, {255, 236, 139, 255}, {238, 220, 130, 255}, {205, 190, 112, 255}, {139, 129, 76, 255}, {250, 250, 210, 255}, {144, 238, 144, 255}, {255, 182, 193, 255}, {255, 174, 185, 255}, {238, 162, 173, 255}, {205, 140, 149, 255}, {139, 95, 101, 255}, {255, 160, 122, 255}, {255, 160, 122, 255}, {238, 149, 114, 255}, {205, 129, 98, 255}, {139, 87, 66, 255}, {32, 178, 170, 255}, {135, 206, 250, 255}, {176, 226, 255, 255}, {164, 211, 238, 255}, {141, 182, 205, 255}, {96, 123, 139, 255}, {132, 112, 255, 255}, {176, 196, 222, 255}, {202, 225, 255, 255}, {188, 210, 238, 255}, {162, 181, 205, 255}, {110, 123, 139, 255}, {255, 255, 224, 255}, {255, 255, 224, 255}, {238, 238, 209, 255}, {205, 205, 180, 255}, {139, 139, 122, 255}, {250, 240, 230, 255}, {0, 255, 0, 255}, {50, 205, 50, 255}, {255, 0, 255, 255}, {255, 0, 255, 255}, {238, 0, 238, 255}, {205, 0, 205, 255}, {139, 0, 139, 255}, {176, 48, 96, 255}, {255, 52, 179, 255}, {238, 48, 167, 255}, {205, 41, 144, 255}, {139, 28, 98, 255}, {102, 205, 170, 255}, {0, 0, 205, 255}, {186, 85, 211, 255}, {224, 102, 255, 255}, {209, 95, 238, 255}, {180, 82, 205, 255}, {122, 55, 139, 255}, {147, 112, 219, 255}, {171, 130, 255, 255}, {159, 121, 238, 255}, {137, 104, 205, 255}, {93, 71, 139, 255}, {60, 179, 113, 255}, {123, 104, 238, 255}, {0, 250, 154, 255}, {72, 209, 204, 255}, {199, 21, 133, 255}, {25, 25, 112, 255}, {245, 255, 250, 255}, {255, 228, 225, 255}, {255, 228, 225, 255}, {238, 213, 210, 255}, {205, 183, 181, 255}, {139, 125, 123, 255}, {255, 228, 181, 255}, {0, 0, 128, 255}, {0, 0, 128, 255}, {253, 245, 230, 255}, {128, 128, 0, 255}, {107, 142, 35, 255}, {192, 255, 62, 255}, {179, 238, 58, 255}, {154, 205, 50, 255}, {105, 139, 34, 255}, {255, 165, 0, 255}, {255, 165, 0, 255}, {238, 154, 0, 255}, {205, 133, 0, 255}, {139, 90, 0, 255}, {255, 69, 0, 255}, {255, 69, 0, 255}, {238, 64, 0, 255}, {205, 55, 0, 255}, {139, 37, 0, 255}, {218, 112, 214, 255}, {255, 131, 250, 255}, {238, 122, 233, 255}, {205, 105, 201, 255}, {139, 71, 137, 255}, {152, 251, 152, 255}, {154, 255, 154, 255}, {144, 238, 144, 255}, {124, 205, 124, 255}, {84, 139, 84, 255}, {238, 232, 170, 255}, {175, 238, 238, 255}, {187, 255, 255, 255}, {174, 238, 238, 255}, {150, 205, 205, 255}, {102, 139, 139, 255}, {219, 112, 147, 255}, {255, 130, 171, 255}, {238, 121, 159, 255}, {205, 104, 137, 255}, {139, 71, 93, 255}, {255, 239, 213, 255}, {255, 218, 185, 255}, {255, 218, 185, 255}, {238, 203, 173, 255}, {205, 175, 149, 255}, {139, 119, 101, 255}, {205, 133, 63, 255}, {255, 192, 203, 255}, {255, 181, 197, 255}, {238, 169, 184, 255}, {205, 145, 158, 255}, {139, 99, 108, 255}, {221, 160, 221, 255}, {255, 187, 255, 255}, {238, 174, 238, 255}, {205, 150, 205, 255}, {139, 102, 139, 255}, {176, 224, 230, 255}, {160, 32, 240, 255}, {155, 48, 255, 255}, {145, 44, 238, 255}, {125, 38, 205, 255}, {85, 26, 139, 255}, {255, 0, 0, 255}, {255, 0, 0, 255}, {238, 0, 0, 255}, {205, 0, 0, 255}, {139, 0, 0, 255}, {188, 143, 143, 255}, {255, 193, 193, 255}, {238, 180, 180, 255}, {205, 155, 155, 255}, {139, 105, 105, 255}, {65, 105, 225, 255}, {72, 118, 255, 255}, {67, 110, 238, 255}, {58, 95, 205, 255}, {39, 64, 139, 255}, {250, 128, 114, 255}, {255, 140, 105, 255}, {238, 130, 98, 255}, {205, 112, 84, 255}, {139, 76, 57, 255}, {139, 69, 19, 255}, {244, 164, 96, 255}, {46, 139, 87, 255}, {84, 255, 159, 255}, {78, 238, 148, 255}, {67, 205, 128, 255}, {46, 139, 87, 255}, {255, 245, 238, 255}, {255, 245, 238, 255}, {238, 229, 222, 255}, {205, 197, 191, 255}, {139, 134, 130, 255}, {160, 82, 45, 255}, {255, 130, 71, 255}, {238, 121, 66, 255}, {205, 104, 57, 255}, {139, 71, 38, 255}, {192, 192, 192, 255}, {135, 206, 235, 255}, {135, 206, 255, 255}, {126, 192, 238, 255}, {108, 166, 205, 255}, {74, 112, 139, 255}, {106, 90, 205, 255}, {131, 111, 255, 255}, {122, 103, 238, 255}, {105, 89, 205, 255}, {71, 60, 139, 255}, {0, 255, 127, 255}, {0, 255, 127, 255}, {0, 238, 118, 255}, {0, 205, 102, 255}, {0, 139, 69, 255}, {70, 130, 180, 255}, {99, 184, 255, 255}, {92, 172, 238, 255}, {79, 148, 205, 255}, {54, 100, 139, 255}, {210, 180, 140, 255}, {255, 165, 79, 255}, {238, 154, 73, 255}, {205, 133, 63, 255}, {139, 90, 43, 255}, {0, 128, 128, 255}, {216, 191, 216, 255}, {255, 225, 255, 255}, {238, 210, 238, 255}, {205, 181, 205, 255}, {139, 123, 139, 255}, {255, 99, 71, 255}, {255, 99, 71, 255}, {238, 92, 66, 255}, {205, 79, 57, 255}, {139, 54, 38, 255}, {64, 224, 208, 255}, {0, 245, 255, 255}, {0, 229, 238, 255}, {0, 197, 205, 255}, {0, 134, 139, 255}, {238, 130, 238, 255}, {208, 32, 144, 255}, {255, 62, 150, 255}, {238, 58, 140, 255}, {205, 50, 120, 255}, {139, 34, 82, 255}, {245, 222, 179, 255}, {255, 231, 186, 255}, {238, 216, 174, 255}, {205, 186, 150, 255}, {139, 126, 102, 255}, {255, 255, 0, 255}, {255, 255, 0, 255}, {238, 238, 0, 255}, {205, 205, 0, 255}, {139, 139, 0, 255}, {154, 205, 50, 255}};
// generated in python copied over here ^^^, I am so SORRY
std::string descText = "";
Color g_col;
SpaceObject objects[objectsNumber];
void updateObjects(int W, int H) {
for (int i = 0; i < objectsNumber; ++i) {
objects[i].update(W, H, gravityBool, motionBool, rotationBool, gravityAngle);
}
}
void drawObjects() {
for (int i = 0; i < objectsNumber; ++i) {
objects[i].draw();
}
}
int get_abs_angle(float angle) {
int int_angle = round(angle);
int_angle %= 360;
if (int_angle < 0) {
return 360 + int_angle;
} return int_angle;
}
Vector2 pointer_coords(int W, int H) {
float x, y;
x = cos((gravityAngle-90) * PI / 180) * 150 + W/2;
y = sin((gravityAngle-90) * PI / 180) * 150 + H/2;
return Vector2{ x, y };
}
void drawButtons(std::map<char, Rectangle> buttons) {
for (auto itr = buttons.begin(); itr != buttons.end(); ++itr) {
Color col;
switch (itr->first) {
case 'G':
col = g_col; break;
case 'M':
if (motionBool) { col = GRAY; }
else { col = RED; }
break;
case 'R':
if (rotationBool) { col = GRAY; }
else { col = RED; }
break;
case 'Q':
col = RED; break;
}
std::string key_char{ itr->first };
DrawRectangleLinesEx(itr->second, 4, col);
DrawText(key_char.c_str(), itr->second.x + 16, itr->second.y + 12, 30, col);
}
}
void drawUI(int W, int H) {
DrawText(std::to_string(get_abs_angle(gravityAngle)).c_str(), W / 2 - 10, H / 2 - 10, 30, g_col);
DrawCircleLines(W/2, H/2, 150, g_col);
Vector2 pos = pointer_coords(W, H);
DrawCircleV(pos, 10, g_col);
}
float getGravityAngle(Vector2 MousePos, int W, int H) {
return atan2(MousePos.y - H / 2, MousePos.x - W / 2)*180/PI +90;
}
int main (){
// window creation
InitWindow(0, 0, "Hello Raylib");
const float W = GetScreenWidth();
const float H = GetScreenHeight();
SetWindowState(FLAG_FULLSCREEN_MODE | FLAG_VSYNC_HINT);
SetTargetFPS(60);
for (int i = 0; i < objectsNumber; ++i) {
SpaceObject object;
int o = GetRandomValue(0, 421);
Color col = Color{ COLORS[o][0], COLORS[o][1], COLORS[o][2], 255 };
object.pos.x = GetRandomValue(0, W);
object.pos.y = GetRandomValue(0, H);
object.speed = GetRandomValue(1, 10);
object.directionAngle = GetRandomValue(0, 360);
object.radius = 30;
object.sides = GetRandomValue(3, 6);
object.color = col;
object.angular_speed = GetRandomValue(-2, 2);
objects[i] = object;
}
std::map<char, Rectangle> buttons = {
{'G', Rectangle{W/5 , 10, 50, 50}},
{'M', Rectangle{W/5*2, 10, 50, 50}},
{'R', Rectangle{W/5*3, 10, 50, 50}},
{'Q', Rectangle{W/5*4, 10, 50, 50}}
};
while (!WindowShouldClose() && !exitIt){
//logic
Vector2 MousePos = { GetMouseX(), GetMouseY() };
bool CLICK = IsMouseButtonReleased(0);
if (gravityBool) {
g_col = GRAY;
} else { g_col = RED; }
//key input
if (IsKeyPressed(KEY_SPACE) || IsKeyPressed(KEY_G)) { gravityBool = !gravityBool; }
else if (IsKeyPressed(KEY_ENTER) || IsKeyPressed(KEY_M)) { motionBool = !motionBool; }
else if (IsKeyPressed(KEY_RIGHT_SHIFT) || IsKeyPressed(KEY_R)) { rotationBool = !rotationBool; }
else if (IsKeyPressed(KEY_Q)) { exitIt = true; }
//button press
for (auto itr = buttons.begin(); itr != buttons.end(); ++itr) {
char key = itr->first;
Rectangle rect = itr->second;
bool emptyDesc = true;
if (CheckCollisionPointRec(MousePos, rect)) {
if (key == 'G') {
emptyDesc = false;
descText = "toggle Gravity";
if (CLICK) { gravityBool = !gravityBool;}
break;
} else if (key == 'M') {
emptyDesc = false;
descText = "toggle Object Motion";
if (CLICK) { motionBool = !motionBool;}
break;
} else if (key == 'R') {
emptyDesc = false;
descText = "toggle Object Rotation";
if (CLICK) { rotationBool = !rotationBool;}
break;
} else if (key == 'Q') {
emptyDesc = false;
descText = "Exit";
if (CLICK) { exitIt = true;}
break;
} else {break; }
}
if (emptyDesc) { descText = ""; }
}
//change gravity angle
if (descText.empty() && IsMouseButtonDown(0) && (GetMouseDelta().x || GetMouseDelta().y)) {
gravityAngle = getGravityAngle(MousePos, W, H);
}
// update objects
updateObjects(W, H);
//draw bg
BeginDrawing();
ClearBackground(Color {200, 200, 200, 255});
//draw objects
drawObjects();
//draw UI
drawButtons(buttons);
if (descText.empty() && IsMouseButtonDown(0)) {
drawUI(W, H);
}
//draw text
DrawText(descText.c_str(), W / 2 - (descText.size() * 12), H - 50, 40, BLUE);
DrawFPS(10, H - 30);
EndDrawing();
}
CloseWindow();
return 0;
}