fbdev version works with truetype
[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 static SDL_Surface *fbsurf;
8
9 static Rect screen_rect = {0, 0, 1024, 768};
10 static int color_depth = 32; // bits per pixel
11
12 static Pixmap *pixmap;
13
14 bool init_gfx()
15 {
16         if(SDL_Init(SDL_INIT_VIDEO) == -1) {
17                 fprintf(stderr, "failed to initialize SDL\n");
18                 return false;
19         }
20
21         if(!(fbsurf = SDL_SetVideoMode(screen_rect.width, screen_rect.height, color_depth, 0))) {
22                 fprintf(stderr, "failed to set video mode\n");
23                 return false;
24         }
25         SDL_ShowCursor(0);
26
27         pixmap = new Pixmap;
28         pixmap->width = screen_rect.width;
29         pixmap->height = screen_rect.height;
30         pixmap->pixels = (unsigned char*)fbsurf->pixels;
31
32         return true;
33 }
34
35 void destroy_gfx()
36 {
37         pixmap->pixels = 0;
38         delete pixmap;
39         SDL_Quit();
40 }
41
42 unsigned char *get_framebuffer()
43 {
44         return (unsigned char*)fbsurf->pixels;
45 }
46
47 Pixmap *get_framebuffer_pixmap()
48 {
49         return pixmap;
50 }
51
52 Rect get_screen_size()
53 {
54         return screen_rect;
55 }
56
57 int get_color_depth()
58 {
59         return color_depth;
60 }
61
62 void clear_screen(int r, int g, int b)
63 {
64         fill_rect(screen_rect, r, g, b);
65 }
66
67 void fill_rect(const Rect &rect, int r, int g, int b)
68 {
69         uint32_t color = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
70         
71         SDL_Rect sdl_rect;
72         sdl_rect.x = rect.x;
73         sdl_rect.y = rect.y;
74         sdl_rect.w = rect.width;
75         sdl_rect.h = rect.height;
76
77         SDL_FillRect(fbsurf, &sdl_rect, color);
78 }
79
80 void set_cursor_visibility(bool visible)
81 {
82 }
83
84 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
85                 const Rect &dest_rect, int dest_x, int dest_y)
86 {
87         int width = src_rect.width;
88         int height = src_rect.height;
89
90         int xoffs = dest_x - dest_rect.x;
91         if(xoffs < 0) {
92                 dest_x = dest_rect.x;
93                 width += xoffs;
94         }
95
96         int yoffs = dest_y - dest_rect.y;
97         if(yoffs < 0) {
98                 dest_y = dest_rect.y;
99                 height += yoffs;
100         }
101
102         int xend = dest_x + width;
103         if(xend >= dest_rect.width) {
104                 width -= xend - dest_rect.width;
105         }
106
107         int yend = dest_y + height;
108         if(yend >= dest_rect.height) {
109                 height -= yend - dest_rect.height;
110         }
111
112         if(width <= 0 || height <= 0) {
113                 return;
114         }
115
116         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
117         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
118
119         for(int i=0; i<height; i++) {
120                 memcpy(dptr, sptr, width * 4);
121                 sptr += src_rect.width * 4;
122                 dptr += dest_rect.width * 4;
123         }
124 }
125
126 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
127                 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
128 {
129         int width = src_rect.width;
130         int height = src_rect.height;
131
132         int xoffs = dest_x - dest_rect.x;
133         if(xoffs < 0) {
134                 dest_x = dest_rect.x;
135                 width += xoffs;
136         }
137
138         int yoffs = dest_y - dest_rect.y;
139         if(yoffs < 0) {
140                 dest_y = dest_rect.y;
141                 height += yoffs;
142         }
143
144         int xend = dest_x + width;
145         if(xend >= dest_rect.width) {
146                 width -= xend - dest_rect.width;
147         }
148
149         int yend = dest_y + height;
150         if(yend >= dest_rect.height) {
151                 height -= yend - dest_rect.height;
152         }
153
154         if(width <= 0 || height <= 0) {
155                 return;
156         }
157
158         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
159         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
160
161         for(int i=0; i<height; i++) {
162                 for(int j=0; j<width; j++) {
163                         int r = sptr[j * 4];
164                         int g = sptr[j * 4 + 1];
165                         int b = sptr[j * 4 + 2];
166
167                         if(r != key_r || g != key_g || b != key_b) {
168                                 dptr[j * 4] = r;
169                                 dptr[j * 4 + 1] = g;
170                                 dptr[j * 4 + 2] = b;
171                         }
172                 }
173
174                 sptr += src_rect.width * 4;
175                 dptr += dest_rect.width * 4;
176         }
177 }
178
179 void gfx_update()
180 {
181         SDL_UpdateRect(fbsurf, 0, 0, 0, 0);
182 }
183
184 void wait_vsync()
185 {
186 }
187
188 #endif // WINNIE_SDL