00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include "guichan/allegro/allegrographics.hpp"
00062 #include "guichan/allegro/allegroimage.hpp"
00063 #include "guichan/rectangle.hpp"
00064 #include "guichan/exception.hpp"
00065 #include "guichan/cliprectangle.hpp"
00066 #include "guichan/color.hpp"
00067
00068 namespace gcn
00069 {
00070 AllegroGraphics::AllegroGraphics()
00071 {
00072 mTarget = NULL;
00073 mClipNull = false;
00074 }
00075
00076 AllegroGraphics::AllegroGraphics(BITMAP *target)
00077 {
00078 mTarget = target;
00079 }
00080
00081 AllegroGraphics::~AllegroGraphics()
00082 {
00083 }
00084
00085 void AllegroGraphics::setTarget(BITMAP *target)
00086 {
00087 mTarget = target;
00088 }
00089
00090 BITMAP *AllegroGraphics::getTarget()
00091 {
00092 return mTarget;
00093 }
00094
00095 void AllegroGraphics::_beginDraw()
00096 {
00097 if (mTarget == NULL)
00098 {
00099 throw GCN_EXCEPTION("Target BITMAP is null, set it with setTarget first.");
00100 }
00101
00102
00103 pushClipArea(Rectangle(0, 0, mTarget->w, mTarget->h));
00104 }
00105
00106 void AllegroGraphics::_endDraw()
00107 {
00108
00109 popClipArea();
00110 }
00111
00112 bool AllegroGraphics::pushClipArea(Rectangle area)
00113 {
00114 bool result = Graphics::pushClipArea(area);
00115
00116 ClipRectangle cr = mClipStack.top();
00117
00118
00119
00120
00121 if (cr.width == 0 || cr.height == 0)
00122 {
00123 mClipNull = true;
00124 }
00125 else
00126 {
00127 mClipNull = false;
00128 #if ALLEGRO_VERSION == 4 && ALLEGRO_SUB_VERSION == 0
00129 set_clip(mTarget, cr.x, cr.y, cr.x + cr.width - 1, cr.y + cr.height - 1);
00130 #else
00131 set_clip_rect(mTarget, cr.x, cr.y, cr.x + cr.width - 1, cr.y + cr.height - 1);
00132 #endif
00133 }
00134
00135 return result;
00136 }
00137
00138 void AllegroGraphics::popClipArea()
00139 {
00140 Graphics::popClipArea();
00141
00142 if (mClipStack.empty())
00143 {
00144 return;
00145 }
00146
00147 ClipRectangle cr = mClipStack.top();
00148
00149
00150
00151
00152 if (cr.width == 0 || cr.height == 0)
00153 {
00154 mClipNull = true;
00155 }
00156 else
00157 {
00158 mClipNull = false;
00159 #if ALLEGRO_VERSION == 4 && ALLEGRO_SUB_VERSION == 0
00160 set_clip(mTarget, cr.x, cr.y, cr.x + cr.width - 1, cr.y + cr.height - 1);
00161 #else
00162 set_clip_rect(mTarget, cr.x, cr.y, cr.x + cr.width - 1, cr.y + cr.height - 1);
00163 #endif
00164 }
00165 }
00166
00167 void AllegroGraphics::drawImage(const Image* image,
00168 int srcX, int srcY,
00169 int dstX, int dstY,
00170 int width, int height)
00171 {
00172 if (mClipNull)
00173 {
00174 return;
00175 }
00176
00177 dstX += mClipStack.top().xOffset;
00178 dstY += mClipStack.top().yOffset;
00179
00180 const AllegroImage* srcImage = dynamic_cast<const AllegroImage*>(image);
00181
00182 if (srcImage == NULL)
00183 {
00184 throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be an AllegroImage.");
00185 }
00186
00187 masked_blit(srcImage->getBitmap(), mTarget, srcX, srcY, dstX, dstY, width, height);
00188 }
00189
00190 void AllegroGraphics::drawPoint(int x, int y)
00191 {
00192 if (mClipNull)
00193 {
00194 return;
00195 }
00196
00197 int xOffset = mClipStack.top().xOffset;
00198 int yOffset = mClipStack.top().yOffset;
00199
00200 putpixel(mTarget,
00201 x + xOffset,
00202 y + yOffset,
00203 mAlColor);
00204 }
00205
00206 void AllegroGraphics::drawLine(int x1, int y1, int x2, int y2)
00207 {
00208 if (mClipNull)
00209 {
00210 return;
00211 }
00212
00213 int xOffset = mClipStack.top().xOffset;
00214 int yOffset = mClipStack.top().yOffset;
00215
00216 line(mTarget,
00217 x1 + xOffset,
00218 y1 + yOffset,
00219 x2 + xOffset,
00220 y2 + yOffset,
00221 mAlColor);
00222 }
00223
00224 void AllegroGraphics::drawRectangle(const Rectangle& rectangle)
00225 {
00226 if (mClipNull)
00227 {
00228 return;
00229 }
00230
00231 int xOffset = mClipStack.top().xOffset;
00232 int yOffset = mClipStack.top().yOffset;
00233
00234 rect(mTarget,
00235 rectangle.x + xOffset,
00236 rectangle.y + yOffset,
00237 rectangle.x + rectangle.width - 1 + xOffset,
00238 rectangle.y + rectangle.height - 1 + yOffset,
00239 mAlColor);
00240 }
00241
00242 void AllegroGraphics::fillRectangle(const Rectangle& rectangle)
00243 {
00244 if (mClipNull)
00245 {
00246 return;
00247 }
00248
00249 int xOffset = mClipStack.top().xOffset;
00250 int yOffset = mClipStack.top().yOffset;
00251
00252 rectfill(mTarget,
00253 rectangle.x + xOffset,
00254 rectangle.y + yOffset,
00255 rectangle.x + rectangle.width - 1 + xOffset,
00256 rectangle.y + rectangle.height - 1 + yOffset,
00257 mAlColor);
00258 }
00259
00260 void AllegroGraphics::setColor(const Color& color)
00261 {
00262 mColor = color;
00263 mAlColor = makecol(color.r, color.g, color.b);
00264
00265 if (color.a != 255)
00266 {
00267 set_trans_blender(255, 255, 255, color.a);
00268 drawing_mode(DRAW_MODE_TRANS, NULL, 0, 0);
00269 }
00270 else
00271 {
00272 solid_mode();
00273 }
00274 }
00275
00276 const Color& AllegroGraphics::getColor()
00277 {
00278 return mColor;
00279 }
00280 }