git @ Cat's Eye Technologies linapple / master src / stretch.cpp
master

Tree @master (Download .tar.gz)

stretch.cpp @masterraw · history · blame

  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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2004 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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 PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Sam Lantinga
    slouken@libsdl.org
*/


/* This a stretch blit implementation based on ideas given to me by
   Tomasz Cejner - thanks! :)

   April 27, 2000 - Sam Lantinga
*/


/* This isn't ready for general consumption yet - it should be folded
   into the general blitting mechanism.
*/

//static unsigned char copy_row[4096];

#include "stdafx.h"

#include "asset.h"

#define DEFINE_COPY_ROW(name, type)      \
void name(type *src, int src_w, type *dst, int dst_w)  \
{              \
  int i;            \
  int pos, inc;          \
  type pixel = 0;          \
              \
  pos = 0x10000;          \
  inc = (src_w << 16) / dst_w;      \
  for ( i=dst_w; i>0; --i ) {      \
    while ( pos >= 0x10000L ) {    \
      pixel = *src++;      \
      pos -= 0x10000L;    \
    }          \
    *dst++ = pixel;        \
    pos += inc;        \
  }            \
}
// the same as above, but pixels ORed with the destination
#define DEFINE_COPY_ROW_OR(name, type)      \
void name(type *src, int src_w, type *dst, int dst_w)  \
{              \
  int i;            \
  int pos, inc;          \
  type pixel = 0;          \
              \
  pos = 0x10000;          \
  inc = (src_w << 16) / dst_w;      \
  for ( i=dst_w; i>0; --i ) {      \
    while ( pos >= 0x10000L ) {    \
      pixel = *src++;      \
      pos -= 0x10000L;    \
}          \
    *dst++ |= pixel;      \
    pos += inc;        \
}            \
}

DEFINE_COPY_ROW(copy_row1, Uint8)
DEFINE_COPY_ROW(copy_row2, Uint16)
DEFINE_COPY_ROW(copy_row4, Uint32)

DEFINE_COPY_ROW_OR(copy_row_or1, Uint8)
DEFINE_COPY_ROW_OR(copy_row_or2, Uint16)
DEFINE_COPY_ROW_OR(copy_row_or4, Uint32)

/* The ASM code doesn't handle 24-bpp stretch blits */
void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
{
  int i;
  int pos, inc;
  Uint8 pixel[3];

  pos = 0x10000;
  inc = (src_w << 16) / dst_w;
  for ( i=dst_w; i>0; --i ) {
    while ( pos >= 0x10000L ) {
      pixel[0] = *src++;
      pixel[1] = *src++;
      pixel[2] = *src++;
      pos -= 0x10000L;
    }
    *dst++ = pixel[0];
    *dst++ = pixel[1];
    *dst++ = pixel[2];
    pos += inc;
  }
}

