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

Last change on this file since 3495 was 3495, checked in by Sam Hocevar, 10 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.4 KB
Line 
1/*
2 *  text          canvas text import/export
3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: text.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#define STRING \
26  "              |_| \n" \
27  "   _,----._   | | \n" \
28  "  (/ @  @ \\)   __ \n" \
29  "   |  OO  |   |_  \n" \
30  "   \\ `--' /   |__ \n" \
31  "    `----'        \n" \
32  "              |_| \n" \
33  " Hello world!  |  \n" \
34  "                  \n"
35
36int main(int argc, char *argv[])
37{
38    caca_canvas_t *cv, *pig;
39    void *buffer;
40    size_t len;
41    int i, j;
42
43    pig = caca_create_canvas(0, 0);
44    caca_import_canvas_from_memory(pig, STRING, strlen(STRING), "text");
45
46    cv = caca_create_canvas(caca_get_canvas_width(pig) * 2,
47                             caca_get_canvas_height(pig) * 2);
48
49    if(cv == NULL || pig == NULL)
50    {
51        printf("Can't created canvas\n");
52        return -1;
53    }
54
55    caca_blit(cv, 0, 0, pig, NULL);
56    caca_flip(pig);
57    caca_blit(cv, caca_get_canvas_width(pig), 0, pig, NULL);
58    caca_flip(pig);
59    caca_flop(pig);
60    caca_blit(cv, 0, caca_get_canvas_height(pig), pig, NULL);
61    caca_flop(pig);
62    caca_rotate_180(pig);
63    caca_blit(cv, caca_get_canvas_width(pig),
64                   caca_get_canvas_height(pig), pig, NULL);
65
66    for(j = 0; j < caca_get_canvas_height(cv); j++)
67    {
68        for(i = 0; i < caca_get_canvas_width(cv); i += 2)
69        {
70            unsigned long int a;
71            caca_set_color_ansi(cv, CACA_LIGHTBLUE + (i + j) % 6,
72                                 CACA_DEFAULT);
73            a = caca_get_attr(cv, -1, -1);
74            caca_put_attr(cv, i, j, a);
75            caca_put_attr(cv, i + 1, j, a);
76        }
77    }
78
79    buffer = caca_export_canvas_to_memory(cv, "utf8", &len);
80    fwrite(buffer, len, 1, stdout);
81    free(buffer);
82
83    caca_rotate_left(cv);
84    buffer = caca_export_canvas_to_memory(cv, "utf8", &len);
85    fwrite(buffer, len, 1, stdout);
86    free(buffer);
87
88    caca_free_canvas(pig);
89    caca_free_canvas(cv);
90
91    return 0;
92}
93
Note: See TracBrowser for help on using the repository browser.