X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fgfx.cc;h=1da29539767792a7823f303ad0bc70abed2473a9;hb=ed66dbc4e6c017dad675a83dbcd7619b1b5d1e7d;hp=87b5939e7cde8ba15a14b926935b063eb6e65a6d;hpb=ffd2c0a0f8b898cb4199a0c63aff255a85cc0f11;p=winnie diff --git a/src/gfx.cc b/src/gfx.cc index 87b5939..1da2953 100644 --- a/src/gfx.cc +++ b/src/gfx.cc @@ -1,110 +1,155 @@ -#include -#include -#include #include -#include -#include -#include -#include - -#include - +#include "geom.h" #include "gfx.h" -#define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT) +static Rect clipping_rect; -static unsigned char *framebuffer; -static int dev_fd = -1; +void set_clipping_rect(const Rect &rect) +{ + clipping_rect = rect_intersection(rect, get_screen_size()); +} -static Rect screen_rect; -static int color_depth; //bits per pixel +const Rect &get_clipping_rect() +{ + return clipping_rect; +} -bool init_gfx() +void clear_screen(int r, int g, int b) { - if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) { - fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno)); - return false; - } + Rect screen_rect = get_screen_size(); + fill_rect(screen_rect, r, g, b); +} - 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; - } +void fill_rect(const Rect &rect, int r, int g, int b) +{ + Rect drect = rect; + Rect screen_rect = get_screen_size(); - 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); + if(drect.x < clipping_rect.x) { + drect.width -= clipping_rect.x - drect.x; + drect.x = clipping_rect.x; + } - 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; + if(drect.y < clipping_rect.y) { + drect.height -= clipping_rect.y - drect.y; + drect.y = clipping_rect.y; + } - 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); + if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) { + drect.width = clipping_rect.width + clipping_rect.x - drect.x; + } - if(framebuffer == (void*)-1) { - close(dev_fd); - dev_fd = -1; - fprintf(stderr, "Cannot map the framebuffer to memory : %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; } - return true; + unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4; + for(int i=0; i= irect.width) { + width -= xend - irect.width; + } -void clear_screen(int r, int g, int b) -{ - fill_rect(screen_rect, r, g, b); -} + int yend = dest_y + height; + if(yend >= irect.height) { + height -= yend - irect.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= 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