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

  1. /***************************************************************************
  2. practice.c 
  3. -  description: practice module
  4. -------------------
  5. begin                : Friday Jan 25, 2003
  6. copyright            : (C) 2003 by Jesse Andrews
  7. email                : jdandr2@uky.edu
  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. SDL_Surface *hands;
  23. SDL_Surface *hand[11];
  24. SDL_Rect hand_loc, letter_loc;
  25. TTF_Font *font;
  26.  
  27. Mix_Chunk *wrong;
  28.  
  29. void practice_load_media(void) {
  30.     int i;    
  31.     unsigned char fn[FNLEN];
  32.     unsigned char let[5];
  33.  
  34.     LOG("Loading practice media\n");
  35.     for (i=0; i<10; i++) {
  36.         sprintf(fn, "hands/%d.png", i);
  37.         hand[i] = LoadImage(fn, IMG_ALPHA);
  38.     }
  39.     hands = LoadImage("hands/hands.png", IMG_ALPHA);
  40.  
  41.     hand_loc.x = (screen->w/2) - (hand[0]->w/2);
  42.     hand_loc.y = screen->h - (hand[0]->h);
  43.     hand_loc.w = (hand[0]->w);
  44.     hand_loc.h = (hand[0]->h);
  45.  
  46.     bkg = LoadImage("main_bkg.png", IMG_ALPHA);
  47.  
  48.     font = LoadFont( ttf_font, 72 );
  49.  
  50.     wrong = LoadSound("tock.wav");
  51.  
  52.     let[1]=0;
  53.     for (i=1; i<255; i++)
  54.         if (ALPHABET[i]) {
  55.             let[0]=i;
  56.             letters[i] = black_outline(let, font, &white); 
  57.         }
  58.  
  59.     LOG("DONE - Loading practice media\n");
  60. }
  61.  
  62. void practice_unload_media(void) {
  63.     int i;
  64.     SDL_FreeSurface(bkg);
  65.     SDL_FreeSurface(hands);
  66.     TTF_CloseFont(font);
  67.  
  68.     for (i=0; i<10; i++) 
  69.         SDL_FreeSurface(hand[i]);
  70.  
  71.     for (i=1; i<255; i++) 
  72.         if (ALPHABET[i]) 
  73.             SDL_FreeSurface(letters[i]);
  74.  
  75.     Mix_FreeChunk(wrong);
  76. }
  77.  
  78. void show(unsigned char t) {
  79.     SDL_Rect dst;
  80.     dst.x = 320 - (letters[(int)t]->w/2);
  81.     dst.y = 100;
  82.     dst.w = letters[(int)t]->w;
  83.     dst.h = letters[(int)t]->h;
  84.     SDL_BlitSurface(letters[(int)t], NULL, screen, &dst);
  85. }
  86.  
  87. int Practice(void) {
  88.     unsigned char cur_letter=0;
  89.     int quit=0, i;
  90.     Uint32 start=0;
  91.     int state=0;
  92.     practice_load_media();
  93.     do {
  94.         switch (state) {
  95.             case 0:
  96.                 start = SDL_GetTicks();
  97.                 SDL_BlitSurface(bkg, NULL, screen, NULL);
  98.                 SDL_BlitSurface(hands, NULL, screen, &hand_loc);
  99.                 cur_letter = get_letter();
  100.                 show(cur_letter);
  101.                 SDL_Flip(screen);
  102.                 state = 1;
  103.                 break;
  104.             case 1:
  105.                 if (SDL_GetTicks() - start > 500) {
  106.                     for (i=0; i<10; i++)
  107.                         if (FINGER[(int)cur_letter][i])
  108.                             SDL_BlitSurface(hand[i], NULL, screen, &hand_loc);
  109.                     SDL_Flip(screen);
  110.                     state = 2;
  111.                 }
  112.                 break;
  113.             case 2:
  114.                 if (state == 2 && SDL_GetTicks() - start > 1500) {
  115.                     state = 3;
  116.                 }
  117.                 break;
  118.             case 3:
  119.                 SDL_BlitSurface(hands, NULL, screen, &hand_loc);
  120.                 SDL_Flip(screen);
  121.                 state = 12;
  122.                 break;
  123.             case 4:
  124.                 for (i=0; i<10; i++)
  125.                     if (FINGER[(int)cur_letter][i])
  126.                         SDL_BlitSurface(hand[i], NULL, screen, &hand_loc);
  127.                 SDL_Flip(screen);
  128.                 state = 11;
  129.                 break;
  130.             default:
  131.                 state -= 2; // this is to make the flashing slower
  132.         }
  133.  
  134.         while (SDL_PollEvent(&event)) {
  135.             if (event.type == SDL_QUIT)
  136.                 quit = 1;
  137.             if (event.type == SDL_KEYDOWN) {
  138.                 if (event.key.keysym.sym == SDLK_ESCAPE)
  139.                     quit = 1;
  140.                 if (KEYMAP[event.key.keysym.unicode & 0xff] == cur_letter)
  141.                     state = 0;
  142.                 else
  143.                     playsound(wrong);
  144.             }
  145.  
  146.         }
  147.         SDL_Delay(33);
  148.     } while (!quit);
  149.     practice_unload_media();
  150.     return 1;
  151. }
  152.  
  153.