[cairo] drawing inconsistency

Adi Oanca adioanca at gmail.com
Sun Jan 20 12:09:00 PST 2008


Hi,

   I'm working with SDL and Cairo and with the following code:

#include <stdio.h>

#include "SDL.h"
#include "cairo/cairo.h"

int main(int argc, char **argv)
{
	int		gWidth = 640;
	int		gHeight = 480;
	
	if (SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		printf("Cannot init SDL. Error: %s\n", SDL_GetError());
		exit(-1);
	}

	SDL_Surface *screen = SDL_SetVideoMode(gWidth, gHeight, 32,
								SDL_SWSURFACE | SDL_RESIZABLE);
	if (screen == NULL)
	{
		printf("Cannot init SDL. Error: %s\n", SDL_GetError());
		exit(-1);
	}

	SDL_WM_SetCaption("Play","ICON");

	cairo_surface_t *cairoSurface;
	cairoSurface = cairo_image_surface_create_for_data(
		(unsigned char*)screen->pixels,
		CAIRO_FORMAT_ARGB32,
		gWidth,
		gHeight,
		screen->pitch);
	
	cairo_t *cr = cairo_create(cairoSurface);

	cairo_set_line_width(cr, 1);
	cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
	
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_rectangle(cr, 100, 100, 120, 40);
	cairo_stroke_preserve(cr);

	SDL_UpdateRect(screen, 0, 0, 0, 0);
	SDL_Delay(2000);
	
	cairo_set_source_rgb(cr, 0.82, 0.16, 0.22);
	cairo_fill(cr);

	cairo_destroy(cr);
	cairo_surface_destroy(cairoSurface);

	SDL_UpdateRect(screen, 0, 0, 0, 0);
	
	SDL_Event event;
	while(SDL_WaitEvent(&event)) {
		switch (event.type)
		{
		case SDL_KEYDOWN:
			if (event.key.keysym.sym == SDLK_q) {
				exit(0);
			}
			break;
		default:
			break;
		}
	}
	SDL_Quit();
	return 0;	
}

$ g++ -o test.exe test.o [-lmingw32] -lSDLmain -lSDL -lcairo

   ... is overwriting the top and the right side of the white
rectangle previously stroked.

   Anyone has any idea what I am doing wrong?
   I think I am to blame, or SDL, because if I create the surface this way:
cairoSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 640, 480);
... and write that into a .png, the result is OK. But if in the code
above I just add:
cairo_surface_write_to_png(cairoSurface, "image2.png");
... before I destroy *cr and *cairoSurface, the result is wrong.

   Can somebody help me?

Thanks,
Adi.


More information about the cairo mailing list