comparison test/testintersections.c @ 2997:e4f025078c1c

indent
author Sam Lantinga <slouken@libsdl.org>
date Sun, 04 Jan 2009 23:41:09 +0000
parents 7563b99e9a49
children 438ba87e9578
comparison
equal deleted inserted replaced
2996:972a69e47cd9 2997:e4f025078c1c
65 } 65 }
66 66
67 #define MAX_LINES 16 67 #define MAX_LINES 16
68 int num_lines = 0; 68 int num_lines = 0;
69 SDL_Rect lines[MAX_LINES]; 69 SDL_Rect lines[MAX_LINES];
70 static int add_line(int x1, int y1, int x2, int y2) { 70 static int
71 if (num_lines >= MAX_LINES) return 0; 71 add_line(int x1, int y1, int x2, int y2)
72 if ((x1 == x2) && (y1 == y2)) return 0; 72 {
73 if (num_lines >= MAX_LINES)
74 return 0;
75 if ((x1 == x2) && (y1 == y2))
76 return 0;
73 77
74 printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2); 78 printf("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
75 lines[num_lines].x = x1; 79 lines[num_lines].x = x1;
76 lines[num_lines].y = y1; 80 lines[num_lines].y = y1;
77 lines[num_lines].w = x2; 81 lines[num_lines].w = x2;
108 } 112 }
109 113
110 #define MAX_RECTS 16 114 #define MAX_RECTS 16
111 int num_rects = 0; 115 int num_rects = 0;
112 SDL_Rect rects[MAX_RECTS]; 116 SDL_Rect rects[MAX_RECTS];
113 static int add_rect(int x1, int y1, int x2, int y2) { 117 static int
114 if (num_rects >= MAX_RECTS) return 0; 118 add_rect(int x1, int y1, int x2, int y2)
115 if ((x1 == x2) || (y1 == y2)) return 0; 119 {
116 120 if (num_rects >= MAX_RECTS)
117 if (x1 > x2) SWAP(int, x1, x2); 121 return 0;
118 if (y1 > y2) SWAP(int, y1, y2); 122 if ((x1 == x2) || (y1 == y2))
119 123 return 0;
120 printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2, x2-x1, y2-y1); 124
125 if (x1 > x2)
126 SWAP(int, x1, x2);
127 if (y1 > y2)
128 SWAP(int, y1, y2);
129
130 printf("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
131 x2 - x1, y2 - y1);
121 132
122 rects[num_rects].x = x1; 133 rects[num_rects].x = x1;
123 rects[num_rects].y = y1; 134 rects[num_rects].y = y1;
124 rects[num_rects].w = x2 - x1; 135 rects[num_rects].w = x2 - x1;
125 rects[num_rects].h = y2 - y1; 136 rects[num_rects].h = y2 - y1;
153 SDL_GetWindowSize(window, &window_w, &window_h); 164 SDL_GetWindowSize(window, &window_w, &window_h);
154 165
155 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); 166 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
156 167
157 for (i = 0; i < num_rects; i++) 168 for (i = 0; i < num_rects; i++)
158 for (j = 0; j < num_lines; j++) { 169 for (j = 0; j < num_lines; j++) {
159 int x1, y1, x2, y2; 170 int x1, y1, x2, y2;
160 SDL_Rect r; 171 SDL_Rect r;
161 172
162 r = rects[i]; 173 r = rects[i];
163 x1 = lines[j].x; 174 x1 = lines[j].x;
164 y1 = lines[j].y; 175 y1 = lines[j].y;
165 x2 = lines[j].w; 176 x2 = lines[j].w;
166 y2 = lines[j].h; 177 y2 = lines[j].h;
167 178
168 if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) { 179 if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
169 SDL_SetRenderDrawColor(0, 255, 55, 255); 180 SDL_SetRenderDrawColor(0, 255, 55, 255);
170 SDL_RenderLine(x1, y1, x2, y2); 181 SDL_RenderLine(x1, y1, x2, y2);
171 } 182 }
172 } 183 }
173 184
174 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); 185 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
175 } 186 }
176 187
177 static void 188 static void
180 int i, j; 191 int i, j;
181 192
182 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); 193 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
183 194
184 for (i = 0; i < num_rects; i++) 195 for (i = 0; i < num_rects; i++)
185 for (j = i+1; j < num_rects; j++) { 196 for (j = i + 1; j < num_rects; j++) {
186 SDL_Rect r; 197 SDL_Rect r;
187 if (SDL_IntersectRect(&rects[i], &rects[j], &r)) { 198 if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
188 SDL_SetRenderDrawColor(255, 200, 0, 255); 199 SDL_SetRenderDrawColor(255, 200, 0, 255);
189 SDL_RenderFill(&r); 200 SDL_RenderFill(&r);
190 } 201 }
191 } 202 }
192 203
193 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE); 204 SDL_SetRenderDrawBlendMode(SDL_BLENDMODE_NONE);
194 } 205 }
195 206
196 int 207 int
283 } 294 }
284 break; 295 break;
285 case SDL_MOUSEBUTTONUP: 296 case SDL_MOUSEBUTTONUP:
286 if (event.button.which == 0) { 297 if (event.button.which == 0) {
287 if (event.button.button == 3) 298 if (event.button.button == 3)
288 add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); 299 add_line(mouse_begin_x, mouse_begin_y, event.button.x,
300 event.button.y);
289 if (event.button.button == 1) 301 if (event.button.button == 1)
290 add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y); 302 add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
303 event.button.y);
291 } 304 }
292 break; 305 break;
293 case SDL_KEYDOWN: 306 case SDL_KEYDOWN:
294 switch (event.key.keysym.sym) { 307 switch (event.key.keysym.sym) {
295 case 'l': 308 case 'l':
296 if (event.key.keysym.mod & KMOD_SHIFT) num_lines = 0; 309 if (event.key.keysym.mod & KMOD_SHIFT)
297 else add_line(rand()%640, rand()%480, rand()%640, rand()%480); 310 num_lines = 0;
298 break; 311 else
299 case 'r': 312 add_line(rand() % 640, rand() % 480, rand() % 640,
300 if (event.key.keysym.mod & KMOD_SHIFT) num_rects = 0; 313 rand() % 480);
301 else add_rect(rand()%640, rand()%480, rand()%640, rand()%480); 314 break;
302 break; 315 case 'r':
316 if (event.key.keysym.mod & KMOD_SHIFT)
317 num_rects = 0;
318 else
319 add_rect(rand() % 640, rand() % 480, rand() % 640,
320 rand() % 480);
321 break;
303 } 322 }
304 break; 323 break;
305 case SDL_WINDOWEVENT: 324 case SDL_WINDOWEVENT:
306 switch (event.window.event) { 325 switch (event.window.event) {
307 case SDL_WINDOWEVENT_EXPOSED: 326 case SDL_WINDOWEVENT_EXPOSED: