home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / edu / tux / Tuxtype2-1.5.3-installer.exe / src / pause.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-03-25  |  8.2 KB  |  338 lines

  1. /***************************************************************************
  2. pause.c 
  3. -  description: pause screen for the games
  4. -------------------
  5. begin                : Jan 22, 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. Mix_Chunk *pause_sfx;
  23. SDL_Surface *up, *down, *left, *right;
  24. SDL_Rect rectUp, rectDown, rectLeft, rectRight;
  25. TTF_Font *f1, *f2;
  26. extern settings localsettings;
  27.  
  28. void pause_load_media(void) {
  29.     if (sys_sound) 
  30.         pause_sfx = LoadSound( "tock.wav" );
  31.  
  32.     up = LoadImage("up.png", IMG_ALPHA);
  33.     rectUp.w = up->w; rectUp.h = up->h;
  34.  
  35.     down = LoadImage("down.png", IMG_ALPHA);
  36.     rectDown.w = down->w; rectDown.h = down->h;
  37.  
  38.     left = LoadImage("left.png", IMG_ALPHA);
  39.     rectLeft.w = left->w; rectLeft.h = left->h;
  40.  
  41.     right = LoadImage("right.png", IMG_ALPHA);
  42.     rectRight.w = right->w; rectRight.h = right->h;
  43.  
  44.     f1 = LoadFont( ttf_font, 24 );
  45.     f2 = LoadFont( ttf_font, 36 );
  46. }
  47.  
  48. void pause_unload_media(void) {
  49.     if (sys_sound)
  50.         Mix_FreeChunk(pause_sfx);
  51.     SDL_FreeSurface(up);
  52.     SDL_FreeSurface(down);
  53.     SDL_FreeSurface(left);
  54.     SDL_FreeSurface(right);
  55.     TTF_CloseFont(f1);
  56.     TTF_CloseFont(f2);
  57. }
  58.  
  59. void pause_draw_info(void) {
  60.     SDL_Rect s;
  61.     SDL_Surface *t;
  62.  
  63.     rectLeft.y = rectRight.y = 200;
  64.     rectDown.y = rectUp.y = 300;
  65.  
  66.     rectLeft.x = rectDown.x = 320 - (7*16) - rectLeft.w - 4;
  67.     rectRight.x = rectUp.x  = 320 + (7*16) + 4;
  68.  
  69.     if (sys_sound) {
  70.  
  71.         SDL_BlitSurface(left, NULL, screen, &rectLeft);
  72.         SDL_BlitSurface(right, NULL, screen, &rectRight);
  73.  
  74.         SDL_BlitSurface(down, NULL, screen, &rectDown);
  75.         SDL_BlitSurface(up, NULL, screen, &rectUp);
  76.     }
  77.  
  78.     if (sys_sound) {
  79.  
  80.         t = black_outline(_("Sound Effects Volume"), f1, &white);
  81.         s.y = 160;
  82.         s.x = 320 - t->w/2;
  83.         SDL_BlitSurface(t, NULL, screen, &s);
  84.         SDL_FreeSurface(t);
  85.  
  86.         t = black_outline(_("Music Volume"), f1, &white);
  87.         s.y = 260;
  88.         s.x = 320 - t->w/2;
  89.         SDL_BlitSurface(t, NULL, screen, &s);
  90.         SDL_FreeSurface(t);
  91.  
  92.     } else {
  93.  
  94.         t = black_outline(_("Sound & Music Disabled"), f1, &white);
  95.         s.y = 160;
  96.         s.x = 320 - t->w/2;
  97.         SDL_BlitSurface(t, NULL, screen, &s);
  98.         SDL_FreeSurface(t);
  99.     }
  100.  
  101.     t = black_outline(_("Paused!"), f2, &white);
  102.     s.y = 60;
  103.     s.x = 320 - t->w/2;
  104.     SDL_BlitSurface(t, NULL, screen, &s);
  105.     SDL_FreeSurface(t);
  106.  
  107.     t = black_outline(_("Press escape again to return to menu"), f1, &white);
  108.     s.y = 400;
  109.     s.x = 320 - t->w/2;
  110.     SDL_BlitSurface(t, NULL, screen, &s);
  111.     SDL_FreeSurface(t);
  112.  
  113.     t = black_outline(_("Press space bar to return to game"), f1, &white);
  114.     s.y = 440;
  115.     s.x = 320 - t->w/2;
  116.     SDL_BlitSurface(t, NULL, screen, &s);
  117.     SDL_FreeSurface(t);
  118. }
  119.  
  120. void draw_vols(int sfx, int mus) {
  121.     SDL_Rect s,m;
  122.     int i;
  123.  
  124.     s.y = rectLeft.y; 
  125.     m.y = rectDown.y;
  126.     m.w = s.w = 5;
  127.     s.x = rectLeft.x + rectLeft.w + 5;
  128.     m.x = rectDown.x + rectDown.w + 5;
  129.     m.h = s.h = 40;
  130.  
  131.     for (i = 1; i<=32; i++){
  132.         if (sfx >= i*4)
  133.             SDL_FillRect(screen, &s, SDL_MapRGB(screen->format, 0, 0, 127+sfx));
  134.         else
  135.             SDL_FillRect(screen, &s, SDL_MapRGB(screen->format, 0, 0, 0));
  136.  
  137.         if (mus >= i*4)
  138.             SDL_FillRect(screen, &m, SDL_MapRGB(screen->format, 0, 0, 127+mus));
  139.         else
  140.             SDL_FillRect(screen, &m, SDL_MapRGB(screen->format, 0, 0, 0));
  141.  
  142.         m.x = s.x += 7;
  143.     }
  144. }
  145.  
  146. /* ==== fillscreen ====
  147.  * RESULT: it will darken the screen by a factor of 4
  148.  * WARNING: only works on 16bit screens right now!
  149.  */
  150. void darkenscreen( void ){
  151.     Uint16 rm = screen->format->Rmask;
  152.     Uint16 gm = screen->format->Gmask;
  153.     Uint16 bm = screen->format->Bmask;
  154.     Uint16 *p; 
  155.     int x, y;
  156.  
  157.     p = screen->pixels;
  158.  
  159.     for (y = 0; y<480; y++) 
  160.         for (x = 0; x<640; x++) {
  161.             *p = (((*p&rm)>>2)&rm) | (((*p&gm)>>2)&gm) | (((*p&bm)>>2)&bm); p++;
  162.         }
  163. }
  164.  
  165. int inRect( SDL_Rect r, int x, int y) {
  166.     if ((x < r.x) || (y < r.y) || (x > r.x + r.w) || (y > r.y + r.h))
  167.         return 0;
  168.     return 1;
  169. }
  170.  
  171. // QUESTION: For usability sake, should escape return to the game
  172. //           and the user have to choose to quit the game, or ???
  173. /**********************
  174. Pause : Pause the game
  175. ***********************/
  176. int Pause( void ) {
  177.     int paused = 1;
  178.     int sfx_volume=0;
  179.     int old_sfx_volume;
  180.     int mus_volume=0;
  181.     int old_mus_volume;
  182.     int mousePressed = 0;
  183.     int quit=0;
  184.     int tocks=0;  // used for keeping track of when a tock has happened
  185.     SDL_Event event;
  186.  
  187.     LOG( "---GAME PAUSED---\n" );
  188.  
  189.     /* --- stop all sounds, play pause noise --- */
  190.  
  191.     if (sys_sound) {
  192.          Mix_Pause(-1);
  193.         Mix_PlayChannel(-1, pause_sfx, 0);
  194.         sfx_volume = Mix_Volume(-1, -1);  // get sfx volume w/o changing it
  195.         mus_volume = Mix_VolumeMusic(-1); // get mus volume w/o changing it
  196.     }
  197.  
  198.     /* --- show the pause screen --- */
  199.  
  200.     SDL_ShowCursor(1);
  201.  
  202.     // Darken the screen...
  203.     darkenscreen(); 
  204.  
  205.     pause_draw_info();
  206.     if (sys_sound) {
  207.         draw_vols(sfx_volume, mus_volume);
  208.     }
  209.  
  210.     SDL_Flip(screen);
  211.  
  212.     SDL_EnableKeyRepeat( 1, 20 );
  213.  
  214.     /* --- wait for space, click, or exit --- */
  215.  
  216.     while (paused) {
  217.         old_sfx_volume = sfx_volume;
  218.         old_mus_volume = mus_volume;
  219.         while (SDL_PollEvent(&event)) 
  220.             switch (event.type) {
  221.                 case SDL_QUIT: 
  222.                     exit(0);
  223.                     break;
  224.                 case SDL_KEYUP:
  225.                     if (sys_sound && 
  226.                        ((event.key.keysym.sym == SDLK_RIGHT) ||
  227.                         (event.key.keysym.sym == SDLK_LEFT))) 
  228.                             tocks = 0;
  229.                     break;
  230.                 case SDL_KEYDOWN:
  231.                     if (event.key.keysym.sym == SDLK_SPACE) 
  232.                         paused = 0;
  233.                     if (event.key.keysym.sym == SDLK_ESCAPE) {
  234.                         paused = 0;
  235.                         quit = 1;
  236.                     }
  237.                     if (sys_sound) { 
  238.                         if (event.key.keysym.sym == SDLK_RIGHT) 
  239.                             sfx_volume += 4;
  240.                         if (event.key.keysym.sym == SDLK_LEFT) 
  241.                             sfx_volume -= 4;
  242.                         if (event.key.keysym.sym == SDLK_UP) 
  243.                             mus_volume += 4;
  244.                         if (event.key.keysym.sym == SDLK_DOWN) 
  245.                             mus_volume -= 4;
  246.                     }
  247.                     break;
  248.                 case SDL_MOUSEBUTTONDOWN:
  249.                     mousePressed = 1;
  250.                     tocks = 0;
  251.                     break;
  252.                 case SDL_MOUSEBUTTONUP:
  253.                     mousePressed = 0;
  254.                     break;
  255.  
  256.                     break;
  257.             }
  258.         if (sys_sound && mousePressed) {
  259.             int x, y;
  260.  
  261.             SDL_GetMouseState(&x, &y);
  262.             /* check to see if they clicked on a button */
  263.  
  264.             if (inRect(rectUp, x, y)) {
  265.                 mus_volume += 4;
  266.             } else if (inRect(rectDown, x, y)) {
  267.                 mus_volume -= 4;
  268.             } else if (inRect(rectRight, x, y)) {
  269.                 sfx_volume += 4;
  270.             } else if (inRect(rectLeft, x, y)) {
  271.                 sfx_volume -= 4;
  272.             } else {
  273.  
  274.                 /* check to see if they clicked a bar */
  275.  
  276.                 if ((x > rectLeft.x + rectLeft.w) && (x < rectRight.x)) {
  277.                     if ((y >= rectLeft.y) && (y <= rectLeft.y + rectLeft.h)) {
  278.                         sfx_volume = 4+(int)(128.0 * ((x - rectLeft.x - rectLeft.w - 1.0) / (rectRight.x - rectLeft.x - rectLeft.w - 2.0)));
  279.                     }
  280.                     if ((y >= rectDown.y) && (y <= rectDown.y + rectDown.h)) {
  281.                         mus_volume = 4+(int)(128.0 * ((x - rectLeft.x - rectLeft.w - 1.0) / (rectRight.x - rectLeft.x - rectLeft.w - 2.0)));
  282.                     }
  283.  
  284.                 }
  285.             }
  286.         }
  287.  
  288.         if (sys_sound) {
  289.  
  290.             if (sfx_volume > MIX_MAX_VOLUME)
  291.                 sfx_volume = MIX_MAX_VOLUME;
  292.             if (sfx_volume < 0)
  293.                 sfx_volume = 0;
  294.             if (mus_volume > MIX_MAX_VOLUME)
  295.                 mus_volume = MIX_MAX_VOLUME;
  296.             if (mus_volume < 0)
  297.                 mus_volume = 0;
  298.  
  299.             if ((mus_volume != old_mus_volume) || 
  300.                 (sfx_volume != old_sfx_volume)) {
  301.  
  302.                 if (mus_volume != old_mus_volume)
  303.                     Mix_VolumeMusic(mus_volume);
  304.  
  305.                 if (sfx_volume != old_sfx_volume) {
  306.                     Mix_Volume(-1,sfx_volume);
  307.                     if (tocks%4==0)
  308.                         Mix_PlayChannel(-1, pause_sfx, 0);
  309.                     tocks++;
  310.                 }
  311.  
  312.                 draw_vols(sfx_volume, mus_volume);
  313.                 localsettings.mus_volume=mus_volume;
  314.                 localsettings.sfx_volume=sfx_volume;
  315.                 SDL_Flip(screen);
  316.             }
  317.         }
  318.  
  319.         SDL_Delay(33);
  320.     }
  321.  
  322.     /* --- Return to previous state --- */
  323.  
  324.     SDL_EnableKeyRepeat( 0, SDL_DEFAULT_REPEAT_INTERVAL );
  325.  
  326.     SDL_ShowCursor(0);
  327.  
  328.     if (sys_sound) {
  329.         Mix_PlayChannel(-1, pause_sfx, 0);
  330.         Mix_Resume(-1);
  331.     }
  332.  
  333.     LOG( "---GAME RESUMED---\n" );
  334.  
  335.     return (quit);
  336. }
  337.  
  338.