-#include <errno.h>
-#include <limits.h>
-#include <stdio.h>
#include <string.h>
-#include <fcntl.h>
-#include <sys/ioctl.h>
-#include <sys/mman.h>
-#include <unistd.h>
+#include "geom.h"
+#include "gfx.h"
-#include <linux/fb.h>
+void clear_screen(int r, int g, int b)
+{
+ Rect screen_rect = get_screen_size();
+ fill_rect(screen_rect, r, g, b);
+}
-#include "gfx.h"
+void fill_rect(const Rect &rect, int r, int g, int b)
+{
+ Rect drect = rect;
+ Rect screen_rect = get_screen_size();
+ Rect clipping_rect = get_clipping_rect();
-#define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
+ if(drect.x < clipping_rect.x) {
+ drect.width -= clipping_rect.x - drect.x;
+ drect.x = clipping_rect.x;
+ }
-static unsigned char *framebuffer;
-static int dev_fd = -1;
+ if(drect.y < clipping_rect.y) {
+ drect.height -= clipping_rect.y - drect.y;
+ drect.y = clipping_rect.y;
+ }
-static Rect screen_rect;
-static int color_depth; //bits per pixel
+ if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
+ drect.width = clipping_rect.width + clipping_rect.x - drect.x;
+ }
-bool init_gfx()
-{
- if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
- fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
- return false;
+ if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
+ drect.height = clipping_rect.height + clipping_rect.y - drect.y;
}
- fb_var_screeninfo sinfo;
- if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
- close(dev_fd);
- dev_fd = -1;
- fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
- return false;
+ unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
+ for(int i=0; i<drect.height; i++) {
+ for(int j=0; j<drect.width; j++) {
+ fb[j * 4] = b;
+ fb[j * 4 + 1] = g;
+ fb[j * 4 + 2] = r;
+ }
+ fb += screen_rect.width * 4;
}
+}
- printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
- printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
+void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
+ const Rect &dest_rect, int dest_x, int dest_y)
+{
+ Rect irect = rect_intersection(get_clipping_rect(), dest_rect);
- screen_rect.x = screen_rect.y = 0;
- screen_rect.width = sinfo.xres_virtual;
- screen_rect.height = sinfo.yres_virtual;
- color_depth = sinfo.bits_per_pixel;
+ int width = src_rect.width;
+ int height = src_rect.height;
- int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
- framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
+ int xoffs = dest_x - irect.x;
+ if(xoffs < 0) {
+ dest_x = irect.x;
+ width += xoffs;
+ }
- if(framebuffer == (void*)-1) {
- close(dev_fd);
- dev_fd = -1;
- fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
- return false;
+ int yoffs = dest_y - irect.y;
+ if(yoffs < 0) {
+ dest_y = irect.y;
+ height += yoffs;
}
- return true;
-}
+ int xend = dest_x + width;
+ if(xend >= irect.width) {
+ width -= xend - irect.width;
+ }
-void destroy_gfx()
-{
- close(dev_fd);
- dev_fd = -1;
+ int yend = dest_y + height;
+ if(yend >= irect.height) {
+ height -= yend - irect.height;
+ }
- munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
- framebuffer = 0;
-}
+ if(width <= 0 || height <= 0) {
+ return;
+ }
-unsigned char *get_framebuffer()
-{
- return framebuffer;
-}
+ unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
+ unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
-Rect get_screen_size()
-{
- return screen_rect;
+ for(int i=0; i<height; i++) {
+ memcpy(dptr, sptr, width * 4);
+ sptr += src_rect.width * 4;
+ dptr += dest_rect.width * 4;
+ }
}
-int get_color_depth()
+void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
+ const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
{
- return color_depth;
-}
+ Rect irect = rect_intersection(get_clipping_rect(), dest_rect);
-void clear_screen(int r, int g, int b)
-{
- fill_rect(screen_rect, r, g, b);
-}
+ int width = src_rect.width;
+ int height = src_rect.height;
-void fill_rect(const Rect &rect, int r, int g, int b)
-{
- unsigned char *fb = framebuffer + (rect.x + screen_rect.width * rect.y) * 4;
- for(int i=0; i<rect.height; i++) {
- for(int j=0; j<rect.width; j++) {
- fb[j * 4] = b;
- fb[j * 4 + 1] = g;
- fb[j * 4 + 2] = r;
- }
- fb += screen_rect.width * 4;
+ int xoffs = dest_x - irect.x;
+ if(xoffs < 0) {
+ dest_x = irect.x;
+ width += xoffs;
}
-}
-void set_cursor_visibility(bool visible)
-{
- fb_cursor curs;
- curs.enable = visible ? 1 : 0;
+ int yoffs = dest_y - irect.y;
+ if(yoffs < 0) {
+ dest_y = irect.y;
+ height += yoffs;
+ }
+
+ int xend = dest_x + width;
+ if(xend >= irect.width) {
+ width -= xend - irect.width;
+ }
+
+ int yend = dest_y + height;
+ if(yend >= irect.height) {
+ height -= yend - irect.height;
+ }
+
+ if(width <= 0 || height <= 0) {
+ return;
+ }
+
+ unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
+ unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
+
+ for(int i=0; i<height; i++) {
+ for(int j=0; j<width; j++) {
+ int r = sptr[j * 4];
+ int g = sptr[j * 4 + 1];
+ int b = sptr[j * 4 + 2];
+
+ if(r != key_r || g != key_g || b != key_b) {
+ dptr[j * 4] = r;
+ dptr[j * 4 + 1] = g;
+ dptr[j * 4 + 2] = b;
+ }
+ }
- if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
- fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
+ sptr += src_rect.width * 4;
+ dptr += dest_rect.width * 4;
}
}