titlescreen.c 39.6 KB
Newer Older
David Bruce's avatar
David Bruce committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/***************************************************************************
 -  file: titlescreen.c
 -  description: splash, title and menu screen functionality 
                            ------------------
    begin                : Thur May 4 2000
    copyright            : (C) 2000 by Sam Hart
                         : (C) 2003 by Jesse Andrews
    email                : tuxtype-dev@tux4kids.net
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "globals.h"
#include "funcs.h"
#include "titlescreen.h"
23
#include "SDL_extras.h"
24
#include "convert_utf.h"
25
#include "editor.h"
David Bruce's avatar
David Bruce committed
26 27 28 29

/* --- media for menus --- */

/* images of regular and selected text of menu items: */
30
static SDL_Surface* reg_text[TITLE_MENU_ITEMS + 1][TITLE_MENU_DEPTH + 1] = {{NULL}};
31
static SDL_Surface* sel_text[TITLE_MENU_ITEMS + 1][TITLE_MENU_DEPTH + 1] = {{NULL}};
David Bruce's avatar
David Bruce committed
32
/* this will contain pointers to all of the menu 'icons' */
33
static sprite* menu_gfx[TITLE_MENU_ITEMS + 1][TITLE_MENU_DEPTH + 1] = {{NULL}};
David Bruce's avatar
David Bruce committed
34 35 36 37 38 39 40 41

/* --- other media --- */
static SDL_Surface* title = NULL;
static SDL_Surface* speaker = NULL;
static SDL_Surface* speakeroff = NULL;
static sprite* Tux = NULL;
static Mix_Chunk* snd_move = NULL;
static Mix_Chunk* snd_select = NULL;
42
static Mix_Chunk* snd_welcome = NULL; 
David Bruce's avatar
David Bruce committed
43 44

/* --- locations we need --- */
45 46 47 48 49
static SDL_Rect text_dst[TITLE_MENU_ITEMS + 1];     // location of menu text
static SDL_Rect menu_gfxdest[TITLE_MENU_ITEMS + 1]; // location of menu sprite
static SDL_Rect menu_button[TITLE_MENU_ITEMS + 1];  // menu mouse event buttons
/* keep track of the width of each menu: */
static int menu_width[TITLE_MENU_DEPTH + 1];
David Bruce's avatar
David Bruce committed
50

51 52 53 54
static SDL_Rect Tuxdest;
static SDL_Rect Titledest;
static SDL_Rect spkrdest;
static SDL_Rect cursor;
David Bruce's avatar
David Bruce committed
55 56

/* Local function prototypes: */
57
static void show_logo(void);
David Bruce's avatar
David Bruce committed
58 59
static int load_media(void);
static void load_menu(void);
60 61
static void recalc_rects(void);
static int chooseWordlist(void);
David Bruce's avatar
David Bruce committed
62 63
static void unload_media(void);
static void unload_menu(void);
64
static void not_implemented(void);
David Bruce's avatar
David Bruce committed
65

66 67
/* --- menu text --- */

68 69
/* --- define menu structure --- */
/* (these values are all in the Game_Type enum in globals.h) */
70
const int menu_item[][6] = {{0, 0,         0,         0,          0},
David Bruce's avatar
David Bruce committed
71
			   {0, CASCADE,   LEVEL1,    LEVEL1,  EDIT_WORDLIST   },
72
			   {0, LASER,     LEVEL2,    LEVEL2,  PHRASE_TYPING },
David Bruce's avatar
David Bruce committed
73 74 75
			   {0, LESSONS,   LEVEL3,    LEVEL3,  PROJECT_INFO    },
			   {0, OPTIONS,   INSTRUCT,  LEVEL4,  SET_LANGUAGE    },
			   {0, QUIT_GAME, MAIN,      MAIN,    MAIN            }};
76 77

/* --- menu icons --- */
78
const char* menu_icon[][6] = 
79 80 81 82 83 84
{{"", "", "", "", ""},
 {"", "cascade", "easy",   "grade1_", "list"   },
 {"", "comet",   "medium", "grade2_", "practice" },
 {"", "lesson","hard",   "grade3_", "keyboard"   },
 {"", "tux_config",  "tutor",  "grade4_", "lang" },
 {"", "quit",    "main",   "main",    "main"   }};
85 86

     
87
static const char* menu_text[] = 
88 89
{"", "",            "",             "",            ""    ,
 "",gettext_noop("Fish Cascade"),gettext_noop("Easy"),gettext_noop("Space Cadet"),gettext_noop("Edit Word Lists"),
90
 "",gettext_noop("Comet Zap"),gettext_noop("Medium"),gettext_noop("Pilot"),gettext_noop("Phrase Typing"),
91 92 93 94 95 96 97
 "",gettext_noop("Lessons"),gettext_noop("Hard"), gettext_noop("Ace"),gettext_noop("Project Info"),
 "", gettext_noop("Options"),gettext_noop("Instructions"),gettext_noop("Commander"),gettext_noop("Setup Language"),
 "", gettext_noop("Quit"),gettext_noop("Main Menu"),gettext_noop("Main Menu"),gettext_noop("Main Menu")};




David Bruce's avatar
David Bruce committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
/************************************************************************/
/*                                                                      */ 
/*         "Public" functions (callable throughout program)             */
/*                                                                      */
/************************************************************************/


/****************************************
* TitleScreen: Display the title screen *
*****************************************
* display title screen, get input
*/
void TitleScreen(void)
{

  Uint32 frame = 0;
  Uint32 start = 0;

116 117 118 119
  /* NOTE for 'depth', think pages like a restaurant menu, */
  /* not heirarchical depth - choice of term is misleading */
  int menu_depth; // how deep we are in the menu

David Bruce's avatar
David Bruce committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  int i, j, tux_frame = 0;
  int done = 0;
  int firstloop = 1;
  int menu_opt = NONE;
  int sub_menu = NONE;
  int update_locs = 1;
  int redraw = 0;
  int key_menu = 1;
  int old_key_menu = 5;


  if (settings.sys_sound)
  {
    settings.menu_sound = 1;
    settings.menu_music = 1;
  }

  start = SDL_GetTicks();


  /*
141
  * Display the Standby screen.... 
David Bruce's avatar
David Bruce committed
142
  */
143
  show_logo();
144 145 146 147 148 149
  snd_welcome = LoadSound("harp.wav");

  if (snd_welcome && settings.menu_sound)
  {
    PlaySound(snd_welcome);
  }
David Bruce's avatar
David Bruce committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  /* Load media and menu data: */
  if (!load_media())
  {
    fprintf(stderr, "TitleScreen - load_media() failed!");
    return;
  }

  SDL_WM_GrabInput(SDL_GRAB_ON); // User input goes to TuxType, not window manager


  /***************************
  * Tux and Title animations *
  ***************************/

  LOG( "->Now Animating Tux and Title onto the screen\n" );

  Tuxdest.x = 0;
  Tuxdest.y = screen->h;
  Tuxdest.w = Tux->frame[0]->w;
  Tuxdest.h = Tux->frame[0]->h;

  Titledest.x = screen->w;
  Titledest.y = 10;
  Titledest.w = title->w;
  Titledest.h = title->h;

177 178
  spkrdest.x = screen->w - speaker->w - 10;
  spkrdest.y = screen->h - speaker->h - 10;
David Bruce's avatar
David Bruce committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  spkrdest.w = speaker->w;
  spkrdest.h = speaker->h;

  /* --- wait if the first time in the game --- */

  if (settings.show_tux4kids)
  {
    while ((SDL_GetTicks() - start) < 2000)
    {
      SDL_Delay(50);
    }
    settings.show_tux4kids = 0;
  }

  SDL_ShowCursor(1);    
  /* FIXME not sure the next line works in Windows: */
195
  TransWipe(CurrentBkgd(), RANDOM_WIPE, 10, 20);
David Bruce's avatar
David Bruce committed
196 197
  /* Make sure background gets drawn (since TransWipe() doesn't */
  /* seem to work reliably as of yet):                          */
198
  SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL);
