@@ -22,20 +22,29 @@ int main(int argc, char *argv[]) {
22
22
SDL_Event event ; //used for reading events in the event queue
23
23
Line point ; //this line object will be manipulated by adjusting its end-point coordinates, and rendered into the renderer
24
24
25
- int xPixels = (WIDTH / UNIT ); //defines the number of horizontal pixels to be added to/subtracted from the current x-coordinate of a point
26
- int yPixels = (WIDTH / UNIT ); //defines the number of vertical pixels to be added to/subtracted from the current y-coordinate of a point
27
-
28
25
//mark the center point
29
26
int center [2 ] = {WIDTH / 2 , HEIGHT / 2 };
30
27
31
- //mark the beginning of the animation
32
- //this is probably one of the few instances where goto is advantageous
33
- BEGIN :
28
+ int xPixels = (WIDTH - MARGIN ) / UNIT ; //defines the number of horizontal pixels to be added to/subtracted from the current x-coordinate of a point
29
+ int yPixels = (HEIGHT - MARGIN ) / UNIT ; //defines the number of vertical pixels to be added to/subtracted from the current y-coordinate of a point
30
+
31
+ if (xPixels < yPixels ) {
32
+ yPixels = xPixels ;
33
+ xPixels = yPixels ;
34
+ }
35
+ else {
36
+ xPixels = yPixels ;
37
+ yPixels = xPixels ;
38
+ }
34
39
35
40
//set the background color to {199, 0, 57, 255}
36
41
SDL_SetRenderDrawColor (renderer , 199 , 0 , 57 , 255 );
37
42
SDL_RenderClear (renderer );
38
43
44
+ //mark the beginning of the animation
45
+ //this is probably one of the few scenarios where goto is advantageous
46
+ BEGIN :
47
+
39
48
//initially set the drawing color to white
40
49
SDL_RenderPresent (renderer );
41
50
SDL_SetRenderDrawColor (renderer , 255 , 255 , 255 , 255 );
@@ -44,13 +53,13 @@ int main(int argc, char *argv[]) {
44
53
while (1 ) {
45
54
//set the initial position of the line to vertical, centered
46
55
point .A [0 ] = center [0 ];
47
- point .A [1 ] = xPixels ;
56
+ point .A [1 ] = HEIGHT - ( yPixels * UNIT ) ;
48
57
49
58
point .B [0 ] = center [0 ];
50
59
point .B [1 ] = center [1 ];
51
60
52
61
//FOR QUADRANT 1:
53
- while (point .A [1 ] + yPixels <= point . B [1 ]) {
62
+ while (point .A [1 ] < center [1 ]) {
54
63
//check if user wants to restart the animation from the beginning or terminate the program
55
64
switch (userRequest (& event )) {
56
65
case RESTART :
@@ -59,18 +68,18 @@ int main(int argc, char *argv[]) {
59
68
goto END ;
60
69
}
61
70
62
- //Change the position of the line accordingly
63
- point .A [1 ] += yPixels ;
64
- point .B [0 ] += xPixels ;
65
-
66
71
//display the line in its updated position
67
72
drawLine (& renderer , point .A , point .B );
68
73
SDL_RenderPresent (renderer );
69
- SDL_Delay (100 );
74
+ SDL_Delay (DELAY );
75
+
76
+ //Change the position of the line accordingly
77
+ point .A [1 ] += yPixels ;
78
+ point .B [0 ] += xPixels ;
70
79
}
71
80
72
81
//FOR QUADRANT 4:
73
- while (point .B [0 ] - xPixels > center [0 ]) {
82
+ while (point .B [0 ] > center [0 ]) {
74
83
//check if user wants to restart the animation from the beginning or terminate the program
75
84
switch (userRequest (& event )) {
76
85
case RESTART :
@@ -79,18 +88,18 @@ int main(int argc, char *argv[]) {
79
88
goto END ;
80
89
}
81
90
82
- //Change the position of the line accordingly
83
- point .B [0 ] -= xPixels ;
84
- point .A [1 ] += yPixels ;
85
-
86
91
//display the line in its updated position
87
92
drawLine (& renderer , point .A , point .B );
88
93
SDL_RenderPresent (renderer );
89
- SDL_Delay (100 );
94
+ SDL_Delay (DELAY );
95
+
96
+ //Change the position of the line accordingly
97
+ point .B [0 ] -= xPixels ;
98
+ point .A [1 ] += yPixels ;
90
99
}
91
100
92
101
//FOR QUADRANT 3:
93
- while (point .A [1 ] - yPixels > center [1 ]) {
102
+ while (point .A [1 ] - 5 > center [1 ]) {
94
103
//check if user wants to restart the animation from the beginning or terminate the program
95
104
switch (userRequest (& event )) {
96
105
case RESTART :
@@ -99,14 +108,14 @@ int main(int argc, char *argv[]) {
99
108
goto END ;
100
109
}
101
110
102
- //Change the position of the line accordingly
103
- point .B [0 ] -= xPixels ;
104
- point .A [1 ] -= yPixels ;
105
-
106
111
//display the line in its updated position
107
112
drawLine (& renderer , point .A , point .B );
108
113
SDL_RenderPresent (renderer );
109
- SDL_Delay (100 );
114
+ SDL_Delay (DELAY );
115
+
116
+ //Change the position of the line accordingly
117
+ point .B [0 ] -= xPixels ;
118
+ point .A [1 ] -= yPixels ;
110
119
}
111
120
112
121
//FOR QUADRANT 2:
@@ -119,14 +128,14 @@ int main(int argc, char *argv[]) {
119
128
goto END ;
120
129
}
121
130
122
- //Change the position of the line accordingly
123
- point .B [0 ] += xPixels ;
124
- point .A [1 ] -= yPixels ;
125
-
126
131
//display the line in its updated position
127
132
drawLine (& renderer , point .A , point .B );
128
133
SDL_RenderPresent (renderer );
129
- SDL_Delay (100 );
134
+ SDL_Delay (DELAY );
135
+
136
+ //Change the position of the line accordingly
137
+ point .B [0 ] += xPixels ;
138
+ point .A [1 ] -= yPixels ;
130
139
}
131
140
132
141
//set the drawing color to a randomly generated color
0 commit comments