9b708e006e9d87d665c8b0bad4af171b14b7c3eb
[winnie] / src / sdl / gfx.cc
1 #ifdef WINNIE_SDL
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <SDL/SDL.h>
5 #include "gfx.h"
6
7 struct Graphics {
8         SDL_Surface *fbsurf;
9         Rect screen_rect;
10         Rect clipping_rect;
11         int color_depth; // bits per pixel
12         Pixmap *pixmap;
13 };
14
15 static Graphics *gfx;
16
17 bool init_gfx()
18 {
19         if(SDL_Init(SDL_INIT_VIDEO) == -1) {
20                 fprintf(stderr, "failed to initialize SDL\n");
21                 return false;
22         }
23
24         if(!(gfx = (Graphics*)malloc(sizeof *gfx))) {
25                 return false;
26         }
27
28         Rect scr_rect = {0, 0, 1024, 768};
29         gfx->screen_rect = scr_rect;
30         gfx->color_depth = 32;
31
32         if(!(gfx->fbsurf = SDL_SetVideoMode(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth, 0))) {
33                 fprintf(stderr, "failed to set video mode\n");
34                 return false;
35         }
36         SDL_ShowCursor(0);
37
38         gfx->pixmap = new Pixmap;
39         gfx->pixmap->width = gfx->screen_rect.width;
40         gfx->pixmap->height = gfx->screen_rect.height;
41         gfx->pixmap->pixels = (unsigned char*)gfx->fbsurf->pixels;
42
43         set_clipping_rect(gfx->screen_rect);
44
45         return true;
46 }
47
48 void destroy_gfx()
49 {
50         gfx->pixmap->pixels = 0;
51         delete gfx->pixmap;
52         free(gfx);
53         SDL_Quit();
54 }
55
56 unsigned char *get_framebuffer()
57 {
58         return (unsigned char*)gfx->fbsurf->pixels;
59 }
60
61 Pixmap *get_framebuffer_pixmap()
62 {
63         return gfx->pixmap;
64 }
65
66 Rect get_screen_size()
67 {
68         return gfx->screen_rect;
69 }
70
71 int get_color_depth()
72 {
73         return gfx->color_depth;
74 }
75
76 /*void set_clipping_rect(const Rect &rect)
77 {
78         gfx->clipping_rect = rect_intersection(rect, gfx->screen_rect);
79
80         SDL_Rect sdl_rect;
81         sdl_rect.x = gfx->clipping_rect.x;
82         sdl_rect.y = gfx->clipping_rect.y;
83         sdl_rect.w = gfx->clipping_recvoid fill_rect(const Rect &rect, int r, int g, int b)
84 {
85         Rect drect = rect;
86         Rect screen_rect = get_screen_size();
87
88         if(drect.x < clipping_rect.x) {
89                 drect.width -= clipping_rect.x - drect.x;
90                 drect.x = clipping_rect.x;
91         }
92
93         if(drect.y < clipping_rect.y) {
94                 drect.height -= clipping_rect.y - drect.y;
95                 drect.y = clipping_rect.y;
96         }
97
98         if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
99                 drect.width = clipping_rect.width + clipping_rect.x - drect.x;
100         }
101
102         if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
103                 drect.height = clipping_rect.height + clipping_rect.y - drect.y;
104         }
105
106         unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
107         for(int i=0; i<drect.height; i++) {
108                 for(int j=0; j<drect.width; j++) {
109                         fb[j * 4] = b;
110                         fb[j * 4 + 1] = g;
111                         fb[j * 4 + 2] = r;
112                 }
113                 fb += screen_rect.width * 4;
114         }
115 }
116 t.width;
117         sdl_rect.h = gfx->clipping_rect.height;
118
119         SDL_SetClipRect(gfx->fbsurf, &sdl_rect);
120 }
121
122 const Rect &get_clipping_rect()
123 {
124         return gfx->clipping_rect;
125 }
126
127 void clear_screen(int r, int g, int b)
128 {
129         fill_rect(gfx->screen_rect, r, g, b);
130 }
131
132 void fill_rect(const Rect &rect, int r, int g, int b)
133 {
134         uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
135         
136         SDL_Rect sdl_rect;
137         sdl_rect.x = rect.x;
138         sdl_rect.y = rect.y;
139         sdl_rect.w = rect.width;
140         sdl_rect.h = rect.height;
141
142         SDL_FillRect(gfx->fbsurf, &sdl_rect, color);
143 }*/
144
145 void set_clipping_rect(const Rect &rect)
146 {
147         gfx->clipping_rect = rect_intersection(rect, get_screen_size());
148 }
149
150 const Rect &get_clipping_rect()
151 {
152         return gfx->clipping_rect;
153 }
154
155
156 void set_cursor_visibility(bool visible)
157 {
158 }
159
160 void gfx_update()
161 {
162         SDL_UpdateRect(gfx->fbsurf, 0, 0, 0, 0);
163 }
164
165 void wait_vsync()
166 {
167 }
168
169 #endif // WINNIE_SDL