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

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

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

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