| 1 | // Copyright (C) 2006-2009 by Tor Andersson. |
| 2 | // Copyright (C) 2010 by Ben Cressey. |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle 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 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle 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 Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | #include <memory> |
| 21 | |
| 22 | #include "glk.h" |
| 23 | #include "garglk.h" |
| 24 | |
| 25 | #define mul255(a,b) (((short)(a) * (b) + 127) / 255) |
| 26 | |
| 27 | static void |
| 28 | drawpicture(const picture_t *src, window_graphics_t *dst, |
| 29 | int x0, int y0, int width, int height, glui32 linkval); |
| 30 | |
| 31 | static void win_graphics_touch(window_graphics_t *dest) |
| 32 | { |
| 33 | dest->dirty = true; |
| 34 | winrepaint( |
| 35 | dest->owner->bbox.x0, |
| 36 | dest->owner->bbox.y0, |
| 37 | dest->owner->bbox.x1, |
| 38 | dest->owner->bbox.y1); |
| 39 | } |
| 40 | |
| 41 | void win_graphics_rearrange(window_t *win, rect_t *box) |
| 42 | { |
| 43 | window_graphics_t *dwin = win->wingraphics(); |
| 44 | int newwid, newhgt; |
| 45 | int oldw, oldh; |
| 46 | |
| 47 | win->bbox = *box; |
| 48 | |
| 49 | newwid = box->x1 - box->x0; |
| 50 | newhgt = box->y1 - box->y0; |
| 51 | oldw = dwin->w; |
| 52 | oldh = dwin->h; |
| 53 | |
| 54 | if (newwid <= 0 || newhgt <= 0) { |
| 55 | dwin->w = 0; |
| 56 | dwin->h = 0; |
| 57 | dwin->rgb.clear(); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | dwin->rgb.resize(newwid, newhgt, true); |
| 62 | dwin->w = newwid; |
| 63 | dwin->h = newhgt; |
| 64 | |
| 65 | if (newwid > oldw) { |
| 66 | win_graphics_erase_rect(dwin, false, oldw, 0, newwid - oldw, newhgt); |
| 67 | } |
| 68 | if (newhgt > oldh) { |
| 69 | win_graphics_erase_rect(dwin, false, 0, oldh, newwid, newhgt - oldh); |
| 70 | } |
| 71 | |
| 72 | win_graphics_touch(dwin); |
| 73 | } |
| 74 | |
| 75 | void win_graphics_redraw(window_t *win) |
| 76 | { |
| 77 | window_graphics_t *dwin = win->wingraphics(); |
| 78 | int x, y; |
| 79 | |
| 80 | int x0 = win->bbox.x0; |
| 81 | int y0 = win->bbox.y0; |
| 82 | |
| 83 | if (dwin->dirty || gli_force_redraw) { |
| 84 | dwin->dirty = false; |
| 85 | |
| 86 | if (dwin->rgb.empty()) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | for (y = 0; y < dwin->h; y++) { |
| 91 | for (x = 0; x < dwin->w; x++) { |
| 92 | gli_draw_pixel(x + x0, y + y0, dwin->rgb[y][x]); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void win_graphics_click(window_graphics_t *dwin, int sx, int sy) |
| 99 | { |
| 100 | window_t *win = dwin->owner; |
| 101 | int x = sx - win->bbox.x0; |
| 102 | int y = sy - win->bbox.y0; |
| 103 | |
| 104 | if (win->mouse_request) { |
| 105 | gli_event_store(evtype_MouseInput, win, gli_unzoom_int(x), gli_unzoom_int(y)); |
| 106 | win->mouse_request = false; |
| 107 | if (gli_conf_safeclicks) { |
| 108 | gli_forceclick = true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (win->hyper_request) { |
| 113 | glui32 linkval = gli_get_hyperlink(gli_unzoom_int(sx), gli_unzoom_int(sy)); |
| 114 | if (linkval != 0) { |
| 115 | gli_event_store(evtype_Hyperlink, win, linkval, 0); |
| 116 | win->hyper_request = false; |
| 117 | if (gli_conf_safeclicks) { |
| 118 | gli_forceclick = true; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | bool win_graphics_draw_picture(std::shared_ptr<picture_t> pic, window_graphics_t *dwin, |
| 125 | glsi32 xpos, glsi32 ypos, |
| 126 | glui32 imagewidth, glui32 imageheight) |
| 127 | { |
| 128 | glui32 hyperlink = dwin->owner->attr.hyper; |
| 129 | xpos = gli_zoom_int(xpos); |
| 130 | ypos = gli_zoom_int(ypos); |
| 131 | |
| 132 | imagewidth = gli_zoom_int(imagewidth); |
| 133 | imageheight = gli_zoom_int(imageheight); |
| 134 | |
| 135 | drawpicture(pic.get(), dwin, xpos, ypos, imagewidth, imageheight, hyperlink); |
| 136 | |
| 137 | win_graphics_touch(dwin); |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | void win_graphics_erase_rect(window_graphics_t *dwin, bool whole, |
| 143 | glsi32 x0, glsi32 y0, glui32 width, glui32 height) |
| 144 | { |
| 145 | int x1 = x0 + width; |
| 146 | int y1 = y0 + height; |
| 147 | int x, y; |
| 148 | int hx0, hx1, hy0, hy1; |
| 149 | |
| 150 | if (whole) { |
| 151 | x0 = 0; |
| 152 | y0 = 0; |
| 153 | x1 = dwin->w; |
| 154 | y1 = dwin->h; |
| 155 | } |
| 156 | |
| 157 | if (x0 < 0) { |
| 158 | x0 = 0; |
| 159 | } |
| 160 | if (y0 < 0) { |
| 161 | y0 = 0; |
| 162 | } |
| 163 | if (x1 < 0) { |
| 164 | x1 = 0; |
| 165 | } |
| 166 | if (y1 < 0) { |
| 167 | y1 = 0; |
| 168 | } |
| 169 | if (x0 >= dwin->w) { |
| 170 | x0 = dwin->w; |
| 171 | } |
| 172 | if (y0 >= dwin->h) { |
| 173 | y0 = dwin->h; |
| 174 | } |
| 175 | if (x1 >= dwin->w) { |
| 176 | x1 = dwin->w; |
| 177 | } |
| 178 | if (y1 >= dwin->h) { |
| 179 | y1 = dwin->h; |
| 180 | } |
| 181 | |
| 182 | hx0 = dwin->owner->bbox.x0 + x0; |
| 183 | hx1 = dwin->owner->bbox.x0 + x1; |
| 184 | hy0 = dwin->owner->bbox.y0 + y0; |
| 185 | hy1 = dwin->owner->bbox.y0 + y1; |
| 186 | |
| 187 | // zero out hyperlinks for these coordinates |
| 188 | gli_put_hyperlink(0, hx0, hy0, hx1, hy1); |
| 189 | |
| 190 | for (y = y0; y < y1; y++) { |
| 191 | for (x = x0; x < x1; x++) { |
| 192 | dwin->rgb[y][x] = dwin->bgnd; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | win_graphics_touch(dwin); |
| 197 | } |
| 198 | |
| 199 | void win_graphics_fill_rect(window_graphics_t *dwin, glui32 color, |
| 200 | glsi32 x0, glsi32 y0, glui32 width, glui32 height) |
| 201 | { |
| 202 | int x1 = x0 + width; |
| 203 | int y1 = y0 + height; |
| 204 | x0 = gli_zoom_int(x0); |
| 205 | y0 = gli_zoom_int(y0); |
| 206 | x1 = gli_zoom_int(x1); |
| 207 | y1 = gli_zoom_int(y1); |
| 208 | int x, y; |
| 209 | int hx0, hx1, hy0, hy1; |
| 210 | |
| 211 | Pixel<3> col((color >> 16) & 0xff, |
| 212 | (color >> 8) & 0xff, |
| 213 | (color >> 0) & 0xff); |
| 214 | |
| 215 | x0 = garglk::clamp(x0, 0, dwin->w); |
| 216 | y0 = garglk::clamp(y0, 0, dwin->h); |
| 217 | x1 = garglk::clamp(x1, 0, dwin->w); |
| 218 | y1 = garglk::clamp(y1, 0, dwin->h); |
| 219 | |
| 220 | hx0 = dwin->owner->bbox.x0 + x0; |
| 221 | hx1 = dwin->owner->bbox.x0 + x1; |
| 222 | hy0 = dwin->owner->bbox.y0 + y0; |
| 223 | hy1 = dwin->owner->bbox.y0 + y1; |
| 224 | |
| 225 | // zero out hyperlinks for these coordinates |
| 226 | gli_put_hyperlink(0, hx0, hy0, hx1, hy1); |
| 227 | |
| 228 | for (y = y0; y < y1; y++) { |
| 229 | for (x = x0; x < x1; x++) { |
| 230 | dwin->rgb[y][x] = col; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | win_graphics_touch(dwin); |
| 235 | } |
| 236 | |
| 237 | void win_graphics_set_background_color(window_graphics_t *dwin, glui32 color) |
| 238 | { |
| 239 | dwin->bgnd = Pixel<3>((color >> 16) & 0xff, |
| 240 | (color >> 8) & 0xff, |
| 241 | (color >> 0) & 0xff); |
| 242 | } |
| 243 | |
| 244 | static void drawpicture(const picture_t *src, window_graphics_t *dst, |
| 245 | int x0, int y0, int width, int height, glui32 linkval) |
| 246 | { |
| 247 | int dx1, dy1, x1, y1, sx0, sy0, sx1, sy1; |
| 248 | int w, h; |
| 249 | int hx0, hx1, hy0, hy1; |
| 250 | std::shared_ptr<picture_t> scaled; |
| 251 | |
| 252 | if (width != src->w || height != src->h) { |
| 253 | scaled = gli_picture_scale(src, width, height); |
| 254 | if (!scaled) { |
| 255 | return; |
| 256 | } |
| 257 | src = scaled.get(); |
| 258 | } |
| 259 | |
| 260 | sx0 = 0; |
| 261 | sy0 = 0; |
| 262 | sx1 = src->w; |
| 263 | sy1 = src->h; |
| 264 | dx1 = dst->w; |
| 265 | dy1 = dst->h; |
| 266 | |
| 267 | x1 = x0 + src->w; |
| 268 | y1 = y0 + src->h; |
| 269 | |
| 270 | if (x1 <= 0 || x0 >= dx1) { |
| 271 | return; |
| 272 | } |
| 273 | if (y1 <= 0 || y0 >= dy1) { |
| 274 | return; |
| 275 | } |
| 276 | if (x0 < 0) { |
| 277 | sx0 -= x0; |
| 278 | x0 = 0; |
| 279 | } |
| 280 | if (y0 < 0) { |
| 281 | sy0 -= y0; |
| 282 | y0 = 0; |
| 283 | } |
| 284 | if (x1 > dx1) { |
| 285 | sx1 += dx1 - x1; |
| 286 | x1 = dx1; |
| 287 | } |
| 288 | if (y1 > dy1) { |
| 289 | sy1 += dy1 - y1; |
| 290 | y1 = dy1; |
| 291 | } |
| 292 | |
| 293 | hx0 = dst->owner->bbox.x0 + x0; |
| 294 | hx1 = dst->owner->bbox.x0 + x1; |
| 295 | hy0 = dst->owner->bbox.y0 + y0; |
| 296 | hy1 = dst->owner->bbox.y0 + y1; |
| 297 | |
| 298 | // zero out or set hyperlink for these coordinates |
| 299 | gli_put_hyperlink(linkval, hx0, hy0, hx1, hy1); |
| 300 | |
| 301 | w = sx1 - sx0; |
| 302 | h = sy1 - sy0; |
| 303 | |
| 304 | for (int y = 0; y < h; y++) { |
| 305 | for (int x = 0; x < w; x++) { |
| 306 | auto existing = dst->rgb[y + y0][x + x0]; |
| 307 | unsigned char sa = src->rgba[y + sy0][x + sx0][3]; |
| 308 | unsigned char na = 255 - sa; |
| 309 | unsigned char sr = mul255(src->rgba[y + sy0][x + sx0][0], sa); |
| 310 | unsigned char sg = mul255(src->rgba[y + sy0][x + sx0][1], sa); |
| 311 | unsigned char sb = mul255(src->rgba[y + sy0][x + sx0][2], sa); |
| 312 | dst->rgb[y + y0][x + x0] =Pixel<3>(sr + mul255(existing[0], na), |
| 313 | sg + mul255(existing[1], na), |
| 314 | sb + mul255(existing[2], na)); |
| 315 | } |
| 316 | } |
| 317 | } |