Skip to content

Commit

Permalink
fix bug in Canvas.cloneSection
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Mar 19, 2012
1 parent 053bf40 commit 09df332
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/base/Rect.hpp
Expand Up @@ -114,6 +114,12 @@ namespace sphere {
(that.lr.y >= ul.y && that.lr.y <= lr.y));
}

bool isInside(T x1, T y1, T x2, T y2) const {
return ((ul.x >= x1 && ul.x <= x2) &&
(lr.x >= x1 && lr.x <= x2) &&
(ul.y >= y1 && ul.y <= y2) &&
(lr.y >= y1 && lr.y <= y2));
}
};

typedef Rect<i32> Recti;
Expand Down
17 changes: 8 additions & 9 deletions src/graphics/Canvas.cpp
Expand Up @@ -39,19 +39,18 @@ namespace sphere {

//-----------------------------------------------------------------
Canvas*
Canvas::cloneSection(const Recti& section)
Canvas::cloneSection(const Recti& rect)
{
Recti sec = section.getIntersection(Recti(0, 0, _width - 1, _height - 1));
if (!sec.isValid()) {
if (!rect.isValid() || !rect.isInside(0, 0, _width - 1, _height - 1)) {
return 0;
}
CanvasPtr canvas = Create(sec.getWidth(), sec.getHeight());
for (int i = 0; i < sec.getHeight(); ++i) {
memcpy(canvas->getPixels() + (i * canvas->getWidth()),
_pixels + (i * _width) + sec.getX(),
sec.getWidth() * sizeof(RGBA));
CanvasPtr section = Create(rect.getWidth(), rect.getHeight());
for (int iy = 0; iy < rect.getHeight(); ++iy) {
memcpy(section->getPixels() + (iy * section->getWidth()),
_pixels + ((rect.ul.y + iy) * _width) + rect.ul.x,
rect.getWidth() * sizeof(RGBA));
}
return canvas.release();
return section.release();
}

//-----------------------------------------------------------------
Expand Down

0 comments on commit 09df332

Please sign in to comment.