David Bruce's avatar
David Bruce committed
199 200 201
  SDL_UpdateRect(screen, 0, 0, 0, 0);

  /* --- Pull tux & logo onscreen --- */
202
  for (i = 0; i <= (PRE_ANIM_FRAMES * PRE_FRAME_MULT); i++)
David Bruce's avatar
David Bruce committed
203 204
  {
    start = SDL_GetTicks();
205 206
    SDL_BlitSurface(CurrentBkgd(), &Tuxdest, screen, &Tuxdest);
    SDL_BlitSurface(CurrentBkgd(), &Titledest, screen, &Titledest);
David Bruce's avatar
David Bruce committed
207

208

David Bruce's avatar
David Bruce committed
209 210
    Tuxdest.y -= Tux->frame[0]->h / (PRE_ANIM_FRAMES * PRE_FRAME_MULT);
    Titledest.x -= (screen->w) / (PRE_ANIM_FRAMES * PRE_FRAME_MULT);
211 212 213 214 215
    /* Don't go past 0; */
    if (Tuxdest.y < 0)
      Tuxdest.y = 0;
    if (Titledest.x < 0)
      Titledest.x = 0;
David Bruce's avatar
David Bruce committed
216 217 218 219 220

    SDL_BlitSurface(Tux->frame[0], NULL, screen, &Tuxdest);
    SDL_BlitSurface(title, NULL, screen, &Titledest);

    SDL_UpdateRect(screen, Tuxdest.x, Tuxdest.y, Tuxdest.w, Tuxdest.h);
221
    SDL_UpdateRect(screen, Titledest.x, Titledest.y, Titledest.w + 40, Titledest.h);
David Bruce's avatar
David Bruce committed
222 223 224 225 226 227 228

    while ((SDL_GetTicks() - start) < 33) 
    {
      SDL_Delay(2);
    }
  }

229
  recalc_rects();
