*in progress*
[winnie] / src / gfx.cc
index 54ed7f9..226d269 100644 (file)
@@ -14,7 +14,7 @@
 
 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
 
-static unsigned char* framebuffer;
+static unsigned char *framebuffer;
 static int dev_fd = -1;
 
 static Rect screen_rect;
@@ -58,14 +58,19 @@ 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));
        framebuffer = 0;
 }
 
-unsigned char* get_framebuffer()
+unsigned char *get_framebuffer()
 {
        return framebuffer;
 }
@@ -82,12 +87,19 @@ int get_color_depth()
 
 void clear_screen(int r, int g, int b)
 {
-       unsigned char* fb = framebuffer;
-       for(int i=0; i<screen_rect.width * screen_rect.height; i++) {
-               *fb++ = r;
-               *fb++ = g;
-               *fb++ = b;
-               fb++;
+       fill_rect(screen_rect, r, g, b);
+}
+
+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;
        }
 }
 
@@ -100,3 +112,18 @@ 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, int dest_x, int dest_y)
+{
+       Rect dest_rect;
+
+       if(dest_x < screen_rect.x) {
+               dest_rect.x = screen_rect.x;
+       }
+
+       if(dest_y < screen_rect.y) {
+               dest_rect.y = screen_rect.y;
+       }
+
+       //TODO :p zzz
+}