X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fgfx.cc;h=f0be4605174b24fbb178f9218c19b2214550c4d7;hb=5984d5479693fd7519674a5bc40ebf804f8d0a46;hp=7d3595c1ca3ff679b3b76c30c6c5b8f469106f2e;hpb=4047a2dc058e7e54e4ff95311fb556ae8eeeedb9;p=winnie diff --git a/src/gfx.cc b/src/gfx.cc index 7d3595c..f0be460 100644 --- a/src/gfx.cc +++ b/src/gfx.cc @@ -1,108 +1,39 @@ -#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 unsigned char *framebuffer; -static int dev_fd = -1; - -static Rect screen_rect; -static int color_depth; //bits per pixel - -bool init_gfx() -{ - if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) { - fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno)); - return false; - } - - 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; - } - - 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); - - 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 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(framebuffer == (void*)-1) { - close(dev_fd); - dev_fd = -1; - fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno)); - return false; - } - - return true; -} - -void destroy_gfx() -{ - clear_screen(0, 0, 0); - - if(dev_fd != -1) { - close(dev_fd); - } - - dev_fd = -1; - - munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth)); - framebuffer = 0; -} - -unsigned char *get_framebuffer() -{ - return framebuffer; -} - -Rect get_screen_size() -{ - return screen_rect; -} - -int get_color_depth() -{ - return color_depth; -} - void clear_screen(int r, int g, int b) { + Rect screen_rect = get_screen_size(); fill_rect(screen_rect, r, g, b); } 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(); - if(drect.x < 0) { - drect.x = 0; + if(drect.x < clipping_rect.x) { + drect.width -= clipping_rect.x - drect.x; + drect.x = clipping_rect.x; } - if(drect.y < 0) { - drect.y = 0; + if(drect.y < clipping_rect.y) { + drect.height -= clipping_rect.y - drect.y; + drect.y = clipping_rect.y; } - unsigned char *fb = framebuffer + (drect.x + screen_rect.width * drect.y) * 4; + if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) { + drect.width = clipping_rect.width + clipping_rect.x - drect.x; + } + + if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) { + drect.height = clipping_rect.height + clipping_rect.y - drect.y; + } + + unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4; for(int i=0; i= dest_rect.width) { - width -= xend - dest_rect.width; + if(xend >= irect.width) { + width -= xend - irect.width; } int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; + if(yend >= irect.height) { + height -= yend - irect.height; } if(width <= 0 || height <= 0) { @@ -168,29 +91,31 @@ void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, 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) { + Rect irect = rect_intersection(get_clipping_rect(), dest_rect); + int width = src_rect.width; int height = src_rect.height; - int xoffs = dest_x - dest_rect.x; + int xoffs = dest_x - irect.x; if(xoffs < 0) { - dest_x = dest_rect.x; + dest_x = irect.x; width += xoffs; } - int yoffs = dest_y - dest_rect.y; + int yoffs = dest_y - irect.y; if(yoffs < 0) { - dest_y = dest_rect.y; + dest_y = irect.y; height += yoffs; } int xend = dest_x + width; - if(xend >= dest_rect.width) { - width -= xend - dest_rect.width; + if(xend >= irect.width) { + width -= xend - irect.width; } int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; + if(yend >= irect.height) { + height -= yend - irect.height; } if(width <= 0 || height <= 0) {