source: libcaca/trunk/cucul/sprite.c @ 1338

Last change on this file since 1338 was 1338, checked in by Sam Hocevar, 13 years ago
  • Prepare the way for per-frame width/height information.
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: sprite.c 1338 2006-11-11 12:57:03Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains a small framework for canvas frame management.
16 */
17
18#include "config.h"
19#include "common.h"
20
21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#   if defined(HAVE_ERRNO_H)
26#       include <errno.h>
27#   endif
28#endif
29
30#include "cucul.h"
31#include "cucul_internals.h"
32
33static void save_frame_info(cucul_canvas_t *);
34static void load_frame_info(cucul_canvas_t *);
35
36/** \brief Get the number of frames in a canvas.
37 *
38 *  Return the current canvas' frame count.
39 *
40 *  This function never fails.
41 *
42 *  \param cv A libcucul canvas
43 *  \return The frame count
44 */
45unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
46{
47    return cv->framecount;
48}
49
50/** \brief Activate a given canvas frame.
51 *
52 *  Set the active canvas frame. All subsequent drawing operations will
53 *  be performed on that frame. The current painting context set by
54 *  cucul_set_attr() is inherited.
55 *
56 *  If the frame index is outside the canvas' frame range, nothing happens.
57 *
58 *  If an error occurs, -1 is returned and \b errno is set accordingly:
59 *  - \c EINVAL Requested frame is out of range.
60 *
61 *  \param cv A libcucul canvas
62 *  \param frame The canvas frame to activate
63 *  \return 0 in case of success, -1 if an error occurred.
64 */
65int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
66{
67    if(frame >= cv->framecount)
68    {
69#if defined(HAVE_ERRNO_H)
70        errno = EINVAL;
71#endif
72        return -1;
73    }
74
75    save_frame_info(cv);
76    cv->frame = frame;
77    load_frame_info(cv);
78
79    return 0;
80}
81
82/** \brief Add a frame to a canvas.
83 *
84 *  Create a new frame within the given canvas. Its contents are copied
85 *  from the currently active frame.
86 *
87 *  The frame index indicates where the frame should be inserted. Valid
88 *  values range from 0 to the current canvas frame count. If the frame
89 *  index is greater than or equals the current canvas frame count, the new
90 *  frame is appended at the end of the canvas.
91 *
92 *  The active frame does not change, but its index may be renumbered due
93 *  to the insertion.
94 *
95 *  If an error occurs, -1 is returned and \b errno is set accordingly:
96 *  - \c ENOMEM Not enough memory to allocate new frame.
97 *
98 *  \param cv A libcucul canvas
99 *  \param frame The index where to insert the new frame
100 *  \return 0 in case of success, -1 if an error occurred.
101 */
102int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
103{
104    unsigned int size = cv->width * cv->height * sizeof(uint32_t);
105    unsigned int f;
106
107    if(frame > cv->framecount)
108        frame = cv->framecount;
109
110    cv->framecount++;
111    cv->frames = realloc(cv->frames,
112                         sizeof(struct cucul_frame) * cv->framecount);
113
114    for(f = cv->framecount - 1; f > frame; f--)
115        cv->frames[f] = cv->frames[f - 1];
116
117    cv->frames[frame].width = cv->width;
118    cv->frames[frame].height = cv->height;
119    cv->frames[frame].chars = malloc(size);
120    memcpy(cv->frames[frame].chars, cv->chars, size);
121    cv->frames[frame].attrs = malloc(size);
122    memcpy(cv->frames[frame].attrs, cv->attrs, size);
123    cv->frames[frame].curattr = cv->curattr;
124
125    if(cv->frame >= frame)
126        cv->frame++;
127
128    return 0;
129}
130
131/** \brief Remove a frame from a canvas.
132 *
133 *  Delete a frame from a given canvas.
134 *
135 *  The frame index indicates the frame to delete. Valid values range from
136 *  0 to the current canvas frame count minus 1. If the frame index is
137 *  greater the or equals the current canvas frame count, the last frame
138 *  is deleted.
139 *
140 *  If the active frame is deleted, frame 0 becomes the new active frame.
141 *  Otherwise, the active frame does not change, but its index may be
142 *  renumbered due to the deletion.
143 *
144 *  If an error occurs, -1 is returned and \b errno is set accordingly:
145 *  - \c EINVAL Requested frame is out of range, or attempt to delete the
146 *    last frame of the canvas.
147 *
148 *  \param cv A libcucul canvas
149 *  \param frame The index of the frame to delete
150 *  \return 0 in case of success, -1 if an error occurred.
151 */
152int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
153{
154    unsigned int f;
155
156    if(frame >= cv->framecount)
157    {
158#if defined(HAVE_ERRNO_H)
159        errno = EINVAL;
160#endif
161        return -1;
162    }
163
164    if(cv->framecount == 1)
165    {
166#if defined(HAVE_ERRNO_H)
167        errno = EINVAL;
168#endif
169        return -1;
170    }
171
172    free(cv->frames[frame].chars);
173    free(cv->frames[frame].attrs);
174
175    for(f = frame + 1; f < cv->framecount; f++)
176        cv->frames[f - 1] = cv->frames[f];
177
178    cv->framecount--;
179    cv->frames = realloc(cv->frames,
180                         sizeof(struct cucul_frame) * cv->framecount);
181
182    if(cv->frame > frame)
183        cv->frame--;
184    else if(cv->frame == frame)
185    {
186        cv->frame = 0;
187        load_frame_info(cv);
188    }
189
190    return 0;
191}
192
193/*
194 * XXX: the following functions are local.
195 */
196
197static void save_frame_info(cucul_canvas_t *cv)
198{
199    cv->frames[cv->frame].width = cv->width;
200    cv->frames[cv->frame].height = cv->height;
201
202    cv->frames[cv->frame].curattr = cv->curattr;
203}
204
205static void load_frame_info(cucul_canvas_t *cv)
206{
207    cv->width = cv->frames[cv->frame].width;
208    cv->height = cv->frames[cv->frame].height;
209
210    cv->chars = cv->frames[cv->frame].chars;
211    cv->attrs = cv->frames[cv->frame].attrs;
212
213    cv->curattr = cv->frames[cv->frame].curattr;
214}
215
Note: See TracBrowser for help on using the repository browser.