7 static SDL_Surface *fbsurf;
9 static Rect screen_rect = {0, 0, 1024, 768};
10 static int color_depth = 32; // bits per pixel
14 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
15 fprintf(stderr, "failed to initialize SDL\n");
19 if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) {
20 fprintf(stderr, "failed to set video mode\n");
33 unsigned char *get_framebuffer()
35 return (unsigned char*)fbsurf->pixels;
38 Rect get_screen_size()
48 void clear_screen(int r, int g, int b)
50 fill_rect(screen_rect, r, g, b);
53 void fill_rect(const Rect &rect, int r, int g, int b)
55 uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
60 sdl_rect.w = rect.width;
61 sdl_rect.h = rect.height;
63 SDL_FillRect(fbsurf, &sdl_rect, color);
66 void set_cursor_visibility(bool visible)
70 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
71 const Rect &dest_rect, int dest_x, int dest_y)
73 int width = src_rect.width;
74 int height = src_rect.height;
76 int xoffs = dest_x - dest_rect.x;
82 int yoffs = dest_y - dest_rect.y;
88 int xend = dest_x + width;
89 if(xend >= dest_rect.width) {
90 width -= xend - dest_rect.width;
93 int yend = dest_y + height;
94 if(yend >= dest_rect.height) {
95 height -= yend - dest_rect.height;
98 if(width <= 0 || height <= 0) {
102 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
103 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
105 for(int i=0; i<height; i++) {
106 memcpy(dptr, sptr, width * 4);
107 sptr += src_rect.width * 4;
108 dptr += dest_rect.width * 4;
112 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
113 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
115 int width = src_rect.width;
116 int height = src_rect.height;
118 int xoffs = dest_x - dest_rect.x;
120 dest_x = dest_rect.x;
124 int yoffs = dest_y - dest_rect.y;
126 dest_y = dest_rect.y;
130 int xend = dest_x + width;
131 if(xend >= dest_rect.width) {
132 width -= xend - dest_rect.width;
135 int yend = dest_y + height;
136 if(yend >= dest_rect.height) {
137 height -= yend - dest_rect.height;
140 if(width <= 0 || height <= 0) {
144 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
145 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
147 for(int i=0; i<height; i++) {
148 for(int j=0; j<width; j++) {
150 int g = sptr[j * 4 + 1];
151 int b = sptr[j * 4 + 2];
153 if(r != key_r || g != key_g || b != key_b) {
160 sptr += src_rect.width * 4;
161 dptr += dest_rect.width * 4;
167 SDL_UpdateRect(fbsurf, 0, 0, 0, 0);