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 sprite.c |
---|
23 | * \version \$Id: sprite.c 268 2003-12-23 13:27:40Z sam $ |
---|
24 | * \author Sam Hocevar <sam@zoy.org> |
---|
25 | * \brief Sprite loading and blitting |
---|
26 | * |
---|
27 | * This file contains a small framework for sprite loading and blitting. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "config.h" |
---|
31 | |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <string.h> |
---|
35 | |
---|
36 | #include "caca.h" |
---|
37 | #include "caca_internals.h" |
---|
38 | |
---|
39 | struct caca_frame |
---|
40 | { |
---|
41 | int w, h; |
---|
42 | int dx, dy; |
---|
43 | char *chars; |
---|
44 | int *color; |
---|
45 | }; |
---|
46 | |
---|
47 | struct caca_sprite |
---|
48 | { |
---|
49 | int nf; |
---|
50 | struct caca_frame *frames; |
---|
51 | }; |
---|
52 | |
---|
53 | /** |
---|
54 | * \brief Allocate a sprite loaded from a file. |
---|
55 | * |
---|
56 | * \param file The filename. |
---|
57 | * \return The sprite, or NULL if an error occured. |
---|
58 | */ |
---|
59 | struct caca_sprite *caca_load_sprite(const char *file) |
---|
60 | { |
---|
61 | char buf[BUFSIZ]; |
---|
62 | struct caca_sprite *sprite; |
---|
63 | FILE *fd; |
---|
64 | |
---|
65 | fd = fopen(file, "r"); |
---|
66 | if(fd == NULL) |
---|
67 | return NULL; |
---|
68 | |
---|
69 | sprite = malloc(sizeof(struct caca_sprite)); |
---|
70 | if(sprite == NULL) |
---|
71 | goto sprite_alloc_failed; |
---|
72 | |
---|
73 | sprite->nf = 0; |
---|
74 | sprite->frames = NULL; |
---|
75 | |
---|
76 | while(!feof(fd)) |
---|
77 | { |
---|
78 | int x, y; |
---|
79 | int w = 0, h = 0, dx = 0, dy = 0; |
---|
80 | struct caca_frame *frame; |
---|
81 | |
---|
82 | /* Get width and height */ |
---|
83 | if(!fgets(buf, BUFSIZ, fd)) |
---|
84 | break; |
---|
85 | |
---|
86 | sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy); |
---|
87 | if(w <= 0 || h <= 0 || w > BUFSIZ / 2) |
---|
88 | break; |
---|
89 | |
---|
90 | if(sprite->nf) |
---|
91 | { |
---|
92 | void *tmp = realloc(sprite->frames, |
---|
93 | (sprite->nf + 1) * sizeof(struct caca_frame)); |
---|
94 | if(tmp == NULL) |
---|
95 | goto frame_failed; |
---|
96 | sprite->frames = tmp; |
---|
97 | sprite->nf++; |
---|
98 | } |
---|
99 | else |
---|
100 | { |
---|
101 | sprite->frames = malloc((sprite->nf + 1) * sizeof(struct caca_frame)); |
---|
102 | if(sprite->frames == NULL) |
---|
103 | goto sprite_failed; |
---|
104 | sprite->nf++; |
---|
105 | } |
---|
106 | |
---|
107 | frame = &sprite->frames[sprite->nf - 1]; |
---|
108 | |
---|
109 | frame->w = w; |
---|
110 | frame->h = h; |
---|
111 | frame->dx = dx; |
---|
112 | frame->dy = dy; |
---|
113 | frame->chars = malloc(w * h * sizeof(char)); |
---|
114 | if(frame->chars == NULL) |
---|
115 | { |
---|
116 | sprite->nf--; |
---|
117 | goto frame_failed; |
---|
118 | } |
---|
119 | frame->color = malloc(w * h * sizeof(int)); |
---|
120 | if(frame->color == NULL) |
---|
121 | { |
---|
122 | free(frame->chars); |
---|
123 | sprite->nf--; |
---|
124 | goto frame_failed; |
---|
125 | } |
---|
126 | |
---|
127 | for(y = 0; y < h; y++) |
---|
128 | { |
---|
129 | if(!fgets(buf, BUFSIZ, fd)) |
---|
130 | goto frame_failed; |
---|
131 | |
---|
132 | for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++) |
---|
133 | frame->chars[w * y + x] = buf[x]; |
---|
134 | |
---|
135 | for(; x < w; x++) |
---|
136 | frame->chars[w * y + x] = ' '; |
---|
137 | } |
---|
138 | |
---|
139 | for(y = 0; y < h; y++) |
---|
140 | { |
---|
141 | if(!fgets(buf, BUFSIZ, fd)) |
---|
142 | goto frame_failed; |
---|
143 | |
---|
144 | for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++) |
---|
145 | frame->color[w * y + x] = buf[x] - 'a'; |
---|
146 | |
---|
147 | for(; x < w; x++) |
---|
148 | frame->color[w * y + x] = ' ' - 'a'; |
---|
149 | } |
---|
150 | |
---|
151 | continue; |
---|
152 | } |
---|
153 | |
---|
154 | if(sprite->nf == 0) |
---|
155 | goto sprite_failed; |
---|
156 | |
---|
157 | fclose(fd); |
---|
158 | return sprite; |
---|
159 | |
---|
160 | frame_failed: |
---|
161 | while(sprite->nf) |
---|
162 | { |
---|
163 | free(sprite->frames[sprite->nf - 1].color); |
---|
164 | free(sprite->frames[sprite->nf - 1].chars); |
---|
165 | sprite->nf--; |
---|
166 | } |
---|
167 | sprite_failed: |
---|
168 | free(sprite); |
---|
169 | sprite_alloc_failed: |
---|
170 | fclose(fd); |
---|
171 | return NULL; |
---|
172 | } |
---|
173 | |
---|
174 | /** |
---|
175 | * \brief Return the number of frames in a sprite. |
---|
176 | * |
---|
177 | * \param sprite The sprite. |
---|
178 | * \return The number of frames. |
---|
179 | */ |
---|
180 | int caca_get_sprite_frames(const struct caca_sprite *sprite) |
---|
181 | { |
---|
182 | if(sprite == NULL) |
---|
183 | return 0; |
---|
184 | |
---|
185 | return sprite->nf; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * \brief Return the width of a sprite. |
---|
190 | * |
---|
191 | * \param sprite The sprite. |
---|
192 | * \param f The frame index. |
---|
193 | * \return The width of the given frame of the sprite. |
---|
194 | */ |
---|
195 | int caca_get_sprite_width(const struct caca_sprite *sprite, int f) |
---|
196 | { |
---|
197 | if(sprite == NULL) |
---|
198 | return 0; |
---|
199 | |
---|
200 | if(f < 0 || f >= sprite->nf) |
---|
201 | return 0; |
---|
202 | |
---|
203 | return sprite->frames[f].w; |
---|
204 | } |
---|
205 | |
---|
206 | /** |
---|
207 | * \brief Return the height of a sprite. |
---|
208 | * |
---|
209 | * \param sprite The sprite. |
---|
210 | * \param f The frame index. |
---|
211 | * \return The height of the given frame of the sprite. |
---|
212 | */ |
---|
213 | int caca_get_sprite_height(const struct caca_sprite *sprite, int f) |
---|
214 | { |
---|
215 | if(sprite == NULL) |
---|
216 | return 0; |
---|
217 | |
---|
218 | if(f < 0 || f >= sprite->nf) |
---|
219 | return 0; |
---|
220 | |
---|
221 | return sprite->frames[f].h; |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * \brief Return the X coordinate of a sprite's handle. |
---|
226 | * |
---|
227 | * \param sprite The sprite. |
---|
228 | * \param f The frame index. |
---|
229 | * \return The X coordinate of the given frame's handle. |
---|
230 | */ |
---|
231 | int caca_get_sprite_dx(const struct caca_sprite *sprite, int f) |
---|
232 | { |
---|
233 | if(sprite == NULL) |
---|
234 | return 0; |
---|
235 | |
---|
236 | if(f < 0 || f >= sprite->nf) |
---|
237 | return 0; |
---|
238 | |
---|
239 | return sprite->frames[f].dx; |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * \brief Return the Y coordinate of a sprite's handle. |
---|
244 | * |
---|
245 | * \param sprite The sprite. |
---|
246 | * \param f The frame index. |
---|
247 | * \return The Y coordinate of the given frame's handle. |
---|
248 | */ |
---|
249 | int caca_get_sprite_dy(const struct caca_sprite *sprite, int f) |
---|
250 | { |
---|
251 | if(sprite == NULL) |
---|
252 | return 0; |
---|
253 | |
---|
254 | if(f < 0 || f >= sprite->nf) |
---|
255 | return 0; |
---|
256 | |
---|
257 | return sprite->frames[f].dy; |
---|
258 | } |
---|
259 | |
---|
260 | /** |
---|
261 | * \brief Draw a sprite's specific frame at the given coordinates. If the |
---|
262 | * frame does not exist, nothing is displayed. |
---|
263 | * |
---|
264 | * \param x The X coordinate. |
---|
265 | * \param y The Y coordinate. |
---|
266 | * \param sprite The sprite. |
---|
267 | * \param f The frame index. |
---|
268 | * \return void |
---|
269 | */ |
---|
270 | void caca_draw_sprite(int x, int y, const struct caca_sprite *sprite, int f) |
---|
271 | { |
---|
272 | int i, j; |
---|
273 | enum caca_color oldfg, oldbg; |
---|
274 | struct caca_frame *frame; |
---|
275 | |
---|
276 | if(sprite == NULL) |
---|
277 | return; |
---|
278 | |
---|
279 | if(f < 0 || f >= sprite->nf) |
---|
280 | return; |
---|
281 | |
---|
282 | frame = &sprite->frames[f]; |
---|
283 | |
---|
284 | oldfg = caca_get_fg_color(); |
---|
285 | oldbg = caca_get_bg_color(); |
---|
286 | |
---|
287 | for(j = 0; j < frame->h; j++) |
---|
288 | { |
---|
289 | for(i = 0; i < frame->w; i++) |
---|
290 | { |
---|
291 | int col = frame->color[frame->w * j + i]; |
---|
292 | if(col >= 0) |
---|
293 | { |
---|
294 | caca_set_color(col, CACA_COLOR_BLACK); |
---|
295 | caca_putchar(x + i - frame->dx, y + j - frame->dy, |
---|
296 | frame->chars[frame->w * j + i]); |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | caca_set_color(oldfg, oldbg); |
---|
302 | } |
---|
303 | |
---|
304 | /** |
---|
305 | * \brief Free the memory associated with a sprite. |
---|
306 | * |
---|
307 | * \param sprite The sprite to be freed. |
---|
308 | * \return void |
---|
309 | */ |
---|
310 | void caca_free_sprite(struct caca_sprite *sprite) |
---|
311 | { |
---|
312 | int i; |
---|
313 | |
---|
314 | if(sprite == NULL) |
---|
315 | return; |
---|
316 | |
---|
317 | for(i = sprite->nf; i--;) |
---|
318 | { |
---|
319 | struct caca_frame *frame = &sprite->frames[i]; |
---|
320 | free(frame->chars); |
---|
321 | free(frame->color); |
---|
322 | } |
---|
323 | |
---|
324 | free(sprite->frames); |
---|
325 | free(sprite); |
---|
326 | } |
---|
327 | |
---|