[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] pygame.image.frombuffer problems



> I want a more effecient way of using pygame and pycairo together.

The solution is surprisingly easy and very fast: Cairo can draw
directly to an SDL surface.

I don't know if it's work on every platform, I use it only on Ubuntu
on x86 machines. But maybe it can work everywhere with the help of
SDL_PixelFormat's masks.



The main code in C:

cairo_surface_t *
create_for_sdl_data(SDL_Surface *sdl_surface)
{
   int btpp = 4;
   cairo_format_t format = CAIRO_FORMAT_RGB24;

   unsigned char *data = sdl_surface->pixels;
   int width = sdl_surface->w;
   int height = sdl_surface->h;
   int stride = width * btpp;

   cairo_surface_t* s;
   s = cairo_image_surface_create_for_data (data,format,width,height,stride);
   return s;
}


and in Python it's as easy as:

screen = pygame.display.set_mode((width, height))
cairo_surface = create_for_sdl_data(screen)
ctx = cairo.Context(cairo_surface)




The full code is here: https://svn.synch.hu:8081/svn/hobby/cairo_sdl/
Be aware! This is my first C program.. Suggestions are welcome.

Norbert Sebok