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