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

Last change on this file since 1254 was 1254, checked in by Sam Hocevar, 14 years ago
  • Use the LSB of alpha and blue components to encode additional flags such as bold or underline. No higher level support for these yet, but at least everything is consistent.
  • Created cucul_set_attr_ansi() and cucul_set_attr_argb() which are bound to replace cucul_set_color() and cucul_set_truecolor().
  • Property svn:keywords set to Id
File size: 5.3 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 1254 2006-10-28 23:53:46Z 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
33/** \brief Get the number of frames in a canvas.
34 *
35 *  Return the current canvas' frame count.
36 *
37 *  This function never fails.
38 *
39 *  \param cv A libcucul canvas
40 *  \return The frame count
41 */
42unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
43{
44    return cv->framecount;
45}
46
47/** \brief Activate a given canvas frame.
48 *
49 *  Set the active canvas frame. All subsequent drawing operations will
50 *  be performed on that frame. The current painting context set by
51 *  cucul_set_color() or cucul_set_truecolor() is inherited.
52 *
53 *  If the frame index is outside the canvas' frame range, nothing happens.
54 *
55 *  If an error occurs, -1 is returned and \b errno is set accordingly:
56 *  - \c EINVAL Requested frame is out of range.
57 *
58 *  \param cv A libcucul canvas
59 *  \param frame The canvas frame to activate
60 *  \return 0 in case of success, -1 if an error occurred.
61 */
62int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
63{
64    if(frame >= cv->framecount)
65    {
66#if defined(HAVE_ERRNO_H)
67        errno = EINVAL;
68#endif
69        return -1;
70    }
71
72    cv->frame = frame;
73
74    cv->chars = cv->allchars[cv->frame];
75    cv->attrs = cv->allattrs[cv->frame];
76
77    return 0;
78}
79
80/** \brief Add a frame to a canvas.
81 *
82 *  Create a new frame within the given canvas. Its contents are copied
83 *  from the currently active frame.
84 *
85 *  The frame index indicates where the frame should be inserted. Valid
86 *  values range from 0 to the current canvas frame count. If the frame
87 *  index is greater the or equals the current canvas frame count, the new
88 *  frame is appended at the end of the canvas.
89 *
90 *  The active frame does not change, but its index may be renumbered due
91 *  to the insertion.
92 *
93 *  If an error occurs, -1 is returned and \b errno is set accordingly:
94 *  - \c ENOMEM Not enough memory to allocate new frame.
95 *
96 *  \param cv A libcucul canvas
97 *  \param frame The index where to insert the new frame
98 *  \return 0 in case of success, -1 if an error occurred.
99 */
100int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
101{
102    unsigned int size = cv->width * cv->height * sizeof(uint32_t);
103    unsigned int f;
104
105    if(frame > cv->framecount)
106        frame = cv->framecount;
107
108    cv->framecount++;
109    cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
110    cv->allattrs = realloc(cv->allattrs, sizeof(uint32_t *) * cv->framecount);
111
112    for(f = cv->framecount - 1; f > frame; f--)
113    {
114        cv->allchars[f] = cv->allchars[f - 1];
115        cv->allattrs[f] = cv->allattrs[f - 1];
116    }
117
118    cv->allchars[frame] = malloc(size);
119    memcpy(cv->allchars[frame], cv->chars, size);
120    cv->allattrs[frame] = malloc(size);
121    memcpy(cv->allattrs[frame], cv->attrs, size);
122
123    if(cv->frame >= frame)
124        cv->frame++;
125
126    cv->chars = cv->allchars[cv->frame];
127    cv->attrs = cv->allattrs[cv->frame];
128
129    return 0;
130}
131
132/** \brief Remove a frame from a canvas.
133 *
134 *  Delete a frame from a given canvas.
135 *
136 *  The frame index indicates the frame to delete. Valid values range from
137 *  0 to the current canvas frame count minus 1. If the frame index is
138 *  greater the or equals the current canvas frame count, the last frame
139 *  is deleted.
140 *
141 *  If the active frame is deleted, frame 0 becomes the new active frame.
142 *  Otherwise, the active frame does not change, but its index may be
143 *  renumbered due to the deletion.
144 *
145 *  If an error occurs, -1 is returned and \b errno is set accordingly:
146 *  - \c EINVAL Requested frame is out of range, or attempt to delete the
147 *    last frame of the canvas.
148 *
149 *  \param cv A libcucul canvas
150 *  \param frame The index of the frame to delete
151 *  \return 0 in case of success, -1 if an error occurred.
152 */
153int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
154{
155    unsigned int f;
156
157    if(frame >= cv->framecount)
158    {
159#if defined(HAVE_ERRNO_H)
160        errno = EINVAL;
161#endif
162        return -1;
163    }
164
165    if(cv->framecount == 1)
166    {
167#if defined(HAVE_ERRNO_H)
168        errno = EINVAL;
169#endif
170        return -1;
171    }
172
173    free(cv->allchars[frame]);
174    free(cv->allattrs[frame]);
175
176    for(f = frame + 1; f < cv->framecount; f++)
177    {
178        cv->allchars[f - 1] = cv->allchars[f];
179        cv->allattrs[f - 1] = cv->allattrs[f];
180    }
181
182    cv->framecount--;
183    cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
184    cv->allattrs = realloc(cv->allattrs, sizeof(uint32_t *) * cv->framecount);
185
186    if(cv->frame > frame)
187        cv->frame--;
188    else if(cv->frame == frame)
189        cv->frame = 0;
190
191    cv->chars = cv->allchars[cv->frame];
192    cv->attrs = cv->allattrs[cv->frame];
193
194    return 0;
195}
196
Note: See TracBrowser for help on using the repository browser.