c517b399a952309dcfa885a68e57794c951e67b8
[winnie] / src / gfx.cc
1 #include <string.h>
2
3 #include "geom.h"
4 #include "gfx.h"
5
6 void clear_screen(int r, int g, int b)
7 {
8         Rect screen_rect = get_screen_size();
9         fill_rect(screen_rect, r, g, b);
10 }
11
12 void fill_rect(const Rect &rect, int r, int g, int b)
13 {
14         Rect drect = rect;
15         Rect screen_rect = get_screen_size();
16         Rect clipping_rect = get_clipping_rect();
17
18         if(drect.x < clipping_rect.x) {
19                 drect.width -= clipping_rect.x - drect.x;
20                 drect.x = clipping_rect.x;
21         }
22
23         if(drect.y < clipping_rect.y) {
24                 drect.height -= clipping_rect.y - drect.y;
25                 drect.y = clipping_rect.y;
26         }
27
28         if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
29                 drect.width = clipping_rect.width + clipping_rect.x - drect.x;
30         }
31
32         if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
33                 drect.height = clipping_rect.height + clipping_rect.y - drect.y;
34         }
35
36         unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
37         for(int i=0; i<drect.height; i++) {
38                 for(int j=0; j<drect.width; j++) {
39                         fb[j * 4] = b;
40                         fb[j * 4 + 1] = g;
41                         fb[j * 4 + 2] = r;
42                 }
43                 fb += screen_rect.width * 4;
44         }
45 }
46
47 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
48                 const Rect &dest_rect, int dest_x, int dest_y)
49 {
50         int red_offs, green_offs, blue_offs;
51         get_rgb_order(&red_offs, &green_offs, &blue_offs);
52
53         Rect irect = rect_intersection(get_clipping_rect(), dest_rect);
54
55         int width = src_rect.width;
56         int height = src_rect.height;
57
58         int xoffs = dest_x - irect.x;
59         if(xoffs < 0) {
60                 dest_x = irect.x;
61                 width += xoffs;
62         }
63
64         int yoffs = dest_y - irect.y;
65         if(yoffs < 0) {
66                 dest_y = irect.y;
67                 height += yoffs;
68         }
69
70         int xend = dest_x + width;
71         if(xend >= irect.width) {
72                 width -= xend - irect.width;
73         }
74
75         int yend = dest_y + height;
76         if(yend >= irect.height) {
77                 height -= yend - irect.height;
78         }
79
80         if(width <= 0 || height <= 0) {
81                 return;
82         }
83
84         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
85         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
86
87         for(int i=0; i<height; i++) {
88                 for(int j=0; j<width; j++) {
89                         dptr[j * 4 + red_offs] = sptr[j * 4];
90                         dptr[j * 4 + green_offs] = sptr[j * 4 + 1];
91                         dptr[j * 4 + blue_offs] = sptr[j * 4 + 2];
92                 }
93                 sptr += src_rect.width * 4;
94                 dptr += dest_rect.width * 4;
95         }
96 }
97
98 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
99                 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
100 {
101         int red_offs, green_offs, blue_offs;
102         get_rgb_order(&red_offs, &green_offs, &blue_offs);
103
104         Rect irect = rect_intersection(get_clipping_rect(), dest_rect);
105
106         int width = src_rect.width;
107         int height = src_rect.height;
108
109         int xoffs = dest_x - irect.x;
110         if(xoffs < 0) {
111                 dest_x = irect.x;
112                 width += xoffs;
113         }
114
115         int yoffs = dest_y - irect.y;
116         if(yoffs < 0) {
117                 dest_y = irect.y;
118                 height += yoffs;
119         }
120
121         int xend = dest_x + width;
122         if(xend >= irect.width) {
123                 width -= xend - irect.width;
124         }
125
126         int yend = dest_y + height;
127         if(yend >= irect.height) {
128                 height -= yend - irect.height;
129         }
130
131         if(width <= 0 || height <= 0) {
132                 return;
133         }
134
135         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
136         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
137
138         for(int i=0; i<height; i++) {
139                 for(int j=0; j<width; j++) {
140                         int r = sptr[j * 4];
141                         int g = sptr[j * 4 + 1];
142                         int b = sptr[j * 4 + 2];
143
144                         if(r != key_r || g != key_g || b != key_b) {
145                                 dptr[j * 4 + red_offs] = r;
146                                 dptr[j * 4 + green_offs] = g;
147                                 dptr[j * 4 + blue_offs] = b;
148                         }
149                 }
150
151                 sptr += src_rect.width * 4;
152                 dptr += dest_rect.width * 4;
153         }
154 }