File: fading_effect.h

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (498 lines) | stat: -rw-r--r-- 13,703 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A ARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU GeneralPublic License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * fading effect largely copy/paste from SDL_Gfx
 *****************************************************************************/

#ifndef FADING_EFFECT_H
#define FADING_EFFECT_H

#include <SDL_gfxPrimitives.h>
#include <SDL.h>

/* ----- Defines for pixel clipping tests */

#define clip_xmin(surface) surface->clip_rect.x
#define clip_xmax(surface) surface->clip_rect.x+surface->clip_rect.w-1
#define clip_ymin(surface) surface->clip_rect.y
#define clip_ymax(surface) surface->clip_rect.y+surface->clip_rect.h-1

/* AA Line */

#define AAlevels 256
#define AAbits 8

#define CLIP_LEFT_EDGE   0x1
#define CLIP_RIGHT_EDGE  0x2
#define CLIP_BOTTOM_EDGE 0x4
#define CLIP_TOP_EDGE    0x8
#define CLIP_INSIDE(a)   (!a)
#define CLIP_REJECT(a,b) (a&b)
#define CLIP_ACCEPT(a,b) (!(a|b))

int clipEncode(Sint16 x, Sint16 y, Sint16 left, Sint16 top, Sint16 right, Sint16 bottom)
{
  int code = 0;

  if (x < left) {
    code |= CLIP_LEFT_EDGE;
  } else if (x > right) {
    code |= CLIP_RIGHT_EDGE;
  }
  if (y < top) {
    code |= CLIP_TOP_EDGE;
  } else if (y > bottom) {
    code |= CLIP_BOTTOM_EDGE;
  }
  return code;
}

int clipLine(SDL_Surface * dst, Sint16 * x1, Sint16 * y1, Sint16 * x2, Sint16 * y2)
{
  Sint16 left, right, top, bottom;
  int code1, code2;
  int draw = 0;
  Sint16 swaptmp;
  float m;

  /*
   * Get clipping boundary
   */
  left = dst->clip_rect.x;
  right = dst->clip_rect.x + dst->clip_rect.w - 1;
  top = dst->clip_rect.y;
  bottom = dst->clip_rect.y + dst->clip_rect.h - 1;

  while (1) {
    code1 = clipEncode(*x1, *y1, left, top, right, bottom);
    code2 = clipEncode(*x2, *y2, left, top, right, bottom);
    if (CLIP_ACCEPT(code1, code2)) {
      draw = 1;
      break;
    } else if (CLIP_REJECT(code1, code2))
      break;
    else {
      if (CLIP_INSIDE(code1)) {
        swaptmp = *x2;
        *x2 = *x1;
        *x1 = swaptmp;
        swaptmp = *y2;
        *y2 = *y1;
        *y1 = swaptmp;
        swaptmp = code2;
        code2 = code1;
        code1 = swaptmp;
      }
      if (*x2 != *x1) {
        m = (*y2 - *y1) / (float) (*x2 - *x1);
      } else {
        m = 1.0f;
      }
      if (code1 & CLIP_LEFT_EDGE) {
        *y1 += (Sint16) ((int)((left - *x1) * m));
        *x1 = left;
      } else if (code1 & CLIP_RIGHT_EDGE) {
        *y1 += (Sint16) ((int)((right - *x1) * m));
        *x1 = right;
      } else if (code1 & CLIP_BOTTOM_EDGE) {
        if (*x2 != *x1) {
          *x1 += (Sint16) ((int)((bottom - *y1) / m));
        }
        *y1 = bottom;
      } else if (code1 & CLIP_TOP_EDGE) {
        if (*x2 != *x1) {
          *x1 += (Sint16) ((int)((top - *y1) / m));
        }
        *y1 = top;
      }
    }
  }

  return draw;
}

