[151] | 1 | /* |
---|
[185] | 2 | * libcaca ASCII-Art library |
---|
[151] | 3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
| 4 | * All Rights Reserved |
---|
| 5 | * |
---|
[192] | 6 | * This library is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2 of the License, or (at your option) any later version. |
---|
[151] | 10 | * |
---|
[192] | 11 | * This library is distributed in the hope that it will be useful, |
---|
[151] | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[192] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
[151] | 15 | * |
---|
[192] | 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 19 | * 02111-1307 USA |
---|
[151] | 20 | */ |
---|
| 21 | |
---|
[205] | 22 | /** \file box.c |
---|
| 23 | * \version \$Id: box.c 257 2003-12-18 00:11:52Z sam $ |
---|
| 24 | * \author Sam Hocevar <sam@zoy.org> |
---|
| 25 | * \brief Simple box drawing functions |
---|
| 26 | * |
---|
| 27 | * This file contains box drawing functions, both filled and outline. |
---|
| 28 | */ |
---|
| 29 | |
---|
[151] | 30 | #include "config.h" |
---|
| 31 | |
---|
| 32 | #include <stdlib.h> |
---|
| 33 | |
---|
[185] | 34 | #include "caca.h" |
---|
| 35 | #include "caca_internals.h" |
---|
[151] | 36 | |
---|
[257] | 37 | /** |
---|
| 38 | * \brief Draw a box on the screen using the given character. |
---|
| 39 | * |
---|
| 40 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
| 41 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
| 42 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
| 43 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
| 44 | * \param c Character to draw the box outline with. |
---|
| 45 | * \return void |
---|
| 46 | */ |
---|
[185] | 47 | void caca_draw_box(int x1, int y1, int x2, int y2, char c) |
---|
[151] | 48 | { |
---|
[185] | 49 | caca_draw_line(x1, y1, x1, y2, c); |
---|
| 50 | caca_draw_line(x1, y2, x2, y2, c); |
---|
| 51 | caca_draw_line(x2, y2, x2, y1, c); |
---|
| 52 | caca_draw_line(x2, y1, x1, y1, c); |
---|
[151] | 53 | } |
---|
| 54 | |
---|
[257] | 55 | /** |
---|
| 56 | * \brief Draw a thin box on the screen. |
---|
| 57 | * |
---|
| 58 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
| 59 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
| 60 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
| 61 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
| 62 | * \return void |
---|
| 63 | */ |
---|
[185] | 64 | void caca_draw_thin_box(int x1, int y1, int x2, int y2) |
---|
[151] | 65 | { |
---|
| 66 | int x, y, xmax, ymax; |
---|
| 67 | |
---|
| 68 | if(x1 > x2) |
---|
| 69 | { |
---|
| 70 | int tmp = x1; |
---|
| 71 | x1 = x2; x2 = tmp; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | if(y1 > y2) |
---|
| 75 | { |
---|
| 76 | int tmp = y1; |
---|
| 77 | y1 = y2; y2 = tmp; |
---|
| 78 | } |
---|
| 79 | |
---|
[246] | 80 | xmax = _caca_width - 1; |
---|
| 81 | ymax = _caca_height - 1; |
---|
[151] | 82 | |
---|
| 83 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
| 84 | return; |
---|
| 85 | |
---|
| 86 | /* Draw edges */ |
---|
| 87 | if(y1 >= 0) |
---|
| 88 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
[185] | 89 | caca_putchar(x, y1, '-'); |
---|
[151] | 90 | |
---|
[153] | 91 | if(y2 <= ymax) |
---|
[151] | 92 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
[185] | 93 | caca_putchar(x, y2, '-'); |
---|
[151] | 94 | |
---|
| 95 | if(x1 >= 0) |
---|
| 96 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
[185] | 97 | caca_putchar(x1, y, '|'); |
---|
[151] | 98 | |
---|
[153] | 99 | if(x2 <= xmax) |
---|
[151] | 100 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
[185] | 101 | caca_putchar(x2, y, '|'); |
---|
[151] | 102 | |
---|
| 103 | /* Draw corners */ |
---|
| 104 | if(x1 >= 0 && y1 >= 0) |
---|
[185] | 105 | caca_putchar(x1, y1, ','); |
---|
[151] | 106 | |
---|
| 107 | if(x1 >= 0 && y2 <= ymax) |
---|
[185] | 108 | caca_putchar(x1, y2, '`'); |
---|
[151] | 109 | |
---|
| 110 | if(x2 <= xmax && y1 >= 0) |
---|
[185] | 111 | caca_putchar(x2, y1, '.'); |
---|
[151] | 112 | |
---|
| 113 | if(x2 <= xmax && y2 <= ymax) |
---|
[185] | 114 | caca_putchar(x2, y2, '\''); |
---|
[151] | 115 | } |
---|
| 116 | |
---|
[257] | 117 | /** |
---|
| 118 | * \brief Fill a box on the screen using the given character. |
---|
| 119 | * |
---|
| 120 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
| 121 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
| 122 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
| 123 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
| 124 | * \param c Character to fill the box with. |
---|
| 125 | * \return void |
---|
| 126 | */ |
---|
[185] | 127 | void caca_fill_box(int x1, int y1, int x2, int y2, char c) |
---|
[151] | 128 | { |
---|
| 129 | int x, y, xmax, ymax; |
---|
| 130 | |
---|
| 131 | if(x1 > x2) |
---|
| 132 | { |
---|
| 133 | int tmp = x1; |
---|
| 134 | x1 = x2; x2 = tmp; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | if(y1 > y2) |
---|
| 138 | { |
---|
| 139 | int tmp = y1; |
---|
| 140 | y1 = y2; y2 = tmp; |
---|
| 141 | } |
---|
| 142 | |
---|
[246] | 143 | xmax = _caca_width - 1; |
---|
| 144 | ymax = _caca_height - 1; |
---|
[151] | 145 | |
---|
| 146 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
| 147 | return; |
---|
| 148 | |
---|
| 149 | if(x1 < 0) x1 = 0; |
---|
| 150 | if(y1 < 0) y1 = 0; |
---|
| 151 | if(x2 > xmax) x2 = xmax; |
---|
| 152 | if(y2 > ymax) y2 = ymax; |
---|
| 153 | |
---|
| 154 | for(y = y1; y <= y2; y++) |
---|
| 155 | for(x = x1; x <= x2; x++) |
---|
[185] | 156 | caca_putchar(x, y, c); |
---|
[151] | 157 | } |
---|
| 158 | |
---|