added license GPL
[winnie] / src / sdl / gfx.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
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.
10
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.
15
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/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #ifdef WINNIE_SDL
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <SDL/SDL.h>
26
27 #include "gfx.h"
28 #include "shalloc.h"
29
30 static SDL_Surface *fbsurf;
31
32 struct Graphics {
33         Rect screen_rect;
34         Rect clipping_rect;
35         int color_depth; // bits per pixel
36         Pixmap *pixmap;
37 };
38
39 static Graphics *gfx;
40
41 bool init_gfx()
42 {
43         if(SDL_Init(SDL_INIT_VIDEO) == -1) {
44                 fprintf(stderr, "failed to initialize SDL\n");
45                 return false;
46         }
47
48         if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
49                 return false;
50         }
51
52         Rect scr_rect(0, 0, 1024, 768);
53         gfx->screen_rect = scr_rect;
54         gfx->color_depth = 32;
55
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");
58                 return false;
59         }
60         SDL_ShowCursor(0);
61
62         if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
63                 fprintf(stderr, "Failed to allocate pixmap.\n");
64                 return false;
65         }
66
67         gfx->pixmap->width = gfx->screen_rect.width;
68         gfx->pixmap->height = gfx->screen_rect.height;
69
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");
73                 return false;
74         }
75
76         set_clipping_rect(gfx->screen_rect);
77
78         return true;
79 }
80
81 void destroy_gfx()
82 {
83         sh_free(gfx->pixmap->pixels);
84         gfx->pixmap->pixels = 0;
85         sh_free(gfx->pixmap);
86         sh_free(gfx);
87         SDL_Quit();
88 }
89
90 unsigned char *get_framebuffer()
91 {
92         return gfx->pixmap->pixels;
93 }
94
95 Pixmap *get_framebuffer_pixmap()
96 {
97         return gfx->pixmap;
98 }
99
100 Rect get_screen_size()
101 {
102         return gfx->screen_rect;
103 }
104
105 int get_color_depth()
106 {
107         return gfx->color_depth;
108 }
109
110 void set_clipping_rect(const Rect &rect)
111 {
112         gfx->clipping_rect = rect_intersection(rect, get_screen_size());
113 }
114
115 const Rect &get_clipping_rect()
116 {
117         return gfx->clipping_rect;
118 }
119
120
121 void set_cursor_visibility(bool visible)
122 {
123 }
124
125 void gfx_update(const Rect &upd_rect)
126 {
127         if(SDL_MUSTLOCK(fbsurf)) {
128                 SDL_LockSurface(fbsurf);
129         }
130
131         Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
132
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;
135
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;
140         }
141
142         if(SDL_MUSTLOCK(fbsurf)) {
143                 SDL_UnlockSurface(fbsurf);
144         }
145         SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height);
146 }
147
148 void wait_vsync()
149 {
150 }
151
152 void get_rgb_order(int *r, int *g, int *b)
153 {
154         *r = fbsurf->format->Rshift / 8;
155         *g = fbsurf->format->Gshift / 8;
156         *b = fbsurf->format->Bshift / 8;
157 }
158
159 #endif // WINNIE_SDL