Debian Codegraph @

src/draw.cpp

raw symbols SCIP
1 #include <SDL.h>
2 #include <SDL/SDL.h>
3 #include <SDL/SDL_image.h>
4 #include "SDL/SDL_image.h"
5 #include <SDL/SDL_thread.h>
6 #include <math.h>
7 #include "draw.h"
8
9
10 void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color){
11 //this only works for 16bpp screens
12 //are we outside the screen?????
13 //If we are bail out now before it's too late!
14
15 if (x > 1023 || x < 0 || y > 759 || y < 0) {
16 return;
17 }
18
19 //place the pixel on the screen
20 Uint16 *pixel_location;
21 pixel_location = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
22 *pixel_location = color;
23 }
24
25 void DrawLine(SDL_Surface *screen, int X1, int Y1, int X2, int Y2, Uint32 Color){
26
27 //don't even ask me about this stuff All I know is it works
28 //and thats ALL I care about...
29
30 int dx,dy,sdx,sdy,py,px,x,y;
31 dx = X2 - X1;
32 dy = Y2 - Y1;
33 if (dx < 0) sdx = -1;
34 else sdx = 1;
35 if (dy < 0) sdy = -1;
36 else sdy = 1;
37 dx = sdx * dx + 1;
38 dy = sdy * dy + 1;
39 x = 0;
40 y = 0;
41 px = X1;
42 py = Y1;
43 if (dx >= dy){
44 for (int x = 0; x < dx; x++){
45 DrawPixel(screen, px, py, Color);
46 y = y + dy;
47 if (y >= dx){
48 y = y - dx;
49 py = py + sdy;
50 }
51 px = px + sdx;
52 }
53 }else{
54 for (int y = 0; y < dy; y++){
55 DrawPixel(screen, px, py, Color);
56 x = x + dx;
57 if (x >= dy){
58 x = x - dy;
59 px = px + sdx;
60 }
61 py = py + sdy;
62 }
63 }
64 }
65
66
67 void DrawArc(SDL_Surface *screen, int X1, int Y1, int Radius, int Theta1, int Theta2, Uint32 Color){
68
69 //Draw an arc at (X1,Y1) of a given radius from theta1 to theta2 using specified Color.
70 int x, y, xc, yc, radius;
71 int theta, theta1, theta2;
72 xc = X1;
73 yc = Y1;
74 radius = Radius;
75 theta1 = Theta1;
76 theta2 = Theta2;
77
78 for(theta=theta1;theta<=theta2;theta+=5) {
79 x = xc + int(radius*cos(theta*3.14/180.0));
80 y = yc - int(radius*sin(theta*3.14/180.0));
81 DrawPixel(screen, x, y, Color);
82 }
83 }
84
85 void DrawCircle(SDL_Surface *screen, int X1, int Y1, int Radius, Uint32 Color){
86
87 //Draw a circle at (X1,Y1) of a given radius using specified Color.
88 /*
89 int xc, yc, radius;
90 xc = X1;
91 yc = Y1;
92 radius = Radius;
93 */
94 DrawArc(screen, X1, Y1, Radius, 0, 360, Color);
95 }
96
97
98 void FillCircle(SDL_Surface *screen, int X1, int Y1, int Radius, Uint32 Color)
99 {
100 int my_radius;
101 my_radius = Radius;
102 while (my_radius > 1)
103 {
104 DrawCircle(screen, X1, Y1, my_radius, Color);
105 my_radius--;
106 }
107 }
108
109 void DrawDiamond(SDL_Surface *screen, int X1, int Y1, int Size, char Direction, Uint32 Color){
110
111 //Draw a diamond at (X1,Y1) of Size using specified Color.
112 // Direction: T = Top, B = Bottom, F = Full
113 int xc, yc;
114 int size;
115 xc = X1;
116 yc = Y1;
117 size = Size;
118
119 switch(Direction){
120
121 case 'B':
122 DrawLine(screen, xc-size, yc, xc, yc+size, Color); //bottomleft
123 DrawLine(screen, xc+size, yc, xc, yc+size, Color); //bottomright
124 break;
125
126 case 'T':
127 DrawLine(screen, xc, yc-size, xc-size, yc, Color); //topleft
128 DrawLine(screen, xc, yc-size, xc+size, yc, Color); //topright
129 break;
130
131 default:
132 DrawLine(screen, xc-size, yc, xc, yc+size, Color); //bottomleft
133 DrawLine(screen, xc+size, yc, xc, yc+size, Color); //bottomright
134 DrawLine(screen, xc, yc-size, xc-size, yc, Color); //topleft
135 DrawLine(screen, xc, yc-size, xc+size, yc, Color); //topright
136 }
137 }
138
139 void DrawBox(SDL_Surface *screen, int X1, int Y1, int Size, char Direction, Uint32 Color){
140
141 //Draw a Box at (X1,Y1) of Size using specified Color.
142 // Direction: T = Top, B = Bottom, F = Full
143 int xc, yc;
144 int size;
145 char direction;
146 xc = X1;
147 yc = Y1;
148 size = Size;
149 direction = Direction;
150
151 switch(direction){
152
153 case 'T':
154 DrawLine(screen, xc-size, yc-size, xc-size, yc, Color);
155 DrawLine(screen, xc-size, yc-size, xc+size, yc-size, Color);
156 DrawLine(screen, xc+size, yc-size, xc+size, yc, Color);
157 break;
158
159 case 'B':
160 DrawLine(screen, xc-size, yc, xc-size, yc+size, Color);
161 DrawLine(screen, xc-size, yc+size, xc+size, yc+size, Color);
162 DrawLine(screen, xc+size, yc, xc+size, yc+size, Color);
163 break;
164
165 default:
166 DrawLine(screen, xc-size, yc-size, xc+size, yc-size, Color);
167 DrawLine(screen, xc+size, yc-size, xc+size, yc+size, Color);
168 DrawLine(screen, xc-size, yc-size, xc-size, yc+size, Color);
169 DrawLine(screen, xc-size, yc+size, xc+size, yc+size, Color);
170 }
171 }
172
173
174 void DrawRectangle(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint32 color)
175 {
176 DrawLine(screen, x1, y1, x1, y2, color);
177 DrawLine(screen, x1, y1, x2, y1, color);
178 DrawLine(screen, x1, y2, x2, y2, color);
179 DrawLine(screen, x2, y1, x2, y2, color);
180 }
181
182
183 void FillRectangle(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint32 color)
184 {
185 int x, y;
186
187 for (y = y1; y <= y2; y++)
188 {
189 for (x = x1; x <= x2; x++)
190 {
191 DrawPixel(screen, x, y, color);
192 }
193 }
194 }
195
196
197
198 void DrawCross(SDL_Surface *screen, int X1, int Y1, int Size, char Direction, Uint32 Color){
199
200 //Draw a Cross at (X1,Y1) of Size using specified Color.
201 // Direction: T = Top, B = Bottom, F = Full
202 int xc, yc;
203 int size;
204 char direction;
205 xc = X1;
206 yc = Y1;
207 size = Size;
208 direction = Direction;
209
210 switch(direction){
211
212 case 'T':
213 DrawLine(screen, xc-size, yc-size/2, xc-size, yc, Color);
214 DrawLine(screen, xc+size, yc-size/2, xc+size, yc, Color);
215 DrawLine(screen, xc-size/2, yc-size, xc+size/2, yc-size, Color);
216 DrawLine(screen, xc-size/2, yc-size, xc-size/2, yc-size/2, Color);
217 DrawLine(screen, xc+size/2, yc-size, xc+size/2, yc-size/2, Color);
218 DrawLine(screen, xc+size/2, yc-size/2, xc+size, yc-size/2, Color);
219 DrawLine(screen, xc-size, yc-size/2, xc-size/2, yc-size/2, Color);
220 break;
221
222 case 'B':
223 DrawLine(screen, xc-size, yc, xc-size, yc+size/2, Color);
224 DrawLine(screen, xc+size, yc, xc+size, yc+size/2, Color);
225 DrawLine(screen, xc-size/2, yc+size, xc+size/2, yc+size, Color);
226 DrawLine(screen, xc-size/2, yc+size/2, xc-size/2, yc+size, Color);
227 DrawLine(screen, xc+size/2, yc+size/2, xc+size/2, yc+size, Color);
228 DrawLine(screen, xc-size, yc+size/2, xc-size/2, yc+size/2, Color);
229 DrawLine(screen, xc+size/2, yc+size/2, xc+size, yc+size/2, Color);
230 break;
231
232 default:
233 DrawLine(screen, xc-size, yc-size/2, xc-size, yc+size/2, Color);
234 DrawLine(screen, xc+size, yc-size/2, xc+size, yc+size/2, Color);
235 DrawLine(screen, xc-size/2, yc-size, xc+size/2, yc-size, Color);
236 DrawLine(screen, xc-size/2, yc+size, xc+size/2, yc+size, Color);
237 DrawLine(screen, xc-size/2, yc-size, xc-size/2, yc-size/2, Color);
238 DrawLine(screen, xc-size/2, yc+size/2, xc-size/2, yc+size, Color);
239 DrawLine(screen, xc+size/2, yc-size, xc+size/2, yc-size/2, Color);
240 DrawLine(screen, xc+size/2, yc+size/2, xc+size/2, yc+size, Color);
241 DrawLine(screen, xc-size, yc+size/2, xc-size/2, yc+size/2, Color);
242 DrawLine(screen, xc+size/2, yc-size/2, xc+size, yc-size/2, Color);
243 DrawLine(screen, xc-size, yc-size/2, xc-size/2, yc-size/2, Color);
244 DrawLine(screen, xc+size/2, yc+size/2, xc+size, yc+size/2, Color);
245 }
246 }