-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
659 lines (627 loc) · 32.1 KB
/
main.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#include <SDL.h>
#include <stdio.h>
#include <string>
#include <windows.h>
#include <time.h>
#include <cstdlib>
#include <unistd.h>
#include <cmath>
#include <iostream>
#include"Rect.h"
#include"Line.h"
#include"Stack.h"
#include"Stack_redo.h"
#define fps 60
/*
Important notes:
- the whole main program (main.cpp)is divided into three parts. these three parts are 90% same,except the shape that
they are creating.
- a linked list is used to store shapes
- a linked list (stack_redo) is used to store the redo and the undo elements
- you may find me adding and subtracting some constants to variables, such as length of stack. this is to adjust so that the program
can run properly
*/
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Key press surfaces constants
enum KeyPressSurfaces
{
KEY_PRESS_SURFACE_DEFAULT,
KEY_PRESS_SURFACE_UP,
KEY_PRESS_SURFACE_DOWN,
KEY_PRESS_SURFACE_LEFT,
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
//
bool init();
bool loadMedia();
void close();
void redoArrayFunction();
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
bool init() //same code as given
{
bool success = true;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "0" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
}
}
}
return success;
}
bool loadMedia() //same code as given
{
bool success = true;
return success;
}
void close() //same code as given //both stacks have been deallocated int he main function in the end
{
//Destroy window
SDL_DestroyRenderer( gRenderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
gRenderer = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
Shapes* swapUpDown[100]; //used to store the shapes that are sent up and down
srand (time(NULL));
Uint32 starting_tick;
Stack stk;
Stack_redo stkRedo;
int shapesInStack=0;
int redoArrayLength=0;
int redoNumber=0;
//Start linePermission SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
if( !loadMedia() ) //Load media
{
printf( "Failed to load media!\n" );
}
else
{
bool quit = false; //Main loop controller
int swapIndex=0; //
int swapInitialIndex=0; // position of last element in swapstack
Shapes* swapIndexShape;
SDL_Event e; //Event handler that takes care of all events
bool mouseClicked = false;
bool allowUp = false;
SDL_Rect fillRect;
bool rectanglePermission=false; //rectangle
bool linePermission = false; //line
bool swapPermission = true;
bool pointPermission = false; //point
//for coordinates
int oldx ;
int oldy ;
int x, y ;
int lineoldx, lineoldy;
int linex, liney;
//creating objects
Shapes* rect = NULL;
Line* line = NULL;
Point* point = NULL;
rectanglePermission = true;
rect = new Rect(fillRect);
line = new Line();
point = new Point();
point->giveCoods(0,0); //creates the first point
stk.Push(point);
stk.Show(gRenderer);
shapesInStack++;
while( !quit )
{
while( SDL_PollEvent( &e ) != 0 )
{
if( e.type == SDL_QUIT )
{
quit = true;
}
else
{
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
rectanglePermission = true;
while(!quit)
{
starting_tick = SDL_GetTicks(); // used to decrease the frame rate to ensure smooth running of program
//Handle events on queue
while (rectanglePermission ==true) // loop for rectangle. same is done for line and point
{
starting_tick = SDL_GetTicks();
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
rectanglePermission = false;
}
if( e.type == SDL_MOUSEMOTION || e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP )
{
SDL_GetMouseState( &x, &y );
if(e.type == SDL_MOUSEMOTION)
{
if(mouseClicked == true)
fillRect = { oldx, oldy, x - oldx, y - oldy };
rect->inFillrect(&fillRect); //send the coordinates
}
if(e.type == SDL_MOUSEBUTTONDOWN)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
if(mouseClicked == false)
{
mouseClicked = true;
oldx = x;
oldy = y;
}
}
}
if(e.type == SDL_MOUSEBUTTONUP)
{
if (e.button.button == SDL_BUTTON_LEFT) //draws
{
rect = new Rect(fillRect);
mouseClicked = false;
stk.Push(rect);
shapesInStack++;
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer); //draws all the shapes
}
if (e.button.button == SDL_BUTTON_RIGHT) //undo
{
if (shapesInStack > 1)
{
shapesInStack--;
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stkRedo.Push_redo(stk.Pop());
redoArrayLength ++;
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
}
}
if (e.button.button == SDL_BUTTON_MIDDLE) //redo
{
if (redoNumber-1 <= redoArrayLength+1)
{
(stkRedo.Pop_redo())->draw(gRenderer);
redoNumber = redoNumber+1;
redoArrayLength = redoArrayLength -1;
}
}
}
}
}
SDL_RenderPresent( gRenderer );
switch( e.key.keysym.sym ) // get the keyboard inputs
{
case SDLK_p:
{
rectanglePermission = false;
pointPermission = true;
break;
}
case SDLK_l:
{
rectanglePermission = false;
linePermission = true;
break;
}
case SDLK_DOWN:
{
if (e.type == SDL_KEYUP)
{
allowUp = true;
swapPermission = true;
}
if (e.type == SDL_KEYDOWN)
{
if (swapPermission ==true)
{
if (swapIndex==0)
{
swapInitialIndex=stk.length()-1;
}
if ((stk.length())-2>swapIndex)
{
stk.swap(swapInitialIndex-1,swapInitialIndex);
swapIndex++;
swapInitialIndex--;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
swapPermission = false;
break;
}
}
}
case SDLK_UP:
{
if (allowUp == true)
{
if (e.type == SDL_KEYUP)
{
swapPermission = true;
}
if (e.type == SDL_KEYDOWN)
{
if (swapPermission ==true)
{
if (swapIndex==0)
{
swapInitialIndex=stk.length()-1;
}
if ((stk.length()-1)>swapIndex)
{
stk.swap(swapInitialIndex+1,swapInitialIndex);
swapIndex--;
swapInitialIndex++;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
swapPermission = false;
break;
}
}
}
}
}
if((500/fps)> SDL_GetTicks() - starting_tick) //decreasing frame rate
{
SDL_Delay(500/fps - (SDL_GetTicks() - starting_tick ));
}
}
while (linePermission ==true)
{
starting_tick = SDL_GetTicks();
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
linePermission = false;
}
if( e.type == SDL_MOUSEMOTION || e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP )
{
SDL_GetMouseState( &linex, &liney );
if(e.type == SDL_MOUSEMOTION)
{
swapPermission = true;
if(mouseClicked == true)
line->giveCoordinates2(linex, liney);
}
if(e.type == SDL_MOUSEBUTTONDOWN)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
if(mouseClicked == false)
{
mouseClicked = true;
lineoldx = linex;
lineoldy = liney;
line->giveCoordinates1(lineoldx,lineoldy);
}
}
}
if(e.type == SDL_MOUSEBUTTONUP)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
mouseClicked = false;
line = new Line();
line->draw(gRenderer);
stk.Push(line);
shapesInStack++;
stk.Show(gRenderer);
}
if (e.button.button == SDL_BUTTON_RIGHT)
{
if (shapesInStack > 1)
{
shapesInStack--;
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear(gRenderer );
stkRedo.Push_redo(stk.Pop());
redoArrayLength++;
stk.Show(gRenderer);
}
}
if (e.button.button == SDL_BUTTON_MIDDLE)
{
if (redoNumber-1 <= redoArrayLength+1)
{
(stkRedo.Pop_redo())->draw(gRenderer);
redoNumber = redoNumber+1;
redoArrayLength = redoArrayLength -1;
}
}
}
}
}
SDL_RenderPresent( gRenderer );
switch( e.key.keysym.sym )
{
if (e.type == SDL_KEYDOWN)
{
case SDLK_p:
linePermission = false;
pointPermission = true;
break;
case SDLK_r:
linePermission = false;
rectanglePermission = true;
break;
case SDLK_DOWN:
{
if (e.type == SDL_KEYUP)
{
allowUp = true;
swapPermission = true;
}
if (e.type == SDL_KEYDOWN)
{
if (swapPermission ==true)
{
if (swapIndex==0)
{
swapInitialIndex=stk.length()-1;
}
if ((stk.length())-2>swapIndex)
{
stk.swap(swapInitialIndex-1,swapInitialIndex);
//stk.swap(stk.length()-1,stk.length());
swapIndex++;
swapInitialIndex--;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
swapPermission = false;
break;
}
}
}
case SDLK_UP:
{
if (allowUp == true)
{
if (e.type == SDL_KEYUP)
{
swapPermission = true;
}
if (e.type == SDL_KEYDOWN)
{
if (swapPermission ==true)
{
if (swapIndex==0)
{
swapInitialIndex=stk.length()-1;
}
if ((stk.length()-1)>swapIndex)
{
stk.swap(swapInitialIndex+1,swapInitialIndex);
swapIndex--;
swapInitialIndex++;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
swapPermission = false;
break;
}
}
}
}
}
}
SDL_RenderPresent( gRenderer ); //tempor
if((500/fps)> SDL_GetTicks() - starting_tick) //decreasing frame rate
{
SDL_Delay(500/fps - (SDL_GetTicks() - starting_tick ));
}
}
while (pointPermission ==true)
{
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
pointPermission = false;
}
if( e.type == SDL_MOUSEMOTION || e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP )
{
SDL_GetMouseState( &x, &y );
if(e.type == SDL_MOUSEBUTTONDOWN)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
if(mouseClicked == false)
{
mouseClicked = true;
oldx = x;
oldy = y;
}
}
}
if(e.type == SDL_MOUSEBUTTONUP)
{
if (e.button.button == SDL_BUTTON_LEFT)
{
mouseClicked = false;
point = new Point();
point->giveCoods(oldx,oldy);
stk.Push(point);
stk.Show(gRenderer);
shapesInStack++;
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
point->draw(gRenderer);
}
if (e.button.button == SDL_BUTTON_RIGHT)
{
if (shapesInStack > 1)
{
shapesInStack--;
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
redoArrayLength++;
stkRedo.Push_redo(stk.Pop());
stk.Show(gRenderer);
}
}
if (e.button.button == SDL_BUTTON_MIDDLE)
{
if (redoNumber-1 <= redoArrayLength+1)
{
(stkRedo.Pop_redo())->draw(gRenderer);
redoNumber = redoNumber+1;
redoArrayLength = redoArrayLength -1;
}
}
}
}
}
SDL_RenderPresent( gRenderer );
switch( e.key.keysym.sym )
{
if (e.type == SDL_KEYDOWN)
{
case SDLK_l:
pointPermission = false;
linePermission = true;
break;
case SDLK_r:
pointPermission = false;
rectanglePermission = true;
break;
case SDLK_DOWN:
{
if (e.type == SDL_KEYUP)
{
allowUp = true;
swapPermission = true;
}
if (e.type == SDL_KEYDOWN)
{
if (swapPermission ==true)
{
if (swapIndex==0)
{
swapInitialIndex=stk.length()-1;
}
if ((stk.length())-2>swapIndex)
{
stk.swap(swapInitialIndex-1,swapInitialIndex);
swapIndex++;
swapInitialIndex--;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
swapPermission = false;
break;
}
}
}
case SDLK_UP:
{
if (allowUp == true)
{
if (e.type == SDL_KEYUP)
{
swapPermission = true;
}
if (e.type == SDL_KEYDOWN)
{
if (swapPermission ==true)
{
if (swapIndex==0)
{
swapInitialIndex=stk.length()-1;
}
if ((stk.length()-1)>swapIndex)
{
stk.swap(swapInitialIndex+1,swapInitialIndex);
swapIndex--;
swapInitialIndex++;
}
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
stk.Show(gRenderer);
swapPermission = false;
break;
}
}
}
}
}
}
if((500/fps)> SDL_GetTicks() - starting_tick) //decreasing frame rate
{
SDL_Delay(500/fps - (SDL_GetTicks() - starting_tick ));
}
}
if((500/fps)> SDL_GetTicks() - starting_tick) //decreasing frame rate
{
SDL_Delay(500/fps - (SDL_GetTicks() - starting_tick ));
}
}
}
}
}
}
}
stk.clearAll();
stk.~Stack(); //destructor /deallocates stack
stkRedo.~Stack_redo();//destructor /deallocates stack
close();
return 0;
}