File: draw.cpp

package info (click to toggle)
openssn 1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,340 kB
  • sloc: cpp: 10,973; makefile: 80
file content (246 lines) | stat: -rw-r--r-- 8,090 bytes parent folder | download | duplicates (3)
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
#include <SDL.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "SDL/SDL_image.h"
#include <SDL/SDL_thread.h>
#include <math.h>
#include "draw.h"


void DrawPixel(SDL_Surface *screen, int x, int y, Uint32 color){
        //this only works for 16bpp screens
        //are we outside the screen?????
        //If we are bail out now before it's too late!

        if (x > 1023 || x < 0 || y > 759 || y < 0) {
                return;
        }

        //place the pixel on the screen
        Uint16 *pixel_location;
        pixel_location = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
        *pixel_location = color;
}

void DrawLine(SDL_Surface *screen, int X1, int Y1, int X2, int Y2, Uint32 Color){

//don't even ask me about this stuff All I know is it works
//and thats ALL I care about...

        int dx,dy,sdx,sdy,py,px,x,y;
        dx = X2 - X1;
        dy = Y2 - Y1;
        if (dx < 0) sdx = -1;
        else sdx = 1;
        if (dy < 0) sdy = -1;
        else sdy = 1;
        dx = sdx * dx + 1;
        dy = sdy * dy + 1;
        x = 0;
        y = 0;
        px = X1;
        py = Y1;
        if (dx >= dy){
                for (int x = 0; x < dx; x++){
                        DrawPixel(screen, px, py, Color);
                        y = y + dy;
                        if (y >= dx){
                                y = y - dx;
                                py = py + sdy;
                        }
                        px = px + sdx;
                }
        }else{
                for (int y = 0; y < dy; y++){
                        DrawPixel(screen, px, py, Color);
                        x = x + dx;
                        if (x >= dy){
                                x = x - dy;
                                px = px + sdx;
                        }
                        py = py + sdy;
                }
        }
}


void DrawArc(SDL_Surface *screen, int X1, int Y1, int Radius, int Theta1, int Theta2, Uint32 Color){

//Draw an arc  at (X1,Y1) of a given radius from theta1 to theta2 using specified Color.
        int x, y, xc, yc, radius;
        int theta, theta1, theta2;
        xc = X1;
        yc = Y1;
        radius = Radius;
        theta1 = Theta1;
        theta2 = Theta2;

        for(theta=theta1;theta<=theta2;theta+=5) {
          x = xc + int(radius*cos(theta*3.14/180.0));
          y = yc - int(radius*sin(theta*3.14/180.0));
          DrawPixel(screen, x, y, Color);
        }
}

void DrawCircle(SDL_Surface *screen, int X1, int Y1, int Radius, Uint32 Color){

//Draw a circle  at (X1,Y1) of a given radius using specified Color.
/*
        int xc, yc, radius;
        xc = X1;
        yc = Y1;
        radius = Radius;
*/
        DrawArc(screen, X1, Y1, Radius, 0, 360, Color);
}


void FillCircle(SDL_Surface *screen, int X1, int Y1, int Radius, Uint32 Color)
{
   int my_radius;
   my_radius = Radius;
   while (my_radius > 1)
   {
       DrawCircle(screen, X1, Y1, my_radius, Color);
       my_radius--;
   }
}

void DrawDiamond(SDL_Surface *screen, int X1, int Y1, int Size, char Direction, Uint32 Color){

  //Draw a diamond  at (X1,Y1) of Size using specified Color.
  // Direction: T = Top, B = Bottom, F = Full
        int xc, yc;
        int size;
        xc = X1;
        yc = Y1;
        size = Size;

        switch(Direction){

        case 'B':
          DrawLine(screen, xc-size, yc, xc, yc+size, Color); //bottomleft
          DrawLine(screen, xc+size, yc, xc, yc+size, Color); //bottomright
          break;

        case 'T':
          DrawLine(screen, xc, yc-size, xc-size, yc, Color); //topleft
          DrawLine(screen, xc, yc-size, xc+size, yc, Color); //topright
          break;

        default:
        DrawLine(screen, xc-size, yc, xc, yc+size, Color); //bottomleft
        DrawLine(screen, xc+size, yc, xc, yc+size, Color); //bottomright
        DrawLine(screen, xc, yc-size, xc-size, yc, Color); //topleft
        DrawLine(screen, xc, yc-size, xc+size, yc, Color); //topright
        }
}

