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, gfx->screen_rect);
94 sdl_rect.x = gfx->clipping_rect.x;
95 sdl_rect.y = gfx->clipping_rect.y;
96 sdl_rect.w = gfx->clipping_recvoid fill_rect(const Rect &rect, int r, int g, int b)
99 Rect screen_rect = get_screen_size();
101 if(drect.x < clipping_rect.x) {
102 drect.width -= clipping_rect.x - drect.x;
103 drect.x = clipping_rect.x;
106 if(drect.y < clipping_rect.y) {
107 drect.height -= clipping_rect.y - drect.y;
108 drect.y = clipping_rect.y;
111 if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
112 drect.width = clipping_rect.width + clipping_rect.x - drect.x;
115 if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
116 drect.height = clipping_rect.height + clipping_rect.y - drect.y;
119 unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
120 for(int i=0; i<drect.height; i++) {
121 for(int j=0; j<drect.width; j++) {
126 fb += screen_rect.width * 4;
130 sdl_rect.h = gfx->clipping_rect.height;
132 SDL_SetClipRect(fbsurf, &sdl_rect);
135 const Rect &get_clipping_rect()
137 return gfx->clipping_rect;
140 void clear_screen(int r, int g, int b)
142 fill_rect(gfx->screen_rect, r, g, b);
145 void fill_rect(const Rect &rect, int r, int g, int b)
147 uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
152 sdl_rect.w = rect.width;
153 sdl_rect.h = rect.height;
155 SDL_FillRect(fbsurf, &sdl_rect, color);
158 void set_clipping_rect(const Rect &rect)
160 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
163 const Rect &get_clipping_rect()
165 return gfx->clipping_rect;
169 void set_cursor_visibility(bool visible)
175 if(SDL_MUSTLOCK(fbsurf)) {
176 SDL_LockSurface(fbsurf);
178 memcpy(fbsurf->pixels, gfx->pixmap->pixels, gfx->pixmap->width * gfx->pixmap->height * (gfx->color_depth / 8));
179 if(SDL_MUSTLOCK(fbsurf)) {
180 SDL_UnlockSurface(fbsurf);
182 SDL_UpdateRect(fbsurf, 0, 0, 0, 0);