~ubuntu-branches/ubuntu/natty/gtkboard/natty

« back to all changes in this revision

Viewing changes to .pc/debian-changes-0.11pre0+cvs.2003.11.02-1/src/aaball.c

  • Committer: Bazaar Package Importer
  • Author(s): Barak A. Pearlmutter
  • Date: 2011-02-28 11:25:02 UTC
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20110228112502-e9aah248wxelm7ao
Tags: 0.11pre0+cvs.2003.11.02-2
autotools tweaks, most notably -lSDL to supplement -lSDL_mixer

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is a part of gtkboard, a board games system.
2
 
    Copyright (C) 2003, Arvind Narayanan <arvindn@users.sourceforge.net>
3
 
 
4
 
    This program is free software; you can redistribute it and/or modify
5
 
    it under the terms of the GNU General Public License as published by
6
 
    the Free Software Foundation; either version 2 of the License, or
7
 
    (at your option) any later version.
8
 
 
9
 
    This program is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
    GNU General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU General Public License
15
 
    along with this program; if not, write to the Free Software
16
 
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
17
 
 
18
 
*/
19
 
#include <stdio.h>
20
 
#include <math.h>
21
 
#include <glib.h>
22
 
#include <string.h>
23
 
 
24
 
#include "aaball.h"
25
 
 
26
 
 
27
 
/** \file aaball.c
28
 
  \brief routines for generating antialiased images, primarily balls.
29
 
  */
30
 
 
31
 
static char hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8' ,
32
 
                                        '9', 'A', 'B', 'C', 'D', 'E', 'F'};
33
 
 
34
 
 
35
 
static int pixmap_get_color(int fg, int bg, float ratio)
36
 
{
37
 
        int f1, f2, f3, b1, b2, b3, w1, w2, w3;
38
 
        float lw, lf, lb;
39
 
        if (ratio < 0) ratio = 0;
40
 
        if (ratio > 1) ratio = 1;
41
 
        // FIXME: could we have problems with equality of floats?
42
 
        if (ratio == 0)
43
 
        {
44
 
                w1 = fg >> 16;
45
 
                w2 = (fg >> 8) & 0xff;
46
 
                w3 = fg & 0xff;
47
 
        }
48
 
        else if (ratio == 1)
49
 
        {
50
 
                w1 = bg >> 16;
51
 
                w2 = (bg >> 8) & 0xff;
52
 
                w3 = bg & 0xff;
53
 
        }
54
 
        else
55
 
        {
56
 
                f1 = fg >> 16;
57
 
                f2 = (fg >> 8) & 0xff;
58
 
                f3 = fg & 0xff;
59
 
                b1 = bg >> 16;
60
 
                b2 = (bg >> 8) & 0xff;
61
 
                b3 = bg & 0xff;
62
 
                w1 =  (1 - ratio) * f1 +  (ratio) * b1;
63
 
                w2 =  (1 - ratio) * f2 +  (ratio) * b2;
64
 
                w3 =  (1 - ratio) * f3 +  (ratio) * b3;
65
 
                lf = sqrt(f1 * f1 + f2 * f2 + f3 * f3);
66
 
                lb = sqrt(b1 * b1 + b2 * b2 + b3 * b3);
67
 
                lw = sqrt(w1 * w1 + w2 * w2 + w3 * w3);
68
 
                lw /= ((1 - ratio) * lf + ratio * lb);
69
 
                w1 /= lw; if (w1 > 0xff) w1 = 0xff;
70
 
                w2 /= lw; if (w2 > 0xff) w2 = 0xff;
71
 
                w3 /= lw; if (w3 > 0xff) w3 = 0xff;
72
 
        }
73
 
        return (w1 << 16) + (w2 << 8) + w3;
74
 
}
75
 
 
76
 
static char *pixmap_get_hex_color(int fg, int bg, float ratio)
77
 
{
78
 
        int red, green, blue, val;
79
 
        static char color[7] = { 0 };
80
 
        val = pixmap_get_color (fg, bg, ratio);
81
 
        red = val >> 16;
82
 
        green = (val >> 8) & 0xff;
83
 
        blue = val & 0xff;
84
 
        color[0] = hex[red/16];
85
 
        color[1] = hex[red%16];
86
 
        color[2] = hex[green/16];
87
 
        color[3] = hex[green%16];
88
 
        color[4] = hex[blue/16];
89
 
        color[5] = hex[blue%16];
90
 
        return color;
91
 
}
92
 
        
93
 
static char *pixmap_map [256];
94
 
 
95
 
//! Generates a ball.
96
 
/** 
97
 
 @param len = size of ball
98
 
 @param pixbuf = buffer to store the pixmap in
99
 
 @param fg = foreground color (color of the ball)
100
 
 @param bg = background color (color of the square)
101
 
 @param rad = radius of the ball
102
 
 @param grad = gradient with which the ball merges into the backgound (larger gradient indicates sharper boundary)*/
103
 
