void UnityBerkeliumWindow::onPaint(Berkelium::Window *pWindow, const unsigned char *sourceBuffer, const Berkelium::Rect &rect, int dx, int dy, const Berkelium::Rect &scrollRect)
{
#ifdef DEBUG
	cerr << "[UnityBerkeliumWindow] onPaint called (window: " << pWindow << ")" << endl;
	cerr << "  rect: (left=" << rect.left() << ", width=" << rect.width() << ", top=" << rect.top() << ", height=" << rect.height() << ")" << endl;
	cerr << "  sourceBuffer: " << (void *) sourceBuffer << endl;
#endif

	// Scrolling
	if(dx != 0 || dy != 0)
	{
#if 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);
		Berkelium::Rect scrolled_rect = scrollRect;
		scrolled_rect.mLeft -= dx;
		scrolled_rect.mTop -= 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)
			//Berkelium::Rect shared_rect = scrolled_shared_rect.translate(dx, dy);
			Berkelium::Rect shared_rect = scrolled_shared_rect;
			shared_rect.mLeft += dx;
			shared_rect.mTop += dy;

			for(int y = scrolled_shared_rect.top(); y < shared_rect.bottom(); ++y)
			{
				::memcpy(
					m_buffer + y * m_width * 4 /*+ scrolled_shared_rect.left() * 4*/,
					m_buffer + ((scrolled_shared_rect.top() + y) * m_width /*+ scrolled_shared_rect.left()*/) * 4,
					scrolled_shared_rect.width() * 4
				);
			}

			// Copy the data out of the texture
			/*size_t scrollBuffSize = scrolled_shared_rect.width() * scrolled_shared_rect.height() * 4;
			unsigned char *scrollBuffer = new unsigned char[scrollBuffSize];
			
			for(int line = 0; line < scrolled_shared_rect.height(); ++line)
			{
				::memcpy(
					scrollBuffer + line * scrolled_shared_rect.width() * 4,

			}*/
#if 0
			glGetTexImage(
				GL_TEXTURE_2D, 0,
				GL_BGRA, GL_UNSIGNED_BYTE,
				scroll_buffer
				);

			// Annoyingly, OpenGL doesn't provide convenient primitives, so
			// we manually copy out the region to the beginning of the
			// buffer
			int wid = scrolled_shared_rect.width();
			int hig = scrolled_shared_rect.height();
			for(int jj = 0; jj < hig; jj++) {
				memcpy(
					scroll_buffer + (jj*wid * 4),
					scroll_buffer + ((scrolled_shared_rect.top()+jj)*dest_texture_width + scrolled_shared_rect.left()) * 4,
					wid*4
					);
			}

			// And finally, we push it back into the texture in the right
			// location
			glTexSubImage2D(GL_TEXTURE_2D, 0,
				shared_rect.left(), shared_rect.top(),
				shared_rect.width(), shared_rect.height(),
				GL_BGRA, GL_UNSIGNED_BYTE, scroll_buffer
				);
#endif
		}
#endif
	}

	// Apply the dirty rectangle
	convertColors(rect, sourceBuffer);

	// Call the paint callback
	m_lastDirtyRect = rect;
	m_paintFunc(/*rect.left(), rect.top(), rect.width(), rect.height()*/);
}
void EventManager::paint()
{
    m_paintFunc(_user_p);
}