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

Last change on this file since 1231 was 1231, checked in by Sam Hocevar, 14 years ago
  • Removed "This function..." constructs from documentation. Fixed a few documentation errors or imprecisions.
  • Property svn:keywords set to Id
File size: 5.3 KB
RevLine 
[115]1/*
[672]2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
[527]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[115]5 *
[769]6 *  $Id: sprite.c 1231 2006-10-25 22:06:21Z sam $
7 *
[268]8 *  This library is free software; you can redistribute it and/or
[522]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.
[115]12 */
13
[769]14/*
[842]15 *  This file contains a small framework for canvas frame management.
[205]16 */
17
[115]18#include "config.h"
[859]19#include "common.h"
[115]20
[568]21#if !defined(__KERNEL__)
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
[920]25#   if defined(HAVE_ERRNO_H)
26#       include <errno.h>
27#   endif
[568]28#endif
[115]29
[524]30#include "cucul.h"
31#include "cucul_internals.h"
[115]32
[842]33/** \brief Get the number of frames in a canvas.
[257]34 *
[1231]35 *  Return the current canvas' frame count.
[257]36 *
[920]37 *  This function never fails.
38 *
[842]39 *  \param cv A libcucul canvas
40 *  \return The frame count
[257]41 */
[842]42unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
[121]43{
[842]44    return cv->framecount;
[153]45}
46
[842]47/** \brief Activate a given canvas frame.
[257]48 *
[1231]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.
[842]52 *
53 *  If the frame index is outside the canvas' frame range, nothing happens.
54 *
[920]55 *  If an error occurs, -1 is returned and \b errno is set accordingly:
56 *  - \c EINVAL Requested frame is out of range.
57 *
[842]58 *  \param cv A libcucul canvas
59 *  \param frame The canvas frame to activate
[920]60 *  \return 0 in case of success, -1 if an error occurred.
[257]61 */
[920]62int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
[153]63{
[842]64    if(frame >= cv->framecount)
[920]65    {
66#if defined(HAVE_ERRNO_H)
67        errno = EINVAL;
68#endif
69        return -1;
70    }
[153]71
[842]72    cv->frame = frame;
[121]73
[842]74    cv->chars = cv->allchars[cv->frame];
75    cv->attr = cv->allattr[cv->frame];
[920]76
77    return 0;
[121]78}
79
[842]80/** \brief Add a frame to a canvas.
[257]81 *
[1231]82 *  Create a new frame within the given canvas. Its contents are copied
83 *  from the currently active frame.
[842]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 *
[920]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 *
[842]96 *  \param cv A libcucul canvas
97 *  \param frame The index where to insert the new frame
[920]98 *  \return 0 in case of success, -1 if an error occurred.
[257]99 */
[920]100int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
[121]101{
[842]102    unsigned int size = cv->width * cv->height * sizeof(uint32_t);
103    unsigned int f;
[135]104
[842]105    if(frame > cv->framecount)
106        frame = cv->framecount;
[153]107
[842]108    cv->framecount++;
109    cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
110    cv->allattr = realloc(cv->allattr, sizeof(uint32_t *) * cv->framecount);
[121]111
[842]112    for(f = cv->framecount - 1; f > frame; f--)
113    {
114        cv->allchars[f] = cv->allchars[f - 1];
115        cv->allattr[f] = cv->allattr[f - 1];
116    }
[153]117
[842]118    cv->allchars[frame] = malloc(size);
119    memcpy(cv->allchars[frame], cv->chars, size);
120    cv->allattr[frame] = malloc(size);
121    memcpy(cv->allattr[frame], cv->attr, size);
[153]122
[842]123    if(cv->frame >= frame)
124        cv->frame++;
[153]125
[842]126    cv->chars = cv->allchars[cv->frame];
127    cv->attr = cv->allattr[cv->frame];
[920]128
129    return 0;
[153]130}
131
[842]132/** \brief Remove a frame from a canvas.
[257]133 *
[1231]134 *  Delete a frame from a given canvas.
[842]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 *
[920]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 *
[811]149 *  \param cv A libcucul canvas
[842]150 *  \param frame The index of the frame to delete
[920]151 *  \return 0 in case of success, -1 if an error occurred.
[257]152 */
[920]153int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
[153]154{
[842]155    unsigned int f;
[115]156
[842]157    if(frame >= cv->framecount)
[920]158    {
159#if defined(HAVE_ERRNO_H)
160        errno = EINVAL;
161#endif
162        return -1;
163    }
[135]164
[842]165    if(cv->framecount == 1)
[920]166    {
167#if defined(HAVE_ERRNO_H)
168        errno = EINVAL;
169#endif
170        return -1;
171    }
[135]172
[842]173    free(cv->allchars[frame]);
174    free(cv->allattr[frame]);
[153]175
[842]176    for(f = frame + 1; f < cv->framecount; f++)
[115]177    {
[842]178        cv->allchars[f - 1] = cv->allchars[f];
179        cv->allattr[f - 1] = cv->allattr[f];
[115]180    }
[153]181
[842]182    cv->framecount--;
183    cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
184    cv->allattr = realloc(cv->allattr, sizeof(uint32_t *) * cv->framecount);
[115]185
[842]186    if(cv->frame > frame)
187        cv->frame--;
188    else if(cv->frame == frame)
189        cv->frame = 0;
[121]190
[842]191    cv->chars = cv->allchars[cv->frame];
192    cv->attr = cv->allattr[cv->frame];
[920]193
194    return 0;
[115]195}
196
Note: See TracBrowser for help on using the repository browser.