source: libcaca/trunk/cucul/frame.c @ 1381

Last change on this file since 1381 was 1381, checked in by Sam Hocevar, 13 years ago
  • Fix a bug in cucul_set_canvas_size() that caused the default attribute to be ignored.
  • 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: frame.c 1381 2006-11-12 22:26:36Z 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#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30/** \brief Get the number of frames in a canvas.
31 *
32 *  Return the current canvas' frame count.
33 *
34 *  This function never fails.
35 *
36 *  \param cv A libcucul canvas
37 *  \return The frame count
38 */
39unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
40{
41    return cv->framecount;
42}
43
44/** \brief Activate a given canvas frame.
45 *
46 *  Set the active canvas frame. All subsequent drawing operations will
47 *  be performed on that frame. The current painting context set by
48 *  cucul_set_attr() is inherited.
49 *
50 *  If the frame index is outside the canvas' frame range, nothing happens.
51 *
52 *  If an error occurs, -1 is returned and \b errno is set accordingly:
53 *  - \c EINVAL Requested frame is out of range.
54 *
55 *  \param cv A libcucul canvas
56 *  \param frame The canvas frame to activate
57 *  \return 0 in case of success, -1 if an error occurred.
58 */
59int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
60{
61    if(frame >= cv->framecount)
62    {
63        seterrno(EINVAL);
64        return -1;
65    }
66
67    _cucul_save_frame_info(cv);
68    cv->frame = frame;
69    _cucul_load_frame_info(cv);
70
71    return 0;
72}
73
74/** \brief Add a frame to a canvas.
75 *
76 *  Create a new frame within the given canvas. Its contents and attributes
77 *  are copied from the currently active frame.
78 *
79 *  The frame index indicates where the frame should be inserted. Valid
80 *  values range from 0 to the current canvas frame count. If the frame
81 *  index is greater than or equals the current canvas frame count, the new
82 *  frame is appended at the end of the canvas.
83 *
84 *  The active frame does not change, but its index may be renumbered due
85 *  to the insertion.
86 *
87 *  If an error occurs, -1 is returned and \b errno is set accordingly:
88 *  - \c ENOMEM Not enough memory to allocate new frame.
89 *
90 *  \param cv A libcucul canvas
91 *  \param id The index where to insert the new frame
92 *  \return 0 in case of success, -1 if an error occurred.
93 */
94int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int id)
95{
96    unsigned int size = cv->width * cv->height;
97    unsigned int f;
98
99    if(id > cv->framecount)
100        id = cv->framecount;
101
102    cv->framecount++;
103    cv->frames = realloc(cv->frames,
104                         sizeof(struct cucul_frame) * cv->framecount);
105
106    for(f = cv->framecount - 1; f > id; f--)
107        cv->frames[f] = cv->frames[f - 1];
108
109    if(cv->frame >= id)
110        cv->frame++;
111
112    cv->frames[id].width = cv->width;
113    cv->frames[id].height = cv->height;
114    cv->frames[id].chars = malloc(size * sizeof(uint32_t));
115    memcpy(cv->frames[id].chars, cv->chars, size * sizeof(uint32_t));
116    cv->frames[id].attrs = malloc(size * sizeof(uint32_t));
117    memcpy(cv->frames[id].attrs, cv->attrs, size * sizeof(uint32_t));
118    cv->frames[id].curattr = cv->curattr;
119
120    cv->frames[id].x = cv->frames[cv->frame].x;
121    cv->frames[id].y = cv->frames[cv->frame].y;
122    cv->frames[id].handlex = cv->frames[cv->frame].handlex;
123    cv->frames[id].handley = cv->frames[cv->frame].handley;
124
125    return 0;
126}
127
128/** \brief Remove a frame from a canvas.
129 *
130 *  Delete a frame from a given canvas.
131 *
132 *  The frame index indicates the frame to delete. Valid values range from
133 *  0 to the current canvas frame count minus 1. If the frame index is
134 *  greater the or equals the current canvas frame count, the last frame
135 *  is deleted.
136 *
137 *  If the active frame is deleted, frame 0 becomes the new active frame.
138 *  Otherwise, the active frame does not change, but its index may be
139 *  renumbered due to the deletion.
140 *
141 *  If an error occurs, -1 is returned and \b errno is set accordingly:
142 *  - \c EINVAL Requested frame is out of range, or attempt to delete the
143 *    last frame of the canvas.
144 *
145 *  \param cv A libcucul canvas
146 *  \param id The index of the frame to delete
147 *  \return 0 in case of success, -1 if an error occurred.
148 */
149int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int id)
150{
151    unsigned int f;
152
153    if(id >= cv->framecount)
154    {
155        seterrno(EINVAL);
156        return -1;
157    }
158
159    if(cv->framecount == 1)
160    {
161        seterrno(EINVAL);
162        return -1;
163    }
164
165    free(cv->frames[id].chars);
166    free(cv->frames[id].attrs);
167
168    for(f = id + 1; f < cv->framecount; f++)
169        cv->frames[f - 1] = cv->frames[f];
170
171    cv->framecount--;
172    cv->frames = realloc(cv->frames,
173                         sizeof(struct cucul_frame) * cv->framecount);
174
175    if(cv->frame > id)
176        cv->frame--;
177    else if(cv->frame == id)
178    {
179        cv->frame = 0;
180        _cucul_load_frame_info(cv);
181    }
182
183    return 0;
184}
185
186/*
187 * XXX: the following functions are local.
188 */
189
190void _cucul_save_frame_info(cucul_canvas_t *cv)
191{
192    cv->frames[cv->frame].width = cv->width;
193    cv->frames[cv->frame].height = cv->height;
194
195    cv->frames[cv->frame].curattr = cv->curattr;
196}
197
198void _cucul_load_frame_info(cucul_canvas_t *cv)
199{
200    cv->width = cv->frames[cv->frame].width;
201    cv->height = cv->frames[cv->frame].height;
202
203    cv->chars = cv->frames[cv->frame].chars;
204    cv->attrs = cv->frames[cv->frame].attrs;
205
206    cv->curattr = cv->frames[cv->frame].curattr;
207}
208
Note: See TracBrowser for help on using the repository browser.