/* Perform a stretch blit between two surfaces of the same format.
   NOTE:  This function is not safe to call from multiple threads!
*/
int SDL_SoftStretchMy(SDL_Surface *src, SDL_Rect *srcrect,
                    SDL_Surface *dst, SDL_Rect *dstrect)
{
  int src_locked;
  int dst_locked;
  int pos, inc;
  int dst_maxrow;
  int src_row, dst_row;
  Uint8 *srcp = NULL;
  Uint8 *dstp;
  SDL_Rect full_src;
  SDL_Rect full_dst;
  const int bpp = dst->format->BytesPerPixel;

   if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) {
     SDL_SetError("Only works with same format surfaces");
     return(-1);
   }

  /* Verify the blit rectangles */
  if ( !srcrect ){
    full_src.x = 0;
    full_src.y = 0;
    full_src.w = src->w;
    full_src.h = src->h;
    srcrect = &full_src;
  }
  if ( !dstrect ){
    full_dst.x = 0;
    full_dst.y = 0;
    full_dst.w = dst->w;
    full_dst.h = dst->h;
    dstrect = &full_dst;
  }

  /* Lock the destination if it's in hardware */
  dst_locked = 0;
  if ( SDL_MUSTLOCK(dst) ) {
    if(SDL_LockSurface(dst) < 0) return -1;
    dst_locked = 1;
  }
  /* Lock the source if it's in hardware */
  src_locked = 0;
  if ( SDL_MUSTLOCK(src) ) {
    if ( SDL_LockSurface(src) < 0 ) {
      if ( dst_locked ) SDL_UnlockSurface(dst);
//      SDL_SetError("Unable to lock source surface");
      return(-1);
    }
    src_locked = 1;
  }

  /* Set up the data... */
  pos = 0x10000;
  inc = (srcrect->h << 16) / dstrect->h;
  src_row = srcrect->y;
  dst_row = dstrect->y;


  /* Perform the stretch blit */
  for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) {
    dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch)
                                + (dstrect->x*bpp);
    while ( pos >= 0x10000L ) {
      srcp = (Uint8 *)src->pixels + (src_row*src->pitch)
                                  + (srcrect->x*bpp);
      ++src_row;
      pos -= 0x10000L;
    }
    switch (bpp) {
        case 1:
        copy_row1(srcp, srcrect->w, dstp, dstrect->w);
        break;
        case 2:
        copy_row2((Uint16 *)srcp, srcrect->w,
            (Uint16 *)dstp, dstrect->w);
        break;
        case 3:
        copy_row3(srcp, srcrect->w, dstp, dstrect->w);
        break;
        case 4:
        copy_row4((Uint32 *)srcp, srcrect->w,
                (Uint32 *)dstp, dstrect->w);
        break;
    }
    pos += inc;
  }

  /* We need to unlock the surfaces if they're locked */
  if ( dst_locked ) {
    SDL_UnlockSurface(dst);
  }
  if ( src_locked ) {
    SDL_UnlockSurface(src);
  }
  return(0);
}


////////////////////////////////////////////////////////////////////////////////////////////
//     MONO8
////////////////////////////////////////////////////////////////////////////////////////////
/* Perform a monochrome stretch blit between two surfaces of the 8bpp format!
   NOTE:  This function is not safe to call from multiple threads!
*/
void copy8mono(Uint8 *src, int src_w, Uint8 *dst, int dst_w, Uint8 brush)
{
  int i;
  int pos, inc;
  Uint8 pixel = 0;
  pos = 0x10000;
  inc = (src_w << 16) / dst_w;
  for ( i=dst_w; i>0; --i ) {
    while ( pos >= 0x10000L ) {
      pixel = *src++;
      pos -= 0x10000L;
    }
    //*dst++ = pixel;
    if(pixel) *dst++ = brush;
      else *dst++ = 0;
    pos += inc;
  }
}

int SDL_SoftStretchMono8(SDL_Surface *src, SDL_Rect *srcrect,
        SDL_Surface *dst, SDL_Rect *dstrect, Uint8 brush)
{//brush - monochrome color (index from palette)
  int src_locked;
  int dst_locked;
  int pos, inc;
  int dst_maxrow;
  int src_row, dst_row;
  Uint8 *srcp = NULL;
  Uint8 *dstp;
  SDL_Rect full_src;
  SDL_Rect full_dst;
  const int bpp = dst->format->BytesPerPixel;

  /* Verify the blit rectangles */
  if ( !srcrect ){
    full_src.x = 0;
    full_src.y = 0;
    full_src.w = src->w;
    full_src.h = src->h;
    srcrect = &full_src;
  }
  if ( !dstrect ){
    full_dst.x = 0;
    full_dst.y = 0;
    full_dst.w = dst->w;
    full_dst.h = dst->h;
    dstrect = &full_dst;
  }

  /* Lock the destination if it's in hardware */
  dst_locked = 0;
  if ( SDL_MUSTLOCK(dst) ) {
    if(SDL_LockSurface(dst) < 0) return -1;
    dst_locked = 1;
  }
  /* Lock the source if it's in hardware */
  src_locked = 0;
  if ( SDL_MUSTLOCK(src) ) {
    if ( SDL_LockSurface(src) < 0 ) {
      if ( dst_locked ) SDL_UnlockSurface(dst);
//      SDL_SetError("Unable to lock source surface");
      return(-1);
    }
    src_locked = 1;
  }

  /* Set up the data... */
  pos = 0x10000;
  inc = (srcrect->h << 16) / dstrect->h;
  src_row = srcrect->y;
  dst_row = dstrect->y;


  /* Perform the stretch blit */
  for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) {
    dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch)
        + (dstrect->x*bpp);
    while ( pos >= 0x10000L ) {
      srcp = (Uint8 *)src->pixels + (src_row*src->pitch)
          + (srcrect->x*bpp);
      ++src_row;
      pos -= 0x10000L;
    }
    switch (bpp) {
      case 1:
        copy8mono(srcp, srcrect->w, dstp, dstrect->w, brush);
         break;
      default: break;
/*      case 2:
        copy_row2((Uint16 *)srcp, srcrect->w,
             (Uint16 *)dstp, dstrect->w);
        break;
      case 3:
        copy_row3(srcp, srcrect->w, dstp, dstrect->w);
        break;
      case 4:
        copy_row4((Uint32 *)srcp, srcrect->w,
             (Uint32 *)dstp, dstrect->w);
        break;*/
    }
    pos += inc;
  }

  /* We need to unlock the surfaces if they're locked */
  if ( dst_locked ) {
    SDL_UnlockSurface(dst);
  }
  if ( src_locked ) {
    SDL_UnlockSurface(src);
  }
  return(0);
}
///////////////////////////////////////////////////////////////////////////////////////////////
/*  SDL_SoftStretchOr  - the same as SDL_SoftStretch, but ORed with destination
  NOTE: 24bpp does not support  -- beom beotiger 2007 November
*/

