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

Last change on this file since 3582 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
RevLine 
[145]1/*
[268]2 *  spritedit     sprite editor for libcaca
[3495]3 *  Copyright (c) 2003-2009 Sam Hocevar <sam@hocevar.net>
[268]4 *                All Rights Reserved
[145]5 *
[268]6 *  $Id: spritedit.c 3495 2009-05-21 20:55:21Z sam $
[145]7 *
[1462]8 *  This program is free software. It comes without any warranty, to
[1452]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
[522]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[145]13 */
14
15#include "config.h"
[2146]16
[1048]17#if !defined(__KERNEL__)
18#   include <stdio.h>
[2146]19#   include <string.h>
20#   include <stdlib.h>
[1048]21#endif
[2146]22
[185]23#include "caca.h"
[145]24
[2146]25/* Courtesy of Zashi */
26char *guy[] = {
27  "  O_,= \n"
28  "  |    \n"
29  "  /\\   \n"
30  " / /   \n",
[752]31
[2146]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
[145]48int main(int argc, char **argv)
49{
[2821]50    caca_canvas_t *sprite;
[2313]51    size_t len;
[2146]52    void *buffer;
53    int i;
[524]54
[2146]55    /* Create a canvas with 4 frames */
[2821]56    sprite = caca_create_canvas(0, 0);
[2146]57    for(i = 0; i < 3; i++)
[2821]58        caca_create_frame(sprite, 0);
[145]59
[2146]60    /* Load stuff in all 4 frames */
61    for(i = 0; i < 4; i++)
[153]62    {
[2821]63        caca_set_frame(sprite, i);
[3495]64        caca_import_canvas_from_memory(sprite, guy[i], strlen(guy[i]), "utf8");
[153]65    }
66
[2146]67    /* Export our sprite in a memory buffer. We could save this to
68     * disk afterwards. */
[3495]69    buffer = caca_export_canvas_to_memory(sprite, "caca", &len);
[153]70
[2146]71    /* Free our sprite and reload it from the memory buffer. We could
72     * load this from disk, too. */
[2821]73    caca_free_canvas(sprite);
74    sprite = caca_create_canvas(0, 0);
[3495]75    caca_import_canvas_from_memory(sprite, buffer, len, "caca");
[2146]76    free(buffer);
[153]77
[2146]78    /* Print each sprite frame to stdout */
79    for(i = 0; i < 4; i++)
[145]80    {
[2821]81        caca_set_frame(sprite, i);
[2146]82        printf("Frame #%i\n", i);
[3495]83        buffer = caca_export_canvas_to_memory(sprite, "utf8", &len);
[2146]84        fwrite(buffer, len, 1, stdout);
85        free(buffer);
[145]86    }
87
[2146]88    /* Free our sprite */
[2821]89    caca_free_canvas(sprite);
[153]90
[145]91    return 0;
92}
93
Note: See TracBrowser for help on using the repository browser.