added struct subsys so that we know each
[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 #include "winnie.h"
30
31 static SDL_Surface *fbsurf;
32
33 struct Graphics {
34         Rect screen_rect;
35         Rect clipping_rect;
36         int color_depth; // bits per pixel
37         Pixmap *pixmap;
38 };
39
40 static Graphics *gfx;
41
42 bool init_gfx()
43 {
44         if(SDL_Init(SDL_INIT_VIDEO) == -1) {
45                 fprintf(stderr, "failed to initialize SDL\n");
46                 return false;
47         }
48
49         if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
50                 return false;
51         }
52
53         get_subsys()->graphics_offset = (int)((char*)gfx - (char*)get_pool());
54
55         Rect scr_rect(0, 0, 1024, 768);
56         gfx->screen_rect = scr_rect;
57         gfx->color_depth = 32;
58
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");
61                 return false;
62         }
63         SDL_ShowCursor(0);
64
65         if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
66                 fprintf(stderr, "Failed to allocate pixmap.\n");
67                 return false;
68         }
69
70         gfx->pixmap->width = gfx->screen_rect.width;
71         gfx->pixmap->height = gfx->screen_rect.height;
72
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");
76                 return false;
77         }
78
79         set_clipping_rect(gfx->screen_rect);
80
81         return true;
82 }
83
84 void destroy_gfx()
85 {
86         sh_free(gfx->pixmap->pixels);
87         gfx->pixmap->pixels = 0;
88         sh_free(gfx->pixmap);
89         sh_free(gfx);
90         SDL_Quit();
91 }
92
93 unsigned char *get_framebuffer()
94 {
95         return gfx->pixmap->pixels;
96 }
97
98 Pixmap *get_framebuffer_pixmap()
99 {
100         return gfx->pixmap;
101 }
102
103 Rect get_screen_size()
104 {
105         return gfx->screen_rect;
106 }
107
108 int get_color_depth()
109 {
110         return gfx->color_depth;
111 }
112
113 void set_clipping_rect(const Rect &rect)
114 {
115         gfx->clipping_rect = rect_intersection(rect, get_screen_size());
116 }
117
118 const Rect &get_clipping_rect()
119 {
120         return gfx->clipping_rect;
121 }
122
123
124 void set_cursor_visibility(bool visible)
125 {
126 }
127
128 void gfx_update(const Rect &upd_rect)
129 {
130         if(SDL_MUSTLOCK(fbsurf)) {
131                 SDL_LockSurface(fbsurf);
132         }
133
134         Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
135
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;
138
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;
143         }
144
145         if(SDL_MUSTLOCK(fbsurf)) {
146                 SDL_UnlockSurface(fbsurf);
147         }
148         SDL_UpdateRect(fbsurf, rect.x, rect.y, rect.width, rect.height);
149 }
150
151 void wait_vsync()
152 {
153 }
154
155 void get_rgb_order(int *r, int *g, int *b)
156 {
157         *r = fbsurf->format->Rshift / 8;
158         *g = fbsurf->format->Gshift / 8;
159         *b = fbsurf->format->Bshift / 8;
160 }
161
162 #endif // WINNIE_SDL