15 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
17 static unsigned char *framebuffer;
18 static int dev_fd = -1;
20 static Rect screen_rect;
21 static int color_depth; //bits per pixel
25 if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
26 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
30 fb_var_screeninfo sinfo;
31 if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
34 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
38 printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
39 printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
41 screen_rect.x = screen_rect.y = 0;
42 screen_rect.width = sinfo.xres_virtual;
43 screen_rect.height = sinfo.yres_virtual;
44 color_depth = sinfo.bits_per_pixel;
46 int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
47 framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
49 if(framebuffer == (void*)-1) {
52 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
61 clear_screen(0, 0, 0);
69 munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
73 unsigned char *get_framebuffer()
78 Rect get_screen_size()
88 void clear_screen(int r, int g, int b)
90 fill_rect(screen_rect, r, g, b);
93 void fill_rect(const Rect &rect, int r, int g, int b)
103 unsigned char *fb = framebuffer + (rect.x + screen_rect.width * rect.y) * 4;
104 for(int i=0; i<rect.height; i++) {
105 for(int j=0; j<rect.width; j++) {
110 fb += screen_rect.width * 4;
114 void set_cursor_visibility(bool visible)
117 curs.enable = visible ? 1 : 0;
119 if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
120 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
124 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
125 const Rect &dest_rect, int dest_x, int dest_y)
127 int width = src_rect.width;
128 int height = src_rect.height;
130 int xoffs = dest_x - dest_rect.x;
132 dest_x = dest_rect.x;
136 int yoffs = dest_y - dest_rect.y;
138 dest_y = dest_rect.y;
142 int xend = dest_x + width;
143 if(xend >= dest_rect.width) {
144 width -= xend - dest_rect.width;
147 int yend = dest_y + height;
148 if(yend >= dest_rect.height) {
149 height -= yend - dest_rect.height;
152 if(width <= 0 || height <= 0) {
156 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
157 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
159 for(int i=0; i<height; i++) {
160 memcpy(dptr, sptr, width * 4);
161 sptr += src_rect.width * 4;
162 dptr += dest_rect.width * 4;
166 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
167 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
169 int width = src_rect.width;
170 int height = src_rect.height;
172 int xoffs = dest_x - dest_rect.x;
174 dest_x = dest_rect.x;
178 int yoffs = dest_y - dest_rect.y;
180 dest_y = dest_rect.y;
184 int xend = dest_x + width;
185 if(xend >= dest_rect.width) {
186 width -= xend - dest_rect.width;
189 int yend = dest_y + height;
190 if(yend >= dest_rect.height) {
191 height -= yend - dest_rect.height;
194 if(width <= 0 || height <= 0) {
198 unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
199 unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
201 for(int i=0; i<height; i++) {
202 for(int j=0; j<width; j++) {
204 int g = sptr[j * 4 + 1];
205 int b = sptr[j * 4 + 2];
207 if(r != key_r || g != key_g || b != key_b) {
214 sptr += src_rect.width * 4;
215 dptr += dest_rect.width * 4;