Debian Codegraph @

lib/Games/FrozenBubble/CStuff.xs

raw
1 /*******************************************************************************
2 *
3 * Copyright (c) 2001 - 2010 Guillaume Cottenceau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2, as
7 * published by the Free Software Foundation.
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-1307, USA.
17 *
18 ******************************************************************************/
19
20 /* for some reason sys/time.h has to be included before perl-core headers */
21 /* otherwise Win32 users will experience troubles */
22 #include <sys/time.h>
23
24 #include "EXTERN.h"
25 #include "perl.h"
26 #include "XSUB.h"
27 #include <iconv.h>
28 #include <math.h>
29 #include <unistd.h>
30
31
32 #include <SDL/SDL.h>
33 #include <SDL/SDL_mixer.h>
34
35 const int XRES = 640;
36 const int YRES = 480;
37
38 int x, y;
39 int i, j;
40
41 const int Rdec = 0;
42 const int Gdec = 1;
43 const int Bdec = 2;
44 const int Adec = 3;
45
46 const int ANIM_SPEED = 20;
47 Uint32 ticks;
48 Uint32 to_wait;
49
50 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
51
52 void set_pixel(SDL_Surface * s, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a) // only 32bit surfaces yet
53 {
54 ((Uint32 *)s->pixels)[x + y * s->w] = (((r >> s->format->Rloss) << s->format->Rshift) & s->format->Rmask)
55 | (((g >> s->format->Gloss) << s->format->Gshift) & s->format->Gmask)
56 | (((b >> s->format->Bloss) << s->format->Bshift) & s->format->Bmask)
57 | (((a >> s->format->Aloss) << s->format->Ashift) & s->format->Amask);
58 }
59
60 void get_pixel(SDL_Surface * s, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
61 {
62 SDL_GetRGBA(((Uint32 *)s->pixels)[CLAMP(x, 0, s->w) + CLAMP(y, 0, s->h) * s->w], s->format, r, g, b, a);
63 }
64
65 void myLockSurface(SDL_Surface * s)
66 {
67 while (SDL_MUSTLOCK(s) == 1 && SDL_LockSurface(s) < 0)
68 SDL_Delay(10);
69 }
70 void myUnlockSurface(SDL_Surface * s)
71 {
72 if (SDL_MUSTLOCK(s))
73 SDL_UnlockSurface(s);
74 }
75 void synchro_before(SDL_Surface * s)
76 {
77 ticks = SDL_GetTicks();
78 myLockSurface(s);
79 }
80 void synchro_after(SDL_Surface * s)
81 {
82 myUnlockSurface(s);
83 SDL_Flip(s);
84 to_wait = SDL_GetTicks() - ticks;
85 if (to_wait < ANIM_SPEED) {
86 SDL_Delay(ANIM_SPEED - to_wait);
87 }
88 // else { printf("slow (%d)", ANIM_SPEED - to_wait); }
89 }
90 void fb__out_of_memory(void)
91 {
92 fprintf(stderr, "**ERROR** Out of memory\n");
93 abort();
94 }
95
96 int rand_(double val) { return 1+(int) (val*rand()/(RAND_MAX+1.0)); }
97
98
99 /************************** Graphical effects ****************************/
100
101 /*
102 * Features:
103 *
104 * - plasma-ordered fill (with top-bottom and/or left-right mirrored plasma's)
105 * - random points
106 * - horizontal blinds
107 * - vertical blinds
108 * - center=>edge circle
109 * - up=>down bars
110 * - top-left=>bottom-right squares
111 *
112 */
113
114 /* -------------- Double Store ------------------ */
115 void copy_line(int l, SDL_Surface * s, SDL_Surface * img) {
116 memcpy(s->pixels + l*img->pitch, img->pixels + l*img->pitch, img->pitch);
117 }
118 void copy_column(int c, SDL_Surface *s, SDL_Surface * img) {
119 int bpp = img->format->BytesPerPixel;
120 for (y=0; y<YRES; y++)
121 memcpy(s->pixels + y*img->pitch + c*bpp, img->pixels + y*img->pitch + c*bpp, bpp);
122 }
123
124 void store_effect(SDL_Surface * s, SDL_Surface * img)
125 {
126
127 int step = 0;
128 int store_thickness = 15;
129
130 if (rand_(2) == 1) {
131 while (step < YRES/2/store_thickness + store_thickness) {
132
133 synchro_before(s);
134
135 for (i=0; i<=YRES/2/store_thickness; i++) {
136 int v = step - i;
137 if (v >= 0 && v < store_thickness) {
138 copy_line(i*store_thickness + v, s, img);
139 copy_line(YRES - 1 - (i*store_thickness + v), s, img);
140 }
141 }
142 step++;
143
144 synchro_after(s);
145 }
146 }
147 else {
148 while (step < XRES/2/store_thickness + store_thickness) {
149
150 synchro_before(s);
151
152 for (i=0; i<=XRES/2/store_thickness; i++) {
153 int v = step - i;
154 if (v >= 0 && v < store_thickness) {
155 copy_column(i*store_thickness + v, s, img );
156 copy_column(XRES - 1 - (i*store_thickness + v), s, img);
157 }
158 }
159 step++;
160
161 synchro_after(s);
162 }
163 }
164 }
165
166
167 /* -------------- Bars ------------------ */
168
169 void bars_effect(SDL_Surface * s, SDL_Surface * img)
170 {
171 int bpp = img->format->BytesPerPixel;
172 const int bars_max_steps = 40;
173 const int bars_num = 16;
174 for (i=0; i<bars_max_steps; i++)
175 {
176 synchro_before(s);
177 for (y=0; y<YRES/bars_max_steps; y++)
178 {
179 int y_ = (i*YRES/bars_max_steps + y) * img->pitch;
180 int y__ = (YRES - 1 - (i*YRES/bars_max_steps + y)) * img->pitch;
181 for (j=0; j<bars_num/2; j++)
182 {
183 int x_ = (j*2) * (XRES/bars_num) * bpp;
184 int x__ = (j*2+1) * (XRES/bars_num) * bpp;
185 memcpy(s->pixels + y_ + x_, img->pixels + y_ + x_, (XRES/bars_num) * bpp);
186 memcpy(s->pixels + y__ + x__, img->pixels + y__ + x__, (XRES/bars_num) * bpp);
187 }
188 }
189 synchro_after(s);
190 }
191 }
192
193
194 /* -------------- Squares ------------------ */
195 int fillrect(int i, int j, SDL_Surface * s, SDL_Surface * img,int bpp, const int squares_size)
196 {
197 int c, v;
198 if (i >= XRES/squares_size || j >= YRES/squares_size)
199 return 0;
200 v = i*squares_size*bpp + j*squares_size*img->pitch;
201 for (c=0; c<squares_size; c++)
202 memcpy(s->pixels + v + c*img->pitch, img->pixels + v + c*img->pitch, squares_size*bpp);
203 return 1;
204 }
205
206
207 void squares_effect(SDL_Surface * s, SDL_Surface * img)
208 {
209 int bpp = img->format->BytesPerPixel;
210 const int squares_size = 32;
211
212 int still_moving = 1;
213
214 for (i=0; still_moving; i++) {
215 int k = 0;
216
217 synchro_before(s);
218
219 still_moving = 0;
220 for (j=i; j>=0; j--) {
221 if (fillrect(j, k, s ,img, bpp, squares_size))
222 still_moving = 1;
223 k++;
224 }
225
226 synchro_after(s);
227 }
228 }
229
230
231 /* -------------- Circle ------------------ */
232
233 int * circle_steps;
234 const int circle_max_steps = 40;
235 int sqr(int v) { return v*v; }
236
237 void circle_init(void)
238 {
239
240 circle_steps = malloc(XRES * YRES * sizeof(int));
241 if (!circle_steps)
242 fb__out_of_memory();
243
244 for (y=0; y<YRES; y++)
245 for (x=0; x<XRES; x++) {
246 int max = sqrt(sqr(XRES/2) + sqr(YRES/2));
247 int value = sqrt(sqr(x-XRES/2) + sqr(y-YRES/2));
248 circle_steps[x+y*XRES] = (max-value)*circle_max_steps/max;
249 }
250 }
251
252 void circle_effect(SDL_Surface * s, SDL_Surface * img)
253 {
254 int step = circle_max_steps;
255 int bpp = img->format->BytesPerPixel;
256 int in_or_out = rand_(2);
257
258 while (step >= 0) {
259
260 synchro_before(s);
261
262 for (y=0; y<YRES; y++) {
263 void* src_line = img->pixels + y*img->pitch;
264 void* dest_line = s->pixels + y*img->pitch;
265 for (x=0; x<XRES; x++)
266 if (in_or_out == 1) {
267 if (circle_steps[x+y*XRES] == step)
268 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
269 } else {
270 if (circle_steps[x+y*XRES] == circle_max_steps - step)
271 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
272 }
273 }
274 step--;
275
276 synchro_after(s);
277 }
278
279 }
280
281
282 /* -------------- Plasma ------------------ */
283
284 unsigned char * plasma, * plasma2, * plasma3;
285 int plasma_max;
286 const int plasma_steps = 40;
287 void plasma_init(char * datapath)
288 {
289 char * finalpath;
290 char mypath[] = "/data/plasma.raw";
291 FILE * f;
292 finalpath = malloc(strlen(datapath) + sizeof(mypath) + 1);
293 if (!finalpath)
294 fb__out_of_memory();
295 sprintf(finalpath, "%s%s", datapath, mypath);
296 f = fopen(finalpath, "rb");
297 free(finalpath);
298
299 if (!f) {
300 fprintf(stderr, "Ouch, could not open plasma.raw for reading\n");
301 exit(1);
302 }
303
304 plasma = malloc(XRES * YRES);
305 if (!plasma)
306 fb__out_of_memory();
307 if (fread(plasma, 1, XRES * YRES, f) != XRES * YRES) {
308 fprintf(stderr, "Ouch, could not read %d bytes from plasma file\n", XRES * YRES);
309 exit(1);
310 }
311
312 fclose(f);
313
314 plasma_max = -1;
315 for (x=0; x<XRES; x++)
316 for (y=0; y<YRES; y++)
317 if (plasma[x+y*XRES] > plasma_max)
318 plasma_max = plasma[x+y*XRES];
319
320 for (y=0; y<YRES; y++)
321 for (x=0; x<XRES; x++)
322 plasma[x+y*XRES] = (plasma[x+y*XRES]*plasma_steps)/(plasma_max+1);
323
324
325 plasma2 = malloc(XRES * YRES);
326 if (!plasma2)
327 fb__out_of_memory();
328 for (i=0; i<XRES*YRES; i++)
329 plasma2[i] = rand_(256) - 1;
330
331 for (y=0; y<YRES; y++)
332 for (x=0; x<XRES; x++)
333 plasma2[x+y*XRES] = (plasma2[x+y*XRES]*plasma_steps)/256;
334
335 plasma3 = malloc(XRES * YRES);
336 if (!plasma3)
337 fb__out_of_memory();
338 }
339
340 void plasma_effect(SDL_Surface * s, SDL_Surface * img)
341 {
342 int step = 0;
343 int bpp = img->format->BytesPerPixel;
344 int rnd_plasma = rand_(4);
345
346 int plasma_type;
347 if (!img->format->palette) {
348 plasma_type = rand_(3);
349 } else {
350 plasma_type = rand_(2);
351 }
352
353 if (plasma_type == 3) {
354 int int_or_out = rand_(2);
355 // pixel brightness
356 for (y=0; y<YRES; y++)
357 for (x=0; x<XRES; x++) {
358 Uint32 pixelvalue = 0;
359 float r, g, b;
360 memcpy(&pixelvalue, img->pixels + y*img->pitch + x*bpp, bpp);
361 r = ( (float) ( ( pixelvalue & img->format->Rmask ) >> img->format->Rshift ) ) / ( img->format->Rmask >> img->format->Rshift );
362 g = ( (float) ( ( pixelvalue & img->format->Gmask ) >> img->format->Gshift ) ) / ( img->format->Gmask >> img->format->Gshift );
363 b = ( (float) ( ( pixelvalue & img->format->Bmask ) >> img->format->Bshift ) ) / ( img->format->Bmask >> img->format->Bshift );
364 plasma3[x+y*XRES] = 255 * ( r * .299 + g * .587 + b * .114 ) * plasma_steps / 256;
365 if (int_or_out == 1)
366 plasma3[x+y*XRES] = plasma_steps - 1 - plasma3[x+y*XRES];
367 }
368 }
369
370 while (step < plasma_steps) {
371
372 synchro_before(s);
373
374 if (plasma_type == 1) {
375 // with plasma file
376 /* I need to un-factorize the 'plasma' call in order to let gcc optimize (tested!) */
377 for (y=0; y<YRES; y++) {
378 void* src_line = img->pixels + y*img->pitch;
379 void* dest_line = s->pixels + y*img->pitch;
380 if (rnd_plasma == 1) {
381 for (x=0; x<XRES; x++)
382 if (plasma[x+y*XRES] == step)
383 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
384 }
385 else if (rnd_plasma == 2) {
386 for (x=0; x<XRES; x++)
387 if (plasma[(XRES-1-x)+y*XRES] == step)
388 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
389 }
390 else if (rnd_plasma == 3) {
391 for (x=0; x<XRES; x++)
392 if (plasma[x+(YRES-1-y)*XRES] == step)
393 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
394 }
395 else {
396 for (x=0; x<XRES; x++)
397 if (plasma[(XRES-1-x)+(YRES-1-y)*XRES] == step)
398 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
399 }
400 }
401 } else {
402 // random points or brightness
403 unsigned char* p = plasma_type == 2 ? plasma2 : plasma3;
404 for (y=0; y<YRES; y++) {
405 void* src_line = img->pixels + y*img->pitch;
406 void* dest_line = s->pixels + y*img->pitch;
407 for (x=0; x<XRES; x++)
408 if (p[x+y*XRES] == step)
409 memcpy(dest_line + x*bpp, src_line + x*bpp, bpp);
410 }
411 }
412
413 step++;
414
415 synchro_after(s);
416 }
417 }
418
419 void shrink_(SDL_Surface * dest, SDL_Surface * orig, int xpos, int ypos, SDL_Rect * orig_rect, int factor)
420 {
421 int bpp = dest->format->BytesPerPixel;
422 int rx = orig_rect->x / factor;
423 int rw = orig_rect->w / factor;
424 int ry = orig_rect->y / factor;
425 int rh = orig_rect->h / factor;
426 xpos -= rx;
427 ypos -= ry;
428 myLockSurface(orig);
429 myLockSurface(dest);
430 for (x=rx; x<rx+rw; x++) {
431 for (y=ry; y<ry+rh; y++) {
432 if (!dest->format->palette) {
433 /* there is no palette, it's cool, I can do (uber-slow) high-quality shrink */
434 int r = 0;
435 int g = 0;
436 int b = 0;
437 int a = 0;
438 Uint8 r_, g_, b_ , a_;
439 for (i=0; i<factor; i++) {
440 for (j=0; j<factor; j++) {
441 SDL_GetRGBA(((Uint32 *)orig->pixels)[CLAMP(x*factor+i, 0, orig->w) + CLAMP(y*factor+j, 0, orig->h) * orig->w],
442 orig->format, &r_, &g_, &b_, &a_);
443 r += r_;
444 g += g_;
445 b += b_;
446 a += a_;
447 }
448 }
449 r /= factor*factor;
450 g /= factor*factor;
451 b /= factor*factor;
452 a /= factor*factor;
453 set_pixel(dest, CLAMP(xpos+x, 0, dest->w), CLAMP(ypos+y, 0, dest->h), (Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a);
454 }/* else {
455 // there is a palette... I don't care of the bloody oldskoolers who still use
456 // 8-bit displays & al, they can suffer and die ;p
457 memcpy(dest->pixels + (xpos+x)*bpp + (ypos+y)*dest->pitch,
458 orig->pixels + (x*factor)*bpp + (y*factor)*orig->pitch, bpp);
459 }*/
460 }
461 }
462 myUnlockSurface(orig);
463 myUnlockSurface(dest);
464 }
465
466 void rotate_nearest_(SDL_Surface * dest, SDL_Surface * orig, double angle)
467 {
468 int bpp = dest->format->BytesPerPixel;
469 int x_, y_;
470 double cosval = cos(angle);
471 double sinval = sin(angle);
472 if (orig->format->BytesPerPixel != dest->format->BytesPerPixel) {
473 fprintf(stderr, "rotate_nearest: orig and dest surface must be of equal bpp\n");
474 abort();
475 }
476 myLockSurface(orig);
477 myLockSurface(dest);
478 for (x = 0; x < dest->w; x++) {
479 for (y = 0; y < dest->h; y++) {
480 x_ = (x - dest->w/2)*cosval - (y - dest->h/2)*sinval + dest->w/2;
481 y_ = (y - dest->h/2)*cosval + (x - dest->w/2)*sinval + dest->h/2;
482 if (x_ < 0 || x_ > dest->w - 2 || y_ < 0 || y_ > dest->h - 2) {
483 *( (Uint32*) ( dest->pixels + x*bpp + y*dest->pitch ) ) = orig->format->Amask;
484 continue;
485 }
486 memcpy(dest->pixels + x*bpp + y*dest->pitch,
487 orig->pixels + x_*bpp + y_*orig->pitch, bpp);
488 }
489 }
490 myUnlockSurface(orig);
491 myUnlockSurface(dest);
492 }
493
494 #define getr(pixeladdr) ( *( ( (Uint8*) pixeladdr ) + Rdec ) )
495 #define getg(pixeladdr) ( *( ( (Uint8*) pixeladdr ) + Gdec ) )
496 #define getb(pixeladdr) ( *( ( (Uint8*) pixeladdr ) + Bdec ) )
497 #define geta(pixeladdr) ( *( ( (Uint8*) pixeladdr ) + Adec ) )
498
499 void rotate_bilinear_(SDL_Surface * dest, SDL_Surface * orig, double angle)
500 {
501 int Bpp = dest->format->BytesPerPixel;
502 int x_, y_;
503 int r, g, b;
504 double a;
505 double dx, dy;
506 double cosval = cos(angle);
507 double sinval = sin(angle);
508 Uint8 Ar, Ag, Ab, Aa, Br, Bg, Bb, Ba, Cr, Cg, Cb, Ca, Dr, Dg, Db, Da;
509 if (orig->format->BytesPerPixel == 1)
510 {
511 fprintf(stderr, "rotate_bilinear: orig surface must not have a palette\n");
512 abort();
513 }
514 if (dest->format->BytesPerPixel == 1)
515 {
516 fprintf(stderr, "rotate_bilinear: dest surface must not have a palette\n");
517 abort();
518 }
519 myLockSurface(orig);
520 myLockSurface(dest);
521 for (y = 0; y < dest->h; y++)
522 {
523 double x__ = - dest->w/2*cosval - (y - dest->h/2)*sinval + dest->w/2;
524 double y__ = (y - dest->h/2)*cosval - dest->w/2*sinval + dest->h/2;
525 for (x = 0; x < dest->w; x++)
526 {
527 Uint32 *A, *B, *C, *D;
528 x_ = floor(x__);
529 y_ = floor(y__);
530 if (x_ < 0 || x_ > orig->w - 2 || y_ < 0 || y_ > orig->h - 2)
531 {
532 // out of band
533 set_pixel(dest, x, y, 0, 0, 0, 0);
534 }
535 else
536 {
537 dx = x__ - x_;
538 dy = y__ - y_;
539 get_pixel(orig, x_, y_, &Ar, &Ag, &Ab, &Aa);
540 get_pixel(orig, x_ + 1, y_, &Br, &Bg, &Bb, &Ba);
541 get_pixel(orig, x_, y_ + 1, &Cr, &Cg, &Cb, &Ca);
542 get_pixel(orig, x_ + 1, y_ + 1, &Dr, &Dg, &Db, &Da);
543 a = (Aa * ( 1 - dx ) + Ba * dx) * ( 1 - dy ) + (Ca * ( 1 - dx ) + Da * dx) * dy;
544 if (a == 0)
545 {
546 // fully transparent, no use working
547 r = g = b = 0;
548 }
549 else if (a == 255)
550 {
551 // fully opaque, optimized
552 r = (Ar * ( 1 - dx ) + Br * dx) * ( 1 - dy ) + (Cr * ( 1 - dx ) + Dr * dx) * dy;
553 g = (Ag * ( 1 - dx ) + Bg * dx) * ( 1 - dy ) + (Cg * ( 1 - dx ) + Dg * dx) * dy;
554 b = (Ab * ( 1 - dx ) + Bb * dx) * ( 1 - dy ) + (Cb * ( 1 - dx ) + Db * dx) * dy;
555 }
556 else
557 {
558 // not fully opaque, means A B C or D was not fully opaque, need to weight channels with
559 r = ( (Ar * Aa * ( 1 - dx ) + Br * Ba * dx) * ( 1 - dy ) + (Cr * Ca * ( 1 - dx ) + Dr * Da * dx) * dy ) / a;
560 g = ( (Ag * Aa * ( 1 - dx ) + Bg * Ba * dx) * ( 1 - dy ) + (Cg * Ca * ( 1 - dx ) + Dg * Da * dx) * dy ) / a;
561 b = ( (Ab * Aa * ( 1 - dx ) + Bb * Ba * dx) * ( 1 - dy ) + (Cb * Ca * ( 1 - dx ) + Db * Da * dx) * dy ) / a;
562 }
563 set_pixel(dest, x, y, r, g, b, a);
564 }
565 x__ += cosval;
566 y__ += sinval;
567 }
568 }
569 myUnlockSurface(orig);
570 myUnlockSurface(dest);
571 }
572
573 /* assumes the surface is not totally transparent */
574 AV* autopseudocrop_(SDL_Surface * orig)
575 {
576 int x_ = -1, y_ = -1, w = -1, h = -1;
577 Uint8 *ptr;
578 int Adec = orig->format->Ashift / 8; // Adec is non standard from sdlpango_draw* output
579 AV* ret;
580 if (orig->format->BytesPerPixel != 4) {
581 fprintf(stderr, "autocrop: orig surface must be 32bpp\n");
582 abort();
583 }
584 myLockSurface(orig);
585 y = 0;
586 while (y_ == -1) {
587 ptr = orig->pixels + y*orig->pitch;
588 for (x = 0; x < orig->w; x++) {
589 if (*(ptr+Adec) != 0) {
590 y_ = y;
591 break;
592 }
593 ptr += 4;
594 }
595 y++;
596 }
597 y = orig->h - 1;
598 while (h == -1) {
599 ptr = orig->pixels + y*orig->pitch;
600 for (x = 0; x < orig->w; x++) {
601 if (*(ptr+Adec) != 0) {
602 h = y - y_ + 1;
603 break;
604 }
605 ptr += 4;
606 }
607 y--;
608 }
609 x = 0;
610 while (x_ == -1) {
611 ptr = orig->pixels + x*4;
612 for (y = 0; y < orig->h; y++) {
613 if (*(ptr+Adec) != 0) {
614 x_ = x;
615 break;
616 }
617 ptr += orig->pitch;
618 }
619 x++;
620 }
621 x = orig->w - 1;
622 while (w == -1) {
623 ptr = orig->pixels + x*4;
624 for (y = 0; y < orig->h; y++) {
625 if (*(ptr+Adec) != 0) {
626 w = x - x_ + 1;
627 break;
628 }
629 ptr += orig->pitch;
630 }
631 x--;
632 }
633 myUnlockSurface(orig);
634 ret = newAV();
635 av_push(ret, newSViv(x_));
636 av_push(ret, newSViv(y_));
637 av_push(ret, newSViv(w));
638 av_push(ret, newSViv(h));
639 return ret;
640 }
641
642 /* access interleaved pixels */
643 #define CUBIC_ROW(dx, row) transform_cubic(dx, (row)[0], (row)[4], (row)[8], (row)[12])
644
645 #define CUBIC_SCALED_ROW(dx, row, arow) transform_cubic(dx, (arow)[0] * (row)[0], (arow)[4] * (row)[4], (arow)[8] * (row)[8], (arow)[12] * (row)[12])
646
647 static inline double
648 transform_cubic(double dx, int jm1, int j, int jp1, int jp2)
649 {
650 // http://news.povray.org/povray.binaries.tutorials/attachment/%[email protected]%3E/Splines.bas.txt
651 // Catmull-Rom yields the best results
652 return ((( ( - jm1 + 3 * j - 3 * jp1 + jp2 ) * dx +
653 ( 2 * jm1 - 5 * j + 4 * jp1 - jp2 ) ) * dx +
654 ( - jm1 + jp1 ) ) * dx +
655 ( 2 * j ) ) / 2.0;
656 }
657
658 void rotate_bicubic_(SDL_Surface * dest, SDL_Surface * orig, double angle)
659 {
660 Uint8 r, g, b, a;
661 Uint8 r_ [16], g_ [16], b_ [16], a_ [16];
662 int x_, y_;
663 double cosval = cos(angle);
664 double sinval = sin(angle);
665 double a_val, a_recip;
666 double dx, dy;
667 if (orig->format->BytesPerPixel == 1)
668 {
669 fprintf(stderr, "rotate_bicubic: orig surface must not have a palette\n");
670 abort();
671 }
672 if (dest->format->BytesPerPixel == 1)
673 {
674 fprintf(stderr, "rotate_bicubic: orig surface must not have a palette\n");
675 abort();
676 }
677 myLockSurface(orig);
678 myLockSurface(dest);
679 for (y = 0; y < dest->h; y++)
680 {
681 double x__ = - dest->w/2*cosval - (y - dest->h/2)*sinval + dest->w/2 - 1;
682 double y__ = (y - dest->h/2)*cosval - dest->w/2*sinval + dest->h/2 - 1;
683 for (x = 0; x < dest->w; x++)
684 {
685 x_ = floor(x__);
686 y_ = floor(y__);
687 if (x_ < 0 || x_ > orig->w - 4 || y_ < 0 || y_ > orig->h - 4)
688 {
689 set_pixel(dest, x, y, 0, 0, 0, 0);
690 }
691 else
692 {
693 /* matrix of 4 * 4 pixels
694 00 01 02 03
695 04 05 06 07
696 08 09 10 11
697 12 13 14 15
698 */
699 int x___, y___, i = 0;
700 for(y___ = y_; y___ < y_ + 4; y___++)
701 {
702 for(x___ = x_; x___ < x_ + 4; x___++)
703 {
704 SDL_GetRGBA(((Uint32 *)orig->pixels)[CLAMP(x___, 0, dest->w) + CLAMP(y___, 0, dest->h) * dest->w],
705 orig->format, &(r_[i]), &(g_[i]), &(b_[i]), &(a_[i]));
706 i++;
707 }
708 }
709
710 /* the fractional error */
711 dx = x__ - x_;
712 dy = y__ - y_;
713 /* calculate alpha of result */
714 a_val = transform_cubic(dy,
715 transform_cubic(dx, a_[ 0], a_[ 1], a_[ 2], a_[ 3]),
716 transform_cubic(dx, a_[ 4], a_[ 5], a_[ 6], a_[ 7]),
717 transform_cubic(dx, a_[ 8], a_[ 9], a_[10], a_[11]),
718 transform_cubic(dx, a_[12], a_[13], a_[14], a_[15]));
719 if (a_val <= 0.0) {
720 a_recip = 0.0;
721 a = 0;
722 } else if (a_val > 255.0) {
723 a_recip = 1.0 / a_val;
724 a = 255;
725 } else {
726 a_recip = 1.0 / a_val;
727 a = (int) a_val;
728 }
729
730 /* for RGB, result = bicubic (c * alpha) / bicubic (alpha) */
731 r = CLAMP(a_recip * transform_cubic(dy,
732 transform_cubic(dx, r_[ 0] * a_[ 0], r_[ 1] * a_[ 1], r_[ 2] * a_[ 2], r_[ 3] * a_[ 3]),
733 transform_cubic(dx, r_[ 4] * a_[ 4], r_[ 5] * a_[ 5], r_[ 6] * a_[ 6], r_[ 7] * a_[ 7]),
734 transform_cubic(dx, r_[ 8] * a_[ 8], r_[ 9] * a_[ 9], r_[10] * a_[10], r_[11] * a_[11]),
735 transform_cubic(dx, r_[12] * a_[12], r_[13] * a_[13], r_[14] * a_[14], r_[15] * a_[15])), 0, 255);
736 g = CLAMP(a_recip * transform_cubic(dy,
737 transform_cubic(dx, g_[ 0] * a_[ 0], g_[ 1] * a_[ 1], g_[ 2] * a_[ 2], g_[ 3] * a_[ 3]),
738 transform_cubic(dx, g_[ 4] * a_[ 4], g_[ 5] * a_[ 5], g_[ 6] * a_[ 6], g_[ 7] * a_[ 7]),
739 transform_cubic(dx, g_[ 8] * a_[ 8], g_[ 9] * a_[ 9], g_[10] * a_[10], g_[11] * a_[11]),
740 transform_cubic(dx, g_[12] * a_[12], g_[13] * a_[13], g_[14] * a_[14], g_[15] * a_[15])), 0, 255);
741 b = CLAMP(a_recip * transform_cubic(dy,
742 transform_cubic(dx, b_[ 0] * a_[ 0], b_[ 1] * a_[ 1], b_[ 2] * a_[ 2], b_[ 3] * a_[ 3]),
743 transform_cubic(dx, b_[ 4] * a_[ 4], b_[ 5] * a_[ 5], b_[ 6] * a_[ 6], b_[ 7] * a_[ 7]),
744 transform_cubic(dx, b_[ 8] * a_[ 8], b_[ 9] * a_[ 9], b_[10] * a_[10], b_[11] * a_[11]),
745 transform_cubic(dx, b_[12] * a_[12], b_[13] * a_[13], b_[14] * a_[14], b_[15] * a_[15])), 0, 255);
746 set_pixel(dest, x, y, r, g, b, a);
747 }
748 x__ += cosval;
749 y__ += sinval;
750 }
751 }
752 myUnlockSurface(orig);
753 myUnlockSurface(dest);
754 }
755
756 void flipflop_(SDL_Surface * dest, SDL_Surface * orig, int offset)
757 {
758 int Bpp = dest->format->BytesPerPixel;
759 int r, g, b;
760 double a, dx;
761
762 if (orig->format->BytesPerPixel == 1)
763 {
764 fprintf(stderr, "flipflop: orig surface must not have a palette\n");
765 abort();
766 }
767
768 if (dest->format->BytesPerPixel == 1)
769 {
770 fprintf(stderr, "flipflop: orig surface must not have a palette\n");
771 abort();
772 }
773
774 myLockSurface(orig);
775 myLockSurface(dest);
776 for (x = 0; x < dest->w; x++)
777 {
778 double sinval = sin((2*x+offset)/50.0)*5;
779 double shading = 1.1 + cos((2*x+offset)/50.0) / 10; // based on sinval derivative
780 double x__ = x + sinval;
781 int x_ = floor(x__);
782 for (y = 0; y < dest->h; y++)
783 {
784 Uint32 *A, *B;
785 if (x_ < 0 || x_ > orig->w - 2)
786 {
787 // out of band
788 set_pixel(dest, x, y, 0, 0, 0, 0);
789 }
790 else
791 {
792 dx = x__ - x_; // (mono)linear filtering
793 A = orig->pixels + x_*Bpp + y*orig->pitch;
794 B = orig->pixels + (x_+1)*Bpp + y*orig->pitch;
795 a = geta(A) * ( 1 - dx ) + geta(B) * dx;
796 if (a == 0)
797 {
798 // fully transparent, no use working
799 r = g = b = 0;
800 }
801 else if (a == 255)
802 {
803 // fully opaque, optimized
804 r = getr(A) * ( 1 - dx ) + getr(B) * dx;
805 g = getg(A) * ( 1 - dx ) + getg(B) * dx;
806 b = getb(A) * ( 1 - dx ) + getb(B) * dx;
807 }
808 else
809 {
810 // not fully opaque, means A B C or D was not fully opaque, need to weight channels with
811 r = (getr(A) * geta(A) * ( 1 - dx ) + getr(B) * geta(B) * dx) / a;
812 g = (getg(A) * geta(A) * ( 1 - dx ) + getg(B) * geta(B) * dx) / a;
813 b = (getb(A) * geta(A) * ( 1 - dx ) + getb(B) * geta(B) * dx) / a;
814 }
815 set_pixel(dest, x, y, CLAMP(r * shading, 0, 255), CLAMP(g * shading, 0, 255), CLAMP(b * shading, 0, 255), a);
816 }
817 }
818 }
819 myUnlockSurface(orig);
820 myUnlockSurface(dest);
821 }
822 float sqr_fb(float a) { return a*a; }
823
824 // moves an highlighted spot over the surface (shape of ellypse, depending on width and hight)
825 void enlighten_(SDL_Surface * dest, SDL_Surface * orig, int offset)
826 {
827 Uint8 r, g, b, a;
828 int lightx, lighty;
829 double sqdistbase, sqdist, shading;
830
831 if (orig->format->BytesPerPixel == 1)
832 {
833 fprintf(stderr, "enlighten: orig surface must not have a palette\n");
834 abort();
835 }
836
837 if (dest->format->BytesPerPixel == 1)
838 {
839 fprintf(stderr, "enlighten: dest surface must not have a palette\n");
840 abort();
841 }
842
843 myLockSurface(orig);
844 myLockSurface(dest);
845 lightx = dest->w / (2.5 + 0.3 * sin((double)offset / 500)) * sin((double)offset / 100) + dest->w / 2;
846 lighty = dest->h / (2.5 + 0.3 * cos((double)offset / 500)) * cos((double)offset / 100) + dest->h / 2 + 10;
847
848 for (y = 0; y < dest->h; y++)
849 {
850 sqdistbase = sqr_fb(y - lighty) - 3;
851
852 if (y == lighty) sqdistbase -= 4;
853
854 for (x = 0; x < dest->w; x++)
855 {
856 sqdist = sqdistbase + sqr_fb(x - lightx);
857
858 if (x == lightx) sqdist -= 2;
859
860 shading = sqdist <= 0
861 ? 50
862 : 1 + 20 / sqdist;
863
864 SDL_GetRGBA(((Uint32 *)orig->pixels)[x + y * dest->w], orig->format, &r, &g, &b, &a);
865
866 if (shading > 1.02)
867 set_pixel(dest, x, y, CLAMP(r * shading, 0, 255), CLAMP(g * shading, 0, 255), CLAMP(b * shading, 0, 255), a);
868 else
869 set_pixel(dest, x, y, r, g, b, a);
870 }
871 }
872 myUnlockSurface(orig);
873 myUnlockSurface(dest);
874 }
875
876 void stretch_(SDL_Surface * dest, SDL_Surface * orig, int offset)
877 {
878 int Bpp = dest->format->BytesPerPixel;
879 int x_, y_;
880 int r, g, b;
881 Uint8 Ar, Ag, Ab, Aa, Br, Bg, Bb, Ba, Cr, Cg, Cb, Ca, Dr, Dg, Db, Da;
882 double a, dx, dy;
883 double sinval = sin(offset / 50.0) / 10 + 1;
884
885 if (orig->format->BytesPerPixel == 1)
886 {
887 fprintf(stderr, "stretch: orig surface must not have a palette\n");
888 abort();
889 }
890
891 if (dest->format->BytesPerPixel == 1)
892 {
893 fprintf(stderr, "stretch: dest surface must not have a palette\n");
894 abort();
895 }
896
897 myLockSurface(orig);
898 myLockSurface(dest);
899 for (x = 0; x < dest->w; x++)
900 {
901 double x__ = (x - dest->w/2) * sinval + dest->w/2;
902 double cosfory = - sin(offset/50.0) * cos(M_PI*(x - dest->w/2)/dest->w) / sinval / 8 + 1;
903 for (y = 0; y < dest->h; y++)
904 {
905 //Uint32 *A, *B, *C, *D;
906 double y__ = (y - dest->h/2) * cosfory + dest->h/2;
907 x_ = floor(x__);
908 y_ = floor(y__);
909 if (x_ < 0 || x_ > orig->w - 2 || y_ < 0 || y_ > orig->h - 2)
910 {
911 // out of band
912 set_pixel(dest, x, y, 0, 0, 0, 0);
913 }
914 else
915 {
916 dx = x__ - x_;
917 dy = y__ - y_;
918 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + y_ * dest->w], orig->format, &Ar, &Ag, &Ab, &Aa);
919 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + 1 + y_ * dest->w], orig->format, &Br, &Bg, &Bb, &Ba);
920 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + (y_ + 1) * dest->w], orig->format, &Cr, &Cg, &Cb, &Ca);
921 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + 1 + (y_ + 1) * dest->w], orig->format, &Dr, &Dg, &Db, &Da);
922
923 a = (Aa * ( 1 - dx ) + Ba * dx) * ( 1 - dy ) + (Ca * ( 1 - dx ) + Da * dx) * dy;
924 if (a == 0)
925 {
926 // fully transparent, no use working
927 r = g = b = 0;
928 }
929 else if (a == 255)
930 {
931 // fully opaque, optimized
932 r = (Ar * ( 1 - dx ) + Br * dx) * ( 1 - dy ) + (Cr * ( 1 - dx ) + Dr * dx) * dy;
933 g = (Ag * ( 1 - dx ) + Bg * dx) * ( 1 - dy ) + (Cg * ( 1 - dx ) + Dg * dx) * dy;
934 b = (Ab * ( 1 - dx ) + Bb * dx) * ( 1 - dy ) + (Cb * ( 1 - dx ) + Db * dx) * dy;
935 }
936 else
937 {
938 // not fully opaque, means A B C or D was not fully opaque, need to weight channels with
939 r = ( (Ar * Aa * ( 1 - dx ) + Br * Ba * dx) * ( 1 - dy ) + (Cr * Ca * ( 1 - dx ) + Dr * Da * dx) * dy ) / a;
940 g = ( (Ag * Aa * ( 1 - dx ) + Bg * Ba * dx) * ( 1 - dy ) + (Cg * Ca * ( 1 - dx ) + Dg * Da * dx) * dy ) / a;
941 b = ( (Ab * Aa * ( 1 - dx ) + Bb * Ba * dx) * ( 1 - dy ) + (Cb * Ca * ( 1 - dx ) + Db * Da * dx) * dy ) / a;
942 }
943 set_pixel(dest, x, y, r, g, b, a);
944 }
945 }
946 }
947 myUnlockSurface(orig);
948 myUnlockSurface(dest);
949 }
950
951 void tilt_(SDL_Surface * dest, SDL_Surface * orig, int offset)
952 {
953 int Bpp = dest->format->BytesPerPixel;
954 int x_, y_;
955 int r, g, b;
956 Uint8 Ar, Ag, Ab, Aa, Br, Bg, Bb, Ba, Cr, Cg, Cb, Ca, Dr, Dg, Db, Da;
957 double a, dx, dy;
958 double shading;
959 if (orig->format->BytesPerPixel == 1)
960 {
961 fprintf(stderr, "tilt: orig surface must not have a palette\n");
962 abort();
963 }
964
965 if (dest->format->BytesPerPixel == 1)
966 {
967 fprintf(stderr, "tilt: orig surface must not have a palette\n");
968 abort();
969 }
970
971 myLockSurface(orig);
972 myLockSurface(dest);
973 shading = 1 - sin(offset/40.0)/10; // shade as if a lightsource was on the left
974 for (x = 0; x < dest->w; x++)
975 {
976 double zoomfact = 1 + (x - dest->w/2) * sin(offset/40.0) / dest->w / 5;
977 double x__ = (x - dest->w/2) * zoomfact + dest->w/2;
978 for (y = 0; y < dest->h; y++)
979 {
980 double y__ = (y - dest->h/2) * zoomfact + dest->h/2;
981 x_ = floor(x__);
982 y_ = floor(y__);
983 if (x_ < 0 || x_ > orig->w - 2 || y_ < 0 || y_ > orig->h - 2)
984 {
985 // out of band
986 set_pixel(dest, x, y, 0, 0, 0, 0);
987 }
988 else
989 {
990 dx = x__ - x_;
991 dy = y__ - y_;
992 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + y_ * dest->w], orig->format, &Ar, &Ag, &Ab, &Aa);
993 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + 1 + y_ * dest->w], orig->format, &Br, &Bg, &Bb, &Ba);
994 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + (y_ + 1) * dest->w], orig->format, &Cr, &Cg, &Cb, &Ca);
995 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + 1 + (y_ + 1) * dest->w], orig->format, &Dr, &Dg, &Db, &Da);
996
997 a = (Aa * ( 1 - dx ) + Ba * dx) * ( 1 - dy ) + (Ca * ( 1 - dx ) + Da * dx) * dy;
998 if (a == 0)
999 {
1000 // fully transparent, no use working
1001 r = g = b = 0;
1002 }
1003 else if (a == 255)
1004 {
1005 // fully opaque, optimized
1006 r = (Ar * ( 1 - dx ) + Br * dx) * ( 1 - dy ) + (Cr * ( 1 - dx ) + Dr * dx) * dy;
1007 g = (Ag * ( 1 - dx ) + Bg * dx) * ( 1 - dy ) + (Cg * ( 1 - dx ) + Dg * dx) * dy;
1008 b = (Ab * ( 1 - dx ) + Bb * dx) * ( 1 - dy ) + (Cb * ( 1 - dx ) + Db * dx) * dy;
1009 }
1010 else
1011 {
1012 // not fully opaque, means A B C or D was not fully opaque, need to weight channels with
1013 r = ( (Ar * Aa * ( 1 - dx ) + Br * Ba * dx) * ( 1 - dy ) + (Cr * Ca * ( 1 - dx ) + Dr * Da * dx) * dy ) / a;
1014 g = ( (Ag * Aa * ( 1 - dx ) + Bg * Ba * dx) * ( 1 - dy ) + (Cg * Ca * ( 1 - dx ) + Dg * Da * dx) * dy ) / a;
1015 b = ( (Ab * Aa * ( 1 - dx ) + Bb * Ba * dx) * ( 1 - dy ) + (Cb * Ca * ( 1 - dx ) + Db * Da * dx) * dy ) / a;
1016 }
1017 set_pixel(dest, x, y, CLAMP(r*shading, 0, 255), CLAMP(g*shading, 0, 255), CLAMP(b*shading, 0, 255), a);
1018 }
1019 }
1020 }
1021 myUnlockSurface(orig);
1022 myUnlockSurface(dest);
1023 }
1024
1025 struct point { double x; double y; double angle; };
1026
1027 #ifndef MAX
1028 #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
1029 #endif
1030
1031 #ifndef MIN
1032 #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
1033 #endif
1034
1035 void points_(SDL_Surface * dest, SDL_Surface * orig, SDL_Surface * mask)
1036 {
1037 static struct point * points = NULL;
1038 int i, amount = 200;
1039 Uint8 r, g, b, a;
1040 if (orig->format->BytesPerPixel == 1) {
1041 fprintf(stderr, "points: orig surface must not have a palette\n");
1042 abort();
1043 }
1044 if (dest->format->BytesPerPixel == 1) {
1045 fprintf(stderr, "points: dest surface must not have a palette\n");
1046 abort();
1047 }
1048 if (mask->format->BytesPerPixel == 1) {
1049 fprintf(stderr, "points: mask surface must not have a palette\n");
1050 abort();
1051 }
1052 if (points == NULL) {
1053 points = malloc(sizeof(struct point) * amount);
1054 if (!points)
1055 fb__out_of_memory();
1056 for (i = 0; i < amount; i++) {
1057 while (1) {
1058 points[i].x = rand_(dest->w/2) + dest->w/4;
1059 points[i].y = rand_(dest->h/2) + dest->h/4;
1060 SDL_GetRGBA(((Uint32 *)mask->pixels)[CLAMP((int)points[i].x, 0, mask->w) + CLAMP((int)points[i].y, 0, mask->h) * mask->w],
1061 mask->format, &r, &g, &b, &a);
1062 if(r == 255 && g == 255 && b == 255)
1063 break;
1064 }
1065 points[i].angle = 2 * M_PI * rand() / RAND_MAX;
1066 }
1067 }
1068 myLockSurface(orig);
1069 myLockSurface(mask);
1070 myLockSurface(dest);
1071 for (x = 0; x < dest->w; x++) {
1072 for (y = 0; y < dest->h; y++) {
1073 SDL_GetRGBA(((Uint32 *)orig->pixels)[CLAMP(x, 0, orig->w) + CLAMP(y, 0, orig->h) * orig->w], orig->format, &r, &g, &b, &a);
1074 set_pixel(dest, x, y, r, g, b, a);
1075 }
1076 }
1077 for (i = 0; i < amount; i++) {
1078 double angle_distance = 0;
1079 set_pixel(dest, CLAMP((int)points[i].x, 0, dest->w), CLAMP((int)points[i].y, 0, dest->h), 0xFF, 0xCC, 0xCC, 0xCC);
1080 points[i].x += cos(points[i].angle);
1081 points[i].y += sin(points[i].angle);
1082 SDL_GetRGBA(((Uint32 *)mask->pixels)[CLAMP((int)points[i].x, 0, mask->w) + CLAMP((int)points[i].y, 0, mask->h) * mask->w],
1083 mask->format, &r, &g, &b, &a);
1084 if(r != 255 || g != 255 || b != 255) {
1085 // get back on track
1086 points[i].x -= cos(points[i].angle);
1087 points[i].y -= sin(points[i].angle);
1088 while (1) {
1089 angle_distance += 2 * M_PI / 100;
1090 points[i].x += cos(points[i].angle + angle_distance);
1091 points[i].y += sin(points[i].angle + angle_distance);
1092 SDL_GetRGBA(((Uint32 *)mask->pixels)[CLAMP((int)points[i].x, 0, mask->w) + CLAMP((int)points[i].y, 0, mask->h) * mask->w],
1093 mask->format, &r, &g, &b, &a);
1094 if(r == 255 && g == 255 && b == 255) {
1095 points[i].angle += angle_distance;
1096 break;
1097 }
1098 points[i].x -= cos(points[i].angle + angle_distance);
1099 points[i].y -= sin(points[i].angle + angle_distance);
1100 points[i].x += cos(points[i].angle - angle_distance);
1101 points[i].y += sin(points[i].angle - angle_distance);
1102 SDL_GetRGBA(((Uint32 *)mask->pixels)[CLAMP((int)points[i].x, 0, mask->w) + CLAMP((int)points[i].y, 0, mask->h) * mask->w],
1103 mask->format, &r, &g, &b, &a);
1104 if(r == 255 && g == 255 && b == 255) {
1105 points[i].angle -= angle_distance;
1106 break;
1107 }
1108 points[i].x -= cos(points[i].angle - angle_distance);
1109 points[i].y -= sin(points[i].angle - angle_distance);
1110 }
1111 }
1112 }
1113 myUnlockSurface(orig);
1114 myUnlockSurface(mask);
1115 myUnlockSurface(dest);
1116 }
1117
1118 void waterize_(SDL_Surface * dest, SDL_Surface * orig, int offset)
1119 {
1120 int Bpp = dest->format->BytesPerPixel;
1121 int x_, y_;
1122 Uint8 Ar, Ag, Ab, Aa, Br, Bg, Bb, Ba, Cr, Cg, Cb, Ca, Dr, Dg, Db, Da;
1123 int r, g, b;
1124 double a, dx, dy;
1125 static double * precalc_cos = NULL, * precalc_sin = NULL;
1126
1127 if (orig->format->BytesPerPixel != 4)
1128 {
1129 fprintf(stderr, "waterize: orig surface must be 32bpp\n");
1130 abort();
1131 }
1132
1133 if (dest->format->BytesPerPixel != 4)
1134 {
1135 fprintf(stderr, "waterize: dest surface must be 32bpp\n");
1136 abort();
1137 }
1138
1139 if (precalc_cos == NULL) // this precalc nearly suppresses the x__ and y__ processing overhead in innerloop
1140 {
1141 int i;
1142 precalc_cos = malloc(200*sizeof(double));
1143 precalc_sin = malloc(200*sizeof(double));
1144 for (i = 0; i < 200; i++)
1145 {
1146 precalc_cos[i] = cos(i*2*M_PI/200.0) * 2;
1147 precalc_sin[i] = sin(i*2*M_PI/150.0) * 2;
1148 }
1149 }
1150
1151 myLockSurface(orig);
1152 myLockSurface(dest);
1153
1154 for (x = 0; x < dest->w; x++)
1155 {
1156 for (y = 0; y < dest->h; y++)
1157 {
1158 double x__ = x + precalc_cos[(x + y + offset ) % 200];
1159 double y__ = y + precalc_sin[(x + y + offset ) % 150];
1160 x_ = floor(x__);
1161 y_ = floor(y__);
1162
1163 if (x_ < 0 || x_ > orig->w - 2 || y_ < 0 || y_ > orig->h - 2)
1164 {
1165 // out of band
1166 set_pixel(dest, x, y, 0, 0, 0, 0);
1167 }
1168 else
1169 {
1170 dx = x__ - x_;
1171 dy = y__ - y_;
1172 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + y_ * dest->w], orig->format, &Ar, &Ag, &Ab, &Aa);
1173 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + 1 + y_ * dest->w], orig->format, &Br, &Bg, &Bb, &Ba);
1174 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + (y_ + 1) * dest->w], orig->format, &Cr, &Cg, &Cb, &Ca);
1175 SDL_GetRGBA(((Uint32 *)orig->pixels)[x_ + 1 + (y_ + 1) * dest->w], orig->format, &Dr, &Dg, &Db, &Da);
1176
1177 a = (Aa * ( 1 - dx ) + Ba * dx) * ( 1 - dy ) + (Ca * ( 1 - dx ) + Da * dx) * dy;
1178 if (a == 0)
1179 {
1180 // fully transparent, no use working
1181 r = g = b = 0;
1182 }
1183 else if (a == 255)
1184 {
1185 // fully opaque, optimized
1186 r = (Ar * ( 1 - dx ) + Br * dx) * ( 1 - dy ) + (Cr * ( 1 - dx ) + Dr * dx) * dy;
1187 g = (Ag * ( 1 - dx ) + Bg * dx) * ( 1 - dy ) + (Cg * ( 1 - dx ) + Dg * dx) * dy;
1188 b = (Ab * ( 1 - dx ) + Bb * dx) * ( 1 - dy ) + (Cb * ( 1 - dx ) + Db * dx) * dy;
1189 }
1190 else
1191 {
1192 // not fully opaque, means A B C or D was not fully opaque, need to weight channels with
1193 r = ( (Ar * Aa * ( 1 - dx ) + Br * Ba * dx) * ( 1 - dy ) + (Cr * Ca * ( 1 - dx ) + Dr * Da * dx) * dy ) / a;
1194 g = ( (Ag * Aa * ( 1 - dx ) + Bg * Ba * dx) * ( 1 - dy ) + (Cg * Ca * ( 1 - dx ) + Dg * Da * dx) * dy ) / a;
1195 b = ( (Ab * Aa * ( 1 - dx ) + Bb * Ba * dx) * ( 1 - dy ) + (Cb * Ca * ( 1 - dx ) + Db * Da * dx) * dy ) / a;
1196 }
1197 set_pixel(dest, x, y, r, g, b, a);
1198 }
1199 }
1200 }
1201 myUnlockSurface(orig);
1202 myUnlockSurface(dest);
1203 }
1204
1205 void brokentv_(SDL_Surface * dest, SDL_Surface * orig, int offset)
1206 {
1207 Uint8 r, g, b, a;
1208 double throughness, throughness_base = 0.9 + cos(offset/50.0)*0.1;
1209 static int pixelize = 0;
1210
1211 if (pixelize == 0)
1212 {
1213 if (rand_(100) == 1)
1214 pixelize = 15 + 5*cos(offset);
1215 }
1216 else
1217 {
1218 pixelize--;
1219 }
1220
1221 if (orig->format->palette) {
1222 fprintf(stderr, "brokentv: orig surface must not have a palette\n");
1223 abort();
1224 }
1225
1226 if (dest->format->palette) {
1227 fprintf(stderr, "brokentv: dest surface must not have a palette\n");
1228 abort();
1229 }
1230
1231 myLockSurface(orig);
1232 myLockSurface(dest);
1233 for (y = 0; y < dest->h; y++)
1234 {
1235 throughness = CLAMP(sin(y/(12.0+2*sin(offset/50.0))+offset/10.0+sin(offset/100.0)*5) > 0 ? throughness_base : throughness_base + cos(offset/30.0)*0.2, 0, 1);
1236 for (x = 0; x < dest->w; x++)
1237 {
1238 SDL_GetRGBA(((Uint32 *)orig->pixels)[x + y * orig->w], orig->format, &r, &g, &b, &a);
1239
1240 if (pixelize)
1241 throughness = 0.2 + rand_(100)/100.0;
1242 set_pixel(dest, x, y, r, g, b, a * throughness);
1243 }
1244 }
1245 myUnlockSurface(orig);
1246 myUnlockSurface(dest);
1247 }
1248
1249 SV* utf8key_(SDL_Event * e) {
1250 iconv_t cd;
1251 char source[2];
1252 SV* retval = NULL;
1253 source[0] = e->key.keysym.unicode & 0xFF;
1254 source[1] = ( e->key.keysym.unicode & 0xFF00 ) >> 8;
1255 cd = iconv_open("UTF-8", "UTF-16LE");
1256 if (cd != (iconv_t) (-1)) {
1257 // an utf8 char is maximum 4 bytes long
1258 char dest[5];
1259 char *src = source;
1260 char *dst = dest;
1261 size_t source_len = 2;
1262 size_t dest_len = 4;
1263 bzero(dest, 5);
1264 if ((iconv(cd, &src, &source_len, &dst, &dest_len)) != (size_t) (-1)) {
1265 *dst = 0;
1266 retval = newSVpv(dest, 0);
1267 }
1268 iconv_close(cd);
1269 } else {
1270 fprintf(stderr, "**ERROR** iconv_open failed!\n");
1271 }
1272 return retval;
1273 }
1274
1275 void alphaize_(SDL_Surface * surf)
1276 {
1277 myLockSurface(surf);
1278 for (y=0; y<surf->h; y++)
1279 for (x=0; x<surf->w; x++) {
1280 Uint32 pixelvalue = 0;
1281 int a;
1282 memcpy(&pixelvalue, surf->pixels + y*surf->pitch + x*surf->format->BytesPerPixel, surf->format->BytesPerPixel);
1283 a = ( ( pixelvalue & surf->format->Amask ) >> surf->format->Ashift ) / 2;
1284 pixelvalue = ( pixelvalue & (~ surf->format->Amask ) ) + ( a << surf->format->Ashift );
1285 memcpy(surf->pixels + y*surf->pitch + x*surf->format->BytesPerPixel, &pixelvalue, surf->format->BytesPerPixel);
1286 }
1287 myUnlockSurface(surf);
1288 }
1289
1290 void pixelize_(SDL_Surface * dest, SDL_Surface * orig)
1291 {
1292 Uint8 r, g, b, a;
1293
1294 if (orig->format->palette)
1295 {
1296 fprintf(stderr, "pixelize: orig surface must not have a palette\n");
1297 abort();
1298 }
1299
1300 if (dest->format->palette)
1301 {
1302 fprintf(stderr, "pixelize: orig surface must not have a palette\n");
1303 abort();
1304 }
1305
1306 myLockSurface(orig);
1307 myLockSurface(dest);
1308 for (y = 0; y < dest->h; y++)
1309 {
1310 for (x = 0; x < dest->w; x++)
1311 {
1312 SDL_GetRGBA(((Uint32 *)orig->pixels)[x + y * orig->w], orig->format, &r, &g, &b, &a);
1313 set_pixel(dest, x, y, r, g, b, a * ( 0.2 + rand_(100)/100.0 ));
1314 }
1315 }
1316 myUnlockSurface(orig);
1317 myUnlockSurface(dest);
1318 }
1319
1320 void blacken_(SDL_Surface * surf, int step)
1321 {
1322 Uint32 pixelvalue; /* this should also be okay for 16-bit and 24-bit formats */
1323 //Uint8 r, g, b, a;
1324 int r, g, b;
1325 if (surf->format->palette) {
1326 /* there is a palette... I don't care of the bloody oldskoolers who still use
1327 8-bit displays & al, they can suffer and die ;p */
1328 return;
1329 }
1330 myLockSurface(surf);
1331 for (y=(step-1)*surf->h/70; y<step*surf->h/70; y++) {
1332 bzero(surf->pixels + y*surf->pitch, surf->format->BytesPerPixel * XRES);
1333 bzero(surf->pixels + (YRES-1-y)*surf->pitch, surf->format->BytesPerPixel * XRES);
1334 //set_pixel(surf, x, y, 0, 0, 0, 0);
1335 //set_pixel(surf, x, surf->h - y, 0, 0, 0, 0);
1336 }
1337 for (y=step*surf->h/70; y<(step+8)*surf->h/70 && y<surf->h; y++)
1338 for (x=0; x<surf->w; x++)
1339 {
1340
1341 //SDL_GetRGBA(((Uint32 *)surf->pixels)[x + y * surf->w], surf->format, &r, &g, &b, &a);
1342 //set_pixel(surf, x, y, r*3/4, g*3/4, b*3/4, a);
1343
1344 //SDL_GetRGBA(((Uint32 *)surf->pixels)[x + (surf->h-y-1) * surf->w], surf->format, &r, &g, &b, &a);
1345 //set_pixel(surf, x, (surf->h-y-1), r*3/4, g*3/4, b*3/4, a);
1346
1347 memcpy(&pixelvalue, surf->pixels + y*surf->pitch + x*surf->format->BytesPerPixel, surf->format->BytesPerPixel);
1348 r = ( ((pixelvalue & surf->format->Rmask) >> surf->format->Rshift))*3/4;
1349 g = ( ((pixelvalue & surf->format->Gmask) >> surf->format->Gshift))*3/4;
1350 b = ( ((pixelvalue & surf->format->Bmask) >> surf->format->Bshift))*3/4;
1351 pixelvalue = (r << surf->format->Rshift) + (g << surf->format->Gshift) + (b << surf->format->Bshift);
1352 memcpy(surf->pixels + y*surf->pitch + x*surf->format->BytesPerPixel, &pixelvalue, surf->format->BytesPerPixel);
1353
1354 memcpy(&pixelvalue, surf->pixels + (YRES-1-y)*surf->pitch + x*surf->format->BytesPerPixel, surf->format->BytesPerPixel);
1355 r = ( ((pixelvalue & surf->format->Rmask) >> surf->format->Rshift))*3/4;
1356 g = ( ((pixelvalue & surf->format->Gmask) >> surf->format->Gshift))*3/4;
1357 b = ( ((pixelvalue & surf->format->Bmask) >> surf->format->Bshift))*3/4;
1358 pixelvalue = (r << surf->format->Rshift) + (g << surf->format->Gshift) + (b << surf->format->Bshift);
1359 memcpy(surf->pixels + (YRES-1-y)*surf->pitch + x*surf->format->BytesPerPixel, &pixelvalue, surf->format->BytesPerPixel);
1360 }
1361 myUnlockSurface(surf);
1362 }
1363
1364 void overlook_init_(SDL_Surface * surf)
1365 {
1366 int Bpp = surf->format->BytesPerPixel;
1367 if (surf->format->BytesPerPixel != 4) {
1368 fprintf(stderr, "overlook_init: dest surface must be 32bpp\n");
1369 abort();
1370 }
1371 myLockSurface(surf);
1372 for (x = 0; x < surf->w; x++) {
1373 for (y = 0; y < surf->h; y++) {
1374 set_pixel(surf, x, y, 255, 255, 255, 0);
1375 }
1376 }
1377 myUnlockSurface(surf);
1378 }
1379
1380 void overlook_(SDL_Surface * dest, SDL_Surface * orig, int step, int pivot)
1381 {
1382 int Bpp = dest->format->BytesPerPixel;
1383
1384 int x_, y_;
1385 double shading = 1 - CLAMP((double)step / 70, 0, 1);
1386 double x_factor = 1 - (double)step / 700;
1387 static double fade = 0.9;
1388 double dx, dy;
1389 Uint8 Ar, Ag, Ab, Aa, Br, Bg, Bb, Ba, Cr, Cg, Cb, Ca, Dr, Dg, Db, Da;
1390 Uint8 dr, dg, db, da;
1391 if (orig->format->BytesPerPixel != 4) {
1392 fprintf(stderr, "overlook: orig surface must be 32bpp\n");
1393 abort();
1394 }
1395 if (dest->format->BytesPerPixel != 4) {
1396 fprintf(stderr, "overlook: dest surface must be 32bpp\n");
1397 abort();
1398 }
1399 myLockSurface(orig);
1400 myLockSurface(dest);
1401
1402
1403 for (x = 0; x < dest->w; x++) {
1404 double y_factor = 1 - ((double)step) / 150 * MIN(pivot, abs(x - pivot) + pivot/3) / pivot;
1405 double x__ = pivot + (x - pivot) * x_factor;
1406 x_ = floor(x__);
1407
1408
1409 for (y = 0; y < dest->h; y++) {
1410 double y__ = dest->h/2 + (y - dest->h/2) * y_factor;
1411
1412 y_ = floor(y__);
1413
1414 get_pixel(dest, x, y, &dr, &dg, &db, &da);
1415
1416 if (x_ < 0 || x_ > orig->w - 2 || y_ < 0 || y_ > orig->h - 2) {
1417 // out of band
1418 set_pixel(dest, x, y, dr, dg, db, CLAMP(0, da * fade, 255));
1419 } else {
1420 dx = x__ - x_;
1421 dy = y__ - y_;
1422 get_pixel(orig, x_, y_, &Ar, &Ag, &Ab, &Aa);
1423 get_pixel(orig, x_ + 1, y_, &Br, &Bg, &Bb, &Ba);
1424 get_pixel(orig, x_, y_ + 1, &Cr, &Cg, &Cb, &Ca);
1425 get_pixel(orig, x_ + 1, y_ + 1, &Dr, &Dg, &Db, &Da);
1426 int a = (Aa * ( 1 - dx ) + Ba * dx) * ( 1 - dy ) + (Ca * ( 1 - dx ) + Da * dx) * dy;
1427
1428 set_pixel(dest, x, y, dr, dg, db, CLAMP(0, MAX(a * shading, da * fade), 255));
1429 }
1430 }
1431 }
1432 myUnlockSurface(orig);
1433 myUnlockSurface(dest);
1434 }
1435
1436 struct flake { int x; double y; double sinpos; double sincoeff; double wideness; double y_speed; double opacity; };
1437
1438 // create a fake orig flake, we'll bilinear render from here to create beautifully smooth subpixel speed
1439 // 0 0 0 0 0
1440 // 0 50 100 50 0
1441 // 0 100 100 100 0
1442 // 0 50 100 50 0
1443 // 0 0 0 0 0
1444 static Uint32 orig_flake[] = { 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
1445 0x00000000, 0x80FFFFFF, 0xFFFFFFFF, 0x80FFFFFF, 0x00000000,
1446 0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000,
1447 0x00000000, 0x80FFFFFF, 0xFFFFFFFF, 0x80FFFFFF, 0x00000000,
1448 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000 };
1449 static int orig_flake_w = 4, orig_flake_h = 4, orig_flake_pitch = 5;
1450 static int counter_for_new_flake = 1000;
1451
1452 void snow_(SDL_Surface * dest, SDL_Surface * orig)
1453 {
1454 int Bpp = dest->format->BytesPerPixel;
1455 static struct flake * flakes = NULL;
1456 int i, amount = 200;
1457 double wideness = 2.0, y_speed = 0.2, moving_speed = 0.1;
1458 static int new_generated = 0;
1459 double a, fore_a, x_flake, y_flake, dx, dy;
1460 int r, g, b, fore_r, fore_g, fore_b, x_, y_, y__;
1461 Uint8 r_, g_, b_, a_;
1462 if (orig->format->BytesPerPixel == 1)
1463 {
1464 fprintf(stderr, "snow: orig surface must not have a palette\n");
1465 abort();
1466 }
1467 if (dest->format->BytesPerPixel == 1)
1468 {
1469 fprintf(stderr, "snow: dest surface must not have a palette\n");
1470 abort();
1471 }
1472 if (flakes == NULL) {
1473 flakes = malloc(sizeof(struct flake) * amount);
1474 if (!flakes)
1475 fb__out_of_memory();
1476 for (i = 0; i < amount; i++)
1477 {
1478 flakes[i].x = -1;
1479 }
1480 }
1481 myLockSurface(orig);
1482 myLockSurface(dest);
1483 for (x = 0; x < dest->w; x++)
1484 {
1485 for (y = 0; y < dest->h; y++)
1486 {
1487 get_pixel(orig, x, y, &r_, &g_, &b_, &a_);
1488 set_pixel(dest, x, y, r_, g_, b_, a_);
1489 }
1490 }
1491 for (i = 0; i < amount; i++)
1492 {
1493 if (flakes[i].x == -1)
1494 {
1495 if (new_generated == 0)
1496 {
1497 // gen a new one
1498 flakes[i].x = wideness + rand_(dest->w - 3 - wideness*2) - 1;
1499 flakes[i].y = -2;
1500 flakes[i].sinpos = 100.0 * rand() / RAND_MAX;
1501 flakes[i].sincoeff = 0.3 + 0.7 * rand() / RAND_MAX;
1502 flakes[i].y_speed = 0.1 + y_speed * rand() / RAND_MAX;
1503 flakes[i].wideness = wideness/2 + wideness/2 * rand() / RAND_MAX;
1504 flakes[i].opacity = 1;
1505 new_generated = counter_for_new_flake;
1506 if (counter_for_new_flake > 50)
1507 counter_for_new_flake -= 2;
1508 }
1509 else
1510 {
1511 new_generated--;
1512 }
1513 continue;
1514 }
1515 // render existing flakes
1516 x_flake = flakes[i].x + sin(flakes[i].sinpos * flakes[i].sincoeff) * flakes[i].wideness;
1517 y_flake = flakes[i].y;
1518 x_ = floor(x_flake);
1519 y_ = floor(y_flake);
1520 dx = 1 - (x_flake - x_);
1521 dy = 1 - (y_flake - y_);
1522 // collision with background?
1523 get_pixel(orig, x_, y_ + 1, &r_, &g_, &b_, &a_);
1524 if (y_ >= 0)
1525 {
1526 if (a_ > 191 + rand_(64))
1527 {
1528 get_pixel(orig, x_ + 3, y_ + 1, &r_, &g_, &b_, &a_);
1529 if(a_ > 191 + rand_(64))
1530 flakes[i].x = -1;
1531 }
1532 }
1533 for (x = 0; x < orig_flake_w; x++)
1534 {
1535 y__ = 0;
1536 for (y = MAX(0, -y_); y < orig_flake_h; y++)
1537 {
1538 get_pixel(dest, x_ + x, MAX(0, y_) + y__, &r_, &g_, &b_, &a_);
1539 // 1. bilinear filter orig_flake for smooth subpixel movement
1540 Uint32 *A = orig_flake + x + y * orig_flake_pitch;
1541 Uint32 *B = orig_flake + x + 1 + y * orig_flake_pitch;
1542 Uint32 *C = orig_flake + x + (y + 1) * orig_flake_pitch;
1543 Uint32 *D = orig_flake + x + 1 + (y + 1) * orig_flake_pitch;
1544 fore_a = (geta(A) * ( 1 - dx ) + geta(B) * dx) * ( 1 - dy ) + (geta(C) * ( 1 - dx ) + geta(D) * dx) * dy;
1545 if (fore_a == 0)
1546 {
1547 // fully transparent, nothing to do
1548 y__++;
1549 continue;
1550 }
1551
1552 if (fore_a == 255) {
1553 // fully opaque, optimized
1554 fore_r = (getr(A) * ( 1 - dx ) + getr(B) * dx) * ( 1 - dy ) + (getr(C) * ( 1 - dx ) + getr(D) * dx) * dy;
1555 fore_g = (getg(A) * ( 1 - dx ) + getg(B) * dx) * ( 1 - dy ) + (getg(C) * ( 1 - dx ) + getg(D) * dx) * dy;
1556 fore_b = (getb(A) * ( 1 - dx ) + getb(B) * dx) * ( 1 - dy ) + (getb(C) * ( 1 - dx ) + getb(D) * dx) * dy;
1557 }
1558 else
1559 {
1560 // not fully opaque, means A B C or D was not fully opaque, need to weight channels with
1561 fore_r = ( (getr(A) * geta(A) * ( 1 - dx ) + getr(B) * geta(B) * dx) * ( 1 - dy ) + (getr(C) * geta(C) * ( 1 - dx ) + getr(D) * geta(D) * dx) * dy ) / fore_a;
1562 fore_g = ( (getg(A) * geta(A) * ( 1 - dx ) + getg(B) * geta(B) * dx) * ( 1 - dy ) + (getg(C) * geta(C) * ( 1 - dx ) + getg(D) * geta(D) * dx) * dy ) / fore_a;
1563 fore_b = ( (getb(A) * geta(A) * ( 1 - dx ) + getb(B) * geta(B) * dx) * ( 1 - dy ) + (getb(C) * geta(C) * ( 1 - dx ) + getb(D) * geta(D) * dx) * dy ) / fore_a;
1564 }
1565
1566 fore_a *= flakes[i].opacity;
1567
1568 // 2. alpha composite with existing background (other flakes, alpha border of logo)
1569 a = fore_a + (255 - fore_a) * a_ / 255;
1570 if (a == 0)
1571 set_pixel(dest, x_ + x, MAX(0, y_) + y__, 0, 0, 0, 0);
1572 else
1573 {
1574 if (a_ == 0) {
1575 r = fore_r;
1576 g = fore_g;
1577 b = fore_b;
1578 }
1579 else
1580 {
1581 r = (fore_r * fore_a + ((r_ * (255 - fore_a) * a_) / 255)) / a;
1582 g = (fore_g * fore_a + ((g_ * (255 - fore_a) * a_) / 255)) / a;
1583 b = (fore_b * fore_a + ((b_ * (255 - fore_a) * a_) / 255)) / a;
1584 }
1585 if (flakes[i].x == -1)
1586 set_pixel(orig, x_ + x, MAX(0, y_) + y__, (Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a);
1587
1588 set_pixel(dest, x_ + x, MAX(0, y_) + y__, (Uint8)r, (Uint8)g, (Uint8)b, (Uint8)a);
1589 }
1590 y__++;
1591 }
1592 }
1593 flakes[i].sinpos += moving_speed;
1594 flakes[i].y += flakes[i].y_speed;
1595 if (flakes[i].y > dest->h - 22)
1596 flakes[i].opacity = (double)(dest->h - flakes[i].y - 2)/20;
1597 if (flakes[i].y >= dest->h - 4)
1598 flakes[i].x = -1;
1599 }
1600 myUnlockSurface(orig);
1601 myUnlockSurface(dest);
1602 }
1603
1604 void draw_line_(SDL_Surface* surface, int x1, int y1, int x2, int y2, SDL_Color* color)
1605 {
1606 // simple Bresenham line drawing. should be antialiased for better output, but is not.
1607 int bpp = surface->format->BytesPerPixel;
1608 Uint8* p;
1609 int pix = SDL_MapRGB(surface->format, color->r, color->g, color->b);
1610 double xacc, yacc, x, y;
1611 myLockSurface(surface);
1612 if (abs(x2 - x1) > abs(y2 - y1)) {
1613 xacc = x2 > x1 ? 1 : -1;
1614 yacc = xacc * (y2 - y1) / (x2 - x1);
1615 } else {
1616 yacc = y2 > y1 ? 1 : -1;
1617 xacc = yacc * (x2 - x1) / (y2 - y1);
1618 }
1619 x = x1;
1620 y = y1;
1621 while (1) {
1622 x += xacc;
1623 y += yacc;
1624 if ((xacc == 1 && x > x2)
1625 || (xacc == -1 && x < x2)
1626 || (yacc == 1 && y > y2)
1627 || (yacc == -1 && y < y2)) {
1628 myUnlockSurface(surface);
1629 return;
1630 }
1631 p = (Uint8*)surface->pixels + bpp*(int)x + surface->pitch*(int)y;
1632 switch(bpp) {
1633 case 1:
1634 *(Uint8*)p = pix;
1635 break;
1636 case 2:
1637 *(Uint16*)p = pix;
1638 break;
1639 case 3:
1640 if (SDL_BYTEORDER == SDL_BIG_ENDIAN) {
1641 p[0] = (pix >> 16) & 0xff;
1642 p[1] = (pix >> 8) & 0xff;
1643 p[2] = pix & 0xff;
1644 } else {
1645 p[0] = pix & 0xff;
1646 p[1] = (pix >> 8) & 0xff;
1647 p[2] = (pix >> 16) & 0xff;
1648 }
1649 break;
1650 case 4:
1651 *(Uint32*)p = pix;
1652 break;
1653 }
1654 }
1655 }
1656
1657 /************************** Gateway to Perl ****************************/
1658
1659 MODULE = Games::FrozenBubble::CStuff PACKAGE = Games::FrozenBubble::CStuff
1660
1661 void
1662 init_effects(datapath)
1663 char * datapath
1664 CODE:
1665 circle_init();
1666 plasma_init(datapath);
1667 srand(time(NULL));
1668
1669 void
1670 effect(s, img)
1671 SDL_Surface * s
1672 SDL_Surface * img
1673 CODE:
1674 int randvalue = rand_(8);
1675 if (randvalue == 1 || randvalue == 2)
1676 store_effect(s, img);
1677 else if (randvalue == 3 || randvalue == 4 || randvalue == 5)
1678 plasma_effect(s, img);
1679 else if (randvalue == 6)
1680 circle_effect(s, img);
1681 else if (randvalue == 7)
1682 bars_effect(s, img);
1683 else
1684 squares_effect(s, img);
1685
1686 int
1687 get_synchro_value()
1688 CODE:
1689 RETVAL = Mix_GetSynchroValue();
1690 OUTPUT:
1691 RETVAL
1692
1693 void
1694 set_music_position(pos)
1695 double pos
1696 CODE:
1697 Mix_SetMusicPosition(pos);
1698
1699 int
1700 fade_in_music_position(music, loops, ms, pos)
1701 Mix_Music *music
1702 int loops
1703 int ms
1704 int pos
1705 CODE:
1706 RETVAL = Mix_FadeInMusicPos(music, loops, ms, pos);
1707 OUTPUT:
1708 RETVAL
1709
1710 void
1711 shrink(dest, orig, xpos, ypos, orig_rect, factor)
1712 SDL_Surface * dest
1713 SDL_Surface * orig
1714 int xpos
1715 int ypos
1716 SDL_Rect * orig_rect
1717 int factor
1718 CODE:
1719 shrink_(dest, orig, xpos, ypos, orig_rect, factor);
1720
1721 void
1722 rotate_nearest(dest, orig, angle)
1723 SDL_Surface * dest
1724 SDL_Surface * orig
1725 double angle
1726 CODE:
1727 rotate_nearest_(dest, orig, angle);
1728
1729 void
1730 rotate_bilinear(dest, orig, angle)
1731 SDL_Surface * dest
1732 SDL_Surface * orig
1733 double angle
1734 CODE:
1735 rotate_bilinear_(dest, orig, angle);
1736
1737 AV*
1738 autopseudocrop(orig)
1739 SDL_Surface * orig
1740 CODE:
1741 RETVAL = autopseudocrop_(orig);
1742 OUTPUT:
1743 RETVAL
1744
1745 void
1746 rotate_bicubic(dest, orig, angle)
1747 SDL_Surface * dest
1748 SDL_Surface * orig
1749 double angle
1750 CODE:
1751 rotate_bicubic_(dest, orig, angle);
1752
1753 void
1754 flipflop(dest, orig, offset)
1755 SDL_Surface * dest
1756 SDL_Surface * orig
1757 int offset
1758 CODE:
1759 flipflop_(dest, orig, offset);
1760
1761 void
1762 enlighten(dest, orig, offset)
1763 SDL_Surface * dest
1764 SDL_Surface * orig
1765 int offset
1766 CODE:
1767 enlighten_(dest, orig, offset);
1768
1769 void
1770 stretch(dest, orig, offset)
1771 SDL_Surface * dest
1772 SDL_Surface * orig
1773 int offset
1774 CODE:
1775 stretch_(dest, orig, offset);
1776
1777 void
1778 tilt(dest, orig, offset)
1779 SDL_Surface * dest
1780 SDL_Surface * orig
1781 int offset
1782 CODE:
1783 tilt_(dest, orig, offset);
1784
1785 void
1786 points(dest, orig, mask)
1787 SDL_Surface * dest
1788 SDL_Surface * orig
1789 SDL_Surface * mask
1790 CODE:
1791 points_(dest, orig, mask);
1792
1793 void
1794 waterize(dest, orig, offset)
1795 SDL_Surface * dest
1796 SDL_Surface * orig
1797 int offset
1798 CODE:
1799 waterize_(dest, orig, offset);
1800
1801 void
1802 brokentv(dest, orig, offset)
1803 SDL_Surface * dest
1804 SDL_Surface * orig
1805 int offset
1806 CODE:
1807 brokentv_(dest, orig, offset);
1808
1809 void
1810 alphaize(surf)
1811 SDL_Surface * surf
1812 CODE:
1813 alphaize_(surf);
1814
1815 void
1816 pixelize(dest, orig)
1817 SDL_Surface * dest
1818 SDL_Surface * orig
1819 CODE:
1820 pixelize_(dest, orig);
1821
1822 void
1823 blacken(surf, step)
1824 SDL_Surface * surf
1825 int step
1826 CODE:
1827 blacken_(surf, step);
1828
1829 void
1830 overlook_init(surf)
1831 SDL_Surface * surf
1832 CODE:
1833 overlook_init_(surf);
1834
1835 void
1836 overlook(dest, orig, step, pivot)
1837 SDL_Surface * dest
1838 SDL_Surface * orig
1839 int step
1840 int pivot
1841 CODE:
1842 overlook_(dest, orig, step, pivot);
1843
1844 void
1845 snow(dest, orig)
1846 SDL_Surface * dest
1847 SDL_Surface * orig
1848 CODE:
1849 snow_(dest, orig);
1850
1851 void
1852 draw_line(SDL_Surface* surface, int x1, int y1, int x2, int y2, SDL_Color* color)
1853 CODE:
1854 draw_line_(surface, x1, y1, x2, y2, color);
1855
1856 void
1857 _exit(status)
1858 int status
1859
1860 void
1861 fbdelay(ms)
1862 int ms
1863 CODE:
1864 /* Beuh, SDLx::App::delay is bugged, sometimes it doesn't sleep, must be related to signals
1865 or something... but doing the do/while in Perl seems to slow down the game too much on
1866 some machines, so I'll do it from here */
1867 int then;
1868 do {
1869 then = SDL_GetTicks();
1870 SDL_Delay(ms);
1871 ms -= SDL_GetTicks() - then;
1872 } while (ms > 1);
1873
1874 SV *
1875 utf8key(event)
1876 SDL_Event * event
1877 CODE:
1878 RETVAL = utf8key_(event);
1879 OUTPUT:
1880 RETVAL