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>
31 static SDL_Surface *fbsurf;
36 int color_depth; // bits per pixel
44 if(SDL_Init(SDL_INIT_VIDEO) == -1) {
45 fprintf(stderr, "failed to initialize SDL\n");
49 if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
53 get_subsys()->graphics_offset = (int)((char*)gfx - (char*)get_pool());
55 Rect scr_rect(0, 0, 1024, 768);
56 gfx->screen_rect = scr_rect;
57 gfx->color_depth = 32;
59 if(!(fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
60 fprintf(stderr, "Failed to set video mode\n");
65 if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
66 fprintf(stderr, "Failed to allocate pixmap.\n");
70 gfx->pixmap->width = gfx->screen_rect.width;
71 gfx->pixmap->height = gfx->screen_rect.height;
73 int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
74 if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
75 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
79 set_clipping_rect(gfx->screen_rect);
86 sh_free(gfx->pixmap->pixels);
87 gfx->pixmap->pixels = 0;
93 unsigned char *get_framebuffer()
95 return gfx->pixmap->pixels;
98 Pixmap *get_framebuffer_pixmap()
103 Rect get_screen_size()
105 return gfx->screen_rect;
108 int get_color_depth()
110 return gfx->color_depth;
113 void set_clipping_rect(const Rect &rect)
115 gfx->clipping_rect = rect_intersection(rect, get_screen_size());
118 const Rect &get_clipping_rect()
120 return gfx->clipping_rect;
124 void set_cursor_visibility(bool visible)
128 void gfx_update(const Rect &upd_rect)
130 if(SDL_MUSTLOCK(fbsurf)) {
131 SDL_LockSurface(fbsurf);
134 Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
136 unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
137 unsigned char *dptr = (unsigned char*)fbsurf->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
139 for(int i=0; i<rect.height; i++) {
140 memcpy(dptr, sptr, rect.width * 4);
141 sptr += gfx->screen_rect.width * 4;
142 dptr += gfx->screen_rect.width * 4;
145 if(SDL_MUSTLOCK(fbsurf)) {
146 SDL_UnlockSurface(fbsurf);
148 SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height);
155 void get_rgb_order(int *r, int *g, int *b)
157 *r = fbsurf->format->Rshift / 8;
158 *g = fbsurf->format->Gshift / 8;
159 *b = fbsurf->format->Bshift / 8;