David Bruce's avatar
David Bruce committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  /* Pick speaker graphic according to whether music is on: */
  if ( settings.menu_music )
    SDL_BlitSurface(speaker, NULL, screen, &spkrdest);
  else
    SDL_BlitSurface(speakeroff, NULL, screen, &spkrdest);

  /* Start playing menu music if desired: */
  if (settings.menu_music)
    MusicLoad( "tuxi.ogg", -1 );

  LOG( "Tux and Title are in place now\n" );

  SDL_WM_GrabInput(SDL_GRAB_OFF);


  /****************************
  * Main Loop Starts Here ... *
  ****************************/


  menu_depth = 1;
  firstloop = 1;
  Tuxdest.y = screen->h - Tux->frame[0]->h;


  while (!done) 
  {

    start=SDL_GetTicks();

    /* ---process input queue --- */

    menu_opt = NONE; // clear the option so we don't change twice!

    old_key_menu = key_menu;

    /* Retrieve any user interface events: */
    while (SDL_PollEvent(&event))
    {
      switch (event.type)
      {

        case SDL_MOUSEMOTION:
        {
          cursor.x = event.motion.x;
          cursor.y = event.motion.y;
          break;
        }


        /* Handle mouse clicks based on mouse location: */
        case SDL_MOUSEBUTTONDOWN:
        {
          cursor.x = event.motion.x;
          cursor.y = event.motion.y;

          for (j = 1; j <= TITLE_MENU_ITEMS; j++)
          {
289
            if (inRect(menu_button[j], cursor.x, cursor.y))
David Bruce's avatar
David Bruce committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
            {
              menu_opt = menu_item[j][menu_depth];
              if (settings.menu_sound)
              {
                PlaySound(snd_select);
              }
              DEBUGCODE
              {
                fprintf(stderr, "->>BUTTON CLICK menu_opt = %d\n", menu_opt);
                fprintf(stderr, "->J = %d menu_depth=%d\n", j, menu_depth);
              }
            }
          }

          /* If mouse over speaker, toggle menu music off or on: */
305
          if (inRect(spkrdest, cursor.x, cursor.y))
David Bruce's avatar
David Bruce committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
          {
            if (settings.menu_music)
            {
              MusicUnload();
              settings.menu_music = 0;
            }
            else
            {
              settings.menu_music = 1;
              MusicLoad("tuxi.ogg", -1);
            }
            redraw = 1;
          }
          break;
        }



        case SDL_QUIT:
        {
          menu_opt = QUIT_GAME;
          break;
        }


        /* Handle key press events based on key value: */
        case SDL_KEYDOWN:
        {
          switch (event.key.keysym.sym)
          {
            case SDLK_ESCAPE:
            {
              /* Go to main menu (if in submenu) or quit: */
              if (menu_depth != 1) 
                menu_opt = MAIN;
              else
                menu_opt = QUIT_GAME;

              if (settings.menu_sound)
                PlaySound(snd_select);
              break;
            }


            /* Toggle screen mode: */
            case SDLK_F10:
            {
              SwitchScreenMode();
354
              recalc_rects();
David Bruce's avatar
David Bruce committed
355 356 357 358 359 360 361 362 363 364 365
              redraw = 1;
              break;
            }


            /* Toggle menu music: */
            case SDLK_F11:
            {
              if (settings.menu_music)
              {
                MusicUnload( );
366
                settings.menu_music = 0;
David Bruce's avatar
David Bruce committed
367 368 369
              }
              else
              {
370
                settings.menu_music = 1;
David Bruce's avatar
David Bruce committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
                MusicLoad("tuxi.ogg", -1);
              }
              redraw = 1;
              break;
            }


            /* --- reload translation/graphics/media: for themers/translaters --- */
            case SDLK_F12:
            {
              unload_media();
              LoadLang();
              load_media();
              redraw = 1;
              break;
            }


            case SDLK_UP:
            {
              if (settings.menu_sound)
                PlaySound(snd_move);
              key_menu--;
              if (key_menu < 1)
                key_menu = 5;
              break;
            }


            case SDLK_DOWN:
            {
              key_menu++;
              if (settings.menu_sound)
                PlaySound(snd_move);
              if (key_menu > 5)
                key_menu = 1;
              break;
            }


            case SDLK_RETURN:
            {
              if (key_menu)
              {
                menu_opt = menu_item[key_menu][menu_depth];
                if (settings.menu_sound)
                  PlaySound(snd_select);
              }
              break;
            }


            default:     /* Some other key pressed - do nothing: */
            {
              break;
            }
          }             /* End of switch(event.key.keysym.sym) statement */
        }               /* End of case: SDL_KEYDOWN: */


        default:        /* Some other type of SDL event - do nothing;    */
        {
          break;
        }
      }                 /* End of switch(event.type) statement           */
    }	              /* End of while (SDL_PollEvent(&event)) loop     */





    /* --- do menu processing --- */


    if (menu_opt == QUIT_GAME)
      done = 1;


    if (menu_opt == LASER)
    {
      menu_depth = LASER_SUBMENU;
      sub_menu = LASER;
      update_locs = 1;
      redraw = 1;
    }


    if (menu_opt == CASCADE)
    {
      menu_depth = CASCADE_SUBMENU;
      sub_menu = CASCADE;
      update_locs = 1;
      redraw=1;
    }


    if (menu_opt == OPTIONS)
    {
      menu_depth = OPTIONS_SUBMENU;
      sub_menu = OPTIONS;
      update_locs = 1;
      redraw = 1;
    }


    if (menu_opt == MAIN)
    {
      menu_depth = ROOTMENU;
      update_locs = 1;
      redraw = 1;
    }


484
    if (menu_opt == EDIT_WORDLIST)
David Bruce's avatar
David Bruce committed
485
    {
486
      ChooseListToEdit();
David Bruce's avatar
David Bruce committed
487 488 489 490 491 492 493 494 495 496 497 498 499
      redraw = 1;
    }


    if (menu_opt == PROJECT_INFO)
    {
      ProjectInfo();
      redraw = 1;
    }


    if (menu_opt == LESSONS)
    {
500
//      not_implemented();
501
      SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL);
502
      SDL_Flip(screen);
David Bruce's avatar
David Bruce committed
503 504 505 506 507
      unload_media();

      if (settings.menu_music)
        MusicUnload( );

508
      XMLLesson();
David Bruce's avatar
David Bruce committed
509 510

      load_media();
511

David Bruce's avatar
David Bruce committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
      redraw = 1;

      if (settings.menu_music)
        MusicLoad( "tuxi.ogg", -1 );
    }


    if (menu_opt == SET_LANGUAGE)
    {
      unload_media();
      ChooseTheme();
      LoadLang();
      LoadKeyboard();
      load_media();
      redraw = 1;

      if (settings.menu_music)
        MusicLoad( "tuxi.ogg", -1 );
    }


    if (menu_opt == LEVEL1)
    {  
      if (chooseWordlist()) 
      {
        unload_media();

        switch (sub_menu)
        {
          case CASCADE: PlayCascade( EASY ); break;
          case LASER:   PlayLaserGame(  EASY ); break;
        }
      }

      load_media();

      if (settings.menu_music)
        MusicLoad("tuxi.ogg", -1);

      redraw = 1;
    }


    if (menu_opt == LEVEL2)
    {
      if (chooseWordlist())
      {
        unload_media();

        switch (sub_menu)
        {
          case CASCADE: PlayCascade( MEDIUM ); break;
          case LASER:   PlayLaserGame(  MEDIUM ); break;
        }


        if (settings.menu_music)
          MusicLoad( "tuxi.ogg", -1 );
      }

      load_media();
      redraw = 1;
    }



    if (menu_opt == LEVEL3)
    {
      if (chooseWordlist())
      {
        unload_media();

        switch (sub_menu)
        {
          case CASCADE: PlayCascade( HARD ); break;
          case LASER:   PlayLaserGame(  HARD ); break;
        }


        if (settings.menu_music)
          MusicLoad( "tuxi.ogg", -1 );
      }

      load_media();
      redraw = 1;
    }



    if (menu_opt == LEVEL4)
    {
      if (chooseWordlist())
      {
        unload_media();

        switch (sub_menu)
        {
          case CASCADE: PlayCascade( INSANE ); break;
          case LASER:   PlayLaserGame(  INSANE ); break;
        }

        if (settings.menu_music)
          MusicLoad( "tuxi.ogg", -1 );
      }

      load_media();
      redraw = 1;
    }



    if (menu_opt == INSTRUCT)
    {
625 626
//       not_implemented();
       unload_media();
David Bruce's avatar
David Bruce committed
627

628 629 630 631 632
      switch (sub_menu)
      {
        case CASCADE: InstructCascade(); break;
        case LASER:   InstructLaser();   break;
      }
David Bruce's avatar
David Bruce committed
633

634
      load_media();
David Bruce's avatar
David Bruce committed
635

636 637
      if (settings.menu_music)
        MusicLoad( "tuxi.ogg", -1 );
David Bruce's avatar
David Bruce committed
638 639 640 641 642 643

      redraw = 1;
    }



644
    if (menu_opt == PHRASE_TYPING)
David Bruce's avatar
David Bruce committed
645
    {
646 647
//      not_implemented();

David Bruce's avatar
David Bruce committed
648
      unload_media();
649

David Bruce's avatar
David Bruce committed
650
      Phrases(NULL);
651

David Bruce's avatar
David Bruce committed
652
      load_media();
653

David Bruce's avatar
David Bruce committed
654 655
      if (settings.menu_music)
        MusicLoad( "tuxi.ogg", -1 );
656

David Bruce's avatar
David Bruce committed
657
      redraw = 1;
David Bruce's avatar
David Bruce committed
658 659 660 661 662 663 664 665 666
    }

    /* ------ End menu_opt processing ----------- */



    if (redraw)
    {
      LOG("TitleScreen() - redraw requested\n");
667
      recalc_rects();
David Bruce's avatar
David Bruce committed
668

669
      SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL); 
David Bruce's avatar
David Bruce committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
      SDL_BlitSurface(title, NULL, screen, &Titledest);

      if ( settings.menu_music )
        SDL_BlitSurface(speaker, NULL, screen, &spkrdest);
      else
        SDL_BlitSurface(speakeroff, NULL, screen, &spkrdest);

      /* Screen will be updated due to update_locs - see ~30 lines down: */
