X-Git-Url: https://eleni.mutantstargoat.com/git/?a=blobdiff_plain;f=src%2Fsdl%2Fgfx.cc;h=cd1a4a6dde592968f2ecaf88ced80536801338e9;hb=2985bfa49497805b4760c066a6288c4b3752d145;hp=84ab69033a6c0f187dcf504d255cdcf65d657b31;hpb=59cd3a77c4517e387f4c2e21e8b62a9017370116;p=winnie diff --git a/src/sdl/gfx.cc b/src/sdl/gfx.cc index 84ab690..cd1a4a6 100644 --- a/src/sdl/gfx.cc +++ b/src/sdl/gfx.cc @@ -2,16 +2,20 @@ #include #include #include + #include "gfx.h" +#include "shalloc.h" static SDL_Surface *fbsurf; -static Rect screen_rect = {0, 0, 1024, 768}; -static Rect clipping_rect; - -static int color_depth = 32; // bits per pixel +struct Graphics { + Rect screen_rect; + Rect clipping_rect; + int color_depth; // bits per pixel + Pixmap *pixmap; +}; -static Pixmap *pixmap; +static Graphics *gfx; bool init_gfx() { @@ -20,70 +24,122 @@ bool init_gfx() return false; } - if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) { - fprintf(stderr, "failed to set video mode\n"); + if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) { + return false; + } + + Rect scr_rect = {0, 0, 1024, 768}; + gfx->screen_rect = scr_rect; + gfx->color_depth = 32; + + if(!(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; + if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) { + fprintf(stderr, "Failed to allocate pixmap.\n"); + return false; + } - set_clipping_rect(screen_rect); + gfx->pixmap->width = gfx->screen_rect.width; + gfx->pixmap->height = gfx->screen_rect.height; + + int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8; + if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) { + fprintf(stderr, "failed to allocate the pixmap framebuffer.\n"); + return false; + } + + set_clipping_rect(gfx->screen_rect); return true; } void destroy_gfx() { - pixmap->pixels = 0; - delete pixmap; + sh_free(gfx->pixmap->pixels); + gfx->pixmap->pixels = 0; + sh_free(gfx->pixmap); + sh_free(gfx); SDL_Quit(); } unsigned char *get_framebuffer() { - return (unsigned char*)fbsurf->pixels; + return gfx->pixmap->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; + } + + 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; iclipping_rect.height; SDL_SetClipRect(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) @@ -99,12 +155,30 @@ void fill_rect(const Rect &rect, int r, int g, int b) SDL_FillRect(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() { + if(SDL_MUSTLOCK(fbsurf)) { + SDL_LockSurface(fbsurf); + } + memcpy(fbsurf->pixels, gfx->pixmap->pixels, gfx->pixmap->width * gfx->pixmap->height * (gfx->color_depth / 8)); + if(SDL_MUSTLOCK(fbsurf)) { + SDL_UnlockSurface(fbsurf); + } SDL_UpdateRect(fbsurf, 0, 0, 0, 0); }