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

Last change on this file since 159 was 159, checked in by Sam Hocevar, 20 years ago
  • libee/ee.h: + Added ee_color_names[] containing our 16 color names.
  • libee/ee.c: + Extended our color set to the full 16 instead of 10. + Precalculate ncurses attributes.
  • libee/graphics.c: + Clip color value in ee_set_color(). + Clip characters in ee_putchar(). + Partially clip characters in ee_putstr(), overflows aren't checked yet.
  • libee/ee_internals.h: + New file to share extern variables within libee.
  • test/demo.c: + Added a simple demo_color() to output all colors. + Replaced four ee_draw_line() with ee_draw_thin_box(). + Replaced x1, y1, x2 etc. with xa, ya, xb etc. because <math.h> already

defines y1.

+ Randomized colours from 0 to 15 instead of 1 to 10.

  • src/Makefiles.am: + Added -lm to the ttyvaders linking flags because of the intro.
  • README BUGS TODO: + Updated.
  • Property svn:keywords set to Id
File size: 4.9 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 159 2003-11-12 21:18: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#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28
29#include "ee.h"
30#include "ee_internals.h"
31
32struct ee_frame
33{
34    int w, h;
35    int dx, dy;
36    char *chars;
37    int *color;
38};
39
40struct ee_sprite
41{
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->nf = 0;
58    sprite->frames = NULL;
59
60    while(!feof(fd))
61    {
62        int x, y;
63        int w = 0, h = 0, dx = 0, dy = 0;
64        struct ee_frame *frame;
65
66        /* Get width and height */
67        if(!fgets(buf, BUFSIZ, fd))
68            break;
69
70        sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
71        if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
72            break;
73
74        if(sprite->nf++)
75            sprite->frames = realloc(sprite->frames,
76                                     sprite->nf * sizeof(struct ee_frame));
77        else
78            sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
79        frame = &sprite->frames[sprite->nf - 1];
80
81        frame->w = w;
82        frame->h = h;
83        frame->dx = dx;
84        frame->dy = dy;
85        frame->chars = malloc(w * h * sizeof(char));
86        frame->color = malloc(w * h * sizeof(int));
87
88        for(y = 0; y < h; y++)
89        {
90            if(!fgets(buf, BUFSIZ, fd))
91                goto failed;
92
93            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
94                frame->chars[w * y + x] = buf[x];
95
96            for(; x < w; x++)
97                frame->chars[w * y + x] = ' ';
98        }
99
100        for(y = 0; y < h; y++)
101        {
102            if(!fgets(buf, BUFSIZ, fd))
103                goto failed;
104
105            for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
106                frame->color[w * y + x] = buf[x] - 'a';
107
108            for(; x < w; x++)
109                frame->color[w * y + x] = ' ' - 'a';
110        }
111
112        continue;
113
114    failed:
115        free(sprite->frames[sprite->nf - 1].chars);
116        free(sprite->frames[sprite->nf - 1].color);
117        sprite->nf--;
118        break;
119    }
120
121    fclose(fd);
122
123    if(sprite->nf == 0)
124    {
125        free(sprite);
126        return NULL;
127    }
128
129    return sprite;
130}
131
132int ee_get_sprite_frames(struct ee_sprite *sprite)
133{
134    if(sprite == NULL)
135        return 0;
136
137    return sprite->nf;
138}
139
140int ee_get_sprite_width(struct ee_sprite *sprite, int f)
141{
142    if(sprite == NULL)
143        return 0;
144
145    if(f < 0 || f >= sprite->nf)
146        return 0;
147
148    return sprite->frames[f].w;
149}
150
151int ee_get_sprite_height(struct ee_sprite *sprite, int f)
152{
153    if(sprite == NULL)
154        return 0;
155
156    if(f < 0 || f >= sprite->nf)
157        return 0;
158
159    return sprite->frames[f].h;
160}
161
162int ee_get_sprite_dx(struct ee_sprite *sprite, int f)
163{
164    if(sprite == NULL)
165        return 0;
166
167    if(f < 0 || f >= sprite->nf)
168        return 0;
169
170    return sprite->frames[f].dx;
171}
172
173int ee_get_sprite_dy(struct ee_sprite *sprite, int f)
174{
175    if(sprite == NULL)
176        return 0;
177
178    if(f < 0 || f >= sprite->nf)
179        return 0;
180
181    return sprite->frames[f].dy;
182}
183
184void ee_draw_sprite(int x, int y, struct ee_sprite *sprite, int f)
185{
186    int i, j, oldcol;
187    struct ee_frame *frame;
188
189    if(sprite == NULL)
190        return;
191
192    if(f < 0 || f >= sprite->nf)
193        return;
194
195    frame = &sprite->frames[f];
196
197    oldcol = ee_get_color();
198
199    for(j = 0; j < frame->h; j++)
200    {
201        for(i = 0; i < frame->w; i++)
202        {
203            int col = frame->color[frame->w * j + i];
204            if(col >= 0)
205            {
206                ee_set_color(col);
207                ee_putchar(x + i - frame->dx, y + j - frame->dy,
208                           frame->chars[frame->w * j + i]);
209            }
210        }
211    }
212
213    ee_set_color(oldcol);
214}
215
216void ee_free_sprite(struct ee_sprite *sprite)
217{
218    int i;
219
220    if(sprite == NULL)
221        return;
222
223    for(i = sprite->nf; i--;)
224    {
225        struct ee_frame *frame = &sprite->frames[i];
226        free(frame->chars);
227        free(frame->color);
228    }
229
230    free(sprite->frames);
231    free(sprite);
232}
233
Note: See TracBrowser for help on using the repository browser.