char ** pixmap_ball_gen(int len, char *pixbuf, int fg, int bg, float rad, float grad)
104
 
{
105
 
        char **map = pixmap_map;
106
 
        char *buf = pixbuf;
107
 
        static char colbuf[18][20];
108
 
        int i, j;
109
 
        for(i=0; i<18; i++) map[i] = colbuf[i];
110
 
        g_snprintf(map[0], 20, "%d %d 16 1", len, len);
111
 
        for(i=0; i<16; i++)
112
 
        {
113
 
                g_snprintf (map[i+1], 20, "%c c #%s", hex[i],
114
 
                                pixmap_get_hex_color(fg, bg, i / 15.0));
115
 
        }
116
 
        for (i=0; i<len; i++)
117
 
        {
118
 
                char *ptr = map[i+17] = buf + i * (len+1);
119
 
                for (j=0; j<len; j++)
120
 
                {
121
 
                        int mid = len/2, x = i - mid, y = j - mid;
122
 
                        float q = (x * x + y * y) / rad / rad;
123
 
                        int  k;
124
 
                        if (q<1) *ptr++ = '0';else
125
 
                        for(k=1; k<16; k++)
126
 
                                if ((q >= 1 + (k-1) / grad && q < 1 + k/grad) || k == 15)
127
 
                                { *ptr++ = hex[k]; break; }
128
 
                }
129
 
                *ptr++ = 0;
130
 
        }
131
 
        map[17+len] = NULL;
132
 
        return map;
133
 
}
134
 
 
135
 
static void rgbmap_ball_gen_real (int len, unsigned char *rgbbuf, int fg, float rad, float grad, float midx, float midy)
136
 
{
137
 
        int i, j;
138
 
        unsigned char *bufp = rgbbuf;
139
 
        for (i=0; i<len; i++)
140
 
        for (j=0; j<len; j++)
141
 
        {
142
 
                float x = i - midx, y = j - midy;
143
 
                float q = (x * x + y * y) / rad / rad;
144
 
                int bg = (bufp[0] << 16) + (bufp[1] << 8) + bufp[2];
145
 
                int color = q < 1 ? fg : pixmap_get_color (fg, bg, (q - 1) * grad / 16);
146
 
                *bufp++ = color >> 16;
147
 
                *bufp++ = (color >> 8) & 0xFF;
148
 
                *bufp++ = color & 0xFF;
149
 
        }
150
 
}
151
 
 
152
 
void rgbmap_square_gen (int len, unsigned char *rgbbuf, int fg, int bg, float side)
153
 
{
154
 
        int i, j;
155
 
        unsigned char *bufp = rgbbuf;
156
 
        for (i=0; i<len; i++)
157
 
        for (j=0; j<len; j++)
158
 
        {
159
 
                float x = 2*i - len, y = 2*j - len;
160
 
                int color;
161
 
                if (x < 0) x = -x;
162
 
                if (y < 0) y = -y;
163
 
                color =  x < side && y < side ? fg : bg;
164
 
                *bufp++ = color >> 16;
165
 
                *bufp++ = (color >> 8) & 0xFF;
166
 
                *bufp++ = color & 0xFF;
167
 
        }
168
 
}
169
 
 
170
 
void rgbmap_ball_gen (int len, unsigned char *rgbbuf, int fg, int bg, float rad, float grad)
171
 
{
172
 
        int i, j;
173
 
        unsigned char *bufp = rgbbuf;
174
 
        for (i=0; i<len; i++)
175
 
        for (j=0; j<len; j++)
176
 
        {
177
 
                *bufp++ = bg >> 16;
178
 
                *bufp++ = (bg >> 8) & 0xFF;
179
 
                *bufp++ = bg & 0xFF;
180
 
        }
181
 
        rgbmap_ball_gen_real (len, rgbbuf, fg, rad, grad, len/2, len/2);
182
 
}
183
 
 
184
 
//! Same as rgbmap_ball_gen but don't generate the background - i.e, overlay the ball on the existing image
185
 
void rgbmap_ball_gen_nobg (int len, unsigned char *rgbbuf, int fg, int bg, float rad, float grad)
186
 
{
187
 
        rgbmap_ball_gen_real (len, rgbbuf, fg, rad, grad, len/2, len/2);
188
 
}
189
 
 
190
 
void rgbmap_ball_shadow_gen (int len, unsigned char *rgbbuf, int fg, int bg, float rad, float grad, int shadowlen)
191
 