inline int _putPixelAlpha(SDL_Surface * surface, Sint16 x, Sint16 y, Uint32 color, Uint8 alpha)
{
  SDL_PixelFormat *fmt = surface->format;
  Uint32 Rmask = fmt->Rmask, Gmask = fmt->Gmask, Bmask = fmt->Bmask, Amask = fmt->Amask;
  Uint32 R, G, B, A = 0;

  if (x >= clip_xmin(surface) && x <= clip_xmax(surface)
    && y >= clip_ymin(surface) && y <= clip_ymax(surface)) {

    switch (fmt->BytesPerPixel) {
    case 2:        /* Probably 15-bpp or 16-bpp */
      if (alpha == 255) {
        *((Uint16 *) surface->pixels + y * surface->pitch / 2 + x) = color;
      } else {
        Uint16 *pixel = (Uint16 *) surface->pixels + y * surface->pitch / 2 + x;
        Uint32 dc = *pixel;

        R = ((dc & Rmask) + (((color & Rmask) - (dc & Rmask)) * alpha >> 8)) & Rmask;
        G = ((dc & Gmask) + (((color & Gmask) - (dc & Gmask)) * alpha >> 8)) & Gmask;
        B = ((dc & Bmask) + (((color & Bmask) - (dc & Bmask)) * alpha >> 8)) & Bmask;
        if (Amask)
          A = ((dc & Amask) + (((color & Amask) - (dc & Amask)) * alpha >> 8)) & Amask;

        *pixel = R | G | B | A;
      }
      break;

    case 3:        /* Slow 24-bpp mode, usually not used */
      {
        Uint8 *pix = (Uint8 *) surface->pixels + y * surface->pitch + x * 3;
        Uint8 rshift8 = fmt->Rshift / 8;
        Uint8 gshift8 = fmt->Gshift / 8;
        Uint8 bshift8 = fmt->Bshift / 8;
        Uint8 ashift8 = fmt->Ashift / 8;


        if (alpha == 255) {
          *(pix + rshift8) = color >> fmt->Rshift;
          *(pix + gshift8) = color >> fmt->Gshift;
          *(pix + bshift8) = color >> fmt->Bshift;
          *(pix + ashift8) = color >> fmt->Ashift;
        } else {
          Uint8 dR, dG, dB, dA = 0;
          Uint8 sR, sG, sB, sA = 0;

          pix = (Uint8 *) surface->pixels + y * surface->pitch + x * 3;

          dR = *((pix) + rshift8);
          dG = *((pix) + gshift8);
          dB = *((pix) + bshift8);
          dA = *((pix) + ashift8);

          sR = (color >> fmt->Rshift) & 0xff;
          sG = (color >> fmt->Gshift) & 0xff;
          sB = (color >> fmt->Bshift) & 0xff;
          sA = (color >> fmt->Ashift) & 0xff;

          dR = dR + ((sR - dR) * alpha >> 8);
          dG = dG + ((sG - dG) * alpha >> 8);
          dB = dB + ((sB - dB) * alpha >> 8);
          dA = dA + ((sA - dA) * alpha >> 8);

          *((pix) + rshift8) = dR;
          *((pix) + gshift8) = dG;
          *((pix) + bshift8) = dB;
          *((pix) + ashift8) = dA;
        }
      }
      break;

    case 4:        /* Probably 32-bpp */
      if (alpha == 255) {
        *((Uint32 *) surface->pixels + y * surface->pitch / 4 + x) = color;
      } else {
        Uint32 *pixel = (Uint32 *) surface->pixels + y * surface->pitch / 4 + x;
        Uint32 dc = *pixel;

        R = ((dc & Rmask) + (((color & Rmask) - (dc & Rmask)) * alpha >> 8)) & Rmask;
        G = ((dc & Gmask) + (((color & Gmask) - (dc & Gmask)) * alpha >> 8)) & Gmask;
        B = ((dc & Bmask) + (((color & Bmask) - (dc & Bmask)) * alpha >> 8)) & Bmask;
        if (Amask)
          A = ((dc & Amask) + (((color & Amask) - (dc & Amask)) * alpha >> 8)) & Amask;

        *pixel = R | G | B | A;
      }
      break;
    }
  }

  return (0);
}

