1 | /* |
---|
2 | * libcaca ASCII-Art library |
---|
3 | * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
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. |
---|
10 | * |
---|
11 | * This library 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 GNU |
---|
14 | * Lesser General Public License for more details. |
---|
15 | * |
---|
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 |
---|
20 | */ |
---|
21 | |
---|
22 | /** \file box.c |
---|
23 | * \version \$Id: box.c 298 2003-12-31 14:21:08Z sam $ |
---|
24 | * \author Sam Hocevar <sam@zoy.org> |
---|
25 | * \brief Simple box drawing |
---|
26 | * |
---|
27 | * This file contains box drawing functions, both filled and outline. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "config.h" |
---|
31 | |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "caca.h" |
---|
35 | #include "caca_internals.h" |
---|
36 | |
---|
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 | */ |
---|
47 | void caca_draw_box(int x1, int y1, int x2, int y2, char c) |
---|
48 | { |
---|
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); |
---|
53 | } |
---|
54 | |
---|
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 | */ |
---|
64 | void caca_draw_thin_box(int x1, int y1, int x2, int y2) |
---|
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 | |
---|
80 | xmax = _caca_width - 1; |
---|
81 | ymax = _caca_height - 1; |
---|
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++) |
---|
89 | caca_putchar(x, y1, '-'); |
---|
90 | |
---|
91 | if(y2 <= ymax) |
---|
92 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
93 | caca_putchar(x, y2, '-'); |
---|
94 | |
---|
95 | if(x1 >= 0) |
---|
96 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
97 | caca_putchar(x1, y, '|'); |
---|
98 | |
---|
99 | if(x2 <= xmax) |
---|
100 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
101 | caca_putchar(x2, y, '|'); |
---|
102 | |
---|
103 | /* Draw corners */ |
---|
104 | if(x1 >= 0 && y1 >= 0) |
---|
105 | caca_putchar(x1, y1, ','); |
---|
106 | |
---|
107 | if(x1 >= 0 && y2 <= ymax) |
---|
108 | caca_putchar(x1, y2, '`'); |
---|
109 | |
---|
110 | if(x2 <= xmax && y1 >= 0) |
---|
111 | caca_putchar(x2, y1, '.'); |
---|
112 | |
---|
113 | if(x2 <= xmax && y2 <= ymax) |
---|
114 | caca_putchar(x2, y2, '\''); |
---|
115 | } |
---|
116 | |
---|
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 | */ |
---|
127 | void caca_fill_box(int x1, int y1, int x2, int y2, char c) |
---|
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 | |
---|
143 | xmax = _caca_width - 1; |
---|
144 | ymax = _caca_height - 1; |
---|
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++) |
---|
156 | caca_putchar(x, y, c); |
---|
157 | } |
---|
158 | |
---|