From feba563611f39efe4f71cf09347d5aa9cd13ada6 Mon Sep 17 00:00:00 2001
From: pixel <pixel>
Date: Fri, 4 Apr 2003 15:06:29 +0000
Subject: Finished filling

---
 lib/glshape.cc |  54 +++++++++++++++--------------
 lib/shape.cc   | 105 ++++++++++++++++++++++++++++++++++++++++++---------------
 lib/texture.cc |   6 ++++
 3 files changed, 113 insertions(+), 52 deletions(-)

(limited to 'lib')

diff --git a/lib/glshape.cc b/lib/glshape.cc
index 5572e4d..9257257 100644
--- a/lib/glshape.cc
+++ b/lib/glshape.cc
@@ -48,6 +48,12 @@ void mogltk::glshape::pixel(int x, int y, ColorP c) {
     box(x, y, x, y, c);
 }
 
+void mogltk::glshape::obox(int x1, int y1, int x2, int y2, ColorP c) {
+    ENTER;
+    shape::obox(x1, y1, x2, y2, c);
+    LEAVE;
+}
+
 void mogltk::glshape::obox(int x1, int y1, int x2, int y2, ColorP c1, ColorP c2, ColorP c3, ColorP c4) {
     ENTER;
 
@@ -155,32 +161,30 @@ void mogltk::glshape::Leave(bool was2D) {
 	mogltk::engine::glbase_o->Leave2DMode();
 }
 
-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;
-        }
-        oldy = y;
+void mogltk::glshape::fdraw(fill * f, ColorP c, int sx, int sy) {
+    ENTERT;
+    
+    texture * t = f->GetTexture();
+    
+    if (!t) {
+        filldrawer * w = new filldrawer(f, t = f->Talloc(), c);
+        f->walk(w);
+        delete w;
     }
-  private:
-    mogltk::shape * s;
-    mogltk::ColorP c;
-    int oldx, oldy;
-};
 
-void mogltk::glshape::fdraw(fill * f, ColorP c) {
-    ENTER;
-    filldrawer * w = new filldrawer(this, c);
-    f->walk(w);
-    delete w;
+    c.Bind();
+    t->Bind();
+    int w = t->GetWidth();
+    int h = t->GetHeight();
+    int x = f->GetMinX() + sx;
+    int y = f->GetMinY() + sy;
+
+    glBegin(GL_TRIANGLE_STRIP);
+    glTexCoord2i(0, 0); glVertex2i(x    , y    );
+    glTexCoord2i(w, 0); glVertex2i(x + w, y    );
+    glTexCoord2i(0, h); glVertex2i(x    , y + h);
+    glTexCoord2i(w, h); glVertex2i(x + w, y + h);
+    glEnd();
+
     LEAVE;
 }
-															    
\ No newline at end of file
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;
 }
 
diff --git a/lib/texture.cc b/lib/texture.cc
index 80853ca..bea33a4 100644
--- a/lib/texture.cc
+++ b/lib/texture.cc
@@ -39,6 +39,8 @@ mogltk::texture::texture(int w, int h, bool plane) throw (GeneralException) : wi
 	throw GeneralException(_("Can't create RGB Surface"));
     }
     
+    SDL_FillRect(surface, 0, 0);
+    
 #ifdef TRACE_TEXTURES
     next = 0;
     prev = footer;
@@ -154,6 +156,10 @@ SDL_Surface * mogltk::texture::GetSurface() {
     return surface;
 }
 
+SDL_PixelFormat * mogltk::texture::GetFormat() {
+    return surface->format;
+}
+
 void mogltk::texture::Generate() {
     if (texture_allocated) {
 	glDeleteTextures(1, &tex);
-- 
cgit v1.2.3