Hi All,
I am novice to sfml. I am trying to use sfml with cairo. But i am not getting success. Posting my code here. Seeking your help.
const auto numOfChannels = 4;
const auto totalBytes = numOfChannels * _surfaceWidth * _surfaceHeight;
_surfaceData.reset(new ovg::VGubyte[totalBytes]);
_cairoSurface = cairo_image_surface_create_for_data (_surfaceData.get(),
CAIRO_FORMAT_ARGB32,
_surfaceWidth,
_surfaceHeight,
numOfChannels * _surfaceHeight);
if (cairo_surface_status (_cairoSurface) != CAIRO_STATUS_SUCCESS)
{
setLastError(ovg::VG_BAD_HANDLE_ERROR);
return;
}
_cr = cairo_create (_cairoSurface);
if (cairo_status (_cr) != CAIRO_STATUS_SUCCESS)
{
setLastError(ovg::VG_BAD_HANDLE_ERROR);
return;
}
void renderToSurface()
{
cairo_save (_cr);
cairo_set_operator (_cr, CAIRO_OPERATOR_OVER);
cairo_scale (_cr, static_cast<double>(_surfaceWidth), static_cast<double>(_surfaceHeight));
cairo_restore (_cr);
cairo_set_source_rgba (_cr, 0.0f, 0.0f, 0.0f, 1.0f);
cairo_paint(_cr);
// create temporary surface
cairo_push_group (_cr);
cairo_set_source_rgba (_cr, 0.0f, 0.0f, 0.0f, 0.0f);
cairo_paint (_cr);
}
and then
void convertPixels(blib::konark::openvg::_impl::VGContext::SurfaceDataPtrType& aData,
sf::Image& aImg,
int w,
int h,
int pitch = 4)
{
unsigned char *dataptr = aData.get();
for (int y = 0 ; y < h ; ++y)
{
int rx = 0;
for (int x = 0 ; x < w*4 ; x+=4)
{
aImg.setPixel(rx++, y, sf::Color(dataptr[x+2],dataptr[x+1],dataptr[x],dataptr[x+3]));
}
dataptr += pitch;
}
}
void draw()
{
const auto ht = _context.height();
const auto wd = _context.width();
sf::Image img;
img.create(wd, ht, sf::Color::Cyan);
convertPixels(_context.draw(), img, wd, ht);
//sf::Image img;
//img.create(ht, wd, _context.draw().get());
sf::Texture texture;
texture.create(_context.height(), _context.width());
//const sf::IntRect rect(0, 0, wd, ht);
texture.loadFromMemory(_context.draw().get(), 4 * ht * wd);
//texture.update(img);
sf::Sprite sprite;
sprite.setTexture(texture);
_window.draw(sprite);
}
This doesnt render the texture. Any one knows any other way or example?