7 static SDL_Surface *fbsurf;
9 static Rect screen_rect = {0, 0, 1024, 768};
10 static int color_depth = 32; // bits per pixel
12 static Pixmap *pixmap;
16 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
17 fprintf(stderr, "failed to initialize SDL\n");
21 if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) {
22 fprintf(stderr, "failed to set video mode\n");
29 pixmap->width = screen_rect.width;
30 pixmap->height = screen_rect.height;
32 pixmap->pixels = (unsigned char*)fbsurf->pixels;
44 unsigned char *get_framebuffer()
46 return (unsigned char*)fbsurf->pixels;
49 Pixmap *get_framebuffer_pixmap()
54 Rect get_screen_size()
64 void clear_screen(int r, int g, int b)
66 fill_rect(screen_rect, r, g, b);
69 void fill_rect(const Rect &rect, int r, int g, int b)
71 uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
76 sdl_rect.w = rect.width;
77 sdl_rect.h = rect.height;
79 SDL_FillRect(fbsurf, &sdl_rect, color);
82 void set_cursor_visibility(bool visible)
86 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
87 const Rect &dest_rect, int dest_x, int dest_y)
89 int width = src_rect.width;
90 int height = src_rect.height;
92 int xoffs = dest_x - dest_rect.x;
98 int yoffs = dest_y - dest_rect.y;
100 dest_y = dest_rect.y;
104 int xend = dest_x + width;
105 if(xend >= dest_rect.width) {
106 width -= xend - dest_rect.width;
109 int yend = dest_y + height;
110 if(yend >= dest_rect.height) {
111 height -= yend - dest_rect.height;
114 if(width <= 0 || height <= 0) {
118 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
119 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
121 for(int i=0; i<height; i++) {
122 memcpy(dptr, sptr, width * 4);
123 sptr += src_rect.width * 4;
124 dptr += dest_rect.width * 4;
128 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
129 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
131 int width = src_rect.width;
132 int height = src_rect.height;
134 int xoffs = dest_x - dest_rect.x;
136 dest_x = dest_rect.x;
140 int yoffs = dest_y - dest_rect.y;
142 dest_y = dest_rect.y;
146 int xend = dest_x + width;
147 if(xend >= dest_rect.width) {
148 width -= xend - dest_rect.width;
151 int yend = dest_y + height;
152 if(yend >= dest_rect.height) {
153 height -= yend - dest_rect.height;
156 if(width <= 0 || height <= 0) {
160 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
161 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
163 for(int i=0; i<height; i++) {
164 for(int j=0; j<width; j++) {
166 int g = sptr[j * 4 + 1];
167 int b = sptr[j * 4 + 2];
169 if(r != key_r || g != key_g || b != key_b) {
176 sptr += src_rect.width * 4;
177 dptr += dest_rect.width * 4;
183 SDL_UpdateRect(fbsurf, 0, 0, 0, 0);