/* Perform a stretch blit between two surfaces of the same format.
   NOTE:  This function is not safe to call from multiple threads!
*/
int SDL_SoftStretchOr(SDL_Surface *src, SDL_Rect *srcrect,
        SDL_Surface *dst, SDL_Rect *dstrect)
{
  int src_locked;
  int dst_locked;
  int pos, inc;
  int dst_maxrow;
  int src_row, dst_row;
  Uint8 *srcp = NULL;
  Uint8 *dstp;
  SDL_Rect full_src;
  SDL_Rect full_dst;
  const int bpp = dst->format->BytesPerPixel;

   if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) {
     SDL_SetError("Only works with same format surfaces");
     return(-1);
   }

  /* Verify the blit rectangles */
  if ( !srcrect ){
    full_src.x = 0;
    full_src.y = 0;
    full_src.w = src->w;
    full_src.h = src->h;
    srcrect = &full_src;
  }
  if ( !dstrect ){
    full_dst.x = 0;
    full_dst.y = 0;
    full_dst.w = dst->w;
    full_dst.h = dst->h;
    dstrect = &full_dst;
  }

  /* Lock the destination if it's in hardware */
  dst_locked = 0;
  if ( SDL_MUSTLOCK(dst) ) {
    if(SDL_LockSurface(dst) < 0) return -1;
    dst_locked = 1;
  }
  /* Lock the source if it's in hardware */
  src_locked = 0;
  if ( SDL_MUSTLOCK(src) ) {
    if ( SDL_LockSurface(src) < 0 ) {
      if ( dst_locked ) SDL_UnlockSurface(dst);
//      SDL_SetError("Unable to lock source surface");
      return(-1);
    }
    src_locked = 1;
  }

  /* Set up the data... */
  pos = 0x10000;
  inc = (srcrect->h << 16) / dstrect->h;
  src_row = srcrect->y;
  dst_row = dstrect->y;


  /* Perform the stretch blit */
  for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) {
    dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch)
        + (dstrect->x*bpp);
    while ( pos >= 0x10000L ) {
      srcp = (Uint8 *)src->pixels + (src_row*src->pitch)
          + (srcrect->x*bpp);
      ++src_row;
      pos -= 0x10000L;
    }
    switch (bpp) {
      case 1:
        copy_row_or1(srcp, srcrect->w, dstp, dstrect->w);
        break;
      case 2:
        copy_row_or2((Uint16 *)srcp, srcrect->w,
             (Uint16 *)dstp, dstrect->w);
        break;
      case 3:
        copy_row3(srcp, srcrect->w, dstp, dstrect->w);
        break;
      case 4:
        copy_row_or4((Uint32 *)srcp, srcrect->w,
             (Uint32 *)dstp, dstrect->w);
        break;
    }
    pos += inc;
  }

  /* We need to unlock the surfaces if they're locked */
  if ( dst_locked ) {
    SDL_UnlockSurface(dst);
  }
  if ( src_locked ) {
    SDL_UnlockSurface(src);
  }
  return(0);
}


  /* ----------------------------------------------------------------*/
 /* ---------------------- FONT routines ---------------------------*/