//      SDL_UpdateRect(screen, 0, 0, 0, 0);
      frame = redraw = 0;   // so we redraw tux
      update_locs = 1;      // so we redraw menu
      firstloop = 1;
    }



    /* --- create new menu screen when needed --- */

    if (update_locs)
    {
      LOG("TitleScreen() - update_locs requested\n");

      /* --- erase the last menu --- */
      for (i = 1; i <= TITLE_MENU_ITEMS; i++)
      {
695
        text_dst[i].x = screen->w/2 - 70;//290;
David Bruce's avatar
David Bruce committed
696 697
        text_dst[i].w = reg_text[i][menu_depth]->w;
        text_dst[i].h = reg_text[i][menu_depth]->h;
698 699
        SDL_BlitSurface(CurrentBkgd(), &menu_button[i], screen, &menu_button[i]);
        menu_button[i].w = menu_width[menu_depth] + 20;
David Bruce's avatar
David Bruce committed
700 701 702 703 704 705 706 707 708 709
      }


      update_locs = 0;

      /* --- draw the full menu --- */

      for (j = 1; j <= TITLE_MENU_ITEMS; j++)
      {
        DOUT(j);
710
        DrawButton(&menu_button[j], 10, REG_RGBA);
David Bruce's avatar
David Bruce committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
        if (reg_text[j][menu_depth] != NULL)
          SDL_BlitSurface(reg_text[j][menu_depth], NULL, screen, &text_dst[j]);
        if (menu_gfx[j][menu_depth] != NULL)
          SDL_BlitSurface(menu_gfx[j][menu_depth]->default_img, NULL, screen, &menu_gfxdest[j]);
      }

      SDL_UpdateRect(screen, 0, 0, 0, 0);

      LOG("TitleScreen() - update_locs completed\n");
    }



    /* --- make tux blink --- */

    switch (frame % TUX6)
    {
      case 0:    tux_frame = 1; break;
      case TUX1: tux_frame = 2; break;
      case TUX2: tux_frame = 3; break;
      case TUX3: tux_frame = 4; break;			
      case TUX4: tux_frame = 3; break;
      case TUX5: tux_frame = 2; break;
      default: tux_frame = 0;
    }

    if (tux_frame)
    {
739 740 741 742 743 744 745 746 747 748 749
      SDL_Rect blink_src, blink_dest;
      blink_src.x = 0;
      blink_src.y = 0;
      blink_src.w = Tuxdest.w;
      blink_src.h = Tuxdest.h;
      blink_dest.x = Tuxdest.x + blink_src.x;
      blink_dest.y = Tuxdest.y + blink_src.y;
      blink_dest.w = blink_src.w;
      blink_dest.h = blink_src.h;
//      SDL_BlitSurface(CurrentBkgd(), , screen, &Tuxdest);
      SDL_BlitSurface(Tux->frame[tux_frame - 1], &blink_src, screen, &blink_dest);
David Bruce's avatar
David Bruce committed
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    }


    /* --- check if mouse is in a menu option --- */

    key_menu = 0;

    for (j = 1; j <= TITLE_MENU_ITEMS; j++)
    {
      if ((cursor.x >= menu_button[j].x && cursor.x <= (menu_button[j].x + menu_button[j].w)) &&
          (cursor.y >= menu_button[j].y && cursor.y <= (menu_button[j].y + menu_button[j].h)))
      {
        key_menu = j; // update menu to point
        break;        // Don't need to check rest of menu
      }
    }


    /* --- return old selection to unselected state --- */

    if (old_key_menu && (key_menu != old_key_menu))
    {
772 773
      SDL_BlitSurface(CurrentBkgd(), &menu_button[old_key_menu], screen, &menu_button[old_key_menu]);
      DrawButton(&menu_button[old_key_menu], 10, REG_RGBA);
David Bruce's avatar
David Bruce committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
      SDL_BlitSurface(reg_text[old_key_menu][menu_depth], NULL, screen, &text_dst[old_key_menu]);
      SDL_BlitSurface(menu_gfx[old_key_menu][menu_depth]->default_img, NULL, screen, &menu_gfxdest[old_key_menu]);
    }


    /* --- draw current selection --- */

    if ((key_menu != 0) &&
       ((old_key_menu != key_menu) || (frame % 5 == 0))) // Redraw every fifth frame?
    {
      if (key_menu != old_key_menu)
      {
        REWIND(menu_gfx[key_menu][menu_depth]);
        PlaySound(snd_move);
      }

790 791
      SDL_BlitSurface(CurrentBkgd(), &menu_button[key_menu], screen, &menu_button[key_menu]);
      DrawButton(&menu_button[key_menu], 10, SEL_RGBA);
David Bruce's avatar
David Bruce committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
      SDL_BlitSurface(sel_text[key_menu][menu_depth], NULL, screen, &text_dst[key_menu]);
      SDL_BlitSurface(menu_gfx[key_menu][menu_depth]->frame[menu_gfx[key_menu][menu_depth]->cur], NULL, screen, &menu_gfxdest[key_menu]);

      NEXT_FRAME(menu_gfx[key_menu][menu_depth]);
    }


    // HACK This is still more than we need to update every frame but
    // it cuts cpu on my machine %60 so it seems better...
    if ( settings.menu_music )
      SDL_BlitSurface(speaker, NULL, screen, &spkrdest);
    else
      SDL_BlitSurface(speakeroff, NULL, screen, &spkrdest);

    SDL_UpdateRect(screen, spkrdest.x, spkrdest.y, spkrdest.w, spkrdest.h);

808
    for (i = 1; i < 6; i++)
David Bruce's avatar
David Bruce committed
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
    {
      SDL_UpdateRect(screen, menu_button[i].x, menu_button[i].y, menu_button[i].w, menu_button[i].h);
    }

    if (tux_frame)
      SDL_UpdateRect(screen, Tuxdest.x, Tuxdest.y, Tuxdest.w, Tuxdest.h);

    if (firstloop)
      SDL_UpdateRect(screen, Tuxdest.x, Tuxdest.y, Tuxdest.w, Tuxdest.h);

    firstloop = 0;

    /* Wait so we keep frame rate constant: */
    while ((SDL_GetTicks() - start) < 33)
    {
      SDL_Delay(20);
    }

    frame++;
  } /* ----------- End of 'while(!done)' loop ------------  */



  LOG( "->>Freeing title screen images\n" );

  unload_media();

  LOG( "->TitleScreen():END \n" );
}



/************************************************************************/
/*                                                                      */ 
/*       "Private" functions (local to titlescreen.c)                   */
/*                                                                      */
/************************************************************************/

