source: libcaca/trunk/caca/caca0.c @ 1013

Last change on this file since 1013 was 1013, checked in by Sam Hocevar, 14 years ago
  • Added an emulation layer for pre-1.0 libcaca API. Legacy applications just need to include <caca0.h> instead of <caca.h> and should be source- compatible with old versions of libcaca.

Most of the old API works, except features and caca_sqrt(). Sprites do
work API-wise but due to the new format they are no longer working in
the same way.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1/*
2 *  libcaca       Colour ASCII-Art library
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: caca0.c 1013 2006-09-09 15:38:06Z sam $
7 *
8 *  This library is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 *  This file contains glue code for applications using the pre-1.0
16 *  libcaca API.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <stdio.h>
24#endif
25
26#include "caca.h"
27#include "caca_internals.h"
28
29/* These variables are needed to emulate old non-thread safe behaviour */
30cucul_canvas_t *__caca0_cv = NULL;
31caca_display_t *__caca0_dp = NULL;
32unsigned char __caca0_fg;
33unsigned char __caca0_bg;
34char __caca0_utf8[2] = " ";
35
36/* These functions are needed, too */
37int __caca0_init(void);
38void __caca0_end(void);
39unsigned int __caca0_get_event(unsigned int, int);
40int __caca0_get_feature(int);
41void __caca0_set_feature(int);
42char const *__caca0_get_feature_name(int);
43cucul_canvas_t *__caca0_load_sprite(char const *);
44
45/* Emulation functions */
46int __caca0_init(void)
47{
48    __caca0_cv = cucul_create_canvas(0, 0);
49    if(!__caca0_cv)
50        return -1;
51    __caca0_dp = caca_create_display(__caca0_cv);
52    if(!__caca0_dp)
53    {
54        cucul_free_canvas(__caca0_cv);
55        __caca0_cv = NULL;
56        return -1;
57    }
58    __caca0_fg = CUCUL_COLOR_LIGHTGRAY;
59    __caca0_bg = CUCUL_COLOR_BLACK;
60    return 0;
61}
62
63void __caca0_end(void)
64{
65    caca_free_display(__caca0_dp);
66    __caca0_dp = NULL;
67    cucul_free_canvas(__caca0_cv);
68    __caca0_cv = NULL;
69}
70
71unsigned int __caca0_get_event(unsigned int m, int t)
72{
73    caca_event_t ev;
74    int ret;
75
76    ret = caca_get_event(__caca0_dp, (m >> 24) & 0x7f, &ev, t);
77    if(!ret)
78        return 0x00000000;
79
80    switch(ev.type)
81    {
82        case CACA_EVENT_KEY_PRESS:
83            return 0x01000000 | ev.data.key.ch;
84        case CACA_EVENT_KEY_RELEASE:
85            return 0x02000000 | ev.data.key.ch;
86        case CACA_EVENT_MOUSE_PRESS:
87            return 0x04000000 | ev.data.mouse.button;
88        case CACA_EVENT_MOUSE_RELEASE:
89            return 0x08000000 | ev.data.mouse.button;
90        case CACA_EVENT_MOUSE_MOTION:
91            return 0x10000000 | ((ev.data.mouse.x & 0xfff) << 12)
92                              | (ev.data.mouse.y & 0xfff);
93        case CACA_EVENT_RESIZE:
94            return 0x20000000;
95        default:
96            break;
97    }
98
99    return 0x00000000;
100}
101
102enum caca_feature
103{
104    CACA_BACKGROUND       = 0x10,
105    CACA_BACKGROUND_BLACK = 0x11,
106    CACA_BACKGROUND_SOLID = 0x12,
107#define CACA_BACKGROUND_MIN 0x11
108#define CACA_BACKGROUND_MAX 0x12
109    CACA_ANTIALIASING           = 0x20,
110    CACA_ANTIALIASING_NONE      = 0x21,
111    CACA_ANTIALIASING_PREFILTER = 0x22,
112#define CACA_ANTIALIASING_MIN     0x21
113#define CACA_ANTIALIASING_MAX     0x22
114    CACA_DITHERING          = 0x30,
115    CACA_DITHERING_NONE     = 0x31,
116    CACA_DITHERING_ORDERED2 = 0x32,
117    CACA_DITHERING_ORDERED4 = 0x33,
118    CACA_DITHERING_ORDERED8 = 0x34,
119    CACA_DITHERING_RANDOM   = 0x35,
120#define CACA_DITHERING_MIN    0x31
121#define CACA_DITHERING_MAX    0x35
122    CACA_FEATURE_UNKNOWN = 0xffff
123};
124
125int __caca0_get_feature(int feature)
126{
127    return feature;
128}
129
130void __caca0_set_feature(int feature)
131{
132    switch(feature)
133    {
134        case CACA_BACKGROUND:
135            feature = CACA_BACKGROUND_SOLID;
136        case CACA_BACKGROUND_BLACK:
137        case CACA_BACKGROUND_SOLID:
138            //_caca_background = feature;
139            break;
140
141        case CACA_ANTIALIASING:
142            feature = CACA_ANTIALIASING_PREFILTER;
143        case CACA_ANTIALIASING_NONE:
144        case CACA_ANTIALIASING_PREFILTER:
145            //_caca_antialiasing = feature;
146            break;
147
148        case CACA_DITHERING:
149            feature = CACA_DITHERING_ORDERED4;
150        case CACA_DITHERING_NONE:
151        case CACA_DITHERING_ORDERED2:
152        case CACA_DITHERING_ORDERED4:
153        case CACA_DITHERING_ORDERED8:
154        case CACA_DITHERING_RANDOM:
155            //_caca_dithering = feature;
156            break;
157    }
158}
159
160char const *__caca0_get_feature_name(int feature)
161{
162    switch(feature)
163    {
164        case CACA_BACKGROUND_BLACK: return "black background";
165        case CACA_BACKGROUND_SOLID: return "solid background";
166
167        case CACA_ANTIALIASING_NONE:      return "no antialiasing";
168        case CACA_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
169
170        case CACA_DITHERING_NONE:     return "no dithering";
171        case CACA_DITHERING_ORDERED2: return "2x2 ordered dithering";
172        case CACA_DITHERING_ORDERED4: return "4x4 ordered dithering";
173        case CACA_DITHERING_ORDERED8: return "8x8 ordered dithering";
174        case CACA_DITHERING_RANDOM:   return "random dithering";
175
176        default: return "unknown";
177    }
178}
179
180cucul_canvas_t *__caca0_load_sprite(char const *file)
181{
182    cucul_buffer_t *buf;
183    cucul_canvas_t *cv;
184
185    buf = cucul_load_file(file);
186    if(!buf)
187        return NULL;
188    cv = cucul_import_canvas(buf, "");
189    cucul_free_buffer(buf);
190    if(!cv)
191        return NULL;
192
193    return cv;
194}
195
Note: See TracBrowser for help on using the repository browser.