void UnityBerkeliumWindow::onPaint(Berkelium::Window *win, const unsigned char *sourceBuffer, const Berkelium::Rect &sourceBufferRect, size_t numCopyRects, const Berkelium::Rect *copyRects, int dx, int dy, const Berkelium::Rect &scrollRect)
{
#ifdef DEBUG
	cerr << "[UnityBerkeliumWindow] onPaint called (window: " << win << ")" << endl;
	cerr << "  sourceBuffer: " << (void *) sourceBuffer << endl;
	cerr << "  sourceBufferRect: (left=" << sourceBufferRect.left() << ", width=" << sourceBufferRect.width() << ", top=" << sourceBufferRect.top() << ", height=" << sourceBufferRect.height() << ")" << endl;
	cerr << "  num dirty rects: " << numCopyRects << endl;
	for(size_t i = 0; i < numCopyRects; ++i)
		cerr << "  rect " << i << ": (left=" << copyRects[i].left() << ", width=" << copyRects[i].width() << ", top=" << copyRects[i].top() << ", height=" << copyRects[i].height() << ")" << endl;
#endif

	//! @todo Handle Scrolling
	if(dx != 0 || dy != 0)
	{
	}

	// Apply the dirty rectangles
	for(size_t i = 0; i < numCopyRects; ++i)
	{
		convertColors(copyRects[i], sourceBuffer);
		m_lastDirtyRect = copyRects[i];

		if(m_setPixelsFunc)
			m_setPixelsFunc(/*rect.left(), rect.top(), rect.width(), rect.height()*/);
	}

	// Call the paint callback
	if(m_applyTextureFunc)
		m_applyTextureFunc();
}
void UnityBerkeliumWindow::onPaint(Berkelium::Window *win, const unsigned char *sourceBuffer, const Berkelium::Rect &sourceBufferRect, size_t numCopyRects, const Berkelium::Rect *copyRects, int dx, int dy, const Berkelium::Rect &scrollRect)
{
#ifdef DEBUG
	cerr << "[UnityBerkeliumWindow] onPaint called (window: " << win << ")" << endl;
	cerr << "  sourceBuffer: " << (void *) sourceBuffer << endl;
	cerr << "  sourceBufferRect: (left=" << sourceBufferRect.left() << ", width=" << sourceBufferRect.width() << ", top=" << sourceBufferRect.top() << ", height=" << sourceBufferRect.height() << ")" << endl;
	cerr << "  num dirty rects: " << numCopyRects << endl;
	for(size_t i = 0; i < numCopyRects; ++i)
		cerr << "  rect " << i << ": (left=" << copyRects[i].left() << ", width=" << copyRects[i].width() << ", top=" << copyRects[i].top() << ", height=" << copyRects[i].height() << ")" << endl;
#endif

	// Handle Scrolling
	if(dx != 0 || dy != 0)
	{
        // scroll_rect contains the Rect we need to move
        // First we figure out where the the data is moved to by translating it
        Berkelium::Rect scrolled_rect = scrollRect.translate(-dx, -dy);
        // Next we figure out where they intersect, giving the scrolled
        // region
        Berkelium::Rect scrolled_shared_rect = scrollRect.intersect(scrolled_rect);
        // Only do scrolling if they have non-zero intersection
        if (scrolled_shared_rect.width() > 0 && scrolled_shared_rect.height() > 0) {
            // And the scroll is performed by moving shared_rect by (dx,dy)
            scrolled_shared_rect.mTop = m_height - scrolled_shared_rect.bottom();
            dy = -dy;
            if(m_scrollRectFunc)
                m_scrollRectFunc(scrolled_shared_rect.left(), scrolled_shared_rect.top(), scrolled_shared_rect.width(), scrolled_shared_rect.height(), dx, dy);
      }
	}

	// Apply the dirty rectangles
	for(size_t i = 0; i < numCopyRects; ++i)
	{
		convertColors(copyRects[i], sourceBuffer, sourceBufferRect);
		m_lastDirtyRect = copyRects[i];
        m_lastDirtyRect.mTop = m_height - m_lastDirtyRect.bottom();
        
		if(m_setPixelsFunc)
			m_setPixelsFunc(/*rect.left(), rect.top(), rect.width(), rect.height()*/);
	}

	// Call the paint callback
	if(m_applyTextureFunc)
		m_applyTextureFunc();
}