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