basicDraw.cpp

00001 /*
00002   基本図形の描画
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "basicDraw.h"
00008 #include "sdlVideo.h"
00009 #include "deleteObjects.h"
00010 #include "viewComponent.h"
00011 
00012 
00013 VXV::Grid BasicDrawInterface::view_pos;
00014 VXV::Rect BasicDrawInterface::view_range;
00015 double BasicDrawInterface::view_magnify;
00016 
00017 
00018 void BasicDrawInterface::setDrawPosition(const VXV::Grid& pos,
00019                                          const VXV::Rect& range,
00020                                          double magnify) {
00021   view_pos = pos;
00022   view_range = range;
00023   view_magnify = magnify;
00024 }
00025 
00026 
00027 void BasicDrawInterface::draw(const VXV::Grid& pos, const VXV::Rect& range,
00028                               double magnify) {
00029   setDrawPosition(pos, range, magnify);
00030   draw();
00031 }
00032 
00033 
00034 VXV::Rect BasicDrawInterface::calcPixelRange(const VXV::Grid& pos,
00035                                         const VXV::Rect& range,
00036                                         double magnify) {
00037   VXV::Rect pixel_range;
00038   pixel_range.x = pos.x;
00039   pixel_range.y = pos.y;
00040   pixel_range.w = pixel_range.x + range.w;
00041   pixel_range.h = pixel_range.y + range.h;
00042 
00043   return pixel_range;
00044 }
00045 
00046 
00047 void BasicDrawInterface::drawRangedPixel(const VXV::Rect& pixel_range,
00048                                          int x, int y) {
00049   if ((x > pixel_range.x) && (x < pixel_range.w) &&
00050       (y > pixel_range.y) && (y < pixel_range.h)) {
00051     PixelDraw::setPixel(x, y);
00052   }
00053 }
00054 
00055 
00056 unsigned long PixelDraw::static_draw_color = White;
00057 
00058 
00059 PixelDraw::PixelDraw(int x, int y, unsigned long color)
00060   : draw_pos(VXV::Grid(x, y)), draw_color(color) {
00061 }
00062 
00063 
00064 void PixelDraw::draw(void) {
00065   VXV::Grid& pos = view_pos;
00066   VXV::Rect& range = view_range;
00067   double magnify = view_magnify;
00068 
00069   VXV::Rect pixel_range = calcPixelRange(pos, range, magnify);
00070   int x = static_cast<int>(rint((draw_pos.x - range.x) * magnify)) + pos.x;
00071   int y = static_cast<int>(rint((-draw_pos.y - range.y) * magnify)) + pos.y;
00072   setColor(draw_color);
00073   drawRangedPixel(pixel_range, x, y);
00074 }
00075 
00076 
00077 void PixelDraw::setColor(unsigned long color) {
00078   static_draw_color = color;
00079 }
00080 
00081 
00082 void PixelDraw::setPixel(int x, int y) {
00083   VXV::Rect rect;
00084   rect.x = x;
00085   rect.y = y;
00086   rect.w = 1;
00087   rect.h = 1;
00088 
00089   SDL_Video::fillRect(rect, static_draw_color);
00090 }
00091 
00092 
00093 PixelGroupDraw::PixelGroupDraw(std::vector<VXV::Grid>& points,
00094                                unsigned long color)
00095   : draw_points(points), draw_color(color) {
00096 }
00097 
00098 
00099 void PixelGroupDraw::draw(void) {
00100   VXV::Grid& pos = view_pos;
00101   VXV::Rect& range = view_range;
00102   double magnify = view_magnify;
00103 
00104   PixelDraw::setColor(draw_color);
00105   VXV::Rect pixel_range = calcPixelRange(pos, range, magnify);
00106   for (std::vector<VXV::Grid>::iterator it = draw_points.begin();
00107        it != draw_points.end(); ++it) {
00108     int x = static_cast<int>(rint((it->x - range.x) * magnify)) + pos.x;
00109     int y = static_cast<int>(rint((-it->y - range.y) * magnify)) + pos.y;
00110     drawRangedPixel(pixel_range, x, y);
00111   }
00112 }
00113 
00114 
00115 LineDraw::LineDraw(const VXV::Grid& p0, const VXV::Grid& p1,
00116                    unsigned long color)
00117   : draw_p0(p0), draw_p1(p1), draw_color(color) {
00118 }
00119 
00120 
00121 void LineDraw::draw(void) {
00122   VXV::Grid& pos = view_pos;
00123   VXV::Rect& range = view_range;
00124   double magnify = view_magnify;
00125 
00126   VXV::Grid p0, p1;
00127   p0.x = static_cast<int>(rint((draw_p0.x - range.x) * magnify)) + pos.x;
00128   p0.y = static_cast<int>(rint((-draw_p0.y - range.y) * magnify)) + pos.y;
00129   p1.x = static_cast<int>(rint((draw_p1.x - range.x) * magnify)) + pos.x;
00130   p1.y = static_cast<int>(rint((-draw_p1.y - range.y) * magnify)) + pos.y;
00131 
00132   VXV::Rect pixel_range = calcPixelRange(pos, range, magnify);
00133 
00134   int width = abs(p1.x - p0.x);
00135   int height = abs(p1.y - p0.y);
00136   int yy = (p0.y < p1.y) ? +1 : -1;
00137   int xx = (p0.x < p1.x) ? +1 : -1;
00138 
00139   PixelDraw::setColor(draw_color);
00140 
00141   if (width >= height) {
00142     int y = p0.y;
00143     int fraction = width;
00144     for (int x = p0.x; x != p1.x; x += xx) {
00145       drawRangedPixel(pixel_range, x, y);
00146       fraction += (height << 1);
00147       if (fraction >= (width << 1)) {
00148         fraction -= (width << 1);
00149         y += yy;
00150       }
00151     }
00152 
00153   } else {
00154     int x = p0.x;
00155     int fraction = height;
00156     for (int y = p0.y; y != p1.y; y += yy) {
00157       drawRangedPixel(pixel_range, x, y);
00158       fraction += (width << 1);
00159       if (fraction >= (height << 1)) {
00160         fraction -= (height << 1);
00161         x += xx;
00162       }
00163     }
00164   }
00165 }
00166 
00167 
00168 ContLineDraw::ContLineDraw(const std::deque<VXV::Grid>& points,
00169                            unsigned long color) {
00170   if (points.size() < 2) {
00171     return;
00172   }
00173   for (std::deque<VXV::Grid>::const_iterator it = points.begin();
00174        it != points.end() -1; ++it) {
00175     draw_lines.push_back(new LineDraw(*it, *(it+1), color));
00176   }
00177 }
00178 
00179 
00180 ContLineDraw::~ContLineDraw(void) {
00181   for_each(draw_lines.begin(), draw_lines.end(), DeleteObjects());
00182 }
00183 
00184 
00185 void ContLineDraw::draw(void) {
00186   for (std::deque<LineDraw*>::iterator it = draw_lines.begin();
00187        it != draw_lines.end(); ++it) {
00188     (*it)->draw();
00189   }
00190 }
00191 
00192 
00193 CircleDraw::CircleDraw(const VXV::Grid& p, int r, unsigned long color)
00194   : draw_p(p), draw_r(r), draw_color(color) {
00195 }
00196 
00197 
00198 void CircleDraw::draw(void) {
00199   VXV::Grid& pos = view_pos;
00200   VXV::Rect& range = view_range;
00201   double magnify = view_magnify;
00202 
00203   int r = static_cast<int>(draw_r * magnify);
00204   int d = 3 - (r << 1);
00205   int cy = r;
00206 
00207   // 開始点を描画
00208   PixelDraw::setColor(draw_color);
00209   VXV::Grid p;
00210   p.x = static_cast<int>(rint((draw_p.x - range.x) * magnify)) + pos.x;
00211   p.y = static_cast<int>(rint((-draw_p.y - range.y) * magnify)) + pos.y;
00212 
00213   VXV::Rect pixel_range = calcPixelRange(pos, range, magnify);
00214   drawRangedPixel(pixel_range, p.x, p.y + r);
00215   drawRangedPixel(pixel_range, p.x, p.y - r);
00216   drawRangedPixel(pixel_range, p.x + r, p.y);
00217   drawRangedPixel(pixel_range, p.x - r, p.y);
00218 
00219   // 円の描画
00220   for (int cx = 0; cx <= cy; ++cx) {
00221     if (d < 0) {
00222       d += 6 + (cx << 2);
00223     } else {
00224       d += 10 + (cx << 2) - (cy-- << 2);
00225     }
00226 
00227     drawRangedPixel(pixel_range, p.x + cx, p.y + cy);
00228     drawRangedPixel(pixel_range, p.x - cx, p.y + cy);
00229     drawRangedPixel(pixel_range, p.x + cy, p.y + cx);
00230     drawRangedPixel(pixel_range, p.x - cy, p.y + cx);
00231     drawRangedPixel(pixel_range, p.x + cy, p.y - cx);
00232     drawRangedPixel(pixel_range, p.x - cy, p.y - cx);
00233     drawRangedPixel(pixel_range, p.x - cx, p.y - cy);
00234     drawRangedPixel(pixel_range, p.x + cx, p.y - cy);
00235   }
00236 }
00237 
00238 
00239 TextDraw::TextDraw(TTF_Draw& ttf, const char* text, 
00240                    const VXV::Grid& text_pos, int pxSize, bool pin,
00241                    unsigned long color, unsigned long background,
00242                    bool transparent)
00243   : surface(new DrawSurface(ttf.createText(text, pxSize, color, background),
00244                             transparent)),
00245     label(new LabelComponent(surface)), draw_pos(text_pos), pined(pin) {
00246   label->setPosition(draw_pos);
00247 }
00248 
00249 
00250 TextDraw::~TextDraw(void) {
00251   delete surface;
00252   delete label;
00253 }
00254 
00255 
00256 void TextDraw::draw(void) {
00257   label->draw(view_pos, view_range, view_magnify, draw_pos, pined);
00258 }
00259 

Generated on Mon Apr 13 22:52:02 2009 by  doxygen 1.5.7.1