#include <berkelium/Berkelium.hpp>
#include <berkelium/Context.hpp>
#include <berkelium/Window.hpp>
#include <berkelium/WindowDelegate.hpp>
...
// declaration of draw function where onpaint goes
void DrawBerkelium(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);
// Berkelium callback class
class MyDelegate : public Berkelium::WindowDelegate {
virtual void onStartLoading(Berkelium::Window *win, Berkelium::URLString newURL) {
Int i=0;
}
virtual void 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) {
DrawBerkelium(win,sourceBuffer,sourceBufferRect,numCopyRects,copyRects,dx,dy,scrollRect);
}
};
...
Berkelium::Window* window=NULL;
Berkelium::Context* context=NULL;
...
Bool InitGame()
{
...
Berkelium::init(Berkelium::FileString::empty());
context = Berkelium::Context::create();
window = Berkelium::Window::create(context);
window->resize(512, 512);
window->setDelegate( new MyDelegate() ); // set callback class
window->focus();
std::string url = "http://berkelium.org";
window->navigateTo(url.data(), url.length());
...
}
...
// DrawBerkelium, code ripped from rndbit :D
void DrawBerkelium(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) {
Int tex_size=512;
if(!imgcube.is())imgcube.create2D(tex_size,tex_size,IMAGE_B8G8R8A8);
imgcube.lock();
unsigned char* texd = imgcube.data();
UInt tw = imgcube.pitch();
// scrolling
{
Int wid = scrollRect.width();
Int hig = scrollRect.height();
Int top = scrollRect.top();
Int left = scrollRect.left();
if(dy < 0) // scroll down
{
for (int y = -dy; y < hig; y++)
{
UInt tb = ((top + y) * tw + left) * 4;
UInt tb2 = tb + dy * tw * 4;
memcpy(&texd[tb2], &texd[tb], wid * 4);
}
}
else if(dy > 0) // scroll up
{
for (int y = hig - dy; y > -1; y--)
{
UInt tb = ((top + y) * tw + left) * 4;
UInt tb2 = tb + dy * tw * 4;
memcpy(&texd[tb2], &texd[tb], wid * 4);
}
}
if(dx != 0) // scroll
{
int subx = dx > 0 ? 0 : -dx;
for (int y = 0; y < hig; y++)
{
UInt tb = ((top + y) * tw + left) * 4;
UInt tb2 = tb - dx * 4;
memcpy(&texd[tb], &texd[tb2], wid * 4 - subx);
}
}
}
// copy new rects
for(UInt i = 0; i < numCopyRects; i++)
{
Berkelium::Rect cr = copyRects[i];
Int wid = cr.width();
Int hig = cr.height();
Int top = cr.top() - sourceBufferRect.top();
Int left = cr.left() - sourceBufferRect.left();
for(int y = 0; y < hig; y++)
{
UInt tb = ((cr.top() + y) * tw + cr.left()* 4) ;
memcpy(&texd[tb], &sourceBuffer[(left + (y + top) * sourceBufferRect.width()) * 4], wid * 4);
}
}
imgcube.unlock();
}
void DrawGame()
{
...
Renderer(Render);
if(Renderer.rebuildDepthNeededForDebugDrawing())Renderer.rebuildDepth();
Berkelium::update();
if(imgcube.is())imgcube.draw3D(Color(255, 255, 255, 255),512*4,0,Vec(0,0,0));
...
}