fixed grab win initialization
[winnie] / src / sdl / gfx.cc
index 84ab690..9b708e0 100644 (file)
@@ -4,14 +4,15 @@
 #include <SDL/SDL.h>
 #include "gfx.h"
 
-static SDL_Surface *fbsurf;
+struct Graphics {
+       SDL_Surface *fbsurf;
+       Rect screen_rect;
+       Rect clipping_rect;
+       int color_depth; // bits per pixel
+       Pixmap *pixmap;
+};
 
-static Rect screen_rect = {0, 0, 1024, 768};
-static Rect clipping_rect;
-
-static int color_depth = 32; // bits per pixel
-
-static Pixmap *pixmap;
+static Graphics *gfx;
 
 bool init_gfx()
 {
@@ -20,70 +21,112 @@ bool init_gfx()
                return false;
        }
 
-       if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) {
+       if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
+               return false;
+       }
+
+       Rect scr_rect = {0, 0, 1024, 768};
+       gfx->screen_rect = scr_rect;
+       gfx->color_depth = 32;
+
+       if(!(gfx->fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
                fprintf(stderr, "failed to set video mode\n");
                return false;
        }
        SDL_ShowCursor(0);
 
-       pixmap = new Pixmap;
-       pixmap->width = screen_rect.width;
-       pixmap->height = screen_rect.height;
-       pixmap->pixels = (unsigned char*)fbsurf->pixels;
+       gfx->pixmap = new Pixmap;
+       gfx->pixmap->width = gfx->screen_rect.width;
+       gfx->pixmap->height = gfx->screen_rect.height;
+       gfx->pixmap->pixels = (unsigned char*)gfx->fbsurf->pixels;
 
-       set_clipping_rect(screen_rect);
+       set_clipping_rect(gfx->screen_rect);
 
        return true;
 }
 
 void destroy_gfx()
 {
-       pixmap->pixels = 0;
-       delete pixmap;
+       gfx->pixmap->pixels = 0;
+       delete gfx->pixmap;
+       free(gfx);
        SDL_Quit();
 }
 
 unsigned char *get_framebuffer()
 {
-       return (unsigned char*)fbsurf->pixels;
+       return (unsigned char*)gfx->fbsurf->pixels;
 }
 
 Pixmap *get_framebuffer_pixmap()
 {
-       return pixmap;
+       return gfx->pixmap;
 }
 
 Rect get_screen_size()
 {
-       return screen_rect;
+       return gfx->screen_rect;
 }
 
 int get_color_depth()
 {
-       return color_depth;
+       return gfx->color_depth;
 }
 
 /*void set_clipping_rect(const Rect &rect)
 {
-       clipping_rect = rect_intersection(rect, screen_rect);
+       gfx->clipping_rect = rect_intersection(rect, gfx->screen_rect);
 
        SDL_Rect sdl_rect;
-       sdl_rect.x = clipping_rect.x;
-       sdl_rect.y = clipping_rect.y;
-       sdl_rect.w = clipping_rect.width;
-       sdl_rect.h = clipping_rect.height;
+       sdl_rect.x = gfx->clipping_rect.x;
+       sdl_rect.y = gfx->clipping_rect.y;
+       sdl_rect.w = gfx->clipping_recvoid fill_rect(const Rect &rect, int r, int g, int b)
+{
+       Rect drect = rect;
+       Rect screen_rect = get_screen_size();
+
+       if(drect.x < clipping_rect.x) {
+               drect.width -= clipping_rect.x - drect.x;
+               drect.x = clipping_rect.x;
+       }
+
+       if(drect.y < clipping_rect.y) {
+               drect.height -= clipping_rect.y - drect.y;
+               drect.y = clipping_rect.y;
+       }
+
+       if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
+               drect.width = clipping_rect.width + clipping_rect.x - drect.x;
+       }
 
-       SDL_SetClipRect(fbsurf, &sdl_rect);
+       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<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;
+       }
+}
+t.width;
+       sdl_rect.h = gfx->clipping_rect.height;
+
+       SDL_SetClipRect(gfx->fbsurf, &sdl_rect);
 }
 
 const Rect &get_clipping_rect()
 {
-       return clipping_rect;
+       return gfx->clipping_rect;
 }
 
 void clear_screen(int r, int g, int b)
 {
-       fill_rect(screen_rect, r, g, b);
+       fill_rect(gfx->screen_rect, r, g, b);
 }
 
 void fill_rect(const Rect &rect, int r, int g, int b)
@@ -96,16 +139,27 @@ void fill_rect(const Rect &rect, int r, int g, int b)
        sdl_rect.w = rect.width;
        sdl_rect.h = rect.height;
 
-       SDL_FillRect(fbsurf, &sdl_rect, color);
+       SDL_FillRect(gfx->fbsurf, &sdl_rect, color);
 }*/
 
+void set_clipping_rect(const Rect &rect)
+{
+       gfx->clipping_rect = rect_intersection(rect, get_screen_size());
+}
+
+const Rect &get_clipping_rect()
+{
+       return gfx->clipping_rect;
+}
+
+
 void set_cursor_visibility(bool visible)
 {
 }
 
 void gfx_update()
 {
-       SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
+       SDL_UpdateRect(gfx->fbsurf, 0, 0, 0, 0);
 }
 
 void wait_vsync()