aboutsummaryrefslogtreecommitdiff
path: root/graphics/surface.cpp
diff options
context:
space:
mode:
authorEugene Sandulenko2016-06-02 23:44:43 +0200
committerEugene Sandulenko2016-06-03 00:45:56 +0200
commit531b190d5974f2bb396a19cd4869020ce2669c3a (patch)
tree64aceb68b7e143a7dc1d7e276cac512c923a5d9a /graphics/surface.cpp
parent2c7976e4e94d52bbd27a07eba8ae15379977189b (diff)
downloadscummvm-rg350-531b190d5974f2bb396a19cd4869020ce2669c3a.tar.gz
scummvm-rg350-531b190d5974f2bb396a19cd4869020ce2669c3a.tar.bz2
scummvm-rg350-531b190d5974f2bb396a19cd4869020ce2669c3a.zip
GRAPHICS: Added FloodFill class to Surface.
Moved from WAGE engine and is using stack-based classic floodfill implementation.
Diffstat (limited to 'graphics/surface.cpp')
-rw-r--r--graphics/surface.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/graphics/surface.cpp b/graphics/surface.cpp
index 67ed942b0b..8e5a22aec6 100644
--- a/graphics/surface.cpp
+++ b/graphics/surface.cpp
@@ -498,4 +498,64 @@ Graphics::Surface *Surface::convertTo(const PixelFormat &dstFormat, const byte *
return surface;
}
+FloodFill::FloodFill(Graphics::Surface *surface, uint32 oldColor, uint32 fillColor) {
+ _surface = surface;
+ _oldColor = oldColor;
+ _fillColor = fillColor;
+ _w = surface->w;
+ _h = surface->h;
+
+ _visited = (byte *)calloc(_w * _h, 1);
+}
+
+FloodFill::~FloodFill() {
+ while(!_queue.empty()) {
+ Common::Point *p = _queue.front();
+
+ delete p;
+ _queue.pop_front();
+ }
+
+ free(_visited);
+}
+
+void FloodFill::addSeed(int x, int y) {
+ if (x >= 0 && x < _w && y >= 0 && y < _h) {
+ if (!_visited[y * _w + x]) {
+ _visited[y * _w + x] = 1;
+ void *p = _surface->getBasePtr(x, y);
+
+ if (_surface->format.bytesPerPixel == 1) {
+ if (*((byte *)p) == _oldColor)
+ *((byte *)p) = _fillColor;
+ } else if (_surface->format.bytesPerPixel == 2) {
+ if (READ_UINT16(p) == _oldColor)
+ WRITE_UINT16(p, _fillColor);
+ } else if (_surface->format.bytesPerPixel == 4) {
+ if (READ_UINT32(p) == _oldColor)
+ WRITE_UINT32(p, _fillColor);
+ } else {
+ error("Unsupported bpp in FloodFill");
+ }
+
+ Common::Point *pt = new Common::Point(x, y);
+
+ _queue.push_back(pt);
+ }
+ }
+}
+
+void FloodFill::fill() {
+ while (!_queue.empty()) {
+ Common::Point *p = _queue.front();
+ _queue.pop_front();
+ addSeed(p->x , p->y - 1);
+ addSeed(p->x - 1, p->y );
+ addSeed(p->x , p->y + 1);
+ addSeed(p->x + 1, p->y );
+
+ delete p;
+ }
+}
+
} // End of namespace Graphics