{
192
 
        int i, j;
193
 
        unsigned char *bufp = rgbbuf;
194
 
        int red, green, blue, lighting_color, shadow_color;
195
 
        float shadow_factor = 0.25;
196
 
        red   = (fg >> 16) / 4 + 192;
197
 
        green = ((fg >>  8) & 0xff) / 4 + 192;
198
 
        blue  = (fg & 0xff) / 4 + 192;
199
 
        lighting_color = (red << 16) + (green << 8) + blue;
200
 
        red   = (fg >> 16) * shadow_factor;
201
 
        green = ((fg >>  8) & 0xff) * shadow_factor;
202
 
        blue  = (fg & 0xff) * shadow_factor;
203
 
        shadow_color = (red << 16) + (green << 8) + blue;
204
 
        for (i=0; i<len; i++)
205
 
        for (j=0; j<len; j++)
206
 
        {
207
 
                *bufp++ = bg >> 16;
208
 
                *bufp++ = (bg >> 8) & 0xFF;
209
 
                *bufp++ = bg & 0xFF;
210
 
        }
211
 
        rgbmap_ball_gen_real (len, rgbbuf, shadow_color, rad, grad, 
212
 
                        (len + shadowlen)/2, (len + shadowlen)/2);
213
 
        rgbmap_ball_gen_real (len, rgbbuf, lighting_color, rad, grad, 
214
 
                        (len - 1.5 * shadowlen)/2, (len - 1.5 * shadowlen)/2);
215
 
        rgbmap_ball_gen_real (len, rgbbuf, fg, rad, grad, 
216
 
                        (len - shadowlen)/2, (len - shadowlen)/2);
217
 
}
218
 
 
219
 
        
220
 
//! Used if you already have the pixmap and only want to generate the header (i.e, same shape, different color)
221
 
char ** pixmap_header_gen(int len, char *pixbuf, int fg, int bg)
222
 
{
223
 
        char **map = pixmap_map;
224
 
        char *buf = pixbuf;
225
 
        static char colbuf[18][20];
226
 
        int i, j;
227
 
        for(i=0; i<18; i++) map[i] = colbuf[i];
228
 
        g_snprintf(map[0], 20, "%d %d 16 1", len, len);
229
 
        for(i=0; i<16; i++)
230
 
        {
231
 
                g_snprintf (map[i+1], 20, "%c c #%s", hex[i],
232
 
                                pixmap_get_hex_color(fg, bg, i / 15.0));
233
 
        }
234
 
        for (i=0; i<len; i++)
235
 
                map[i+17] = buf + i * (len+1);
236
 
        map[17+len] = NULL;
237
 
        return map;
238
 
}
239
 
 
240
 
 
241
 
//! Generates dice.
242
 
/** Args same as pixmap_ball_gen() except num which is the number on the die */
243
 
char ** pixmap_die_gen(int len, char *pixbuf, int fg, int bg, float rad, float grad, int num)
244
 
{
245
 
        char **map = pixmap_map;
246
 
        char *buf = pixbuf;
247
 
        static char colbuf[18][20];
248
 
        int i, j, k;
249
 
        float cenx[6][6] = 
250
 
        {
251
 
                { 0.5 },
252
 
                { 0.25, 0.75},
253
 
                { 0.2, 0.5, 0.8 },
254
 
                { 0.25, 0.25, 0.75, 0.75 },
255
 
                { 0.2, 0.2, 0.5, 0.8, 0.8 },
256
 
                { 0.25, 0.25, 0.25, 0.75, 0.75, 0.75 }
257
 
        };
258
 
        float ceny[6][6] = 
259
 
        {
260
 
                { 0.5 },
261
 
                { 0.25, 0.75},
262
 
                { 0.2, 0.5, 0.8 },
263
 
                { 0.25, 0.75, 0.25, 0.75 },
264
 
                { 0.2, 0.8, 0.5, 0.2, 0.8 },
265
 
                { 0.2, 0.5, 0.8, 0.2, 0.5, 0.8 }
266
 
        };
267
 
        for(i=0; i<18; i++) map[i] = colbuf[i];
268
 
        g_snprintf(map[0], 20, "%d %d 16 1", len, len);
269
 
        for(i=0; i<16; i++)
270
 
        {
271
 
                g_snprintf (map[i+1], 20, "%c c #%s", hex[i],
272
 
                                pixmap_get_hex_color(fg, bg, i / 15.0));
273
 
        }
274
 
        for (i=0; i<len; i++)
275
 
        {
276
 
                char *ptr = map[i+17] = buf + i * (len+1);
277
 
                memset (ptr, 'F', len);
278
 
                ptr[len] = 0;
279
 
                for (k=0; k<num; k++)
280
 
                {
281
 
                        float midx = cenx[num-1][k] * len;
282
 
                        float midy = (1 - ceny[num-1][k]) * len;
283
 
                        for (j=0; j<len; j++)
284
 
                        {
285
 
                                float x = i - midx, y = j - midy;
286
 
                                float q = (x * x + y * y) / rad / rad;
287
 
                                int  k;
288
 
                                if (q < 1) {ptr[j] = '0';}
289
 
                                else
290
 
                                for(k=1; k<16; k++)
291
 
                                        if ((q >= 1 + (k-1) / grad && q < 1 + k/grad))
292
 
                                        { ptr[j] = hex[k]; break; }
293
 
                        }
294
 
                }
295
 
        }
296
 
        map[17+len] = NULL;
297
 
        return map;
298
 
}
299