inline int pixelColorNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color)
{
  Uint8 alpha;
  Uint32 mcolor;
  int result = 0;

  /*
   * Setup color
   */
  alpha = color & 0x000000ff;
  mcolor =
    SDL_MapRGBA(dst->format, (color & 0xff000000) >> 24,
                (color & 0x00ff0000) >> 16, (color & 0x0000ff00) >> 8, alpha);

  /*
   * Draw
   */
  result = _putPixelAlpha(dst, x, y, mcolor, alpha);

  return result;
}

inline int pixelColorWeightNolock(SDL_Surface * dst, Sint16 x, Sint16 y, Uint32 color, Uint32 weight)
{
  /* Get alpha */
  Uint32 a = color & 0x000000ffU;

  /*
   * Modify Alpha by weight
   */
  a = (a * weight) >> 8;

  return pixelColorNolock(dst, x, y, (color & 0xffffff00U) | a);
}

inline int interpolateInt(int start, int stop, float step)
{
  float diff = stop - start;
  return start + (int)(diff * step);
}

inline Uint32 interpolateColor(Uint32 color1, Uint32 color2, float step)
{
  int c1 = interpolateInt(color1 & 0xFF000000, color2 & 0xFF000000, step) & 0xFF000000;
  int c2 = interpolateInt(color1 & 0x00FF0000, color2 & 0x00FF0000, step) & 0x00FF0000;
  int c3 = interpolateInt(color1 & 0x0000FF00, color2 & 0x0000FF00, step) & 0x0000FF00;
  int c4 = interpolateInt(color1 & 0x000000FF, color2 & 0x000000FF, step) & 0x000000FF;
  return (Uint32)(c1 | c2 | c3 | c4);
}

