summaryrefslogtreecommitdiff
path: root/lib/shape.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/shape.cc')
-rw-r--r--lib/shape.cc105
1 files changed, 78 insertions, 27 deletions
diff --git a/lib/shape.cc b/lib/shape.cc
index 54e552b..45280f5 100644
--- a/lib/shape.cc
+++ b/lib/shape.cc
@@ -19,12 +19,15 @@ mogltk::fillwalker::~fillwalker() {
void mogltk::fillwalker::step(int x, int y) {
}
-mogltk::fill::fill() : minX(INT_MAX), minY(INT_MAX), maxX(INT_MIN), maxY(INT_MIN), header(0) {
+mogltk::fill::fill() : minX(INT_MAX), minY(INT_MAX), maxX(INT_MIN), maxY(INT_MIN), cached(0), header(0) {
}
mogltk::fill::~fill() {
if (header)
delete header;
+
+ if (cached)
+ delete cached;
}
void mogltk::fill::walk(fillwalker * w) {
@@ -32,6 +35,11 @@ void mogltk::fill::walk(fillwalker * w) {
}
void mogltk::fill::insert(int x, int y) {
+ if (cached) {
+ delete cached;
+ cached = 0;
+ }
+
if (!header) {
new sline(y, this);
}
@@ -50,22 +58,39 @@ void mogltk::fill::insert(int x, int y) {
minY = y;
}
-int mogltk::fill::GetMaxX() {
+int mogltk::fill::GetMaxX() const {
return maxX;
}
-int mogltk::fill::GetMaxY() {
+int mogltk::fill::GetMaxY() const {
return maxY;
}
-int mogltk::fill::GetMinX() {
+int mogltk::fill::GetMinX() const {
return minX;
}
-int mogltk::fill::GetMinY() {
+int mogltk::fill::GetMinY() const {
return minY;
}
+mogltk::texture * mogltk::fill::GetTexture() {
+ return cached;
+}
+
+mogltk::texture * mogltk::fill::Talloc() {
+ if (cached)
+ return cached;
+
+ int x; int y;
+
+ for (x = 1; x < (maxX - minX); x <<= 1);
+ for (y = 1; y < (maxY - minY); y <<= 1);
+
+ cached = new texture(x, y, true);
+ return cached;
+}
+
mogltk::fill::sline::sline(int _y, fill * _header) : y(_y), header(_header), pheader(0) {
if (!header->header) {
header->header = this;
@@ -351,32 +376,58 @@ void mogltk::shape::pcircle(int x, int y, int r, ColorP c) {
delete f;
}
-class filldrawer : public mogltk::fillwalker {
- public:
- filldrawer(mogltk::shape * _s, mogltk::ColorP _c = DOS_WHITE) : s(_s), c(_c), oldx(-1), oldy(-1) { }
- virtual ~filldrawer() { }
- virtual void step(int x, int y) {
- if (oldy != y)
- oldx = -1;
- if (oldx == -1) {
- oldx = x;
- } else {
- s->hline(oldx, x, y, c);
- oldx = -1;
+mogltk::filldrawer::filldrawer(fill * _f, texture * _t, ColorP _c) : f(_f), t(_t), c(_c), oldx(-1), oldy(-1) {
+}
+
+mogltk::filldrawer::~filldrawer() {
+}
+
+void mogltk::filldrawer::step(int x, int y) {
+ if (oldy != y) {
+ oldx = -1;
+ }
+ if (oldx == -1) {
+ oldx = x;
+ } else {
+// s->hline(oldx, x, y, c);
+ Uint32 * p = t->GetPixels();
+ int i,
+ first = t->GetWidth() * (y - f->GetMinY()) + oldx - f->GetMinX(),
+ last = first - oldx + x;
+ SDL_PixelFormat * format = t->GetFormat();
+
+ for (i = first; i <= last; i++) {
+ p[i] = c.toSDL(format);
}
- oldy = y;
+
+ oldx = -1;
}
- private:
- mogltk::shape * s;
- mogltk::ColorP c;
- int oldx, oldy;
-};
+ oldy = y;
+}
-void mogltk::shape::fdraw(fill * f, ColorP c) {
+void mogltk::shape::fdraw(fill * f, ColorP c, int sx, int sy) {
ENTER;
- filldrawer * w = new filldrawer(this, c);
- f->walk(w);
- delete w;
+ if (!f->GetTexture()) {
+ filldrawer * w = new filldrawer(f, f->Talloc(), c);
+ f->walk(w);
+ delete w;
+ f->last = c.c;
+ }
+ SDL_PixelFormat * format = f->GetTexture()->GetFormat();
+
+ if (f->last == c.c) {
+ Uint32 * p = f->GetTexture()->GetPixels();
+ int i, n = f->GetTexture()->GetWidth() * f->GetTexture()->GetHeight();
+ for (i = 0; i < n; i++)
+ if (p[i] & 0xff000000)
+ p[i] = c.toSDL(format);
+ }
+ SDL_Rect r;
+ r.x = f->GetMinX() + sx;
+ r.y = f->GetMinY() + sy;
+ SDL_BlitSurface(f->GetTexture()->GetSurface(), 0, mogltk::engine::base_o->getsurface(), &r);
+
+ f->last = c.c;
LEAVE;
}