847 848 849 850 851 852
static void show_logo(void)
{
  if (settings.show_tux4kids)
  {
    SDL_Rect logo_rect;
    SDL_Surface* t4k_logo = LoadImage("standby.png", IMG_REGULAR|IMG_NO_THEME);
853

854 855 856 857 858 859
    if (t4k_logo) /* Avoid segfault */
    { 
      logo_rect.x = screen->w/2 - t4k_logo->w/2;  // Center horizontally
      logo_rect.y = screen->h/2 - t4k_logo->h/2;  // Center vertically
      logo_rect.w = t4k_logo->w;
      logo_rect.h = t4k_logo->h;
David Bruce's avatar
David Bruce committed
860

861 862 863 864 865 866 867 868 869
      /* Black out screen: */
      SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
      SDL_BlitSurface(t4k_logo, NULL, screen, &logo_rect);
      SDL_UpdateRect(screen, 0, 0, 0, 0);
      SDL_FreeSurface(t4k_logo);  // Unload image
    }
    else
      fprintf(stderr, "Couldn't load 'standby.png'\n");
  }
David Bruce's avatar
David Bruce committed
870 871
}

872 873 874

/* Renders the menu text for all items and loads the associated */
/* animated sprites, if any.                                    */
David Bruce's avatar
David Bruce committed
875 876
static void load_menu(void)
{
877
  char fn[FNLEN];
David Bruce's avatar
David Bruce committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891
  int max, i, j;

  SDL_ShowCursor(1);

  LOG("loading & parsing menu\n");

  for (j = 1; j <= TITLE_MENU_DEPTH; j++)  /* Each 'depth' is a different menu */
  {
    max = 0;
    for (i = 1; i <= TITLE_MENU_ITEMS; i++)
    {
      DEBUGCODE
      {
        fprintf(stderr, "i = '%d'\tj = '%d'\ttext = '%s'\n",
892
                i, j,  gettext(menu_text[j + 5 * i]));
David Bruce's avatar
David Bruce committed
893 894 895
      }

      /* --- create text surfaces --- */
896 897
      reg_text[i][j] = BlackOutline( gettext(menu_text[j + 5 * i]), DEFAULT_MENU_FONT_SIZE, &white);
      sel_text[i][j] = BlackOutline( gettext(menu_text[j + 5 * i]), DEFAULT_MENU_FONT_SIZE, &yellow);
David Bruce's avatar
David Bruce committed
898 899 900 901 902 903 904 905 906 907 908 909

      /* (first make sure ptr valid to avoid segfault) */
      if (sel_text[i][j] && sel_text[i][j]->w > max)
        max = sel_text[i][j]->w;

      /* --- load animated icon for menu item --- */
      sprintf(fn, "menu/%s", menu_icon[i][j]);
      menu_gfx[i][j] = LoadSprite(fn, IMG_ALPHA);
    }
    menu_width[j] = max + 20 + 40; // Not clear where '20' and '40' are coming from
  }

910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
  recalc_rects();
}



static void recalc_rects(void)
{
  int i = 0;

  Tuxdest.x = 0;
  Tuxdest.y = screen->h - Tux->frame[0]->h;
  Tuxdest.w = Tux->frame[0]->w;
  Tuxdest.h = Tux->frame[0]->h;

  Titledest.x = 0;
  Titledest.y = 10;
  Titledest.w = title->w;
  Titledest.h = title->h;

  spkrdest.x = screen->w - speaker->w - 10;
  spkrdest.y = screen->h - speaker->h - 10;
  spkrdest.w = speaker->w;
  spkrdest.h = speaker->h;
David Bruce's avatar
David Bruce committed
933 934

  /* --- setup menu item destinations --- */
935
  menu_button[1].x = screen->w/2 - 120; //240;
David Bruce's avatar
David Bruce committed
936 937
  menu_button[1].y = 100;
  menu_button[1].w = menu_width[1];  //calc from width of widest menu item
938
  menu_button[1].h = 50;
David Bruce's avatar
David Bruce committed
939 940 941 942 943 944

  menu_gfxdest[1].x = menu_button[1].x + 6; // inset graphic by (6, 4) */
  menu_gfxdest[1].y = menu_button[1].y + 4;
  menu_gfxdest[1].w = 40;
  menu_gfxdest[1].h = 50;

945
  text_dst[1].x = screen->w/2 - 70;//290;
David Bruce's avatar
David Bruce committed
946 947 948 949
  text_dst[1].y = menu_button[1].y + 15;

  /* FIXME each menu item drawn hardcoded 60 pixels below last - */
  /* perhaps increment should be "menu_button[j-1].h + MENU_ITEM_GAP" */
950
  for (i = 2; i < 6; i++) 
David Bruce's avatar
David Bruce committed
951
  {
952 953 954
    /* --- setup location of button text --- */
    text_dst[i].x = screen->w/2 - 70;
    text_dst[i].y = text_dst[i - 1].y + 60;
David Bruce's avatar
David Bruce committed
955 956

    /* --- setup location of button background --- */
957 958 959 960
    menu_button[i].x = menu_button[i - 1].x;
    menu_button[i].y = menu_button[i - 1].y + 60;
    menu_button[i].w = menu_button[i - 1].w;
    menu_button[i].h = menu_button[i - 1].h;
David Bruce's avatar
David Bruce committed
961 962

    /* --- setup location of animated icon --- */
963 964 965 966
    menu_gfxdest[i].x = menu_gfxdest[i - 1].x;
    menu_gfxdest[i].y = menu_gfxdest[i - 1].y + 60;
    menu_gfxdest[i].w = menu_gfxdest[i - 1].w;
    menu_gfxdest[i].h = menu_gfxdest[i - 1].h;
David Bruce's avatar
David Bruce committed
967 968 969 970
  }
}


971

David Bruce's avatar
David Bruce committed
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
static void unload_menu(void)
{
  int i,j;

  for (i = 1; i <= TITLE_MENU_ITEMS; i++)
  {
    for (j = 1; j <= TITLE_MENU_DEPTH; j++)
    {
      if (reg_text[i][j])
        SDL_FreeSurface(reg_text[i][j]);
      if (sel_text[i][j])
        SDL_FreeSurface(sel_text[i][j]);
      if (menu_gfx[i][j])
        FreeSprite(menu_gfx[i][j]);
      reg_text[i][j] = sel_text[i][j] = NULL;
      menu_gfx[i][j] = NULL;
    }
  }
}


