added license GPL
[winnie] / src / gfx.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #include <string.h>
23
24 #include "geom.h"
25 #include "gfx.h"
26
27 void clear_screen(int r, int g, int b)
28 {
29         Rect screen_rect = get_screen_size();
30         fill_rect(screen_rect, r, g, b);
31 }
32
33 void fill_rect(const Rect &rect, int r, int g, int b)
34 {
35         Rect drect = rect;
36         Rect screen_rect = get_screen_size();
37         Rect clipping_rect = get_clipping_rect();
38
39         if(drect.x < clipping_rect.x) {
40                 drect.width -= clipping_rect.x - drect.x;
41                 drect.x = clipping_rect.x;
42         }
43
44         if(drect.y < clipping_rect.y) {
45                 drect.height -= clipping_rect.y - drect.y;
46                 drect.y = clipping_rect.y;
47         }
48
49         if(drect.x + drect.width >= clipping_rect.x + clipping_rect.width) {
50                 drect.width = clipping_rect.width + clipping_rect.x - drect.x;
51         }
52
53         if(drect.y + drect.height >= clipping_rect.y + clipping_rect.height) {
54                 drect.height = clipping_rect.height + clipping_rect.y - drect.y;
55         }
56
57         unsigned char *fb = get_framebuffer() + (drect.x + screen_rect.width * drect.y) * 4;
58         for(int i=0; i<drect.height; i++) {
59                 for(int j=0; j<drect.width; j++) {
60                         fb[j * 4] = b;
61                         fb[j * 4 + 1] = g;
62                         fb[j * 4 + 2] = r;
63                 }
64                 fb += screen_rect.width * 4;
65         }
66 }
67
68 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
69                 const Rect &dest_rect, int dest_x, int dest_y)
70 {
71         int red_offs, green_offs, blue_offs;
72         get_rgb_order(&red_offs, &green_offs, &blue_offs);
73
74         Rect irect = rect_intersection(get_clipping_rect(), dest_rect);
75
76         int width = src_rect.width;
77         int height = src_rect.height;
78
79         int xoffs = dest_x - irect.x;
80         if(xoffs < 0) {
81                 dest_x = irect.x;
82                 width += xoffs;
83         }
84
85         int yoffs = dest_y - irect.y;
86         if(yoffs < 0) {
87                 dest_y = irect.y;
88                 height += yoffs;
89         }
90
91         int xend = dest_x + width;
92         if(xend >= irect.width) {
93                 width -= xend - irect.width;
94         }
95
96         int yend = dest_y + height;
97         if(yend >= irect.height) {
98                 height -= yend - irect.height;
99         }
100
101         if(width <= 0 || height <= 0) {
102                 return;
103         }
104
105         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
106         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
107
108         for(int i=0; i<height; i++) {
109                 for(int j=0; j<width; j++) {
110                         dptr[j * 4 + red_offs] = sptr[j * 4];
111                         dptr[j * 4 + green_offs] = sptr[j * 4 + 1];
112                         dptr[j * 4 + blue_offs] = sptr[j * 4 + 2];
113                 }
114                 sptr += src_rect.width * 4;
115                 dptr += dest_rect.width * 4;
116         }
117 }
118
119 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
120                 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
121 {
122         int red_offs, green_offs, blue_offs;
123         get_rgb_order(&red_offs, &green_offs, &blue_offs);
124
125         Rect irect = rect_intersection(get_clipping_rect(), dest_rect);
126
127         int width = src_rect.width;
128         int height = src_rect.height;
129
130         int xoffs = dest_x - irect.x;
131         if(xoffs < 0) {
132                 dest_x = irect.x;
133                 width += xoffs;
134         }
135
136         int yoffs = dest_y - irect.y;
137         if(yoffs < 0) {
138                 dest_y = irect.y;
139                 height += yoffs;
140         }
141
142         int xend = dest_x + width;
143         if(xend >= irect.width) {
144                 width -= xend - irect.width;
145         }
146
147         int yend = dest_y + height;
148         if(yend >= irect.height) {
149                 height -= yend - irect.height;
150         }
151
152         if(width <= 0 || height <= 0) {
153                 return;
154         }
155
156         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
157         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
158
159         for(int i=0; i<height; i++) {
160                 for(int j=0; j<width; j++) {
161                         int r = sptr[j * 4];
162                         int g = sptr[j * 4 + 1];
163                         int b = sptr[j * 4 + 2];
164
165                         if(r != key_r || g != key_g || b != key_b) {
166                                 dptr[j * 4 + red_offs] = r;
167                                 dptr[j * 4 + green_offs] = g;
168                                 dptr[j * 4 + blue_offs] = b;
169                         }
170                 }
171
172                 sptr += src_rect.width * 4;
173                 dptr += dest_rect.width * 4;
174         }
175 }