home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / edu / tux / Tuxtype2-1.5.3-installer.exe / src / theme.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-27  |  8.3 KB  |  321 lines

  1. /***************************************************************************
  2.                           theme.c 
  3.  -  description: theme related code
  4.                              -------------------
  5.     begin                : Jan 6 2003
  6.     copyright            : (C) 2003 by Jesse Andrews et al
  7.     email                : jdandr2@tux4kids.net
  8.  ***************************************************************************/
  9.  
  10. /***************************************************************************
  11.  *                                                                         *
  12.  *   This program is free software; you can redistribute it and/or modify  *
  13.  *   it under the terms of the GNU General Public License as published by  *
  14.  *   the Free Software Foundation; either version 2 of the License, or     *
  15.  *   (at your option) any later version.                                   *
  16.  *                                                                         *
  17.  ***************************************************************************/
  18.  
  19. #include "globals.h"
  20. #include "funcs.h"
  21.  
  22. #define NUM_PATHS 4
  23.  
  24. const char PATHS[NUM_PATHS][FNLEN] = {
  25.     "./data",
  26.     "/usr/share/"PACKAGE"/data",
  27.     "/usr/local/share/"PACKAGE"/data",
  28.     DATA_PREFIX"/share/"PACKAGE"/data"
  29. };
  30.  
  31. SDL_Surface *letters[255] = { NULL };
  32. unsigned char ALPHABET[256];
  33. unsigned char KEYMAP[256];
  34. unsigned char FINGER[256][10];
  35. int ALPHABET_SIZE;
  36. unsigned char realPath[2][FNLEN];
  37. char themeName[FNLEN];
  38. int useEnglish;
  39.  
  40.  
  41. #define MAX_LANGUAGES 100
  42.  
  43. /* setup_theme: this function is in charge of setting up
  44.  * the paths that the loaders use.  It will set a variable
  45.  * numPaths = 1 or 2.  1 if we are just using the default,
  46.  * 2 if there are two themes.  Then it will edit the varible
  47.  * Paths[].  It will always put the theme path first, then
  48.  * the default path
  49.  */
  50. void setupTheme( char *dirName ) {
  51.     static struct stat dirStats;
  52.     int i;
  53.  
  54.     useEnglish=1; // default is to use English if we cannot find theme
  55.  
  56.     for (i=0; i<NUM_PATHS; i++) {
  57.         dirStats.st_mode = 0;
  58.         stat( PATHS[i], &dirStats );
  59.         if (S_IFDIR & dirStats.st_mode) {
  60.             strncpy( realPath[1], PATHS[i], FNLEN-1);
  61.             strncpy( themeName, "", FNLEN-1 );
  62.         }
  63.     }
  64.  
  65.     if (dirName != NULL) {
  66.         char fullDir[FNLEN];
  67.  
  68.         /* find the path to the theme */
  69.         sprintf( fullDir, "%s/themes/%s", realPath[1], dirName );
  70.         dirStats.st_mode = 0;
  71.         stat( fullDir, &dirStats );
  72.         if (S_IFDIR & dirStats.st_mode) {
  73.             useEnglish=0;
  74.             strncpy( realPath[0], fullDir, FNLEN-1 );
  75.             strncpy( themeName, dirName, FNLEN-1 );
  76.         }
  77.     }
  78. }
  79.  
  80. void chooseTheme( void ) {
  81.     SDL_Surface *titles[MAX_LANGUAGES];
  82.     SDL_Surface *select[MAX_LANGUAGES];
  83.     SDL_Surface *left, *right;
  84.     SDL_Rect leftRect, rightRect;
  85.     SDL_Surface *world, *map, *photo;
  86.     SDL_Rect worldRect, photoRect;
  87.     SDL_Rect titleRects[8];
  88.     int stop = 0;
  89.     int loc = 0;
  90.     int old_loc = 1;
  91.  
  92.     int themes = 1;
  93.     int i;
  94.     unsigned char fn[FNLEN];
  95.     unsigned char themeNames[MAX_LANGUAGES][FNLEN];
  96.     unsigned char themePaths[MAX_LANGUAGES][FNLEN];
  97.  
  98.     int old_useEnglish;
  99.     char old_realPath[FNLEN];
  100.  
  101.     DIR *themesDir;
  102.     struct dirent *themesFile;
  103.     struct stat fileStats;
  104.  
  105.     old_useEnglish = useEnglish;
  106.     strncpy( old_realPath, realPath[0], FNLEN-1 );
  107.  
  108.     sprintf( fn, "%s/themes/", realPath[1]);
  109.     themesDir = opendir(fn);
  110.  
  111.     do {
  112.         themesFile = readdir(themesDir);
  113.         if (!themesFile)
  114.             break;
  115.  
  116.         /* we ignore any hidden file and CVS */
  117.  
  118.         if (themesFile->d_name[0] == '.') 
  119.             continue;
  120.  
  121.         if (strcmp("CVS", themesFile->d_name)==0)
  122.             continue;
  123.  
  124.         /* check to see if it is a directory */
  125.         sprintf( fn, "%s/themes/%s", realPath[1], themesFile->d_name);
  126.  
  127.         fileStats.st_mode=0;
  128.         stat( fn, &fileStats );
  129.  
  130.         if (S_IFDIR & fileStats.st_mode) {
  131.             /* HACK: we should get the names from file :) */
  132.             strncpy( themeNames[themes], themesFile->d_name, FNLEN-1);
  133.             strncpy( themePaths[themes++], themesFile->d_name, FNLEN-1 );
  134.         }
  135.     } while (1);
  136.  
  137.     closedir(themesDir);
  138.  
  139.     useEnglish = 1;
  140.         // HACK: is font empty now???
  141.     font = LoadFont( ttf_font, ttf_font_size );
  142.  
  143.     titles[0] = black_outline( "english", font, &white );
  144.     select[0] = black_outline( "english", font, &yellow);
  145.     for (i = 1; i<themes; i++) {
  146.         titles[i] = black_outline( themeNames[i], font, &white );
  147.         select[i] = black_outline( themeNames[i], font, &yellow);
  148.     }
  149.  
  150.     world = LoadImage("world.png", IMG_ALPHA);
  151.     worldRect.x = 480 - (world->w/2);
  152.     worldRect.w = world->w;
  153.     worldRect.y = 10;
  154.     worldRect.h = world->h;
  155.  
  156.     
  157.  
  158.     TTF_CloseFont(font);
  159.  
  160.     useEnglish = old_useEnglish;
  161.  
  162.     bkg = LoadImage("main_bkg.png", IMG_REGULAR);
  163.  
  164.     left = LoadImage("left.png", IMG_ALPHA);
  165.     leftRect.w = left->w; leftRect.h = left->h;
  166.     leftRect.x = 160 - 80 - (leftRect.w/2); leftRect.y = 430;
  167.  
  168.     right = LoadImage("right.png", IMG_ALPHA);
  169.     rightRect.w = right->w; rightRect.h = right->h;
  170.     rightRect.x = 160 + 80 - (rightRect.w/2); rightRect.y = 430;
  171.  
  172.     /* set initial rect sizes */
  173.     titleRects[0].y = 30;
  174.     titleRects[0].w = titleRects[0].h = titleRects[0].x = 0;
  175.     for (i = 1; i<8; i++) {
  176.         titleRects[i].y = titleRects[i-1].y + 50;
  177.         titleRects[i].w = titleRects[i].h = titleRects[i].x = 0;
  178.     }
  179.     
  180.  
  181.     while (!stop) {
  182.         while (SDL_PollEvent(&event)) 
  183.             switch (event.type) {
  184.                 case SDL_QUIT:
  185.                     exit(0);
  186.                     break;
  187.                 case SDL_MOUSEMOTION: 
  188.                     for (i=0; (i<8) && (loc-(loc%8)+i<themes); i++)
  189.                         if (inRect( titleRects[i], event.motion.x, event.motion.y )) {
  190.                             loc = loc-(loc%8)+i;
  191.                             break;
  192.                         }
  193.                     
  194.                     break;
  195.                 case SDL_MOUSEBUTTONDOWN: 
  196.                     if (inRect( leftRect, event.button.x, event.button.y )) 
  197.                         if (loc-(loc%8)-8 >= 0) {
  198.                             loc=loc-(loc%8)-8;
  199.                             break;
  200.                         }
  201.                     if (inRect( rightRect, event.button.x, event.button.y )) 
  202.                         if (loc-(loc%8)+8 < themes) {
  203.                             loc=loc-(loc%8)+8;
  204.                             break;
  205.                         }
  206.                     for (i=0; (i<8) && (loc-(loc%8)+i<themes); i++) 
  207.                         if (inRect(titleRects[i], event.button.x, event.button.y)) {
  208.                             loc = loc-(loc%8)+i;
  209.                             if (loc) {
  210.                                 /* --- set theme --- */
  211.                                 setupTheme(themePaths[loc]);
  212.                             } else {
  213.                                 /* --- english --- */
  214.                                 setupTheme(NULL);
  215.                             }
  216.                             stop = 1;
  217.                             break;
  218.                         }
  219.                     break;
  220.                 case SDL_KEYDOWN:
  221.                     if (event.key.keysym.sym == SDLK_ESCAPE) { 
  222.                         useEnglish = old_useEnglish;
  223.                         strncpy( realPath[0], old_realPath, FNLEN-1 );
  224.                         stop = 1; 
  225.                         break; 
  226.                     }
  227.                     if (event.key.keysym.sym == SDLK_RETURN) { 
  228.                         if (loc) {
  229.                             /* --- set theme --- */
  230.                             setupTheme(themePaths[loc]);
  231.                         } else {
  232.                             /* --- english --- */
  233.                             setupTheme(NULL);
  234.                         }
  235.                         stop = 1;
  236.                         break;
  237.                     }
  238.  
  239.                     if ((event.key.keysym.sym == SDLK_LEFT) || (event.key.keysym.sym == SDLK_PAGEUP)) {
  240.                         if (loc-(loc%8)-8 >= 0) 
  241.                             loc=loc-(loc%8)-8;
  242.                     }
  243.  
  244.                     if ((event.key.keysym.sym == SDLK_RIGHT) || (event.key.keysym.sym == SDLK_PAGEDOWN)) {
  245.                         if (loc-(loc%8)+8 < themes)
  246.                             loc=(loc-(loc%8)+8);
  247.                     }
  248.  
  249.  
  250.                     if (event.key.keysym.sym == SDLK_UP) {
  251.                         if (loc > 0)
  252.                             loc--;
  253.                     }
  254.  
  255.                     if (event.key.keysym.sym == SDLK_DOWN) {
  256.                         if (loc+1<themes)
  257.                             loc++;
  258.                     }
  259.             }
  260.  
  261.         if (old_loc != loc) {
  262.             int start;
  263.  
  264.             SDL_BlitSurface( bkg, NULL, screen, NULL );
  265.  
  266.             SDL_BlitSurface( world, NULL, screen, &worldRect );
  267.  
  268.                 if (loc) setupTheme(themePaths[loc]); else setupTheme(NULL);
  269.  
  270.             map = LoadImage( "map.png", IMG_ALPHA|IMG_NOT_REQUIRED );
  271.             if (map) {
  272.                 SDL_BlitSurface( map, NULL, screen, &worldRect );
  273.                 SDL_FreeSurface( map );
  274.             }
  275.  
  276.             photo = LoadImage( "photo.png", IMG_ALPHA|IMG_NOT_REQUIRED );
  277.             if (photo) {
  278.                 photoRect.x = 480 - (photo->w/2);
  279.                 photoRect.y = 250;
  280.                 photoRect.w = photo->w;
  281.                 photoRect.h = photo->h;
  282.                 SDL_BlitSurface( photo, NULL, screen, &photoRect );
  283.                 SDL_FreeSurface( photo );
  284.             }
  285.  
  286.             start = loc - (loc % 8);
  287.             for (i = start; i<MIN(start+8,themes); i++) {
  288.                 titleRects[i%8].x = 160 - (titles[i]->w/2);
  289.                 if (i == loc)
  290.                     SDL_BlitSurface(select[loc], NULL, screen, &titleRects[i%8]);
  291.                 else
  292.                     SDL_BlitSurface(titles[i], NULL, screen, &titleRects[i%8]);
  293.             }
  294.  
  295.             /* --- draw buttons --- */
  296.  
  297.             if (start>0) 
  298.                 SDL_BlitSurface( left, NULL, screen, &leftRect );
  299.  
  300.             if (start+8<themes) 
  301.                 SDL_BlitSurface( right, NULL, screen, &rightRect );
  302.  
  303.             SDL_UpdateRect(screen, 0, 0, 0 ,0);
  304.         }
  305.         SDL_Delay(40);
  306.         old_loc = loc;
  307.     }
  308.  
  309.     /* --- clear graphics before quiting --- */ 
  310.  
  311.     for (i = 0; i<themes; i++) {
  312.         SDL_FreeSurface(titles[i]);
  313.         SDL_FreeSurface(select[i]);
  314.     }
  315.  
  316.     SDL_FreeSurface(world);
  317.     SDL_FreeSurface(bkg);
  318.     SDL_FreeSurface(left);
  319.     SDL_FreeSurface(right);
  320. }
  321.