9 static SDL_Surface *fbsurf;
14 int color_depth; // bits per pixel
22 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
23 fprintf(stderr, "failed to initialize SDL\n");
27 if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
31 Rect scr_rect(0, 0, 1024, 768);
32 gfx->screen_rect = scr_rect;
33 gfx->color_depth = 32;
35 if(!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
36 fprintf(stderr, "Failed to set video mode\n");
41 if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
42 fprintf(stderr, "Failed to allocate pixmap.\n");
46 gfx->pixmap->width = gfx->screen_rect.width;
47 gfx->pixmap->height = gfx->screen_rect.height;
49 int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
50 if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
51 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
55 set_clipping_rect(gfx->screen_rect);
62 sh_free(gfx->pixmap->pixels);
63 gfx->pixmap->pixels = 0;
69 unsigned char *get_framebuffer()
71 return gfx->pixmap->pixels;
74 Pixmap *get_framebuffer_pixmap()
79 Rect get_screen_size()
81 return gfx->screen_rect;
86 return gfx->color_depth;
89 void set_clipping_rect(const Rect &rect)
91 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
94 const Rect &get_clipping_rect()
96 return gfx->clipping_rect;
100 void set_cursor_visibility(bool visible)
104 void gfx_update(const Rect &upd_rect)
106 if(SDL_MUSTLOCK(fbsurf)) {
107 SDL_LockSurface(fbsurf);
110 Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
112 unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
113 unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
115 for(int i=0; i<rect.height; i++) {
116 memcpy(dptr, sptr, rect.width * 4);
117 sptr += gfx->screen_rect.width * 4;
118 dptr += gfx->screen_rect.width * 4;
121 if(SDL_MUSTLOCK(fbsurf)) {
122 SDL_UnlockSurface(fbsurf);
124 SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height);
131 void get_rgb_order(int *r, int *g, int *b)
133 *r = fbsurf->format->Rshift / 8;
134 *g = fbsurf->format->Gshift / 8;
135 *b = fbsurf->format->Bshift / 8;