home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / edu / tux / Tuxtype2-1.5.3-installer.exe / src / loaders.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-13  |  8.2 KB  |  344 lines

  1. /***************************************************************************
  2.  -  file: loaders.c
  3.  -  description: Functions to load multimedia for Tux Typing
  4.                              -------------------
  5.     begin                : Thu May 4 2000
  6.     copyright            : (C) 2000 by Sam Hart
  7.                          : (C) 2003 by Jesse Andrews
  8.     email                : tuxtype-dev@tux4kids.net
  9.  ***************************************************************************/
  10.  
  11. /***************************************************************************
  12.  *                                                                         *
  13.  *   This program is free software; you can redistribute it and/or modify  *
  14.  *   it under the terms of the GNU General Public License as published by  *
  15.  *   the Free Software Foundation; either version 2 of the License, or     *
  16.  *   (at your option) any later version.                                   *
  17.  *                                                                         *
  18.  ***************************************************************************/
  19.  
  20. #include "globals.h"
  21. #include "funcs.h"
  22.  
  23. /* check to see if file exists, if so return true */
  24. int checkFile( const char *file ) {
  25.     static struct stat fileStats;
  26.  
  27.     fileStats.st_mode = 0;
  28.  
  29.     stat( file, &fileStats );
  30.         
  31.     return (S_IFREG & fileStats.st_mode);
  32. }
  33.  
  34. void LoadLang( void ) {
  35.     char fn[FNLEN];
  36.  
  37.     /* we only need to load a lang.po file if we
  38.      * are actually using a theme, so this is a little
  39.      * different than the other loaders 
  40.      */ 
  41.  
  42.     if (useEnglish) {
  43.         /* clear translations and return! */
  44.         return;
  45.     }
  46.  
  47.     /* --- create full path to the lang.po file --- */
  48.  
  49.     sprintf( fn, "%s/lang.po", realPath[0]);
  50.     if (load_trans( fn )) {
  51.         /* failed to find a lang.po file, clear gettext & return */
  52.         return;
  53.     }
  54. }
  55.  
  56. int max( int n1, int n2 ) {
  57.     return (n1 > n2 ? n1 : n2);
  58. }
  59.  
  60. /**********************
  61.  Flip:
  62.    input: a SDL_Surface, x, y
  63.    output: a copy of the SDL_Surface flipped via rules:
  64.  
  65.      if x is a positive value, then flip horizontally
  66.      if y is a positive value, then flip vertically
  67.  
  68.      note: you can have it flip both
  69. **********************/
  70. SDL_Surface *flip( SDL_Surface *in, int x, int y ) {
  71.     SDL_Surface *out, *tmp;
  72.     SDL_Rect from_rect, to_rect;
  73.     Uint32    flags;
  74.     Uint32  colorkey=0;
  75.  
  76.     /* --- grab the settings for the incoming pixmap --- */
  77.  
  78.     SDL_LockSurface(in);
  79.     flags = in->flags;
  80.  
  81.     /* --- change in's flags so ignore colorkey & alpha --- */
  82.  
  83.     if (flags & SDL_SRCCOLORKEY) {
  84.         in->flags &= ~SDL_SRCCOLORKEY;
  85.         colorkey = in->format->colorkey;
  86.     }
  87.     if (flags & SDL_SRCALPHA) {
  88.         in->flags &= ~SDL_SRCALPHA;
  89.     }
  90.  
  91.     SDL_UnlockSurface(in);
  92.  
  93.     /* --- create our new surface --- */
  94.  
  95.     out = SDL_CreateRGBSurface(
  96.         SDL_SWSURFACE,
  97.         in->w, in->h, 32, rmask, gmask, bmask, amask);
  98.  
  99.     /* --- flip horizontally if requested --- */
  100.  
  101.     if (x) {
  102.         from_rect.h = to_rect.h = in->h;
  103.         from_rect.w = to_rect.w = 1;
  104.         from_rect.y = to_rect.y = 0;
  105.         from_rect.x = 0;
  106.         to_rect.x = in->w - 1;
  107.  
  108.         do {
  109.             SDL_BlitSurface(in, &from_rect, out, &to_rect);
  110.             from_rect.x++;
  111.             to_rect.x--;
  112.         } while (to_rect.x >= 0);
  113.     }
  114.  
  115.     /* --- flip vertically if requested --- */
  116.  
  117.     if (y) {
  118.         from_rect.h = to_rect.h = 1;
  119.         from_rect.w = to_rect.w = in->w;
  120.         from_rect.x = to_rect.x = 0;
  121.         from_rect.y = 0;
  122.         to_rect.y = in->h - 1;
  123.  
  124.         do {
  125.             SDL_BlitSurface(in, &from_rect, out, &to_rect);
  126.             from_rect.y++;
  127.             to_rect.y--;
  128.         } while (to_rect.y >= 0);
  129.     }
  130.  
  131.     /* --- restore colorkey & alpha on in and setup out the same --- */
  132.  
  133.     SDL_LockSurface(in);
  134.  
  135.     if (flags & SDL_SRCCOLORKEY) {
  136.         in->flags |= SDL_SRCCOLORKEY;
  137.         in->format->colorkey = colorkey;
  138.         tmp = SDL_DisplayFormat(out);
  139.         SDL_FreeSurface(out);
  140.         out = tmp;
  141.         out->flags |= SDL_SRCCOLORKEY;
  142.         out->format->colorkey = colorkey;
  143.     } else if (flags & SDL_SRCALPHA) {
  144.         in->flags |= SDL_SRCALPHA;
  145.         tmp = SDL_DisplayFormatAlpha(out);
  146.         SDL_FreeSurface(out);
  147.         out = tmp;
  148.     } else {
  149.         tmp = SDL_DisplayFormat(out);
  150.         SDL_FreeSurface(out);
  151.         out = tmp;
  152.     }
  153.  
  154.     SDL_UnlockSurface(in);
  155.  
  156.     return out;
  157. }
  158.  
  159. TTF_Font *LoadFont( char *fontfile, int fontsize ) {
  160.     TTF_Font *loadedFont;
  161.     char fn[FNLEN];
  162.     int i;
  163.  
  164.     /* try to find font first in theme dir, then in default */
  165.     for (i=useEnglish; i<2; i++) {
  166.         sprintf( fn, "%s/fonts/%s", realPath[i], fontfile );
  167.         if ( checkFile(fn) ) {
  168.             /* try to load the font, if successful, return font*/
  169.  
  170.             loadedFont = TTF_OpenFont( fn, fontsize );
  171.  
  172.             if (loadedFont != NULL)
  173.                 return loadedFont;
  174.         }
  175.     }
  176.  
  177.     fprintf(stderr, "FATAL ERROR: couldn't load font: %s\n", fontfile);
  178.     exit(1);
  179.  
  180.     return NULL;
  181. }
  182.  
  183. /***********************
  184.     LoadImage : Load an image and set transparent if requested
  185. ************************/
  186. SDL_Surface *LoadImage( char *datafile, int mode )
  187. {
  188.     int i;
  189.     SDL_Surface  *tmp_pic=NULL, *final_pic=NULL;
  190.     char         fn[FNLEN];
  191.  
  192.     DEBUGCODE { fprintf(stderr, "LoadImage: loading %s\n", datafile ); }
  193.  
  194.     /* truth table for start of loop, since we only use theme on those conditions!
  195.               useEng    IMG_NO_THEME    i
  196.                  0           0          0
  197.                  0           1          1
  198.                  1           0          1
  199.                  1           1          1
  200.      */
  201.  
  202.     for (i = (useEnglish || (mode & IMG_NO_THEME)); i<2; i++) {
  203.  
  204.         sprintf( fn, "%s/images/%s", realPath[i], datafile );
  205.  
  206.         if ( checkFile( fn ) ) {
  207.             tmp_pic = IMG_Load( fn );
  208.             if (tmp_pic != NULL)
  209.                 break; 
  210.             else
  211.                 fprintf(stderr, "Warning: graphics file %s is corrupt\n", fn);
  212.         }
  213.     }
  214.  
  215.     if (tmp_pic == NULL) {
  216.         if (mode & IMG_NOT_REQUIRED) 
  217.             return NULL;
  218.  
  219.         fprintf(stderr, "ERROR could not load required graphics file %s\n", datafile);
  220.         exit(1);
  221.     }
  222.  
  223.     /* finally setup the image to the proper format */
  224.  
  225.     switch (mode & IMG_MODES) {
  226.         case IMG_REGULAR:
  227.             final_pic = SDL_DisplayFormat(tmp_pic);
  228.             SDL_FreeSurface(tmp_pic);
  229.             break;
  230.         case IMG_ALPHA:
  231.             final_pic = SDL_DisplayFormatAlpha(tmp_pic);
  232.             SDL_FreeSurface(tmp_pic);
  233.             break;
  234.         case IMG_COLORKEY:
  235.             SDL_LockSurface(tmp_pic);
  236.             SDL_SetColorKey(tmp_pic, (SDL_SRCCOLORKEY | SDL_RLEACCEL), SDL_MapRGB(tmp_pic->format, 255, 255, 0));
  237.             final_pic = SDL_DisplayFormat(tmp_pic);
  238.             SDL_FreeSurface(tmp_pic);
  239.             break;
  240.     }
  241.  
  242.     LOG( "LOADIMAGE: Done\n" );
  243.  
  244.     return (final_pic);
  245. }
  246.  
  247. sprite* FlipSprite( sprite *in, int X, int Y ) {
  248.     sprite *out;
  249.  
  250.     out = malloc(sizeof(sprite));
  251.     if (in->default_img != NULL)
  252.         out->default_img = flip( in->default_img, X, Y );
  253.     else
  254.         out->default_img = NULL;
  255.     for ( out->num_frames=0; out->num_frames<in->num_frames; out->num_frames++ )
  256.         out->frame[out->num_frames] = flip( in->frame[out->num_frames], X, Y );
  257.     out->cur = 0;
  258.     return out;
  259. }
  260.  
  261. sprite* LoadSprite( char* name, int MODE ) {
  262.     sprite *new_sprite;
  263.     char fn[FNLEN];
  264.     int x;
  265.  
  266.     /* JA --- HACK check out what has changed with new code */
  267.  
  268.     new_sprite = malloc(sizeof(sprite));
  269.  
  270.     sprintf(fn, "%sd.png", name);
  271.     new_sprite->default_img = LoadImage( fn, MODE|IMG_NOT_REQUIRED );
  272.     for (x = 0; x < MAX_SPRITE_FRAMES; x++) {
  273.         sprintf(fn, "%s%d.png", name, x);
  274.         new_sprite->frame[x] = LoadImage( fn, MODE|IMG_NOT_REQUIRED );
  275.         if ( new_sprite->frame[x] == NULL ) {
  276.             new_sprite->cur = 0;
  277.             new_sprite->num_frames = x;
  278.             break;
  279.         }
  280.     }
  281.  
  282.     DEBUGCODE {
  283.         fprintf( stderr, "loading sprite %s - contains %d frames\n",
  284.                 name, new_sprite->num_frames );
  285.     }
  286.  
  287.     return new_sprite;
  288. }
  289.  
  290. void FreeSprite( sprite *gfx ) {
  291.     int x;
  292.     for (x = 0; x < gfx->num_frames; x++)
  293.         SDL_FreeSurface( gfx->frame[x] );
  294.     SDL_FreeSurface( gfx->default_img );
  295.     free(gfx);
  296. }
  297.  
  298. /***************************
  299.     LoadSound : Load a sound/music patch from a file.
  300. ****************************/
  301. Mix_Chunk      *LoadSound( char *datafile )
  302.     Mix_Chunk *tempChunk=NULL;
  303.     char fn[FNLEN];
  304.     int i;
  305.  
  306.     for (i = useEnglish; i<2; i++) {
  307.         sprintf(fn , "%s/sounds/%s", realPath[i], datafile);
  308.         if ( checkFile(fn) ) {
  309.             tempChunk = Mix_LoadWAV(fn);
  310.             if (tempChunk)
  311.                 return tempChunk;
  312.         }
  313.     }
  314.  
  315.     /* didn't find anything... fail peacefully */
  316.  
  317.     return NULL;
  318. }
  319.  
  320. /************************
  321.     LoadMusic : Load
  322.     music from a datafile
  323. *************************/
  324. Mix_Music *LoadMusic(char *datafile )
  325.     char            fn[FNLEN];
  326.     Mix_Music    *tempMusic;
  327.     int i;
  328.  
  329.     for (i = useEnglish; i<2; i++) {
  330.         sprintf( fn , "%s/sounds/%s", realPath[i], datafile );
  331.         if ( checkFile(fn) ) {
  332.             tempMusic = Mix_LoadMUS(fn);
  333.             if (tempMusic)
  334.                 return tempMusic;
  335.         }
  336.     }
  337.  
  338.     /* didn't find anything... fail peacefully */
  339.  
  340.     return NULL;
  341. }
  342.