-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlol.cpp
310 lines (273 loc) · 9.17 KB
/
lol.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
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
#include <iostream>
#include <SFML/Graphics.hpp>
#include <cstdlib>
#include <vector>
#include <sstream>
namespace patch {
template <typename T> std::string to_string(const T& n)
{
std::ostringstream stm;
stm << n;
return stm.str();
}
}
// Initialize some mutable modifiers
int max_shapes = 10;
int size_modder = 1;
int border_mod = 0;
int rotation_mod = 10;
int shape_type = -1;
// ================================
class ModMenu {
private:
struct Item {
public:
int* attrib_ptr;
int MAX; // Treat as const
int MIN; // Treat as const
std::string label;
sf::Time wasIncd;
sf::Time wasDecd;
Item(std::string l, int* att, int min, int max): label(l), attrib_ptr(att), MAX(max), MIN(min) {}
};
sf::Font font;
sf::Text texbuf;
sf::Clock clock;
std::vector<Item> items;
int hilit_index;
int x, y, item_w, item_h;
public:
ModMenu(int xp, int yp, int it_w, int it_h, sf::Font f, sf::Clock& c);
void addItem(std::string label, int* att, int min, int max);
void handleInput(sf::Event& event);
void draw(sf::RenderWindow& window);
};
ModMenu::ModMenu(int xp, int yp, int it_w, int it_h, sf::Font f, sf::Clock& c)
: hilit_index(0), x(xp), y(yp), font(f), item_w(it_w), item_h(it_h), clock(c) {
texbuf.setFont(font);
texbuf.setCharacterSize(16);
texbuf.setColor(sf::Color::Black);
}
void ModMenu::addItem(std::string label, int* att, int min, int max) {
items.push_back(Item(label, att, min, max));
}
void ModMenu::handleInput(sf::Event& event) {
if (event.key.code == sf::Keyboard::Up) {
if(--hilit_index < 0)
hilit_index = items.size() - 1;
}
else if (event.key.code == sf::Keyboard::Down) {
if(++hilit_index >= items.size())
hilit_index = 0;
}
else if (event.key.code == sf::Keyboard::Left) {
if (*(items[hilit_index].attrib_ptr) > items[hilit_index].MIN) {
/* Somehow causes segfault
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
if(*(items[hilit_index].attrib_ptr) - 10 <= items[hilit_index].MIN) {
*(items[hilit_index].attrib_ptr) = items[hilit_index].MIN;
}
else
*(items[hilit_index].attrib_ptr) -= 10;
}
else
*/
--*(items[hilit_index].attrib_ptr);
items[hilit_index].wasDecd = clock.getElapsedTime();
}
}
else if (event.key.code == sf::Keyboard::Right) {
if (*(items[hilit_index].attrib_ptr) < items[hilit_index].MAX) {
/* Somehow causes segfault
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
if(*(items[hilit_index].attrib_ptr) + 10 >= items[hilit_index].MAX)
*(items[hilit_index].attrib_ptr) = items[hilit_index].MAX;
else
*(items[hilit_index].attrib_ptr) += 10;
}
else
*/
++*(items[hilit_index].attrib_ptr);
items[hilit_index].wasIncd = clock.getElapsedTime();
}
}
}
void ModMenu::draw(sf::RenderWindow& window) {
int cur_y = y;
sf::RectangleShape rect;
rect.setOutlineThickness(1);
rect.setOutlineColor(sf::Color::Black);
for(std::vector<Item>::size_type i = 0; i < items.size(); ++i) {
rect.setPosition(x,cur_y);
rect.setSize(sf::Vector2f(item_w, item_h));
// Draw the rectangle
if (i == hilit_index)
rect.setFillColor(sf::Color(255, 255, 255, 244));
else
rect.setFillColor(sf::Color(211,222,233,200));
window.draw(rect);
// Draw the arrows
sf::ConvexShape arrow;
arrow.setOutlineThickness(1);
arrow.setOutlineColor(sf::Color::Black);
if ((clock.getElapsedTime() - items[i].wasDecd).asSeconds() < 0.1) {
arrow.setFillColor(sf::Color(255, 255, 255, 244));
}
else
arrow.setFillColor(sf::Color(211,222,233,200));
arrow.setPointCount(3);
arrow.setPoint(0, sf::Vector2f(x - 10, cur_y + item_h/2));
arrow.setPoint(1, sf::Vector2f(x, cur_y));
arrow.setPoint(2, sf::Vector2f(x, cur_y + item_h));
window.draw(arrow);
if ((clock.getElapsedTime() - items[i].wasIncd).asSeconds() < 0.1) {
arrow.setFillColor(sf::Color(255, 255, 255, 244));
}
else
arrow.setFillColor(sf::Color(211,222,233,200));
arrow.setPoint(0, sf::Vector2f(x + item_w, cur_y));
arrow.setPoint(1, sf::Vector2f(x + item_w, cur_y + item_h));
arrow.setPoint(2, sf::Vector2f(x + item_w + 10, cur_y + item_h/2));
window.draw(arrow);
// Draw the label
texbuf.setString(items[i].label);
int tw = texbuf.getLocalBounds().width;
int th = texbuf.getLocalBounds().height;
texbuf.setPosition(x + (item_w-tw)/2, cur_y);
window.draw(texbuf);
// Draw the value
texbuf.setString(patch::to_string(*items[i].attrib_ptr));
rect.setSize(sf::Vector2f(texbuf.getLocalBounds().width + 8, item_h));
rect.setFillColor(sf::Color(255, 255, 255, 244));
rect.setPosition(x + item_w + 14, cur_y);
window.draw(rect);
texbuf.setPosition(x + item_w + 14, cur_y);
window.draw(texbuf);
// Update the top position for the next value 'widget'
cur_y += item_h + 2;
}
// Tell user to use arrow keys
texbuf.setString("Use the arrow keys\nto navigate the menu");
rect.setPosition(x, cur_y);
rect.setSize(sf::Vector2f(texbuf.getLocalBounds().width + 8, texbuf.getLocalBounds().height + 8));
rect.setFillColor(sf::Color(255, 255, 255, 244));
window.draw(rect);
texbuf.setPosition(x, cur_y);
window.draw(texbuf);
}
// Adds a random shape to a vector<Shape*>
// Delete the added pointer when done
void shape_adder1(std::vector<sf::Shape*>& shapes) {
int r = std::rand() % 10;
if(shape_type >=0 && shape_type <= 9)
r = shape_type;
sf::Shape* s;
sf::Color fill_color(std::rand()%256,
std::rand()%256,
std::rand()%256);
sf::Color outline_color(std::rand()%256,
std::rand()%256,
std::rand()%256);
// Create a circle
if (r == 0) {
s = new sf::CircleShape(std::rand() % 50 + size_modder);
s->setPosition(std::rand() % 1280, std::rand() % 720);
s->setFillColor(fill_color);
s->setOutlineThickness(std::rand() % 15 + border_mod);
s->setOutlineColor(outline_color);
// Rotate to change the origin point
// so circles shrink into a random direction
s->rotate(std::rand() % 360);
shapes.push_back(s);
}
// Create a rectangle
else if(r == 1) {
s = new sf::RectangleShape(sf::Vector2f(std::rand() % 75 + size_modder,
std::rand() % 75 + size_modder));
s->setPosition(std::rand() % 1280, std::rand() % 720);
s->setFillColor(fill_color);
s->setOutlineThickness(std::rand() % 15 + border_mod);
s->setOutlineColor(outline_color);
s->rotate(std::rand() % 360);
shapes.push_back(s);
}
// Create a 3~10gon
else {
s = new sf::CircleShape(std::rand() % 50 + size_modder, r+1);
s->setPosition(std::rand() % 1280, std::rand() % 720);
s->setFillColor(fill_color);
s->setOutlineThickness(std::rand() % 15 + border_mod);
s->setOutlineColor(outline_color);
s->rotate(std::rand() % 360);
shapes.push_back(s);
}
}
int main(int argc, char** argv) {
// Create window and set framerate
sf::RenderWindow window(sf::VideoMode(1280, 720), "Twinkly Shapes");
window.setFramerateLimit(55);
// Store our shapes here
std::vector<sf::Shape*> shapes;
// Load the font
sf::Font font;
if (!font.loadFromFile("CutiveMono-Regular.ttf")) {
std::cout << "Error: Could not load font." << std::endl;
window.close();
return -1;
}
// Start the clock
sf::Clock clock;
// Set up the menu to change modifiers
ModMenu mod_menu(24, 16, 128, 24, font, clock);
mod_menu.addItem("Max Shapes", &max_shapes, 2, 99999);
mod_menu.addItem("Size Mod", &size_modder, -25, 99999);
mod_menu.addItem("Border Mod", &border_mod, -8, 99999);
mod_menu.addItem("Rotation Mod", &rotation_mod, 1, 20);
mod_menu.addItem("Shape Type", &shape_type, -1, 9);
// Main loop
while(window.isOpen()) {
// Input and event handling
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Escape)
window.close();
else
mod_menu.handleInput(event);
}
}
// Add new shapes
int diff = max_shapes - shapes.size();
if (diff > 0) {
// Crude way to add more shapes to get closer to max_shapes
for(int i = 0; i < diff/2+1; ++i)
shape_adder1(shapes);
}
// Clear screen
window.clear(sf::Color::Black);
// Draw shapes
for(std::vector<sf::Shape*>::size_type i = 0; i < shapes.size(); ++i) {
window.draw(*shapes[i]);
}
// Draw the menu
mod_menu.draw(window);
window.display();
// Shrink shapes and delete once they disappear
for(std::vector<sf::Shape*>::iterator it = shapes.begin(); it != shapes.end(); ++it) {
sf::Vector2f scale = (*it)->getScale();
// 1/10 chance to rotate slightly
if(std::rand() % rotation_mod == 0)
(*it)->rotate(std::rand()%15);
// Decreases scale by subtrahend of 0.1 for both x and y
(*it)->setScale(scale.x - 0.1, scale.y - 0.1);
// If the shape is invisible (scale (0,0)) then delete it to free memory
if(scale.x <= 0 && scale.y <= 0) {
delete (*it);
shapes.erase(it);
}
}
}
return 0;
}