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