home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / edu / tux / Tuxtype2-1.5.3-installer.exe / src / alphabet.c next >
Encoding:
C/C++ Source or Header  |  2004-01-21  |  6.8 KB  |  307 lines

  1. /***************************************************************************
  2.                           alphabet.c 
  3.  -  description: Init SDL
  4.                              -------------------
  5.     begin                : Jan 6 2003
  6.     copyright            : (C) 2003 by Jesse Andrews
  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. /* the colors we use throughout the game */
  23.  
  24. SDL_Color black;
  25. SDL_Color gray;
  26. SDL_Color dark_blue;
  27. SDL_Color red;
  28. SDL_Color white;
  29. SDL_Color yellow;
  30.  
  31.  
  32. /* --- setup the alphabet --- */
  33. void set_letters(unsigned char *t) {
  34.     int i;
  35.  
  36.     ALPHABET_SIZE = 0;
  37.     for (i=0; i<256; i++)
  38.         ALPHABET[i]=0;
  39.  
  40.     for (i=0; i<strlen(t); i++)
  41.         if (t[i]!=' ') {
  42.             ALPHABET[(int)t[i]]=1;
  43.             ALPHABET_SIZE++;
  44.         }
  45. }
  46.  
  47. void clear_keyboard( void ) {
  48.     int i,j;
  49.  
  50.     ALPHABET_SIZE = 0;
  51.     for (i=0; i<256; i++) {
  52.         ALPHABET[i]=0;
  53.         for (j=0; j<10; j++)
  54.             FINGER[i][j]=0;
  55.         KEYMAP[i]=i;
  56.     }
  57. }
  58.  
  59. void LoadKeyboard( void ) {
  60.     unsigned char fn[FNLEN];
  61.     int l;
  62.  
  63.     clear_keyboard();
  64.  
  65.     for (l=useEnglish; l<2; l++) {
  66.         sprintf( fn , "%s/keyboard.lst", realPath[l]);
  67.         if ( checkFile(fn) ) {
  68.             unsigned char str[255];
  69.             FILE *f;
  70.             int i,j;
  71.  
  72.             f = fopen( fn, "r" );
  73.  
  74.             if (f == NULL)
  75.                 continue;
  76.  
  77.             do {
  78.                 fscanf( f, "%[^\n]\n", str);
  79.                 if (strlen(str) > 3) {
  80.  
  81.                     /* format is: FINGER(s)|Char(s) Upper/Lower */
  82.  
  83.                     /* advance past the fingers */
  84.  
  85.                     for (i=0; i<strlen(str) && str[i] != '|'; i++);
  86.  
  87.                     i++; // pass the '|'
  88.                     j = i; 
  89.                     ALPHABET[(int)str[j]] = 1;  // first character is default
  90.  
  91.                     for (i++; i<strlen(str); i++)
  92.                         KEYMAP[(int)str[i]] = str[j];
  93.  
  94.                     /* set the fingers for this letter */
  95.  
  96.                     for (i=0; i<j-1; i++)
  97.                         if (str[i]>='0' && str[i]<='9')
  98.                             FINGER[str[j]][(int)(str[i]-'0')]=1;
  99.  
  100.                     ALPHABET_SIZE++;
  101.                 }
  102.  
  103.             } while (!feof(f));
  104.  
  105.             fclose(f);
  106.  
  107.             return;
  108.         }
  109.     }
  110.  
  111.     fprintf( stderr, "Error finding file for keyboard setup!\n" );
  112. }
  113.  
  114. SDL_Surface* black_outline(unsigned char *t, TTF_Font *font, SDL_Color *c) {
  115.     SDL_Surface *out, *tmp;
  116.     SDL_Rect dstrect;
  117.  
  118.     /* --- create the blocky black "outline" of the text --- */
  119.         
  120.         DEBUGCODE { fprintf( stderr, "black_outline of \"%s\"\n", t ); }
  121.  
  122.     tmp = TTF_RenderText_Solid(font, t, black);
  123.     out = SDL_CreateRGBSurface(SDL_SWSURFACE, (tmp->w)+5, (tmp->h)+5, 32, rmask, gmask, bmask, amask);
  124.  
  125.     dstrect.w = tmp->w;
  126.     dstrect.h = tmp->h;
  127.  
  128.         for (dstrect.x = 1; dstrect.x < 4; dstrect.x++)
  129.             for (dstrect.y = 1; dstrect.y < 4; dstrect.y++)
  130.                 SDL_BlitSurface( tmp, NULL, out, &dstrect );
  131.  
  132.     SDL_FreeSurface(tmp);
  133.  
  134.     /* --- Put the color version of the text on top! --- */
  135.  
  136.     tmp = TTF_RenderText_Blended(font, t, *c);
  137.  
  138.     dstrect.x = dstrect.y = 2;
  139.  
  140.     SDL_BlitSurface(tmp, NULL, out, &dstrect);
  141.  
  142.     SDL_FreeSurface(tmp);
  143.  
  144.     /* --- Convert to the screen format for quicker blits --- */
  145.  
  146.     tmp = SDL_DisplayFormatAlpha(out);
  147.     SDL_FreeSurface(out);
  148.  
  149.     return tmp;
  150. }
  151.  
  152. void show_letters( void ) {
  153.     int i, l=0;
  154.     SDL_Surface *abit;
  155.     SDL_Rect dst;
  156.     int stop = 0;
  157.     unsigned char t[255];
  158.  
  159.     for (i=0; i<256; i++)
  160.         if (ALPHABET[i])
  161.             t[l++]=i;
  162.  
  163.     t[l] = 0;
  164.  
  165.     abit = black_outline(t, font, &white);
  166.  
  167.     dst.x = 320 - (abit->w / 2);
  168.     dst.y = 275;
  169.     dst.w = abit->w;
  170.     dst.h = abit->h;
  171.  
  172.     SDL_BlitSurface(abit, NULL, screen, &dst);
  173.  
  174.     SDL_FreeSurface(abit);
  175.  
  176.     abit = black_outline("Alphabet Set To:", font, &white);
  177.     dst.x = 320 - (abit->w / 2);
  178.     dst.y = 200;
  179.     dst.w = abit->w;
  180.     dst.h = abit->h;
  181.  
  182.     SDL_BlitSurface(abit, NULL, screen, &dst);
  183.  
  184.     SDL_UpdateRect(screen, 0, 0, 0 ,0);
  185.  
  186.     while (!stop) 
  187.         while (SDL_PollEvent(&event)) 
  188.             switch (event.type) {
  189.                 case SDL_QUIT:
  190.                     exit(0);
  191.                 case SDL_KEYDOWN:
  192.                 case SDL_MOUSEBUTTONDOWN:
  193.                     stop = 1;
  194.             }
  195.  
  196.     SDL_FreeSurface(abit);
  197. }
  198.  
  199. /* --- get a letter --- */
  200. unsigned char get_letter(void) {
  201.     static int last = -1; // we don't want to return same letter twice in a row
  202.     int letter;
  203.     do {
  204.         letter = int_rand(0,255);
  205.     } while ((letter == last && ALPHABET_SIZE > 1) || ALPHABET[letter] == 0);
  206.  
  207.     last = letter;
  208.  
  209.     return letter;
  210. }
  211.  
  212. /******************************************************************************
  213. *                           WORD FILE & DATA STRUCTURE                        *
  214. ******************************************************************************/
  215.  
  216. int WORD_qty = 0;
  217. unsigned char WORDS[MAX_NUM_WORDS][MAX_WORD_SIZE+1];
  218.  
  219. /* WORDS_init: clears the number of words
  220.  */
  221. void WORDS_init( void ) {
  222.     WORD_qty = 0;
  223. }
  224.  
  225. /* WORDS_use_alphabet: setups the WORDS so that it really
  226.  * returns a LETTER when WORDS_get() is called
  227.  */
  228. void WORDS_use_alphabet( void ) {
  229.     int i;
  230.     WORD_qty=0;
  231.     for (i=0; i<255; i++) 
  232.         if (ALPHABET[i]) {
  233.             WORDS[WORD_qty][0] = (unsigned char)i;
  234.             WORDS[WORD_qty][1] = '\0';
  235.             WORD_qty++;
  236.         }
  237. }
  238.  
  239. /* WORDS_get: returns a random word that wasn't returned
  240.  * the previous time (unless there is only 1 word!!!)
  241.  */
  242. unsigned char* WORDS_get( void ) {
  243.     static int last_choice=-1;
  244.     int choice;
  245.  
  246.     do {
  247.         choice = int_rand( 0, WORD_qty );
  248.     } while ((choice == last_choice) || WORD_qty < 2);
  249.  
  250.     last_choice = choice;
  251.  
  252.     return WORDS[choice];
  253. }
  254.  
  255.  
  256.  
  257. /* WORDS_use: adds the words from a given wordlist
  258.  * it ignores any words too long or that has bad
  259.  * character (such as #)
  260.  */
  261. void WORDS_use( char *wordFn ) {
  262.     int j;
  263.     unsigned char temp_word[FNLEN];
  264.     FILE *wordFile=NULL;
  265.  
  266.     /* --- open the file --- */
  267.  
  268.     wordFile = fopen( wordFn, "r" );
  269.  
  270.     if ( wordFile == NULL ) {
  271.         fprintf(stderr, "ERROR: could not load wordlist: %s\n", wordFn );
  272.         fprintf(stderr, "Using ALPHABET instead\n");
  273.         WORDS_use_alphabet( );
  274.         return;
  275.     }
  276.  
  277.     
  278.     /* --- load words for this curlevel ---  */
  279.  
  280.  
  281.     DEBUGCODE { fprintf(stderr, "WORD FILE OPENNED @ %s\n", wordFn); }
  282.  
  283.     /* ignore the title */
  284.     fscanf( wordFile, "%[^\n]\n", temp_word);
  285.     while (!feof(wordFile) && (WORD_qty < MAX_NUM_WORDS)) {
  286.         fscanf( wordFile, "%[^\n]\n", temp_word);
  287.  
  288.         for (j = 0; j < strlen(temp_word); j++)
  289.             if (temp_word[j] == '\n' || temp_word[j] == '\r')
  290.                 temp_word[j] = '\0';
  291.  
  292.         /* --- check its size, if too big, ignore it --- */
  293.  
  294.         if (strlen(temp_word) >= 1 && strlen(temp_word) <= MAX_WORD_SIZE) {
  295.  
  296.             /* --- add word --- */
  297.             if (WORD_qty < MAX_NUM_WORDS)
  298.                 strcpy( WORDS[WORD_qty++], temp_word );
  299.         }
  300.     }
  301.  
  302.     if (WORD_qty == 0)
  303.         WORDS_use_alphabet( );
  304.  
  305.     fclose(wordFile);
  306. }
  307.