#include #include #include "khepcc.h" #include "display.h" #include "render.h" #include "events.h" /* Surface constructed from pixel data */ SDL_Surface *displaySurface = NULL; /* Shows how to draw with Cairo on SDL surfaces */ void draw_screen(khep_state_t *st, SDL_Surface * screen) { /* The drawing will exactly fit in the screen. */ int width = screen->w; int height = screen->h; /* The number of bytes used for every scanline. */ int stride = width * 4; /* Free old pixel data and allocate new space */ free(displayData); displayData = (unsigned char *) calloc(stride * height, 1); /* Create a cairo surface for our allocated image. */ /* We use the CAIRO_FORMAT_ARGB32 to support transparancy. */ cairo_surface_t *cairo_surface = cairo_image_surface_create_for_data(displayData, CAIRO_FORMAT_ARGB32, width, height, stride); /* Create a cairo drawing context, normalize it and draw. */ /* Delete the context afterwards. */ cairo_t *cr = cairo_create(cairo_surface); cairo_scale(cr, width, height); draw_scene(cr); cairo_destroy(cr); /* We stored our image in ARGB32 format. We have to create a mask */ /* for this format to tell SDL about our data layout. */ Uint32 rmask = 0x00ff0000; Uint32 gmask = 0x0000ff00; Uint32 bmask = 0x000000ff; Uint32 amask = 0xff000000; /* Free old surface and create a new one from our pixel data. */ SDL_FreeSurface(displaySurface); displaySurface = SDL_CreateRGBSurfaceFrom((void *) displayData, width, height, 32, stride, rmask, gmask, bmask, amask); /* Blit the rendering to the screen and refresh */ SDL_BlitSurface(displaySurface, NULL, screen, NULL); SDL_UpdateRect(screen, 0, 0, 0, 0); } Uint32 callback_draw(Uint32 interval, void *param) { SDL_Event event; event.type = SDL_USEREVENT; event.user.code = REDRAW_SCREEN; SDL_PushEvent(&event); /* We want the callback function to be called again in 100 ms. */ return 100; }