X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fsdl%2Fgfx.cc;h=9b708e006e9d87d665c8b0bad4af171b14b7c3eb;hb=5984d5479693fd7519674a5bc40ebf804f8d0a46;hp=b7576888ee6ec7798dad12b346faea47c67175bc;hpb=6dc42c0e7292d6d94de640ddd7de3ee5c808e9c2;p=winnie diff --git a/src/sdl/gfx.cc b/src/sdl/gfx.cc index b757688..9b708e0 100644 --- a/src/sdl/gfx.cc +++ b/src/sdl/gfx.cc @@ -4,10 +4,15 @@ #include #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 int color_depth = 32; // bits per pixel +static Graphics *gfx; bool init_gfx() { @@ -16,155 +21,149 @@ 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); + 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(gfx->screen_rect); + return true; } void destroy_gfx() { + 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; } -Rect get_screen_size() +Pixmap *get_framebuffer_pixmap() { - return screen_rect; + return gfx->pixmap; } -int get_color_depth() +Rect get_screen_size() { - return color_depth; + return gfx->screen_rect; } -void clear_screen(int r, int g, int b) +int get_color_depth() { - fill_rect(screen_rect, r, g, b); + return gfx->color_depth; } -void fill_rect(const Rect &rect, int r, int g, int b) +/*void set_clipping_rect(const Rect &rect) { - uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); - - SDL_Rect sdl_rect; - sdl_rect.x = rect.x; - sdl_rect.y = rect.y; - sdl_rect.w = rect.width; - sdl_rect.h = rect.height; - - SDL_FillRect(fbsurf, &sdl_rect, color); -} + gfx->clipping_rect = rect_intersection(rect, gfx->screen_rect); -void set_cursor_visibility(bool visible) -{ -} - -void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img, - const Rect &dest_rect, int dest_x, int dest_y) + SDL_Rect sdl_rect; + 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) { - int width = src_rect.width; - int height = src_rect.height; + Rect drect = rect; + Rect screen_rect = get_screen_size(); - int xoffs = dest_x - dest_rect.x; - if(xoffs < 0) { - dest_x = dest_rect.x; - width += xoffs; + if(drect.x < clipping_rect.x) { + drect.width -= clipping_rect.x - drect.x; + drect.x = clipping_rect.x; } - int yoffs = dest_y - dest_rect.y; - if(yoffs < 0) { - dest_y = dest_rect.y; - height += yoffs; + if(drect.y < clipping_rect.y) { + drect.height -= clipping_rect.y - drect.y; + drect.y = clipping_rect.y; } - int xend = dest_x + width; - if(xend >= dest_rect.width) { - width -= xend - dest_rect.width; + if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) { + drect.width = clipping_rect.width + clipping_rect.x - drect.x; } - int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; + if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) { + drect.height = clipping_rect.height + clipping_rect.y - drect.y; } - if(width <= 0 || height <= 0) { - return; + unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4; + for(int i=0; iclipping_rect.height; - 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; ifbsurf, &sdl_rect); } -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) +const Rect &get_clipping_rect() { - 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; - } + return gfx->clipping_rect; +} - int xend = dest_x + width; - if(xend >= dest_rect.width) { - width -= xend - dest_rect.width; - } +void clear_screen(int r, int g, int b) +{ + fill_rect(gfx->screen_rect, r, g, b); +} - int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; - } +void fill_rect(const Rect &rect, int r, int g, int b) +{ + uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff); + + SDL_Rect sdl_rect; + sdl_rect.x = rect.x; + sdl_rect.y = rect.y; + sdl_rect.w = rect.width; + sdl_rect.h = rect.height; - if(width <= 0 || height <= 0) { - return; - } + SDL_FillRect(gfx->fbsurf, &sdl_rect, color); +}*/ - 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; +void set_clipping_rect(const Rect &rect) +{ + gfx->clipping_rect = rect_intersection(rect, get_screen_size()); +} - for(int i=0; iclipping_rect; +} - 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; - } +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() +{ } #endif // WINNIE_SDL