int aafadingLineColorInt(SDL_Surface * dst, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2,
                         Uint32 color1, Uint32 color2, int draw_endpoint)
{
  Sint32 xx0, yy0, xx1, yy1;
  int result;
  int dx, dy, tmp, xdir;
  Uint32 color = color1;
  float step;

  if (y1 == y2) {
    /* Horizontal line */
    dx = x2-x1;
    xdir = 1;
    if (dx == 0) {
      return 0;
    }
    if (dx < 0) {
      dx = -dx;
      xdir = -1;
    }
    step = dx;
    while (--dx) {
      color = interpolateColor(color2, color1, dx / step);
      result |= pixelColorNolock(dst, x1, y1, color);
      x1 += xdir;
    }
  }
  if (y1 > y2) {
    color = color1;
    color1 = color2;
    color2 = color;
  }

  /*
   * Clip line and test if we have to draw
   */
  if (!(clipLine(dst, &x1, &y1, &x2, &y2))) {
    return (0);
  }

  /*
   * Keep on working with 32bit numbers
   */
  xx0 = x1;
  yy0 = y1;
  xx1 = x2;
  yy1 = y2;

  /*
   * Reorder points if required
   */
  if (yy0 > yy1) {
    tmp = yy0;
    yy0 = yy1;
    yy1 = tmp;
    tmp = xx0;
    xx0 = xx1;
    xx1 = tmp;
  }

  /*
   * Calculate distance
   */
  dx = xx1 - xx0;
  dy = yy1 - yy0;

  /*
   * Adjust for negative dx and set xdir
   */
  if (dx >= 0) {
    xdir = 1;
  } else {
    xdir = -1;
    dx = (-dx);
  }

  /* Lock surface */
  if (SDL_MUSTLOCK(dst)) {
    if (SDL_LockSurface(dst) < 0) {
      return (-1);
    }
  }

  /*
   * Draw the initial pixel in the foreground color
   */
  result = pixelColorNolock(dst, x1, y1, color1);

  /*
   * Check for special cases
   */
  if (dx == 0 && dy == 0) {
    /* nothing to do */
  } else if (dx == 0) {
    /* Vertical line */
    step = dy;
    while (--dy) {
      color = interpolateColor(color2, color1, dy / step);
      result |= pixelColorNolock(dst, x1, ++yy0, color);
    }
  } else if (dy == 0) {
  } else if (dx == dy) {
    /* Diagonal line */
    step = dx;
    while (--dx) {
      color = interpolateColor(color2, color1, dx / step);
      result |= pixelColorNolock(dst, xx0, ++yy0, color);
      xx0 += xdir;
    }
  } else {
    Uint32 erracc = 0; /* Zero accumulator */
    Uint32 intshift = 32 - AAbits; /* # of bits by which to shift erracc to get intensity level */
    Uint32 erradj;
    Uint32 erracctmp, wgt;

    /*
     * x-major or y-major?
     */
    if (dy > dx) {

      /*
       * y-major.  Calculate 16-bit fixed point fractional part of a
       * pixel that X advances every time Y advances 1 pixel, truncating
       * the result so that we won't overrun the endpoint along the
       * X axis
       */
      /*
       * Not-so-portable version:
       * erradj = ((Uint64)dx << 32) / (Uint64)dy;
       */
      erradj = ((dx << 16) / dy) << 16;

      /*
       * draw all pixels other than the first and last
       */
      int x0pxdir = xx0 + xdir;
      step = dy;
      while (--dy) {
        color = interpolateColor(color2, color1, dy / step);
        erracctmp = erracc;
        erracc += erradj;
        if (erracc <= erracctmp) {
          /*
           * rollover in error accumulator, x coord advances
           */
          xx0 = x0pxdir;
          x0pxdir += xdir;
        }
        yy0++;              /* y-major so always advance Y */

        /*
         * the AAbits most significant bits of erracc give us the
         * intensity weighting for this pixel, and the complement of
         * the weighting for the paired pixel.
         */
        wgt = (erracc >> intshift) & 255;
        result |= pixelColorWeightNolock (dst, xx0, yy0, color, 255 - wgt);
        result |= pixelColorWeightNolock (dst, x0pxdir, yy0, color, wgt);
      }

    } else {

      /*
       * x-major line.  Calculate 16-bit fixed-point fractional part of
       * a pixel that Y advances each time X advances 1 pixel truncating
       * the result so that we won't overrun the endpoint along the
       * X axis.
       */
      /*
       * Not-so-portable version:
       * erradj = ((Uint64)dy << 32) / (Uint64)dx;
       */
      erradj = ((dy << 16) / dx) << 16;

      /*
       * draw all pixels other than the first and last
       */
      int y0p1 = yy0 + 1;
      step = dx;
      while (--dx) {
        color = interpolateColor(color2, color1, dx / step);
        erracctmp = erracc;
        erracc += erradj;
        if (erracc <= erracctmp) {
          /*
           * Accumulator turned over, advance y
           */
          yy0 = y0p1;
          y0p1++;
        }
        xx0 += xdir;        /* x-major so always advance X */
        /*
         * the AAbits most significant bits of erracc give us the
         * intensity weighting for this pixel, and the complement of
         * the weighting for the paired pixel.
         */
        wgt = (erracc >> intshift) & 255;
        result |= pixelColorWeightNolock (dst, xx0, yy0, color, 255 - wgt);
        result |= pixelColorWeightNolock (dst, xx0, y0p1, color, wgt);
      }
    }
  }
  /*
   * Do we have to draw the endpoint
   */
  if (draw_endpoint) {
    /*
     * Draw final pixel, always exactly intersected by the line and doesn't
     * need to be weighted.
     */
    result |= pixelColorNolock (dst, x2, y2, color2);
  }

  /* Unlock surface */
  if (SDL_MUSTLOCK(dst)) {
    SDL_UnlockSurface(dst);
  }

  return (result);
}

int aafadingLineColor(SDL_Surface * dst, Sint16 x1, Sint16 y1,
                      Sint16 x2, Sint16 y2, Uint32 color1, Uint32 color2)
{
  return aafadingLineColorInt(dst, x1, y1, x2, y2, color1, color2, 1);
}

#endif /* FADING_EFFECT_H */