2 winnie - an experimental window system
4 Copyright (C) 2013 Eleni Maria Stea
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
30 static SDL_Surface *fbsurf;
35 int color_depth; // bits per pixel
43 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
44 fprintf(stderr, "failed to initialize SDL\n");
48 if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
52 Rect scr_rect(0, 0, 1024, 768);
53 gfx->screen_rect = scr_rect;
54 gfx->color_depth = 32;
56 if(!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
57 fprintf(stderr, "Failed to set video mode\n");
62 if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
63 fprintf(stderr, "Failed to allocate pixmap.\n");
67 gfx->pixmap->width = gfx->screen_rect.width;
68 gfx->pixmap->height = gfx->screen_rect.height;
70 int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
71 if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
72 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
76 set_clipping_rect(gfx->screen_rect);
83 sh_free(gfx->pixmap->pixels);
84 gfx->pixmap->pixels = 0;
90 unsigned char *get_framebuffer()
92 return gfx->pixmap->pixels;
95 Pixmap *get_framebuffer_pixmap()
100 Rect get_screen_size()
102 return gfx->screen_rect;
105 int get_color_depth()
107 return gfx->color_depth;
110 void set_clipping_rect(const Rect &rect)
112 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
115 const Rect &get_clipping_rect()
117 return gfx->clipping_rect;
121 void set_cursor_visibility(bool visible)
125 void gfx_update(const Rect &upd_rect)
127 if(SDL_MUSTLOCK(fbsurf)) {
128 SDL_LockSurface(fbsurf);
131 Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
133 unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
134 unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
136 for(int i=0; i<rect.height; i++) {
137 memcpy(dptr, sptr, rect.width * 4);
138 sptr += gfx->screen_rect.width * 4;
139 dptr += gfx->screen_rect.width * 4;
142 if(SDL_MUSTLOCK(fbsurf)) {
143 SDL_UnlockSurface(fbsurf);
145 SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height);
152 void get_rgb_order(int *r, int *g, int *b)
154 *r = fbsurf->format->Rshift / 8;
155 *g = fbsurf->format->Gshift / 8;
156 *b = fbsurf->format->Bshift / 8;