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 246 2003-12-11 16:31:49Z 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 | |
---|
30 | #include "config.h" |
---|
31 | |
---|
32 | #include <stdlib.h> |
---|
33 | |
---|
34 | #include "caca.h" |
---|
35 | #include "caca_internals.h" |
---|
36 | |
---|
37 | void caca_draw_box(int x1, int y1, int x2, int y2, char c) |
---|
38 | { |
---|
39 | caca_draw_line(x1, y1, x1, y2, c); |
---|
40 | caca_draw_line(x1, y2, x2, y2, c); |
---|
41 | caca_draw_line(x2, y2, x2, y1, c); |
---|
42 | caca_draw_line(x2, y1, x1, y1, c); |
---|
43 | } |
---|
44 | |
---|
45 | void caca_draw_thin_box(int x1, int y1, int x2, int y2) |
---|
46 | { |
---|
47 | int x, y, xmax, ymax; |
---|
48 | |
---|
49 | if(x1 > x2) |
---|
50 | { |
---|
51 | int tmp = x1; |
---|
52 | x1 = x2; x2 = tmp; |
---|
53 | } |
---|
54 | |
---|
55 | if(y1 > y2) |
---|
56 | { |
---|
57 | int tmp = y1; |
---|
58 | y1 = y2; y2 = tmp; |
---|
59 | } |
---|
60 | |
---|
61 | xmax = _caca_width - 1; |
---|
62 | ymax = _caca_height - 1; |
---|
63 | |
---|
64 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
65 | return; |
---|
66 | |
---|
67 | /* Draw edges */ |
---|
68 | if(y1 >= 0) |
---|
69 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
70 | caca_putchar(x, y1, '-'); |
---|
71 | |
---|
72 | if(y2 <= ymax) |
---|
73 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
74 | caca_putchar(x, y2, '-'); |
---|
75 | |
---|
76 | if(x1 >= 0) |
---|
77 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
78 | caca_putchar(x1, y, '|'); |
---|
79 | |
---|
80 | if(x2 <= xmax) |
---|
81 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
82 | caca_putchar(x2, y, '|'); |
---|
83 | |
---|
84 | /* Draw corners */ |
---|
85 | if(x1 >= 0 && y1 >= 0) |
---|
86 | caca_putchar(x1, y1, ','); |
---|
87 | |
---|
88 | if(x1 >= 0 && y2 <= ymax) |
---|
89 | caca_putchar(x1, y2, '`'); |
---|
90 | |
---|
91 | if(x2 <= xmax && y1 >= 0) |
---|
92 | caca_putchar(x2, y1, '.'); |
---|
93 | |
---|
94 | if(x2 <= xmax && y2 <= ymax) |
---|
95 | caca_putchar(x2, y2, '\''); |
---|
96 | } |
---|
97 | |
---|
98 | void caca_fill_box(int x1, int y1, int x2, int y2, char c) |
---|
99 | { |
---|
100 | int x, y, xmax, ymax; |
---|
101 | |
---|
102 | if(x1 > x2) |
---|
103 | { |
---|
104 | int tmp = x1; |
---|
105 | x1 = x2; x2 = tmp; |
---|
106 | } |
---|
107 | |
---|
108 | if(y1 > y2) |
---|
109 | { |
---|
110 | int tmp = y1; |
---|
111 | y1 = y2; y2 = tmp; |
---|
112 | } |
---|
113 | |
---|
114 | xmax = _caca_width - 1; |
---|
115 | ymax = _caca_height - 1; |
---|
116 | |
---|
117 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
118 | return; |
---|
119 | |
---|
120 | if(x1 < 0) x1 = 0; |
---|
121 | if(y1 < 0) y1 = 0; |
---|
122 | if(x2 > xmax) x2 = xmax; |
---|
123 | if(y2 > ymax) y2 = ymax; |
---|
124 | |
---|
125 | for(y = y1; y <= y2; y++) |
---|
126 | for(x = x1; x <= x2; x++) |
---|
127 | caca_putchar(x, y, c); |
---|
128 | } |
---|
129 | |
---|