source: libcaca/trunk/examples/spritedit.c @ 3495

Last change on this file since 3495 was 3495, checked in by Sam Hocevar, 11 years ago

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/*
2 *  spritedit     sprite editor for libcaca
3 *  Copyright (c) 2003-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: spritedit.c 3495 2009-05-21 20:55:21Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15#include "config.h"
16
17#if !defined(__KERNEL__)
18#   include <stdio.h>
19#   include <string.h>
20#   include <stdlib.h>
21#endif
22
23#include "caca.h"
24
25/* Courtesy of Zashi */
26char *guy[] = {
27  "  O_,= \n"
28  "  |    \n"
29  "  /\\   \n"
30  " / /   \n",
31
32  "  O_,= \n"
33  "  |    \n"
34  "  /|   \n"
35  " / |   \n",
36
37  "  O_,= \n"
38  "  |    \n"
39  "  |\\   \n"
40  "  |/   \n",
41
42  "  O_,= \n"
43  "  |    \n"
44  "  |\\   \n"
45  "  | \\  \n",
46};
47
48int main(int argc, char **argv)
49{
50    caca_canvas_t *sprite;
51    size_t len;
52    void *buffer;
53    int i;
54
55    /* Create a canvas with 4 frames */
56    sprite = caca_create_canvas(0, 0);
57    for(i = 0; i < 3; i++)
58        caca_create_frame(sprite, 0);
59
60    /* Load stuff in all 4 frames */
61    for(i = 0; i < 4; i++)
62    {
63        caca_set_frame(sprite, i);
64        caca_import_canvas_from_memory(sprite, guy[i], strlen(guy[i]), "utf8");
65    }
66
67    /* Export our sprite in a memory buffer. We could save this to
68     * disk afterwards. */
69    buffer = caca_export_canvas_to_memory(sprite, "caca", &len);
70
71    /* Free our sprite and reload it from the memory buffer. We could
72     * load this from disk, too. */
73    caca_free_canvas(sprite);
74    sprite = caca_create_canvas(0, 0);
75    caca_import_canvas_from_memory(sprite, buffer, len, "caca");
76    free(buffer);
77
78    /* Print each sprite frame to stdout */
79    for(i = 0; i < 4; i++)
80    {
81        caca_set_frame(sprite, i);
82        printf("Frame #%i\n", i);
83        buffer = caca_export_canvas_to_memory(sprite, "utf8", &len);
84        fwrite(buffer, len, 1, stdout);
85        free(buffer);
86    }
87
88    /* Free our sprite */
89    caca_free_canvas(sprite);
90
91    return 0;
92}
93
Note: See TracBrowser for help on using the repository browser.