source: libcaca/trunk/libee/sprite.c @ 139

Last change on this file since 139 was 139, checked in by Sam Hocevar, 17 years ago
  • libee/sprite.c: + Made sprite loading more robust.
  • src/explosions.c: + No longer use ee_putchar etc. to draw our sprites, but use

ee_draw_sprite().

  • data/: + Drew explosion_medium and explosion_small sprites.
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 *   libee         ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: sprite.c 139 2003-11-10 21:17:47Z sam $
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include "config.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "ee.h"
30
31struct ee_frame
32{
33    int w, h;
34    int dx, dy;
35    char *chars;
36    int *color;
37};
38
39struct ee_sprite
40{
41    int f;
42    int nf;
43    struct ee_frame *frames;
44};
45
46struct ee_sprite *ee_load_sprite(const char *file)
47{
48    char buf[BUFSIZ];
49    struct ee_sprite *sprite;
50    FILE *fd;
51
52    fd = fopen(file, "r");
53    if(fd == NULL)
54        return NULL;
55
56    sprite = malloc(sizeof(struct ee_sprite));
57    sprite->f = 0;
58    sprite->nf = 0;
59    sprite->frames = NULL;
60
61    while(!feof(fd))
62    {
63        int x, y;
64        int w = 0, h = 0, dx = 0, dy = 0;
65        struct ee_frame *frame;
66
67        /* Get width and height */
68        if(!fgets(buf, BUFSIZ, fd))
69            break;
70
71        sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
72        if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
73            break;
74
75        if(sprite->nf++)
76            sprite->frames = realloc(sprite->frames,
77                                     sprite->nf * sizeof(struct ee_frame));
78        else
79            sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
80        frame = &sprite->frames[sprite->nf - 1];
81
82        frame->w = w;
83        frame->h = h;
84        frame->dx = dx;
85        frame->dy = dy;
86        frame->chars = malloc(w * h * sizeof(char));
87        frame->color = malloc(w * h * sizeof(int));
88
89        for(y = 0; y < h; y++)
90        {
91            if(!fgets(buf, BUFSIZ, fd))
92                goto failed;
93
94            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
95                frame->chars[w * y + x] = buf[x];
96
97            for(; x < w; x++)
98                frame->chars[w * y + x] = ' ';
99        }
100
101        for(y = 0; y < h; y++)
102        {
103            if(!fgets(buf, BUFSIZ, fd))
104                goto failed;
105
106            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
107                frame->color[w * y + x] = buf[x] - 'a';
108
109            for(; x < w; x++)
110                frame->color[w * y + x] = ' ' - 'a';
111        }
112
113        continue;
114
115    failed:
116        free(sprite->frames[sprite->nf - 1].chars);
117        free(sprite->frames[sprite->nf - 1].color);
118        sprite->nf--;
119        break;
120    }
121
122    fclose(fd);
123
124    if(sprite->nf == 0)
125    {
126        free(sprite);
127        return NULL;
128    }
129
130    return sprite;
131}
132
133void ee_set_sprite_frame(struct ee_sprite *sprite, int f)
134{
135    if(sprite == NULL)
136        return;
137
138    if(f < 0 || f >= sprite->nf)
139        return;
140
141    sprite->f = f;
142}
143
144int ee_get_sprite_frame(struct ee_sprite *sprite)
145{
146    if(sprite == NULL)
147        return -1;
148
149    return sprite->f;
150}
151
152void ee_draw_sprite(int x, int y, struct ee_sprite *sprite)
153{
154    int i, j;
155    struct ee_frame *frame;
156
157    if(sprite == NULL)
158        return;
159
160    frame = &sprite->frames[sprite->f];
161
162    for(j = 0; j < frame->h; j++)
163    {
164        for(i = 0; i < frame->w; i++)
165        {
166            int col = frame->color[frame->w * j + i];
167            if(col >= 0)
168            {
169                ee_goto(x + i - frame->dx, y + j - frame->dy);
170                ee_color(col);
171                ee_putchar(frame->chars[frame->w * j + i]);
172            }
173        }
174    }
175}
176
177void ee_free_sprite(struct ee_sprite *sprite)
178{
179    int i;
180
181    if(sprite == NULL)
182        return;
183
184    for(i = sprite->nf; i--;)
185    {
186        struct ee_frame *frame = &sprite->frames[i];
187        free(frame->chars);
188        free(frame->color);
189    }
190
191    free(sprite->frames);
192    free(sprite);
193}
194
Note: See TracBrowser for help on using the repository browser.