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");
28 pixmap->width = screen_rect.width;
29 pixmap->height = screen_rect.height;
30 pixmap->pixels = (unsigned char*)fbsurf->pixels;
42 unsigned char *get_framebuffer()
44 return (unsigned char*)fbsurf->pixels;
47 Pixmap *get_framebuffer_pixmap()
52 Rect get_screen_size()
62 void clear_screen(int r, int g, int b)
64 fill_rect(screen_rect, r, g, b);
67 void fill_rect(const Rect &rect, int r, int g, int b)
69 uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
74 sdl_rect.w = rect.width;
75 sdl_rect.h = rect.height;
77 SDL_FillRect(fbsurf, &sdl_rect, color);
80 void set_cursor_visibility(bool visible)
84 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
85 const Rect &dest_rect, int dest_x, int dest_y)
87 int width = src_rect.width;
88 int height = src_rect.height;
90 int xoffs = dest_x - dest_rect.x;
96 int yoffs = dest_y - dest_rect.y;
102 int xend = dest_x + width;
103 if(xend >= dest_rect.width) {
104 width -= xend - dest_rect.width;
107 int yend = dest_y + height;
108 if(yend >= dest_rect.height) {
109 height -= yend - dest_rect.height;
112 if(width <= 0 || height <= 0) {
116 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
117 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
119 for(int i=0; i<height; i++) {
120 memcpy(dptr, sptr, width * 4);
121 sptr += src_rect.width * 4;
122 dptr += dest_rect.width * 4;
126 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
127 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
129 int width = src_rect.width;
130 int height = src_rect.height;
132 int xoffs = dest_x - dest_rect.x;
134 dest_x = dest_rect.x;
138 int yoffs = dest_y - dest_rect.y;
140 dest_y = dest_rect.y;
144 int xend = dest_x + width;
145 if(xend >= dest_rect.width) {
146 width -= xend - dest_rect.width;
149 int yend = dest_y + height;
150 if(yend >= dest_rect.height) {
151 height -= yend - dest_rect.height;
154 if(width <= 0 || height <= 0) {
158 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
159 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
161 for(int i=0; i<height; i++) {
162 for(int j=0; j<width; j++) {
164 int g = sptr[j * 4 + 1];
165 int b = sptr[j * 4 + 2];
167 if(r != key_r || g != key_g || b != key_b) {
174 sptr += src_rect.width * 4;
175 dptr += dest_rect.width * 4;
181 SDL_UpdateRect(fbsurf, 0, 0, 0, 0);