11 int color_depth; // bits per pixel
19 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
20 fprintf(stderr, "failed to initialize SDL\n");
24 if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
28 Rect scr_rect = {0, 0, 1024, 768};
29 gfx->screen_rect = scr_rect;
30 gfx->color_depth = 32;
32 if(!(gfx->fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
33 fprintf(stderr, "failed to set video mode\n");
38 gfx->pixmap = new Pixmap;
39 gfx->pixmap->width = gfx->screen_rect.width;
40 gfx->pixmap->height = gfx->screen_rect.height;
41 gfx->pixmap->pixels = (unsigned char*)gfx->fbsurf->pixels;
43 set_clipping_rect(gfx->screen_rect);
50 gfx->pixmap->pixels = 0;
56 unsigned char *get_framebuffer()
58 return (unsigned char*)gfx->fbsurf->pixels;
61 Pixmap *get_framebuffer_pixmap()
66 Rect get_screen_size()
68 return gfx->screen_rect;
73 return gfx->color_depth;
76 /*void set_clipping_rect(const Rect &rect)
78 gfx->clipping_rect = rect_intersection(rect, gfx->screen_rect);
81 sdl_rect.x = gfx->clipping_rect.x;
82 sdl_rect.y = gfx->clipping_rect.y;
83 sdl_rect.w = gfx->clipping_recvoid fill_rect(const Rect &rect, int r, int g, int b)
86 Rect screen_rect = get_screen_size();
88 if(drect.x < clipping_rect.x) {
89 drect.width -= clipping_rect.x - drect.x;
90 drect.x = clipping_rect.x;
93 if(drect.y < clipping_rect.y) {
94 drect.height -= clipping_rect.y - drect.y;
95 drect.y = clipping_rect.y;
98 if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
99 drect.width = clipping_rect.width + clipping_rect.x - drect.x;
102 if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
103 drect.height = clipping_rect.height + clipping_rect.y - drect.y;
106 unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
107 for(int i=0; i<drect.height; i++) {
108 for(int j=0; j<drect.width; j++) {
113 fb += screen_rect.width * 4;
117 sdl_rect.h = gfx->clipping_rect.height;
119 SDL_SetClipRect(gfx->fbsurf, &sdl_rect);
122 const Rect &get_clipping_rect()
124 return gfx->clipping_rect;
127 void clear_screen(int r, int g, int b)
129 fill_rect(gfx->screen_rect, r, g, b);
132 void fill_rect(const Rect &rect, int r, int g, int b)
134 uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
139 sdl_rect.w = rect.width;
140 sdl_rect.h = rect.height;
142 SDL_FillRect(gfx->fbsurf, &sdl_rect, color);
145 void set_clipping_rect(const Rect &rect)
147 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
150 const Rect &get_clipping_rect()
152 return gfx->clipping_rect;
156 void set_cursor_visibility(bool visible)
162 SDL_UpdateRect(gfx->fbsurf, 0, 0, 0, 0);