Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.         SDL_anim:  an animation library for SDL
  3.         Copyright (C) 2001, 2002  Michael Leonhard
  4.  
  5.         This library is free software; you can redistribute it and/or
  6.         modify it under the terms of the GNU Library General Public
  7.         License as published by the Free Software Foundation; either
  8.         version 2 of the License, or (at your option) any later version.
  9.  
  10.         This library is distributed in the hope that it will be useful,
  11.         but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.         Library General Public License for more details.
  14.  
  15.         You should have received a copy of the GNU Library General Public
  16.         License along with this library; if not, write to the Free
  17.         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.        
  19.         Michael Leonhard
  20.         mike@tamale.net
  21. */
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <SDL/SDL.h>
  27. #include "SDL_anim.h"
  28.  
  29. /* Draw a Gimpish background pattern to show transparency in the anim */
  30. void draw_background( SDL_Surface *screen ) {
  31.         Uint8 *dst = screen->pixels;
  32.         int x, y;
  33.         int bpp = screen->format->BytesPerPixel;
  34.         Uint32 col[2];
  35.         col[0] = SDL_MapRGB(screen->format, 0x66, 0x66, 0x66);
  36.         col[1] = SDL_MapRGB(screen->format, 0x99, 0x99, 0x99);
  37.         for(y = 0; y < screen->h; y++) {
  38.                 for(x = 0; x < screen->w; x++) {
  39.                         /* use an 8x8 checkerboard pattern */
  40.                         Uint32 c = col[((x ^ y) >> 3) & 1];
  41.                         switch(bpp) {
  42.                                 case 1:
  43.                                         dst[x] = c;
  44.                                         break;
  45.                                 case 2:
  46.                                         ((Uint16 *)dst)[x] = c;
  47.                                         break;
  48.                                 case 3:
  49.                                         if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
  50.                                                 dst[x * 3] = c;
  51.                                                 dst[x * 3 + 1] = c >> 8;
  52.                                                 dst[x * 3 + 2] = c >> 16;
  53.                                                 }
  54.                                         else {
  55.                                                 dst[x * 3] = c >> 16;
  56.                                                 dst[x * 3 + 1] = c >> 8;
  57.                                                 dst[x * 3 + 2] = c;
  58.                                         }
  59.                                         break;
  60.                                 case 4:
  61.                                         ((Uint32 *)dst)[x] = c;
  62.                                         break;
  63.                                 }
  64.                         }
  65.                 dst += screen->pitch;
  66.                 }
  67.         }
  68.  
  69. int app_main(int argc, char *argv[]) {
  70.         SDL_Surface *screen;
  71.         SDL_Animation *anim;
  72.         SDL_Rect rect;
  73.         SDL_Event event;
  74.         SDL_KeyboardEvent *key;
  75.         int depth, done;
  76.         Uint32 start;
  77.        
  78.         argv[1]="ship.ani";
  79.         /* Initialize the SDL library */
  80.         if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
  81.                 fprintf( stderr, "Couldn't initialize SDL: %s\n", SDL_GetError() );
  82.                 return 2;
  83.                 }
  84.  
  85.         /* Open the anim file */
  86.         anim = Anim_Load(argv[1]);
  87.         if( anim == NULL ) {
  88.                 fprintf( stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError() );
  89.                 SDL_Quit();
  90.                 return 3;
  91.                 }
  92.  
  93.         SDL_WM_SetCaption( argv[1], "showanim" );
  94.  
  95.         /* Create a display for the anim */
  96.         depth = SDL_VideoModeOK( anim->w + 1, anim->h, 32, SDL_HWPALETTE );
  97.         /* Use the deepest native mode, except that we emulate 32bpp for */
  98.         /* viewing non-indexed anims on 8bpp screens */
  99.         if( (anim->surface->format->BytesPerPixel > 1) && (depth == 8) ) {
  100.                 depth = 32;
  101.                 }
  102.         screen = SDL_SetVideoMode( anim->w + 10, anim->h, 16, SDL_HWPALETTE );
  103.         if( screen == NULL ) {
  104.                 fprintf( stderr, "Couldn't set %dx%dx%d video mode: %s\n",
  105.                                 anim->w, anim->h, depth, SDL_GetError() );
  106.                 SDL_Quit();
  107.                 return 4;
  108.                 }
  109.  
  110.         /* Set the palette, if one exists */
  111.         if( anim->surface->format->palette ) {
  112.                 SDL_SetColors( screen, anim->surface->format->palette->colors,
  113.                                 0, anim->surface->format->palette->ncolors );
  114.                 }
  115.  
  116.  
  117.         if( !Anim_DisplayFormat( anim ) ) {
  118.                 fprintf( stderr, "Anim_DisplayFormat() failed\n" );
  119.                 return 5;
  120.                 }
  121.  
  122.         done = 0;
  123.         start = SDL_GetTicks();
  124.         while( !done ) {
  125.                 while( SDL_PollEvent( &event ) ) {
  126.                         switch( event.type ) {
  127.                                 case SDL_QUIT:
  128.                                         done = 1;
  129.                                         break;
  130.                                 case SDL_KEYDOWN:
  131.                                         key = (SDL_KeyboardEvent *)&event;
  132.                                         if( key->keysym.sym == SDLK_ESCAPE ) done = 1;
  133.                                         start = SDL_GetTicks();
  134.                                         break;
  135.                                 }
  136.                         }
  137.  
  138.                 draw_background( screen );
  139.                 rect.x = 0;
  140.                 rect.y = 0;
  141.                 Anim_BlitFrame( anim, start, SDL_GetTicks(), screen, &rect );
  142.                 SDL_UpdateRect( screen, 0, 0, 0, 0 );
  143.                 SDL_Delay( 100 );
  144.                 }
  145.  
  146.         Anim_Free( anim );
  147.         SDL_Quit();
  148.         return 0;
  149.         }
  150.