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

Last change on this file since 2299 was 2299, checked in by Sam Hocevar, 12 years ago
  • Remove #include <stdint.h> etc. from "common.h". Instead, make sure that <cucul.h> will provide the C99 types, even if libcaca has been installed.
  • Rename what's left of "common.h" to "stubs.h".
  • Remove all references to erroneous <inttypes.h> from source files.
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1/*
2 *  font          libcucul font test program
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: font.c 2299 2008-04-19 12:42:50Z 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 "cucul.h"
28#include "caca.h"
29
30int main(int argc, char *argv[])
31{
32    cucul_canvas_t *cv;
33    caca_display_t *dp;
34    cucul_font_t *f;
35    cucul_dither_t *d;
36    unsigned char *buf;
37    unsigned int w, h;
38    char const * const * fonts;
39
40    /* Create a canvas */
41    cv = cucul_create_canvas(8, 2);
42    if(cv == NULL)
43    {
44        printf("Can't create canvas\n");
45        return -1;
46    }
47
48
49    /* Draw stuff on our canvas */
50    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLACK);
51    cucul_put_str(cv, 0, 0, "ABcde");
52    cucul_set_color_ansi(cv, CUCUL_LIGHTRED, CUCUL_BLACK);
53    cucul_put_str(cv, 5, 0, "\\o/");
54    cucul_set_color_ansi(cv, CUCUL_WHITE, CUCUL_BLUE);
55    cucul_put_str(cv, 0, 1, "&$âøÿØ?!");
56
57    /* Load a libcucul internal font */
58    fonts = cucul_get_font_list();
59    if(fonts[0] == NULL)
60    {
61        fprintf(stderr, "error: libcucul was compiled without any fonts\n");
62        return -1;
63    }
64    f = cucul_load_font(fonts[0], 0);
65    if(f == NULL)
66    {
67        fprintf(stderr, "error: could not load font \"%s\"\n", fonts[0]);
68        return -1;
69    }
70
71    /* Create our bitmap buffer (32-bit ARGB) */
72    w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
73    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
74    buf = malloc(4 * w * h);
75
76    /* Render the canvas onto our image buffer */
77    cucul_render_canvas(cv, f, buf, w, h, 4 * w);
78
79    /* Just for fun, render the image using libcaca */
80    cucul_set_canvas_size(cv, 80, 32);
81    dp = caca_create_display(cv);
82
83    {
84#if defined(HAVE_ENDIAN_H)
85        if(__BYTE_ORDER == __BIG_ENDIAN)
86#else
87        /* This is compile-time optimised with at least -O1 or -Os */
88        uint32_t const tmp = 0x12345678;
89        if(*(uint8_t const *)&tmp == 0x12)
90#endif
91            d = cucul_create_dither(32, w, h, 4 * w,
92                                    0xff0000, 0xff00, 0xff, 0xff000000);
93        else
94            d = cucul_create_dither(32, w, h, 4 * w,
95                                    0xff00, 0xff0000, 0xff000000, 0xff);
96    }
97
98    cucul_dither_bitmap(cv, 0, 0, cucul_get_canvas_width(cv),
99                                  cucul_get_canvas_height(cv), d, buf);
100    caca_refresh_display(dp);
101
102    caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, -1);
103
104    /* Free everything */
105    caca_free_display(dp);
106    free(buf);
107    cucul_free_dither(d);
108    cucul_free_font(f);
109    cucul_free_canvas(cv);
110
111    return 0;
112}
113
Note: See TracBrowser for help on using the repository browser.