static int load_media(void)
{
  DEBUGCODE
  {
    fprintf(stderr, "Entering load_media():\n");
    fprintf(stderr, "default_data_path = %s\n", settings.default_data_path);
    fprintf(stderr, "theme_data_path = %s\n", settings.theme_data_path);
  }

  /* Make sure everything is unloaded before we start: */
  /* FIXME have not been to do this to run without crashing: */
  //unload_media();

  /* --- load sounds --- */
  if (settings.menu_sound)
  {
    snd_move = LoadSound("tock.wav");
    snd_select = LoadSound("pop.wav");
    if (!snd_move || !snd_select)
    {
      fprintf(stderr, "Could not load menu sounds - turning menu_sound off\n");
      settings.menu_sound = 0;
    }
  }
 
  /* --- load graphics --- */
1019
  LoadBothBkgds("main_bkg.png");
David Bruce's avatar
David Bruce committed
1020 1021 1022
  title = LoadImage( "title1.png", IMG_ALPHA );
  speaker = LoadImage( "sound.png", IMG_ALPHA );
  speakeroff = LoadImage( "nosound.png", IMG_ALPHA );
1023 1024
//  sel = LoadSprite("menu/sel", IMG_ALPHA);
//  reg = LoadSprite("menu/reg", IMG_ALPHA);
David Bruce's avatar
David Bruce committed
1025 1026 1027 1028 1029 1030 1031 1032 1033
  Tux = LoadSprite("tux", IMG_ALPHA);

  DEBUGCODE
  {
    fprintf(stderr, "titlescreen.c load_media(): settings.theme_font_name is %s\n",
            settings.theme_font_name);
  }

  /* Make sure we were successful: */
1034 1035
  if (!CurrentBkgd()
   || !title
David Bruce's avatar
David Bruce committed
1036 1037
   || !speaker
   || !speakeroff
1038
   || !Tux)
David Bruce's avatar
David Bruce committed
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
  {
    fprintf(stderr, "load_media() - could not load all needed files\n");
    unload_media();
    return 0;
  }

  /* Should probably call this directly from TitleScreen() */
  load_menu();
  return 1;
}



static void unload_media(void)
{
  LOG("Entering unload_media():\n");

  /* --- unload sounds --- */
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
  if (snd_move)
  { 
    Mix_FreeChunk(snd_move);
    snd_move = NULL;
  }
  if (snd_select)
  {
    Mix_FreeChunk(snd_select);
    snd_select = NULL;
  }
  if (snd_welcome)
  {
    Mix_FreeChunk(snd_welcome);
    snd_welcome = NULL; 
David Bruce's avatar
David Bruce committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
  }

  /* --- unload graphics --- */
  if (title)
  {
    SDL_FreeSurface(title);
    title = NULL;
  }
  if (speaker)
  {
    SDL_FreeSurface(speaker);
    speaker = NULL;
  }
  if (speakeroff)
  {
    SDL_FreeSurface(speakeroff);
    speakeroff = NULL;
  }

1090
  FreeBothBkgds();
David Bruce's avatar
David Bruce committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

  if (Tux)
  {
    FreeSprite(Tux);
    Tux = NULL;
  }

  LOG("Leaving load_media():\n");

  unload_menu();
}


static void not_implemented(void)
{
  SDL_Surface *s1 = NULL, *s2 = NULL, *s3 = NULL, *s4 = NULL;
  sprite* tux = NULL;
  SDL_Rect loc;
  int finished = 0, i;

  LOG( "NotImplemented() - creating text\n" );

1113 1114 1115
  s1 = BlackOutline( gettext_noop("Work In Progress!"), DEFAULT_MENU_FONT_SIZE, &white);
  s2 = BlackOutline( gettext_noop("This feature is not ready yet"), DEFAULT_MENU_FONT_SIZE, &white);
  s3 = BlackOutline( gettext_noop("Discuss the future of TuxTyping at"), DEFAULT_MENU_FONT_SIZE, &white);
1116
  s4 = BlackOutline( "http://tux4kids.alioth.debian.org/", DEFAULT_MENU_FONT_SIZE, &white);
David Bruce's avatar
David Bruce committed
1117 1118 1119

  tux = LoadSprite("tux/tux-egypt", IMG_ALPHA);

1120
  if (s1 && s2 && s3 && s4 && tux)
David Bruce's avatar
David Bruce committed
1121 1122 1123
  {
    LOG( "NotImplemented() - drawing screen\n" );

1124 1125
    SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL);
    loc.x = screen->w/2 - (s1->w/2); loc.y = 10;
David Bruce's avatar
David Bruce committed
1126
    SDL_BlitSurface( s1, NULL, screen, &loc);
1127
    loc.x = screen->w/2 - (s2->w/2); loc.y = 60;
David Bruce's avatar
David Bruce committed
1128
    SDL_BlitSurface( s2, NULL, screen, &loc);
1129 1130 1131
    loc.x = screen->w/2 - (s3->w/2); loc.y = screen->h/2 + 160;
    SDL_BlitSurface(s3, NULL, screen, &loc);
    loc.x = screen->w/2 - (s4->w/2); loc.y = screen->h/2 + 200;
David Bruce's avatar
David Bruce committed
1132 1133
    SDL_BlitSurface( s4, NULL, screen, &loc);

1134 1135
    loc.x = screen->w/2 - (tux->frame[0]->w/2);
    loc.y = screen->h - 280;
David Bruce's avatar
David Bruce committed
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
    loc.w = tux->frame[0]->w;
    loc.h = tux->frame[0]->h;
    SDL_BlitSurface( tux->frame[tux->cur], NULL, screen, &loc);

    SDL_UpdateRect(screen, 0, 0, 0, 0);

    i = 0;

    while (!finished)
    {
      while (SDL_PollEvent(&event)) 
      {
        switch (event.type)
        {
          case SDL_QUIT:
            exit(0);
          case SDL_MOUSEBUTTONDOWN:
          case SDL_KEYDOWN:
            finished = 1;
        }
      }

      i++;

      if (i %5 == 0)
      {
        NEXT_FRAME(tux);
1163
        SDL_BlitSurface(CurrentBkgd(), &loc, screen, &loc);
David Bruce's avatar
David Bruce committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
        SDL_BlitSurface(tux->frame[tux->cur], NULL, screen, &loc);
        SDL_UpdateRect(screen, loc.x, loc.y, loc.w, loc.h);
      }

      SDL_Delay(40);
    }
  }
  else
    fprintf(stderr, "NotImplemented() - could not load needed graphic\n");

  SDL_FreeSurface(s1);
  SDL_FreeSurface(s2);
  SDL_FreeSurface(s3);
  SDL_FreeSurface(s4);
1178
  s1 = s2 = s3 = s4 = NULL;
David Bruce's avatar
David Bruce committed
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
  FreeSprite(tux);
  tux = NULL;
}




#define MAX_WORD_LISTS 100

/* returns 0 if user pressed escape ...
 *         1 if word list was set correctly
 */