/* ----------------------------------------------------------------*/

SDL_Surface *font_sfc = NULL;  // used for font

// defined in .h file
//#define FONT_SIZE_X  6
//#define FONT_SIZE_Y  8
// chars in row in font bitmap
//#define CHARS_IN_ROW  45

bool fonts_initialization(void)
{
  if(!assets->font) {
	  return false;
  }
  font_sfc = SDL_DisplayFormat(assets->font);

  /* Transparant color is BLACK: */
  SDL_SetColorKey(font_sfc,SDL_SRCCOLORKEY,SDL_MapRGB(font_sfc->format,0,0,0));

  return true;
}/* fonts_initialization */


void fonts_termination(void)
{
  SDL_FreeSurface(font_sfc);
  font_sfc = NULL;
} /* fonts_termination */


void font_print(int x,int y,const char *text,SDL_Surface *surface, double kx, double ky)
{// kx, ky - stretching koefficients for width and height of destination chars respectively
  int i, c;
  SDL_Rect s,d;


  for(i = 0; text[i] != 0 && x < surface->w; i++) {
    int row;
    c = int(text[i]);

    if(c > 127 || c < 0) c = '?';  // cut-off non-ASCII chars

    row = c / CHARS_IN_ROW;

    s.x = (c - (row * CHARS_IN_ROW)) * (FONT_SIZE_X + 1) + 1;
    s.y = (row) * (FONT_SIZE_Y  + 1) + 1;
    s.h = FONT_SIZE_Y;
    s.w = FONT_SIZE_X;

    d.x = x + i * FONT_SIZE_X * kx;
    d.y = y;
    d.w = s.w * kx;
    d.h = s.h * ky;
    SDL_SoftStretchOr(font_sfc, &s, surface, &d);
  } /* for */
} /* font_print */


void font_print_right(int x,int y,const char *text,SDL_Surface *surface, double kx, double ky)
{// kx, ky - stretching koefficients for width and height of destination chars respectively
  int i, c;
  SDL_Rect s,d;

  x -= strlen(text) * FONT_SIZE_X * kx;

  for(i=0;text[i]!=0 && x<surface->w;i++) {
    int row;
    c = int(text[i]);
    if(c > 127 || c < 0) c = '?';  // cut-off non-ASCII chars

    row = c / CHARS_IN_ROW;
    s.x = (c - (row * CHARS_IN_ROW)) * (FONT_SIZE_X + 1) + 1;
    s.y = (row) * (FONT_SIZE_Y + 1) + 1;
    s.h = FONT_SIZE_Y;
    s.w = FONT_SIZE_X;

    d.x = x + i * FONT_SIZE_X * kx;
    d.y = y;
    d.w = s.w * kx;
    d.h = s.h * ky;
    SDL_SoftStretchOr(font_sfc, &s, surface, &d);
  } /* for */
} /* font_print_right */


void font_print_centered(int x, int y, const char *text, SDL_Surface *surface, double kx, double ky)
{// kx, ky - stretching koefficients for width and height of destination chars respectively
  int i, c;
  SDL_Rect s,d;

  x -= strlen(text) * FONT_SIZE_X * kx / 2;  // centered position
  if(x < 0) x = 0;

  for(i=0; text[i] != 0 && ((x * kx) < surface->w); i++) {
    int row;
    c = int(text[i]);
    if(c > 127 || c < 0) c = '?';  // cut-off non-ASCII chars

    row = c / CHARS_IN_ROW;
    s.x = (c - (row * CHARS_IN_ROW)) * (FONT_SIZE_X + 1) + 1;
    s.y = (row) * (FONT_SIZE_Y + 1) + 1;
    s.h = FONT_SIZE_Y;
    s.w = FONT_SIZE_X;

    d.x = x + i * FONT_SIZE_X * kx;
    d.y = y;
    d.w = s.w * kx;
    d.h = s.h * ky;
    SDL_SoftStretchOr(font_sfc, &s, surface, &d);
  } /* for */
} /* font_print_centered */


