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

Last change on this file since 147 was 147, checked in by Sam Hocevar, 18 years ago
  • libee/graphics.c: + Moved ee_putstr() and ee_putchar() in here.
  • libee/ee.h: + Got rid of ee_goto(). + Moved <slang.h> or <curses.h> into libee.
  • Replaced ee_goto()/ee_putstr() pairs with ee_putstr().
  • Ditto for ee_putchar().
  • Property svn:keywords set to Id
File size: 4.4 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 147 2003-11-10 23:38:50Z 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 f;
48    int nf;
49    struct ee_frame *frames;
50};
51
52struct ee_sprite *ee_load_sprite(const char *file)
53{
54    char buf[BUFSIZ];
55    struct ee_sprite *sprite;
56    FILE *fd;
57
58    fd = fopen(file, "r");
59    if(fd == NULL)
60        return NULL;
61
62    sprite = malloc(sizeof(struct ee_sprite));
63    sprite->f = 0;
64    sprite->nf = 0;
65    sprite->frames = NULL;
66
67    while(!feof(fd))
68    {
69        int x, y;
70        int w = 0, h = 0, dx = 0, dy = 0;
71        struct ee_frame *frame;
72
73        /* Get width and height */
74        if(!fgets(buf, BUFSIZ, fd))
75            break;
76
77        sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
78        if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
79            break;
80
81        if(sprite->nf++)
82            sprite->frames = realloc(sprite->frames,
83                                     sprite->nf * sizeof(struct ee_frame));
84        else
85            sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
86        frame = &sprite->frames[sprite->nf - 1];
87
88        frame->w = w;
89        frame->h = h;
90        frame->dx = dx;
91        frame->dy = dy;
92        frame->chars = malloc(w * h * sizeof(char));
93        frame->color = malloc(w * h * sizeof(int));
94
95        for(y = 0; y < h; y++)
96        {
97            if(!fgets(buf, BUFSIZ, fd))
98                goto failed;
99
100            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
101                frame->chars[w * y + x] = buf[x];
102
103            for(; x < w; x++)
104                frame->chars[w * y + x] = ' ';
105        }
106
107        for(y = 0; y < h; y++)
108        {
109            if(!fgets(buf, BUFSIZ, fd))
110                goto failed;
111
112            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
113                frame->color[w * y + x] = buf[x] - 'a';
114
115            for(; x < w; x++)
116                frame->color[w * y + x] = ' ' - 'a';
117        }
118
119        continue;
120
121    failed:
122        free(sprite->frames[sprite->nf - 1].chars);
123        free(sprite->frames[sprite->nf - 1].color);
124        sprite->nf--;
125        break;
126    }
127
128    fclose(fd);
129
130    if(sprite->nf == 0)
131    {
132        free(sprite);
133        return NULL;
134    }
135
136    return sprite;
137}
138
139void ee_set_sprite_frame(struct ee_sprite *sprite, int f)
140{
141    if(sprite == NULL)
142        return;
143
144    if(f < 0 || f >= sprite->nf)
145        return;
146
147    sprite->f = f;
148}
149
150int ee_get_sprite_frame(struct ee_sprite *sprite)
151{
152    if(sprite == NULL)
153        return -1;
154
155    return sprite->f;
156}
157
158void ee_draw_sprite(int x, int y, struct ee_sprite *sprite)
159{
160    int i, j;
161    struct ee_frame *frame;
162
163    if(sprite == NULL)
164        return;
165
166    frame = &sprite->frames[sprite->f];
167
168    for(j = 0; j < frame->h; j++)
169    {
170        for(i = 0; i < frame->w; i++)
171        {
172            int col = frame->color[frame->w * j + i];
173            if(col >= 0)
174            {
175                ee_color(col);
176                ee_putchar(x + i - frame->dx, y + j - frame->dy,
177                           frame->chars[frame->w * j + i]);
178            }
179        }
180    }
181}
182
183void ee_free_sprite(struct ee_sprite *sprite)
184{
185    int i;
186
187    if(sprite == NULL)
188        return;
189
190    for(i = sprite->nf; i--;)
191    {
192        struct ee_frame *frame = &sprite->frames[i];
193        free(frame->chars);
194        free(frame->color);
195    }
196
197    free(sprite->frames);
198    free(sprite);
199}
200
Note: See TracBrowser for help on using the repository browser.