static int chooseWordlist(void)
{
  SDL_Surface* titles[MAX_WORD_LISTS] = {NULL};
  SDL_Surface* select[MAX_WORD_LISTS] = {NULL};
  SDL_Surface* left = NULL, *right = NULL;
  SDL_Rect leftRect, rightRect;
  SDL_Rect titleRects[8];
  int stop = 0;
  int loc = 0;
  int old_loc = 1;
  int lists = 0;
  int i;
1203 1204 1205 1206
  int result = 0;;
  char wordPath[FNLEN];
  char wordlistFile[MAX_WORD_LISTS][200];
  char wordlistName[MAX_WORD_LISTS][200];
David Bruce's avatar
David Bruce committed
1207 1208 1209 1210 1211 1212 1213 1214 1215

  DIR* wordsDir = NULL;
  struct dirent* wordsFile = NULL;
  FILE* tempFile = NULL;

  LOG("Entering chooseWordlist():\n");

  /* find the directory to load wordlists from */

1216 1217 1218
  /* First Check the user's personal settings */
  sprintf(wordPath, "%s/words", settings.user_settings_path);
  if (CheckFile(wordPath))
David Bruce's avatar
David Bruce committed
1219
  {
1220
    DEBUGCODE { fprintf(stderr, "User specific wordlist path found: %s\n", wordPath); }
David Bruce's avatar
David Bruce committed
1221
  }
1222 1223
  /* If nothing found, check under theme directory, if theme selected: */
  else
David Bruce's avatar
David Bruce committed
1224
  {
1225
    if (!settings.use_english)  /* Using theme: */
David Bruce's avatar
David Bruce committed
1226
    {
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
      sprintf(wordPath,"%s/words", settings.theme_data_path);
      if (!CheckFile(wordPath))
      {
        fprintf(stderr, "chooseWordList() - theme contains no wordlist dir \n");
        return 0;
      }
    }
    else  /* No user settings or theme selected - using English: */
    {
      sprintf(wordPath,"%s/words", settings.default_data_path);
      if (!CheckFile(wordPath))
      {
        fprintf(stderr, "chooseWordList() - data path contains no wordlist dir \n");
        return 0;
      }
David Bruce's avatar
David Bruce committed
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
    }
  }

  /* If we get to here, we know there is at least a wordlist directory */
  /* but not necessarily any valid files.                              */

  DEBUGCODE { fprintf(stderr, "wordPath is: %s\n", wordPath); }


  /* FIXME looks like a place for scandir() - or our own w32_scandir() */
  /* create a list of all the .txt files */

  wordsDir = opendir( wordPath );	

  do
  {
    wordsFile = readdir(wordsDir);
    if (!wordsFile)
      break; /* Loop continues until break occurs */

1262 1263
    DEBUGCODE { fprintf(stderr, "wordsFile name is: %s\n", wordsFile->d_name); }

David Bruce's avatar
David Bruce committed
1264 1265 1266 1267
    /* must have at least .txt at the end */
    if (strlen(wordsFile->d_name) < 5)
      continue;

1268
    if (strcmp(&wordsFile->d_name[strlen(wordsFile->d_name) -4 ],".txt"))
David Bruce's avatar
David Bruce committed
1269 1270 1271 1272 1273 1274 1275 1276 1277
      continue;

    sprintf(wordlistFile[lists], "%s/%s", wordPath, wordsFile->d_name);

    /* load the name for the wordlist from the file ... (1st line) */
    tempFile = fopen( wordlistFile[lists], "r" );
    if (!tempFile)
      continue;

1278 1279 1280
    result = fscanf(tempFile, "%[^\n]\n", wordlistName[lists]);
    if (result == EOF)
      continue;
David Bruce's avatar
David Bruce committed
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

    /* check to see if it has a \r at the end of it (dos format!) */
    if (wordlistName[lists][strlen(wordlistName[lists]) - 1] == '\r')
      wordlistName[lists][strlen(wordlistName[lists]) - 1] = '\0';

    lists++;

    fclose(tempFile);
  } while (1); /* Loop continues until break occurs */

  closedir(wordsDir);	
 
1293 1294 1295

/* Adding custom wordlists */
 
1296
    sprintf(wordPath,"%s/words", settings.var_data_path);
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
    if (!CheckFile(wordPath))
    {
      fprintf(stderr, "chooseWordList() - data path contains no wordlist dir \n");
      return 0;
    }
 

  /* If we get to here, we know there is at least a wordlist directory */
  /* but not necessarily any valid files.                              */

  DEBUGCODE { fprintf(stderr, "wordPath is: %s\n", wordPath); }


  /* FIXME looks like a place for scandir() - or our own w32_scandir() */
  /* create a list of all the .txt files */

  wordsDir = opendir( wordPath );	

  do
  {
    wordsFile = readdir(wordsDir);
    if (!wordsFile)
      break; /* Loop continues until break occurs */

1321 1322
    DEBUGCODE { fprintf(stderr, "wordsFile name is: %s\n", wordsFile->d_name); }

1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
    /* must have at least .txt at the end */
    if (strlen(wordsFile->d_name) < 5)
      continue;

    if (strcmp(&wordsFile->d_name[strlen(wordsFile->d_name) -4 ],".txt"))
      continue;

    sprintf(wordlistFile[lists], "%s/%s", wordPath, wordsFile->d_name);

    /* load the name for the wordlist from the file ... (1st line) */
    tempFile = fopen( wordlistFile[lists], "r" );
    if (!tempFile)
      continue;

    result = fscanf(tempFile, "%[^\n]\n", wordlistName[lists]);
    if (result == EOF)
      continue;

    /* check to see if it has a \r at the end of it (dos format!) */
    if (wordlistName[lists][strlen(wordlistName[lists]) - 1] == '\r')
      wordlistName[lists][strlen(wordlistName[lists]) - 1] = '\0';

    lists++;

    fclose(tempFile);
  } while (1); /* Loop continues until break occurs */

  closedir(wordsDir);




David Bruce's avatar
David Bruce committed
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
 DEBUGCODE { fprintf(stderr, "Found %d .txt file(s) in words dir\n", lists); }



  /* let the user pick the list */

  /* Render SDL_Surfaces for list entries: */
//  titles[0] = BlackOutline( _("Alphabet"), font, &white );
//  select[0] = BlackOutline( _("Alphabet"), font, &yellow);

  /* NOTE - no longer hard-coding titles[0] to be alphabet - themes  */
  /* should include a regular word list file called "alphabet.txt"   */
  /* Should sort the list and always put the alphabet file first, if */
  /* present.                                                        */
  for (i = 0; i < lists; i++)
  {
1371 1372
    titles[i] = BlackOutline( wordlistName[i], DEFAULT_MENU_FONT_SIZE, &white );
    select[i] = BlackOutline( wordlistName[i], DEFAULT_MENU_FONT_SIZE, &yellow);
David Bruce's avatar
David Bruce committed
1373 1374 1375 1376 1377 1378
  }

  left = LoadImage("left.png", IMG_ALPHA);
  right = LoadImage("right.png", IMG_ALPHA);

  /* Get out if needed surface not loaded successfully: */
1379
  if (!CurrentBkgd() || !left || !right)
David Bruce's avatar
David Bruce committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
  {
    fprintf(stderr, "chooseWordList(): needed image not available\n");
  
    for (i = 0; i < lists; i++)
    {
      SDL_FreeSurface(titles[i]);
      SDL_FreeSurface(select[i]);
      titles[i] = select[i] = NULL;
    }

    SDL_FreeSurface(left);
    SDL_FreeSurface(right);
1392
    left = right = NULL;
David Bruce's avatar
David Bruce committed
1393 1394 1395 1396 1397

    return 0;
  }


1398 1399 1400
  leftRect.w = left->w;
  leftRect.h = left->h;
  leftRect.x = screen->w/2 - 80 - (leftRect.w/2);
1401
  leftRect.y = screen->h - 50;
David Bruce's avatar
David Bruce committed
1402

1403 1404 1405 1406
  rightRect.w = right->w;
  rightRect.h = right->h;
  rightRect.x = screen->w/2 + 80 - (rightRect.w/2);
  rightRect.y = screen->h - 50;
David Bruce's avatar
David Bruce committed
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438

  /* set initial rect sizes */
  titleRects[0].y = 30;
  titleRects[0].w = titleRects[0].h = titleRects[0].x = 0;

  for (i = 1; i < 8; i++)
  { 
    titleRects[i].y = titleRects[i - 1].y + 50;
    titleRects[i].w = titleRects[i].h = titleRects[i].x = 0;
  }

  /* Main event loop for this screen: */
  while (!stop)
  {
    while (SDL_PollEvent(&event))
    {
      switch (event.type)
      {
        case SDL_QUIT:
          exit(0); /* FIXME may need to cleanup memory and exit more cleanly */
          break;

        case SDL_MOUSEMOTION:
          for (i=0; (i<8) && (loc-(loc%8)+i<lists); i++)
            if (inRect( titleRects[i], event.motion.x, event.motion.y ))
            {
              loc = loc-(loc%8)+i;
              break;
            }
          break;

        case SDL_MOUSEBUTTONDOWN:
1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
          if (inRect( leftRect, event.button.x, event.button.y ))
          {
            if (loc - (loc % 8) - 8 >= 0)
            {
              loc = loc - (loc % 8) - 8;
              break;
            }
          }

          if (inRect(rightRect, event.button.x, event.button.y))
          {
            if (loc - (loc % 8) + 8 < lists)
            {
              loc = loc - (loc % 8) + 8;
              break;
            }
          }

          for (i = 0; (i < 8) && (loc - (loc % 8) + i < lists); i++)
          {
            if (inRect(titleRects[i], event.button.x, event.button.y))
            {
              loc = loc - (loc % 8) + i;
              ClearWordList(); /* clear old selection */
              GenerateWordList(wordlistFile[loc]); 
              stop = 1;
              break;
            }
          }

          break;

        case SDL_KEYDOWN:
          if (event.key.keysym.sym == SDLK_ESCAPE)
          {
            stop = 2;
            break;
          }

          if (event.key.keysym.sym == SDLK_RETURN)
          {
            ClearWordList(); /* clear old selection */
            GenerateWordList(wordlistFile[loc]); 
            stop = 1;
            break;
          }

          if ((event.key.keysym.sym == SDLK_LEFT)
           || (event.key.keysym.sym == SDLK_PAGEUP))
          {
            if (loc - (loc % 8) - 8 >= 0)
              loc = loc - (loc % 8) - 8;
          }

          if ((event.key.keysym.sym == SDLK_RIGHT)
           || (event.key.keysym.sym == SDLK_PAGEDOWN))
          {
            if (loc - (loc % 8) + 8 < lists)
              loc = (loc - (loc % 8) + 8);
          }

          if (event.key.keysym.sym == SDLK_UP)
          {
            if (loc > 0)
              loc--;
          }

          if (event.key.keysym.sym == SDLK_DOWN)
          {
            if (loc+1<lists)
              loc++;
          }
David Bruce's avatar
David Bruce committed
1511 1512 1513
      }
    }

1514 1515 1516 1517
    /* Redraw if we have changed location: */
    if (old_loc != loc)
    {
      int start;
David Bruce's avatar
David Bruce committed
1518

1519
      SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL );
David Bruce's avatar
David Bruce committed
1520

1521
      start = loc - (loc % 8);
David Bruce's avatar
David Bruce committed
1522

1523 1524 1525 1526 1527 1528 1529 1530
      for (i = start; i< MIN(start + 8,lists); i++) 
      {
        titleRects[i % 8].x = screen->w/2 - (titles[i]->w/2);
        if (i == loc)   /* Draw selected text in yellow:  */
          SDL_BlitSurface(select[loc], NULL, screen, &titleRects[i%8]);
        else            /* Draw unselected text in white: */
          SDL_BlitSurface(titles[i], NULL, screen, &titleRects[i%8]);
      }
David Bruce's avatar
David Bruce committed
1531

1532 1533 1534
      /* --- draw arrow buttons --- */
      if (start > 0)
        SDL_BlitSurface(left, NULL, screen, &leftRect);
David Bruce's avatar
David Bruce committed
1535

1536 1537
      if (start + 8 < lists)
        SDL_BlitSurface(right, NULL, screen, &rightRect);
David Bruce's avatar
David Bruce committed
1538

1539 1540 1541 1542 1543
      SDL_UpdateRect(screen, 0, 0, 0 ,0);
    }

    SDL_Delay(40);
    old_loc = loc;
David Bruce's avatar
David Bruce committed
1544 1545 1546
  }

  /* --- clear graphics before leaving function --- */ 
1547
  for (i = 0; i < lists; i++)
David Bruce's avatar
David Bruce committed
1548 1549 1550 1551 1552 1553 1554 1555
  {
    SDL_FreeSurface(titles[i]);
    SDL_FreeSurface(select[i]);
    titles[i] = select[i] = NULL;
  }

  SDL_FreeSurface(left);
  SDL_FreeSurface(right);
1556
  left = right = NULL; /* Maybe overkill - about to be destroyed anyway */
David Bruce's avatar
David Bruce committed
1557 1558 1559 1560 1561 1562 1563 1564

  DEBUGCODE { fprintf( stderr, "Leaving chooseWordlist();\n" ); }

  if (stop == 2)
    return 0;

  return 1;
}