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

Last change on this file since 116 was 116, checked in by Sam Hocevar, 16 years ago
  • libee/sprite.c: + Beginning of sprite handling within the library. + Added support to load files.
  • Property svn:keywords set to Id
File size: 3.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 116 2003-11-10 01:56:24Z 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
28#include "ee.h"
29
30struct ee_frame
31{
32    int w, h;
33    int dx, dy;
34    char *chars;
35    int *color;
36};
37
38struct ee_sprite
39{
40    int f;
41    int nf;
42    struct ee_frame *frames;
43};
44
45struct ee_sprite *ee_load_sprite(const char *file)
46{
47    char buf[BUFSIZ];
48    struct ee_sprite *sprite;
49    FILE *fd;
50
51    fd = fopen(file, "r");
52    if(fd == NULL)
53        return NULL;
54
55    sprite = malloc(sizeof(struct ee_sprite));
56    sprite->f = 0;
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; x++)
94                frame->chars[w * y + x] = buf[x];
95        }
96
97        for(y = 0; y < h; y++)
98        {
99            if(!fgets(buf, BUFSIZ, fd))
100                goto failed;
101
102            for(x = 0; x < w; x++)
103                frame->color[w * y + x] = buf[x] - 'a';
104        }
105
106        continue;
107
108    failed:
109        free(sprite->frames[sprite->nf - 1].chars);
110        free(sprite->frames[sprite->nf - 1].color);
111        sprite->nf--;
112        break;
113    }
114
115    fclose(fd);
116
117    if(sprite->nf == 0)
118    {
119        free(sprite);
120        return NULL;
121    }
122
123    return sprite;
124}
125
126void ee_draw_sprite(int x, int y, struct ee_sprite *sprite)
127{
128    int i, j;
129    struct ee_frame *frame = &sprite->frames[sprite->f];
130
131    for(j = 0; j < frame->h; j++)
132    {
133        for(i = 0; i < frame->w; i++)
134        {
135            int col = frame->color[frame->w * j + i];
136            if(col >= 0)
137            {
138                ee_goto(x + i - frame->dx, y + j - frame->dy);
139                ee_color(col);
140                ee_putchar(frame->chars[frame->w * j + i]);
141            }
142        }
143    }
144}
145
146void ee_free_sprite(struct ee_sprite *sprite)
147{
148    free(sprite);
149}
150
Note: See TracBrowser for help on using the repository browser.