source: libcaca/trunk/examples/font.c @ 4148

Last change on this file since 4148 was 4148, checked in by Sam Hocevar, 10 years ago

Update my e-mail address everywhere.

  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1/*
2 *  font          libcaca font test program
3 *  Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
4 *                All Rights Reserved
5 *
6 *  $Id: font.c 4148 2009-12-19 14:38:38Z 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#   if defined(HAVE_ENDIAN_H)
19#      include <endian.h>
20#   endif
21
22#   include <stdio.h>
23#   include <stdlib.h>
24#   include <string.h>
25#endif
26
27#include "caca.h"
28
29int main(int argc, char *argv[])
30{
31    caca_canvas_t *cv;
32    caca_display_t *dp;
33    caca_font_t *f;
34    caca_dither_t *d;
35    uint8_t *buf;
36    unsigned int w, h;
37    char const * const * fonts;
38
39    /* Create a canvas */
40    cv = caca_create_canvas(8, 2);
41    if(cv == NULL)
42    {
43        printf("Can't create canvas\n");
44        return -1;
45    }
46
47
48    /* Draw stuff on our canvas */
49    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK);
50    caca_put_str(cv, 0, 0, "ABcde");
51    caca_set_color_ansi(cv, CACA_LIGHTRED, CACA_BLACK);
52    caca_put_str(cv, 5, 0, "\\o/");
53    caca_set_color_ansi(cv, CACA_WHITE, CACA_BLUE);
54    caca_put_str(cv, 0, 1, "&$âøÿØ?!");
55
56    /* Load a libcaca internal font */
57    fonts = caca_get_font_list();
58    if(fonts[0] == NULL)
59    {
60        fprintf(stderr, "error: libcaca was compiled without any fonts\n");
61        return -1;
62    }
63    f = caca_load_font(fonts[0], 0);
64    if(f == NULL)
65    {
66        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
67        return -1;
68    }
69
70    /* Create our bitmap buffer (32-bit ARGB) */
71    w = caca_get_canvas_width(cv) * caca_get_font_width(f);
72    h = caca_get_canvas_height(cv) * caca_get_font_height(f);
73    buf = malloc(4 * w * h);
74
75    /* Render the canvas onto our image buffer */
76    caca_render_canvas(cv, f, buf, w, h, 4 * w);
77
78    /* Just for fun, render the image using libcaca */
79    caca_set_canvas_size(cv, 80, 32);
80    dp = caca_create_display(cv);
81
82    {
83#if defined(HAVE_ENDIAN_H)
84        if(__BYTE_ORDER == __BIG_ENDIAN)
85#else
86        /* This is compile-time optimised with at least -O1 or -Os */
87        uint32_t const tmp = 0x12345678;
88        if(*(uint8_t const *)&tmp == 0x12)
89#endif
90            d = caca_create_dither(32, w, h, 4 * w,
91                                    0xff0000, 0xff00, 0xff, 0xff000000);
92        else
93            d = caca_create_dither(32, w, h, 4 * w,
94                                    0xff00, 0xff0000, 0xff000000, 0xff);
95    }
96
97    caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv),
98                                  caca_get_canvas_height(cv), d, buf);
99    caca_refresh_display(dp);
100
101    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
102
103    /* Free everything */
104    caca_free_display(dp);
105    free(buf);
106    caca_free_dither(d);
107    caca_free_font(f);
108    caca_free_canvas(cv);
109
110    return 0;
111}
112
Note: See TracBrowser for help on using the repository browser.