source: ttyvaders/trunk/libee/sprite.c @ 154

Last change on this file since 154 was 154, checked in by Sam Hocevar, 16 years ago
  • libee/graphics.c: + Renamed ee_color() to ee_set_color(), wrote ee_get_color().
  • libee/line.c: + Implemented draw_polyline() and draw_thin_polyline().
  • libee/sprite.c: + Removed the f member of struct ee_sprite. + Implemented ee_get_sprite_{width|height|dx|dy}(). + Restore the color fater ee_draw_sprite() is called.
  • libee/box.c: + Fixed a bug causing improper box clipping at the right and the bottom.
  • data/foo_fighter: + Fixed bugs in the sprite.
  • src/intro.c: + Test effects for the future game's intro.
  • test/spritedit.c: + Added stuff to the sprite editor. We can now navigate through frames.
  • Property svn:keywords set to Id
File size: 5.0 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 154 2003-11-12 01:48:58Z 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#ifdef USE_SLANG
26#   include <slang.h>
27#elif USE_NCURSES
28#   include <curses.h>
29#endif
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34
35#include "ee.h"
36
37struct ee_frame
38{
39    int w, h;
40    int dx, dy;
41    char *chars;
42    int *color;
43};
44
45struct ee_sprite
46{
47    int nf;
48    struct ee_frame *frames;
49};
50
51struct ee_sprite *ee_load_sprite(const char *file)
52{
53    char buf[BUFSIZ];
54    struct ee_sprite *sprite;
55    FILE *fd;
56
57    fd = fopen(file, "r");
58    if(fd == NULL)
59        return NULL;
60
61    sprite = malloc(sizeof(struct ee_sprite));
62    sprite->nf = 0;
63    sprite->frames = NULL;
64
65    while(!feof(fd))
66    {
67        int x, y;
68        int w = 0, h = 0, dx = 0, dy = 0;
69        struct ee_frame *frame;
70
71        /* Get width and height */
72        if(!fgets(buf, BUFSIZ, fd))
73            break;
74
75        sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
76        if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
77            break;
78
79        if(sprite->nf++)
80            sprite->frames = realloc(sprite->frames,
81                                     sprite->nf * sizeof(struct ee_frame));
82        else
83            sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
84        frame = &sprite->frames[sprite->nf - 1];
85
86        frame->w = w;
87        frame->h = h;
88        frame->dx = dx;
89        frame->dy = dy;
90        frame->chars = malloc(w * h * sizeof(char));
91        frame->color = malloc(w * h * sizeof(int));
92
93        for(y = 0; y < h; y++)
94        {
95            if(!fgets(buf, BUFSIZ, fd))
96                goto failed;
97
98            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
99                frame->chars[w * y + x] = buf[x];
100
101            for(; x < w; x++)
102                frame->chars[w * y + x] = ' ';
103        }
104
105        for(y = 0; y < h; y++)
106        {
107            if(!fgets(buf, BUFSIZ, fd))
108                goto failed;
109
110            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
111                frame->color[w * y + x] = buf[x] - 'a';
112
113            for(; x < w; x++)
114                frame->color[w * y + x] = ' ' - 'a';
115        }
116
117        continue;
118
119    failed:
120        free(sprite->frames[sprite->nf - 1].chars);
121        free(sprite->frames[sprite->nf - 1].color);
122        sprite->nf--;
123        break;
124    }
125
126    fclose(fd);
127
128    if(sprite->nf == 0)
129    {
130        free(sprite);
131        return NULL;
132    }
133
134    return sprite;
135}
136
137int ee_get_sprite_frames(struct ee_sprite *sprite)
138{
139    if(sprite == NULL)
140        return 0;
141
142    return sprite->nf;
143}
144
145int ee_get_sprite_width(struct ee_sprite *sprite, int f)
146{
147    if(sprite == NULL)
148        return 0;
149
150    if(f < 0 || f >= sprite->nf)
151        return 0;
152
153    return sprite->frames[f].w;
154}
155
156int ee_get_sprite_height(struct ee_sprite *sprite, int f)
157{
158    if(sprite == NULL)
159        return 0;
160
161    if(f < 0 || f >= sprite->nf)
162        return 0;
163
164    return sprite->frames[f].h;
165}
166
167int ee_get_sprite_dx(struct ee_sprite *sprite, int f)
168{
169    if(sprite == NULL)
170        return 0;
171
172    if(f < 0 || f >= sprite->nf)
173        return 0;
174
175    return sprite->frames[f].dx;
176}
177
178int ee_get_sprite_dy(struct ee_sprite *sprite, int f)
179{
180    if(sprite == NULL)
181        return 0;
182
183    if(f < 0 || f >= sprite->nf)
184        return 0;
185
186    return sprite->frames[f].dy;
187}
188
189void ee_draw_sprite(int x, int y, struct ee_sprite *sprite, int f)
190{
191    int i, j, oldcol;
192    struct ee_frame *frame;
193
194    if(sprite == NULL)
195        return;
196
197    if(f < 0 || f >= sprite->nf)
198        return;
199
200    frame = &sprite->frames[f];
201
202    oldcol = ee_get_color();
203
204    for(j = 0; j < frame->h; j++)
205    {
206        for(i = 0; i < frame->w; i++)
207        {
208            int col = frame->color[frame->w * j + i];
209            if(col >= 0)
210            {
211                ee_set_color(col);
212                ee_putchar(x + i - frame->dx, y + j - frame->dy,
213                           frame->chars[frame->w * j + i]);
214            }
215        }
216    }
217
218    ee_set_color(oldcol);
219}
220
221void ee_free_sprite(struct ee_sprite *sprite)
222{
223    int i;
224
225    if(sprite == NULL)
226        return;
227
228    for(i = sprite->nf; i--;)
229    {
230        struct ee_frame *frame = &sprite->frames[i];
231        free(frame->chars);
232        free(frame->color);
233    }
234
235    free(sprite->frames);
236    free(sprite);
237}
238
Note: See TracBrowser for help on using the repository browser.