///////////////////////////////////////////////////////////////////////////
////// Some auxiliary functions //////////////
///////////////////////////////////////////////////////////////////////////
void surface_fader(SDL_Surface *surface,float r_factor,float g_factor,float b_factor,float a_factor,SDL_Rect *r)
{

// my rebiuld for 8BPP palettized surfaces! --bb

  SDL_Rect r2;
  int i/*,x,y,offs*/;
//  Uint8 rtable[256],gtable[256],btable[256],atable[256];
  SDL_Color mycolors[256];  // faded colors
  SDL_Color *colors;
//  SDL_Surface *tmp;

  if (r==0) {
    r2.x=0;
    r2.y=0;
    r2.w=surface->w;
    r2.h=surface->h;
    r=&r2;
  } /* if */

// fading just for 8BPP surfaces!
  if (surface->format->BytesPerPixel != 1) return;

  colors = (SDL_Color *) surface->format->palette->colors; // get pointer to origin colors
  for(i=0;i<256;i++) {
    mycolors[i].r=(Uint8)(colors[i].r * r_factor);
    mycolors[i].g=(Uint8)(colors[i].g * g_factor);
    mycolors[i].b=(Uint8)(colors[i].b * b_factor);
  } /* for */

  SDL_SetColors(surface, mycolors, 0 ,256);

//   if ((surface->flags&SDL_HWSURFACE)!=0) {
  //     /* HARDWARE SURFACE!!!: */
//     tmp=SDL_CreateRGBSurface(SDL_SWSURFACE,surface->w,surface->h,32,0,0,0,0);
//     SDL_BlitSurface(surface,0,tmp,0);
//     SDL_LockSurface(tmp);
//     pixels = (Uint8 *)(tmp->pixels);
//   } else {
//     SDL_LockSurface(surface);
//     pixels = (Uint8 *)(surface->pixels);
  //   } /* if */
  //
//   for(y=r->y;y<r->y+r->h && y<surface->h;y++) {
//     for(x=r->x,offs=y*surface->pitch+r->x*4;x<r->x+r->w && x<surface->w;x++,offs+=4) {
//       pixels[offs+ROFFSET]=rtable[pixels[offs+ROFFSET]];
//       pixels[offs+GOFFSET]=gtable[pixels[offs+GOFFSET]];
//       pixels[offs+BOFFSET]=btable[pixels[offs+BOFFSET]];
//       pixels[offs+AOFFSET]=atable[pixels[offs+AOFFSET]];
  //     } /* for */
  //   } /* for */
  //
//   if ((surface->flags&SDL_HWSURFACE)!=0) {
  //     /* HARDWARE SURFACE!!!: */
//     SDL_UnlockSurface(tmp);
//     SDL_BlitSurface(tmp,0,surface,0);
//     SDL_FreeSurface(tmp);
//   } else {
//     SDL_UnlockSurface(surface);
  //   } /* if */


} /* surface_fader */

void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
  SDL_Rect clip;
  int bpp = surface->format->BytesPerPixel;

  SDL_GetClipRect(surface,&clip);

  if (x<clip.x || x>=clip.x+clip.w ||
       y<clip.y || y>=clip.y+clip.h) return;

  if (x<0 || x>=surface->w ||
       y<0 || y>=surface->h) return;

  Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

  switch(bpp) {
    case 1:
      *p = pixel;
      break;

    case 2:
      *(Uint16 *)p = pixel;
      break;

    case 3:
      if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
        p[0] = (pixel >> 16) & 0xff;
        p[1] = (pixel >> 8) & 0xff;
        p[2] = pixel & 0xff;
      } else {
        p[0] = pixel & 0xff;
        p[1] = (pixel >> 8) & 0xff;
        p[2] = (pixel >> 16) & 0xff;
      }
      break;

    case 4:
      *(Uint32 *)p = pixel;
      break;
  }
} /* putpixel */


void rectangle(SDL_Surface *surface, int x, int y, int w, int h, Uint32 pixel)
{
  int i;

  for(i=0;i<w;i++) {
    putpixel(surface,x+i,y,pixel);
    putpixel(surface,x+i,y+h,pixel);
  } /* for */
  for(i=0;i<=h;i++) {
    putpixel(surface,x,y+i,pixel);
    putpixel(surface,x+w,y+i,pixel);
  } /* for */
} /* rectangle */