void DrawBox(SDL_Surface *screen, int X1, int Y1, int Size, char Direction, Uint32 Color){

  //Draw a Box  at (X1,Y1) of Size using specified Color.
  // Direction: T = Top, B = Bottom, F = Full
        int xc, yc;
        int size;
        char direction;
        xc = X1;
        yc = Y1;
        size = Size;
        direction = Direction;

        switch(direction){

        case 'T':
          DrawLine(screen, xc-size, yc-size, xc-size, yc, Color);
          DrawLine(screen, xc-size, yc-size, xc+size, yc-size, Color);
          DrawLine(screen, xc+size, yc-size, xc+size, yc, Color);
          break;

        case 'B':
          DrawLine(screen, xc-size, yc, xc-size, yc+size, Color);
          DrawLine(screen, xc-size, yc+size, xc+size, yc+size, Color);
          DrawLine(screen, xc+size, yc, xc+size, yc+size, Color);
          break;

        default:
        DrawLine(screen, xc-size, yc-size, xc+size, yc-size, Color);
        DrawLine(screen, xc+size, yc-size, xc+size, yc+size, Color);
        DrawLine(screen, xc-size, yc-size, xc-size, yc+size, Color);
        DrawLine(screen, xc-size, yc+size, xc+size, yc+size, Color);
        }
}


void DrawRectangle(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint32 color)
{
   DrawLine(screen, x1, y1, x1, y2, color);
   DrawLine(screen, x1, y1, x2, y1, color);
   DrawLine(screen, x1, y2, x2, y2, color);
   DrawLine(screen, x2, y1, x2, y2, color);
}


void FillRectangle(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint32 color)
{
   int x, y;

   for (y = y1; y <= y2; y++)
   {
      for (x = x1; x <= x2; x++)
      {
         DrawPixel(screen, x, y, color);
      }
   }
}


 
void DrawCross(SDL_Surface *screen, int X1, int Y1, int Size, char Direction, Uint32 Color){

  //Draw a Cross  at (X1,Y1) of Size using specified Color.
  // Direction: T = Top, B = Bottom, F = Full
        int xc, yc;
        int size;
        char direction;
        xc = X1;
        yc = Y1;
        size = Size;
        direction = Direction;

        switch(direction){

        case 'T':
          DrawLine(screen, xc-size, yc-size/2, xc-size, yc, Color);
          DrawLine(screen, xc+size, yc-size/2, xc+size, yc, Color);
          DrawLine(screen, xc-size/2, yc-size, xc+size/2, yc-size, Color);
          DrawLine(screen, xc-size/2, yc-size, xc-size/2, yc-size/2, Color);
          DrawLine(screen, xc+size/2, yc-size, xc+size/2, yc-size/2, Color);
          DrawLine(screen, xc+size/2, yc-size/2, xc+size, yc-size/2, Color);
          DrawLine(screen, xc-size, yc-size/2, xc-size/2, yc-size/2, Color);
          break;

        case 'B':
          DrawLine(screen, xc-size, yc, xc-size, yc+size/2, Color);
          DrawLine(screen, xc+size, yc, xc+size, yc+size/2, Color);
          DrawLine(screen, xc-size/2, yc+size, xc+size/2, yc+size, Color);
          DrawLine(screen, xc-size/2, yc+size/2, xc-size/2, yc+size, Color);
          DrawLine(screen, xc+size/2, yc+size/2, xc+size/2, yc+size, Color);
          DrawLine(screen, xc-size, yc+size/2, xc-size/2, yc+size/2, Color);
          DrawLine(screen, xc+size/2, yc+size/2, xc+size, yc+size/2, Color);
          break;

        default:
          DrawLine(screen, xc-size, yc-size/2, xc-size, yc+size/2, Color);
          DrawLine(screen, xc+size, yc-size/2, xc+size, yc+size/2, Color);
          DrawLine(screen, xc-size/2, yc-size, xc+size/2, yc-size, Color);
          DrawLine(screen, xc-size/2, yc+size, xc+size/2, yc+size, Color);
          DrawLine(screen, xc-size/2, yc-size, xc-size/2, yc-size/2, Color);
          DrawLine(screen, xc-size/2, yc+size/2, xc-size/2, yc+size, Color);
          DrawLine(screen, xc+size/2, yc-size, xc+size/2, yc-size/2, Color);
          DrawLine(screen, xc+size/2, yc+size/2, xc+size/2, yc+size, Color);
          DrawLine(screen, xc-size, yc+size/2, xc-size/2, yc+size/2, Color);
          DrawLine(screen, xc+size/2, yc-size/2, xc+size, yc-size/2, Color);
          DrawLine(screen, xc-size, yc-size/2, xc-size/2, yc-size/2, Color);
          DrawLine(screen, xc+size/2, yc+size/2, xc+size, yc+size/2, Color);
        }
}