00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "mxinterface.h"
00017
00018 namespace mx
00019 {
00020
00021 mxSurfacePainter::mxSurfacePainter(mxSurface *surface)
00022 {
00023 this->surface = surface;
00024 pbuf = 0;
00025 locked = false;
00026 font = 0;
00027
00028 }
00029
00030 const bool mxSurfacePainter::paintLock()
00031 {
00032
00033 pbuf = lock(this->surface->getSurface(), this->surface->getSurface()->format->BytesPerPixel);
00034
00035 if(pbuf) locked = true;
00036
00037
00038 return true;
00039
00040 }
00041
00042
00043 const bool mxSurfacePainter::paintUnlock()
00044 {
00045 unlock(this->surface->getSurface());
00046
00047 locked = false;
00048
00049 return true;
00050 }
00051
00052 const bool mxSurfacePainter::getPixel(int x, int y, SDL_Color *col, Color &return_color)
00053 {
00054
00055
00056 if(locked == false) return false;
00057
00058
00059
00060
00061 getpixel(this->surface->getSurface(), x , y , surface->getSurface()->format->BitsPerPixel, surface->getSurface()->pitch, col);
00062
00063 return_color.color.colors[0] = col->r;
00064 return_color.color.colors[1] = col->g;
00065 return_color.color.colors[2] = col->b;
00066
00067
00068 return true;
00069 }
00070
00071 const Uint32 mxSurfacePainter::getPixel(int x, int y, SDL_Color *col)
00072 {
00073 Uint32 rt_val = 0;
00074
00075 rt_val = getpixel(surface->getSurface(), x, y, surface->getSurface()->format->BitsPerPixel, surface->getSurface()->pitch, col);
00076
00077
00078 return rt_val;
00079 }
00080
00081 const bool mxSurfacePainter::setPixel(int x, int y, Color colz)
00082 {
00083
00084
00085 if(locked == false) return false;
00086
00087
00088 setpixel(pbuf, x,y, SDL_MapRGB(this->surface->getSurface()->format, colz.color.colors[0], colz.color.colors[1], colz.color.colors[2]), this->surface->getSurface()->format->BytesPerPixel, this->surface->getSurface()->pitch);
00089
00090 return true;
00091
00092 }
00093
00094
00095 const bool mxSurfacePainter::setPixel(int x, int y, Uint32 color)
00096 {
00097 if(locked == false) return false;
00098
00099 setpixel(pbuf, x, y, color, surface->getSurface()->format->BytesPerPixel, surface->getSurface()->pitch);
00100
00101 return true;
00102
00103 }
00104
00105 void mxSurfacePainter::resetSurface(mxSurface *surf)
00106 {
00107 surface = surf;
00108 }
00109
00110
00111
00112
00113 void mxSurfacePainter::printText(int x, int y, Color col,std::string text)
00114 {
00115
00116 if(font == 0) throw mxException<string>(" No found font set on printtext [:" + text + ":]");
00117
00118 SDL_PrintText(surface->getSurface(), font, x,y, SDL_MapRGB(surface->getSurface()->format, col.color.colors[0], col.color.colors[1], col.color.colors[2]), text.c_str());
00119
00120 }
00121
00122
00123
00124
00125
00126 void mxPainter::lock()
00127 {
00128 surface->lockSurface();
00129 }
00130
00131
00132 void mxPainter::unlock()
00133 {
00134 surface->unlockSurface();
00135 }
00136
00137 void mxPainter::GetPixel(int x, int y, mx::Color &col) {
00138
00139 if(!(x >= 0 && x < surface->size().width && y >= 0 && y < surface->size().height))
00140 return;
00141
00142 unsigned int color = this->getpixel(x,y);
00143 unsigned char r,g,b;
00144 SDL_GetRGB(color,surface->getSurface()->format, &r,&g,&b);
00145 col = mx::Color(r,g,b);
00146 }
00147
00148 void mxPainter::fillRect(SDL_Rect *rc, mx::Color &color)
00149 {
00150 SDL_FillRect(surface->getSurface(), rc, mx::Color::mapRGB(*surface, color));
00151 }
00152
00153 void mxPainter::fillRect(int x, int y, int w, int h, mx::Color &color)
00154 {
00155 SDL_Rect rc = { x,y,w,h };
00156 fillRect(&rc, color);
00157 }
00158
00159 void mxPainter::fillRect(Rect rc, Color col)
00160 {
00161 SDL_Rect rcx = { rc.x(), rc.y(), rc.width(), rc.height() };
00162 SDL_FillRect(surface->getSurface(), &rcx, mx::Color::mapRGB(*surface, col));
00163 }
00164
00165 void mxPainter::drawLine( Line line, Color color )
00166 {
00167 drawLine(line.x1(), line.y1(), line.x2(), line.y2(), color);
00168 }
00169
00170 void mxPainter::drawSquare(Rect rc)
00171 {
00172 Color color = Color(255,255,255);
00173
00174 drawVertical(rc.x(), rc.x()+rc.width(), rc.y(), color);
00175 drawVertical(rc.x(), rc.x()+rc.width(), rc.y()+rc.height(), color);
00176 drawHorizontal(rc.x(), rc.y(), rc.y()+rc.height(), color);
00177 drawHorizontal(rc.x()+rc.width(), rc.y(), rc.y()+rc.height(), color);
00178 }
00179
00180 void mxPainter::drawHorizontal(int orig_x, int start_y, int stop_y, Color color)
00181 {
00182 register int y;
00183 for(y = start_y; y < stop_y; y++)
00184 setpixel(orig_x, y, color);
00185 }
00186
00187 void mxPainter::drawVertical(int start_x, int stop_x, int orig_y, Color color)
00188 {
00189 register int x;
00190 for(x = start_x; x < stop_x; x++)
00191 setpixel(x,orig_y,color);
00192 }
00193
00194
00195 void mxPainter::drawLine(int start_x, int start_y, int stop_x, int stop_y, Color color)
00196 {
00197 unsigned int cur_color = mx::Color::mapRGB(*surface, color);
00198 int y_unit,x_unit;
00199 int ydiff = stop_y-start_y;
00200 if(ydiff<0)
00201 {
00202 ydiff = -ydiff;
00203 y_unit=-1;
00204 }
00205 else
00206 {
00207 y_unit =1;
00208 }
00209 int xdiff=stop_x-start_x;
00210 if(xdiff<0)
00211 {
00212 xdiff=-xdiff;
00213 x_unit = -1;
00214 }
00215 else
00216 {
00217 x_unit = 1;
00218 }
00219
00220 int error_term=0;
00221 if(xdiff>ydiff)
00222 {
00223 int length=xdiff+1;
00224 for(int i = 0; i <length; i++)
00225 {
00226 setpixel(start_x, start_y, cur_color);
00227
00228 start_x += x_unit;
00229 error_term+=ydiff;
00230 if(error_term>xdiff)
00231 {
00232 error_term-=xdiff;
00233 start_y+=y_unit;
00234 }
00235 }
00236 }
00237 else
00238 {
00239 int length = ydiff+1;
00240 for(int i = 0; i < length; i++)
00241 {
00242 setpixel(start_x, start_y, cur_color);
00243 start_y += y_unit;
00244 error_term+=xdiff;
00245 if(error_term>0)
00246 {
00247 error_term-=ydiff;
00248 start_x += x_unit;
00249 }
00250
00251 }
00252 }
00253
00254 }
00255
00256 }
00257
00258
00259
00260
00261
00262
00263
00264