X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fsdl%2Fgfx.cc;h=74aa94d2c14a7c9df1833bdef7372b6550df4d7e;hb=e2626c41c841dbbfb64ddf6341b4e23089036299;hp=5be33e68fd520cb3115ee728950a8f7fb9d47db6;hpb=c4ab1fa2768863098306862047bdb1116bd62a24;p=winnie diff --git a/src/sdl/gfx.cc b/src/sdl/gfx.cc index 5be33e6..74aa94d 100644 --- a/src/sdl/gfx.cc +++ b/src/sdl/gfx.cc @@ -1,13 +1,42 @@ +/* +winnie - an experimental window system + +Copyright (C) 2013 Eleni Maria Stea + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +Author: Eleni Maria Stea +*/ + #ifdef WINNIE_SDL #include #include #include + #include "gfx.h" +#include "shalloc.h" static SDL_Surface *fbsurf; -static Rect screen_rect = {0, 0, 1024, 768}; -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 Graphics *gfx; bool init_gfx() { @@ -16,159 +45,115 @@ 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); + if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) { + fprintf(stderr, "Failed to allocate pixmap.\n"); + return false; + } + + 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() { + 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; } -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, get_screen_size()); } -void set_cursor_visibility(bool visible) +const Rect &get_clipping_rect() { + return gfx->clipping_rect; } -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= dest_rect.width) { - width -= xend - dest_rect.width; - } + Rect rect = rect_intersection(upd_rect, gfx->screen_rect); - int yend = dest_y + height; - if(yend >= dest_rect.height) { - height -= yend - dest_rect.height; - } + unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; + unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4; - if(width <= 0 || height <= 0) { - return; + for(int i=0; iscreen_rect.width * 4; + dptr += gfx->screen_rect.width * 4; } - 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; iformat->Rshift / 8; + *g = fbsurf->format->Gshift / 8; + *b = fbsurf->format->Bshift / 8; } #endif // WINNIE_SDL