*in progress*
[winnie] / src / gfx.cc
index 0afa479..7d3595c 100644 (file)
@@ -58,7 +58,12 @@ bool init_gfx()
 
 void destroy_gfx()
 {
-       close(dev_fd);
+       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));
@@ -87,12 +92,22 @@ void clear_screen(int r, int g, int b)
 
 void fill_rect(const Rect &rect, int r, int g, int b)
 {
-       unsigned char *fb = framebuffer + rect.x + screen_rect.width * rect.y; 
-       for(int i=0; i<rect.height; i++) {
-               for(int j=0; j<rect.width; j++) {
-                       fb[j * 4] = r;
+       Rect drect = rect;
+
+       if(drect.x < 0) {
+               drect.x = 0;
+       }
+
+       if(drect.y < 0) {
+               drect.y = 0;
+       }
+
+       unsigned char *fb = 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] = b;
+                       fb[j * 4 + 2] = r;
                }
                fb += screen_rect.width * 4;
        }
@@ -107,3 +122,98 @@ void set_cursor_visibility(bool visible)
                fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
        }
 }
+
+void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
+               const Rect &dest_rect, int dest_x, int dest_y)
+{
+       int width = src_rect.width;
+       int height = src_rect.height;
+
+       int xoffs = dest_x - dest_rect.x;
+       if(xoffs < 0) {
+               dest_x = dest_rect.x;
+               width += xoffs;
+       }
+
+       int yoffs = dest_y - dest_rect.y;
+       if(yoffs < 0) {
+               dest_y = dest_rect.y;
+               height += yoffs;
+       }
+
+       int xend = dest_x + width;
+       if(xend >= dest_rect.width) {
+               width -= xend - dest_rect.width;
+       }
+
+       int yend = dest_y + height;
+       if(yend >= dest_rect.height) {
+               height -= yend - dest_rect.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++) {
+               memcpy(dptr, sptr, width * 4);
+               sptr += src_rect.width * 4;
+               dptr += dest_rect.width * 4;
+       }
+}
+
+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)
+{
+       int width = src_rect.width;
+       int height = src_rect.height;
+
+       int xoffs = dest_x - dest_rect.x;
+       if(xoffs < 0) {
+               dest_x = dest_rect.x;
+               width += xoffs;
+       }
+
+       int yoffs = dest_y - dest_rect.y;
+       if(yoffs < 0) {
+               dest_y = dest_rect.y;
+               height += yoffs;
+       }
+
+       int xend = dest_x + width;
+       if(xend >= dest_rect.width) {
+               width -= xend - dest_rect.width;
+       }
+
+       int yend = dest_y + height;
+       if(yend >= dest_rect.height) {
+               height -= yend - dest_rect.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;
+                       }
+               }
+
+               sptr += src_rect.width * 4;
+               dptr += dest_rect.width * 4;
+       }
+}