-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.cpp
441 lines (357 loc) · 12.2 KB
/
draw.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "engine.h"
using namespace egn;
const WORD BASIC_FREQUENCY = 20;
const WORD MENU_FREQUENCY = 120;
const Pair DISPLAY_SIZE = { 80, 80 };
const Pair MAX_PICTURE_SIZE = { DISPLAY_SIZE.x % 101 - 50, DISPLAY_SIZE.y % 101 - 50 };
const Pair BUTTON_SIZE = { 24, 7 };
const Pair TEXT_SIZE = { 28, 8 };
const std::string FILE_PATH = "Your-Picture.pict";
Display display;
Devices input;
Font font;
class Canva{
public:
Picture picture;
int scale;
void prepare(int fill_color = WHITE) {
int buffer_size = picture.SIZE.x * picture.SIZE.y;
for (int i = 0; i < buffer_size; i++) {
picture.color_map[i] = fill_color;
}
}
void create(int canva_scale) {
scale = canva_scale;
}
void save(std::string file_name) {
std::fstream picture_file;
int index = 0;
picture_file.open(file_name, std::ios::out);
picture_file << picture.SIZE.x << '\n' << picture.SIZE.y << '\n';
for (int i = 0; i < picture.SIZE.y; i++) {
for (int j = 0; j < picture.SIZE.x; j++) {
picture_file << picture.color_map[index];
if (j != picture.SIZE.x - 1) {
picture_file << '\t';
}
index++;
}
picture_file << '\n';
}
picture_file.close();
}
void load(std::string file_name) {
picture.load(file_name);
}
void render() {
display.draw_picture(&picture, { 0, 0 }, scale);
}
};
class Brush {
public:
Pair position;
int color;
int size;
private:
int get_buffer_index_position(Pair image_size, int scale) {
return position.y / scale * image_size.x + position.x / scale;
}
public:
void init(Pair brush_position, int brush_color, int brush_size) {
color = brush_color;
position = brush_position;
size = brush_size;
}
void draw(int* color_map, Pair image_size, int scale) {
color_map[get_buffer_index_position(image_size, scale)] = color;
}
void set_position(Pair accurate_position, int scale) {
position = { (accurate_position.x / scale) * scale, (accurate_position.y / scale) * scale };
}
void render(int scale, Picture* picture, int background_color = BLACK, int opposite_color = WHITE, int opposite_color_2 = L_PINK) {
int buffer_position;
int render_color;
scale--;
if (color != ALPHA) {
display.draw_rectangle(position, { position.x + (size * scale), position.y + (size * scale)}, color);
}
else {
buffer_position = get_buffer_index_position(picture->SIZE, scale + 1);
if (picture->color_map[buffer_position] == background_color) {
render_color = opposite_color;
}
else if (picture->color_map[buffer_position] == ALPHA) {
render_color = opposite_color_2;
}
else {
render_color = background_color;
}
display.draw_rectangle({ position.x + (scale + 1) / 2, position.y }, { position.x + (size * scale), position.y + (size * scale) - (scale + 1) / 2 }, render_color);
display.draw_rectangle({ position.x, position.y + (scale + 1) / 2 }, { position.x + (size * scale) - (scale + 1) / 2, position.y + (size * scale) }, render_color);
}
}
};
bool confirmation_loop() {
bool loop_on;
GUI_page gui;
gui.init();
gui.add_text("OVERWRITTE FILE?", { (int)(DISPLAY_SIZE.x / 2 - 8 * (MAX_FONT_SIZE_X + 1)), (int)(DISPLAY_SIZE.y / 10) }, SAND);
gui.add_button({ 3 * (MAX_FONT_SIZE_X + 1), MAX_FONT_SIZE_Y + 2 }, { (int)(DISPLAY_SIZE.x / 2 - 4 * (MAX_FONT_SIZE_X + 1)), (int)(DISPLAY_SIZE.y / 10 + MAX_FONT_SIZE_Y + 4) }, GRAY, "YES", GREEN);
gui.add_button({ 2 * (MAX_FONT_SIZE_X + 1), MAX_FONT_SIZE_Y + 2 }, { (int)(DISPLAY_SIZE.x / 2 + 1), (int)(DISPLAY_SIZE.y / 10 + MAX_FONT_SIZE_Y + 4) }, GRAY, "NO", RED);
display.clear();
display.draw_GUI_page(&gui, &font);
display.render();
loop_on = true;
while (loop_on){
Sleep(MENU_FREQUENCY);
gui.update_buttons_state(input.get_mouse_position(), input.get_mouse_key());
if (gui.buttons[0].get_state() == true) {
loop_on = false;
}
else if (gui.buttons[1].get_state() == true) {
loop_on = false;
return false;
}
}
return true;
}
bool new_file_creator_loop(Canva* canva) {
GUI_page gui;
int key;
int mouse_key;
bool loop_on;
Pair cursor_position;
Pair canva_size = {20, 20};
char conversion_buffer[4];
bool update = true;
gui.init();
gui.add_text("IMAGE SIZE", { (int)(DISPLAY_SIZE.x / 2 - TEXT_SIZE.x), (int)(DISPLAY_SIZE.y / 10) });
gui.add_text("X: ", { (int)(DISPLAY_SIZE.x / 2 - TEXT_SIZE.x), (int)(DISPLAY_SIZE.y / 10 + TEXT_SIZE.y) });
gui.add_button({ MAX_FONT_SIZE_X + 1, MAX_FONT_SIZE_Y + 2 }, { (int)(DISPLAY_SIZE.x / 2), (int)(DISPLAY_SIZE.y / 10 + TEXT_SIZE.y) }, SAND, "<", L_BLUE);
gui.add_text("00", { (int)(DISPLAY_SIZE.x / 2 + MAX_FONT_SIZE_X + 2), (int)(DISPLAY_SIZE.y / 10 + TEXT_SIZE.y) });
gui.add_button({ MAX_FONT_SIZE_X + 1, MAX_FONT_SIZE_Y + 2 }, { (int)(DISPLAY_SIZE.x / 2 + 3 * (MAX_FONT_SIZE_X + 2)), (int)(DISPLAY_SIZE.y / 10 + TEXT_SIZE.y) }, SAND, ">", L_BLUE);
gui.add_text("Y: ", { (int)(DISPLAY_SIZE.x / 2 - TEXT_SIZE.x), (int)(DISPLAY_SIZE.y / 10 + 2 * TEXT_SIZE.y) });
gui.add_button({ MAX_FONT_SIZE_X + 1, MAX_FONT_SIZE_Y + 2}, { (int)(DISPLAY_SIZE.x / 2), (int)(DISPLAY_SIZE.y / 10 + 2 * TEXT_SIZE.y) }, SAND, "<", L_BLUE);
gui.add_text("00", { (int)(DISPLAY_SIZE.x / 2 + MAX_FONT_SIZE_X + 2), (int)(DISPLAY_SIZE.y / 10 + 2 * TEXT_SIZE.y) });
gui.add_button({ MAX_FONT_SIZE_X + 1, MAX_FONT_SIZE_Y + 2}, { (int)(DISPLAY_SIZE.x / 2 + 3 * (MAX_FONT_SIZE_X + 2)), (int)(DISPLAY_SIZE.y / 10 + 2 * TEXT_SIZE.y) }, SAND, ">", L_BLUE);
gui.add_button({ 2 * (MAX_FONT_SIZE_X + 1), (MAX_FONT_SIZE_Y + 2) }, { (int)(DISPLAY_SIZE.x / 2), (int)(DISPLAY_SIZE.y / 10 + 4 * TEXT_SIZE.y) }, GREEN, "OK", YELLOW);
loop_on = true;
while (loop_on){
Sleep(MENU_FREQUENCY);
key = input.get_key();
mouse_key = input.get_mouse_key();
cursor_position = input.get_mouse_position();
if (key == 27) {
loop_on = false;
return false;
}
gui.update_buttons_state(cursor_position, mouse_key);
if (gui.buttons[0].get_state() == true and canva_size.x > 1) {
canva_size.x--;
update = true;
}
else if (gui.buttons[1].get_state() == true and canva_size.x < MAX_PICTURE_SIZE.x) {
canva_size.x++;
update = true;
}
else if (gui.buttons[2].get_state() == true and canva_size.y > 1) {
canva_size.y--;
update = true;
}
else if (gui.buttons[3].get_state() == true and canva_size.y < MAX_PICTURE_SIZE.y) {
canva_size.y++;
update = true;
}
else if (gui.buttons[4].get_state() == true) {
loop_on = false;
canva->picture.set_size(canva_size);
canva->prepare();
}
if (update) {
update = false;
_itoa_s(canva_size.x, conversion_buffer, 10);
if (canva_size.x < 10) {
conversion_buffer[1] = (char)0;
}
else {
conversion_buffer[2] = (char)0;
}
gui.texts[2].TEXT = std::string(conversion_buffer);
_itoa_s(canva_size.y, conversion_buffer, 10);
if (canva_size.y < 10) {
conversion_buffer[1] = (char)0;
}
else {
conversion_buffer[2] = (char)0;
}
gui.texts[4].TEXT = std::string(conversion_buffer);
display.clear();
display.draw_GUI_page(&gui, &font);
display.render();
}
}
return true;
}
bool menu_loop(Canva* canva) {
int key;
int mouse_key;
//bool program_on;
bool menu_on;
Pair cursor_position;
GUI_page main_menu;
main_menu.init();
main_menu.add_button(BUTTON_SIZE, { int((DISPLAY_SIZE.x / 2) - int(BUTTON_SIZE.x / 2)), int(DISPLAY_SIZE.y / 10) }, GRAY, "NEW", GREEN);
main_menu.add_button(BUTTON_SIZE, { int((DISPLAY_SIZE.x / 2) - int(BUTTON_SIZE.x / 2)), int(DISPLAY_SIZE.y / 10) + BUTTON_SIZE.y + 1 }, GRAY, "SAVE", GREEN);
main_menu.add_button(BUTTON_SIZE, { int((DISPLAY_SIZE.x / 2) - int(BUTTON_SIZE.x / 2)), int(DISPLAY_SIZE.y / 10) + 2 * (BUTTON_SIZE.y + 1) }, GRAY, "LOAD", GREEN);
main_menu.add_button(BUTTON_SIZE, { int((DISPLAY_SIZE.x / 2) - int(BUTTON_SIZE.x / 2)), int(DISPLAY_SIZE.y / 10) + 3 * (BUTTON_SIZE.y + 1) }, GRAY, "EXIT", GREEN);
menu_on = true;
display.clear();
display.draw_GUI_page(&main_menu, &font);
display.render();
while (menu_on) {
Sleep(MENU_FREQUENCY);
key = input.get_key();
mouse_key = input.get_mouse_key();
cursor_position = input.get_mouse_position();
if (key == 27) {
menu_on = false;
}
main_menu.update_buttons_state(cursor_position, mouse_key);
if (main_menu.buttons[3].get_state() == true) {
menu_on = false;
return false;
}
else if(main_menu.buttons[0].get_state() == true) {
if (new_file_creator_loop(canva) == true) {
menu_on = false;
}
}
else if (main_menu.buttons[1].get_state() == true) {
if (confirmation_loop() == true) {
canva->save(FILE_PATH);
menu_on = false;
}
else {
display.clear();
display.draw_GUI_page(&main_menu, &font);
display.render();
}
}
else if (main_menu.buttons[2].get_state() == true) {
canva->load(FILE_PATH);
menu_on = false;
}
}
return true;
}
void main_loop() {
bool program_on;
int key;
bool update = true;
Pair last_brush_position;
int mouse_key;
Pair cursor_position = {0, 0};
Brush brush{};
Canva canva;
int color_index = 4;
GUI_page gui;
GUI_page panel;
bool mouse_over_panel = false;
gui.init();
panel.init();
gui.add_button({ 2, 10 }, { DISPLAY_SIZE.x - 2, DISPLAY_SIZE.y / 2 - 5 }, L_PINK, "\0", ALPHA);
panel.add_button({ 13, 52 }, { DISPLAY_SIZE.x - 13, DISPLAY_SIZE.y / 2 - 26 }, GREEN, "\0", ALPHA);
for (int i = 0; i < COLOR_AMOUNT; i++) {
panel.add_button({ 2, 2 }, { DISPLAY_SIZE.x - 4, DISPLAY_SIZE.y / 2 - 26 + i * 3}, COLORS[i], "\0", ALPHA);
}
panel.add_button({ MAX_FONT_SIZE_X + 2, MAX_FONT_SIZE_Y + 2 }, { DISPLAY_SIZE.x - 12, DISPLAY_SIZE.y / 2 - 25 }, GRAY, "+", BLACK);
panel.add_button({ MAX_FONT_SIZE_X + 2, MAX_FONT_SIZE_Y + 2 }, { DISPLAY_SIZE.x - 12, DISPLAY_SIZE.y / 2 - 25 + MAX_FONT_SIZE_Y + 3 }, GRAY, "-", BLACK);
canva.create(3);
canva.picture.set_size({ 1, 1 });
canva.prepare();
brush.init({ 0, 0 }, GREEN, 1); //change brush size
program_on = menu_loop(&canva);
while (program_on) {
Sleep(BASIC_FREQUENCY);
key = input.get_key();
mouse_key = input.get_mouse_key();
//last_cursor_position = cursor_position;
cursor_position = input.get_mouse_position();
last_brush_position = brush.position;
brush.set_position(cursor_position, canva.scale);
if (key == 27) {
program_on = menu_loop(&canva);
update = true;
}
else if (key == (int)'c' or mouse_key == RIGHT_MOUSE_KEY) {
if (color_index < 16) {
color_index++;
}
else {
color_index -= 16;
}
brush.color = COLORS[color_index];
panel.buttons[0].COLOR = COLORS[color_index];
update = true;
}
if (mouse_key == LEFT_MOUSE_KEY and cursor_position.x / canva.scale < canva.picture.SIZE.x and cursor_position.y / canva.scale < canva.picture.SIZE.y and mouse_over_panel == false) {
brush.draw(canva.picture.color_map, canva.picture.SIZE, canva.scale);
update = true;
}
else if (mouse_key == MOUSE_WHEEL_UP and canva.scale > 1) {
canva.scale--;
update = true;
}
else if (mouse_key == MOUSE_WHEEL_DOWN and canva.scale < 5) {
canva.scale++;
update = true;
}
if (gui.buttons[0].mouse_over(cursor_position) == true) {
mouse_over_panel = true;
update = true;
}
else if (brush.position.x != last_brush_position.x or brush.position.y != last_brush_position.y or mouse_key != -1) {
update = true;
}
if (update == true) {
update = false;
display.clear();
canva.render();
if (brush.position.x < canva.picture.SIZE.x * canva.scale and brush.position.y < canva.picture.SIZE.y * canva.scale) {
brush.render(canva.scale, &canva.picture);
}
if (mouse_over_panel == true) {
panel.update_buttons_state(cursor_position, mouse_key);
for (int i = 1; i <= COLOR_AMOUNT; i++) {
if (panel.buttons[i].get_state() == true) {
brush.color = COLORS[i - 1];
panel.buttons[0].COLOR = COLORS[i - 1];
}
}
if (panel.buttons[COLOR_AMOUNT + 1].get_state() == true and canva.scale < 5) {
canva.scale++;
}
else if (panel.buttons[COLOR_AMOUNT + 2].get_state() == true and canva.scale > 1) {
canva.scale--;
}
if (panel.buttons[0].mouse_over(cursor_position) == false) {
mouse_over_panel = false;
}
display.draw_GUI_page(&panel, &font);
}
else {
display.draw_GUI_page(&gui, &font);
}
display.render();
//display.clear();
}
}
}
int main() {
display.init_buffer(DISPLAY_SIZE);
input.init();
font.load("pixel-font.font");
main_loop();
input.shut_down();
return 0;
}