-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDraw.cpp
212 lines (174 loc) · 4.76 KB
/
Draw.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
#include "Draw.h"
Draw::Draw()
{
}
Uint32 Draw::getpixel(SDL_Surface *surface, int x, int y)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch (bpp)
{
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void Draw::putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch (bpp)
{
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
}
else
{
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
default:
break; /* shouldn't happen, but avoids warnings */
}
}
void Draw::drawLine(SDL_Surface *surface, float x1, float y1, float x2, float y2, Uint32 pixel )
{
// Bresenham's line algorithm
const bool steep = (fabs(y2 - y1) > fabs(x2 - x1));
if(steep)
{
std::swap(x1, y1);
std::swap(x2, y2);
}
if(x1 > x2)
{
std::swap(x1, x2);
std::swap(y1, y2);
}
const float dx = x2 - x1;
const float dy = fabs(y2 - y1);
float error = dx / 2.0f;
const int ystep = (y1 < y2) ? 1 : -1;
int y = (int)y1;
const int maxX = (int)x2;
for(int x=(int)x1; x<maxX; x++)
{
if(steep)
{
putpixel(surface, y, x, pixel);
}
else
{
putpixel(surface, x, y, pixel);
}
error -= dy;
if(error < 0)
{
y += ystep;
error += dx;
}
}
}
void Draw::drawRect(SDL_Surface *surface, float x, float y, float width, float height, Uint32 pixel )
{
drawLine(surface, x, y, x + width, y, pixel);
drawLine(surface, x, y, x, y + height, pixel);
drawLine(surface, x + width, y + height, x + width, y, pixel);
drawLine(surface, x + width, y + height, x, y + height, pixel);
}
void Draw::drawCircle(SDL_Surface *surface, int n_cx, int n_cy, int radius, Uint32 pixel, bool in_Center = false)
{
// if the first pixel in the screen is represented by (0,0) (which is in sdl)
// remember that the beginning of the circle is not in the middle of the pixel
// but to the left-top from it:
if(!in_Center)
{
n_cx += radius;
n_cy += radius;
}
double error = (double)-radius;
double x = (double)radius -0.5;
double y = (double)0.5;
double cx = n_cx - 0.5;
double cy = n_cy - 0.5;
while (x >= y)
{
putpixel(surface, (int)(cx + x), (int)(cy + y), pixel);
putpixel(surface, (int)(cx + y), (int)(cy + x), pixel);
if (x != 0)
{
putpixel(surface, (int)(cx - x), (int)(cy + y), pixel);
putpixel(surface, (int)(cx + y), (int)(cy - x), pixel);
}
if (y != 0)
{
putpixel(surface, (int)(cx + x), (int)(cy - y), pixel);
putpixel(surface, (int)(cx - y), (int)(cy + x), pixel);
}
if (x != 0 && y != 0)
{
putpixel(surface, (int)(cx - x), (int)(cy - y), pixel);
putpixel(surface, (int)(cx - y), (int)(cy - x), pixel);
}
error += y;
++y;
error += y;
if (error >= 0)
{
--x;
error -= x;
error -= x;
}
}
}
int Draw::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//retangulo para a posicao x,y
SDL_Rect offset;
//pegando as posicoes
offset.x = x;
offset.y = y;
//Colagem da SDL_Surface na screen, com Blit
return SDL_BlitSurface( source, clip, destination, &offset );
}
SDL_Surface* Draw::CreateSurface(Uint32 flags,int width,int height,const SDL_Surface* display)
{
SDL_PixelFormat& fmt = *(display->format);
return SDL_CreateRGBSurface(flags,width,height,
fmt.BitsPerPixel,
fmt.Rmask,fmt.Gmask,fmt.Bmask,fmt.Amask );
}
SDL_Surface* Draw::MyFillRect(int width, int height, const SDL_Surface* display, Uint8 transparency = NULL)
{
SDL_Surface* surface = (*this).CreateSurface(SDL_SWSURFACE, width, height, display);
if(transparency != 0)
SDL_SetAlpha(surface, SDL_SRCALPHA | SDL_RLEACCEL, transparency);
return surface;
}