titlescreen.c 40.9 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 23 24 25 26 27 28
/*
   titlescreen.c:

   Splash screen and menu code.
   
   Copyright 2003, 2004, 2007, 2008, 2009, 2010.
   Authors: Jesse Andrews, David Bruce.
   
   Project email: <tux4kids-tuxtype-dev@lists.alioth.debian.org>
   Project website: http://tux4kids.alioth.debian.org

   titlescreen.c is part of Tux Typing, a.k.a "tuxtype".

Tux Typing 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 3 of the License, or
(at your option) any later version.

Tux Typing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


David Bruce's avatar
David Bruce committed
29 30 31 32

#include "globals.h"
#include "funcs.h"
#include "titlescreen.h"
33
#include "SDL_extras.h"
34
#include "convert_utf.h"
35
#include "editor.h"
David Bruce's avatar
David Bruce committed
36 37 38 39

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

/* images of regular and selected text of menu items: */
40
static SDL_Surface* reg_text[TITLE_MENU_ITEMS + 1][TITLE_MENU_DEPTH + 1] = {{NULL}};
41
static SDL_Surface* sel_text[TITLE_MENU_ITEMS + 1][TITLE_MENU_DEPTH + 1] = {{NULL}};
David Bruce's avatar
David Bruce committed
42
/* this will contain pointers to all of the menu 'icons' */
43
static sprite* menu_gfx[TITLE_MENU_ITEMS + 1][TITLE_MENU_DEPTH + 1] = {{NULL}};
David Bruce's avatar
David Bruce committed
44 45 46 47 48 49 50 51

/* --- 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;
52
static Mix_Chunk* snd_welcome = NULL; 
David Bruce's avatar
David Bruce committed
53 54

/* --- locations we need --- */
55 56 57 58 59
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
60

61 62 63 64
static SDL_Rect Tuxdest;
static SDL_Rect Titledest;
static SDL_Rect spkrdest;
static SDL_Rect cursor;
David Bruce's avatar
David Bruce committed
65 66

/* Local function prototypes: */
67
static void show_logo(void);
David Bruce's avatar
David Bruce committed
68 69
static int load_media(void);
static void load_menu(void);
70 71
static void recalc_rects(void);
static int chooseWordlist(void);
David Bruce's avatar
David Bruce committed
72 73
static void unload_media(void);
static void unload_menu(void);
74
static void not_implemented(void);
David Bruce's avatar
David Bruce committed
75

76 77
/* --- menu text --- */

78 79
/* --- define menu structure --- */
/* (these values are all in the Game_Type enum in globals.h) */
80
const int menu_item[][6] = {{0, 0,         0,         0,          0},
David Bruce's avatar
David Bruce committed
81
			   {0, CASCADE,   LEVEL1,    LEVEL1,  EDIT_WORDLIST   },
82
			   {0, LASER,     LEVEL2,    LEVEL2,  PHRASE_TYPING },
David Bruce's avatar
David Bruce committed
83 84 85
			   {0, LESSONS,   LEVEL3,    LEVEL3,  PROJECT_INFO    },
			   {0, OPTIONS,   INSTRUCT,  LEVEL4,  SET_LANGUAGE    },
			   {0, QUIT_GAME, MAIN,      MAIN,    MAIN            }};
86 87

/* --- menu icons --- */
88
const char* menu_icon[][6] = 
89 90 91 92 93 94
{{"", "", "", "", ""},
 {"", "cascade", "easy",   "grade1_", "list"   },
 {"", "comet",   "medium", "grade2_", "practice" },
 {"", "lesson","hard",   "grade3_", "keyboard"   },
 {"", "tux_config",  "tutor",  "grade4_", "lang" },
 {"", "quit",    "main",   "main",    "main"   }};
95 96

     
97
static const char* menu_text[] = 
98
{"", "",            "",             "",            ""    ,
David Bruce's avatar
David Bruce committed
99 100 101 102 103
 "",N_("Fish Cascade"),N_("Easy"),N_("Space Cadet"),N_("Edit Word Lists"),
 "",N_("Comet Zap"),N_("Medium"),N_("Pilot"),N_("Phrase Typing"),
 "",N_("Lessons"),N_("Hard"), N_("Ace"),N_("Project Info"),
 "", N_("Options"),N_("Instructions"),N_("Commander"),N_("Setup Language"),
 "", N_("Quit"),N_("Main Menu"),N_("Main Menu"),N_("Main Menu")};
104 105 106 107




David Bruce's avatar
David Bruce committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/************************************************************************/
/*                                                                      */ 
/*         "Public" functions (callable throughout program)             */
/*                                                                      */
/************************************************************************/


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

  Uint32 frame = 0;
  Uint32 start = 0;

126 127 128 129
  /* 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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  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();


  /*
151
  * Display the Standby screen.... 
David Bruce's avatar
David Bruce committed
152
  */
153
  show_logo();
154 155 156 157 158 159
  snd_welcome = LoadSound("harp.wav");

  if (snd_welcome && settings.menu_sound)
  {
    PlaySound(snd_welcome);
  }
David Bruce's avatar
David Bruce committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

  /* 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;

187 188
  spkrdest.x = screen->w - speaker->w - 10;
  spkrdest.y = screen->h - speaker->h - 10;
David Bruce's avatar
David Bruce committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  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: */
205
  TransWipe(CurrentBkgd(), RANDOM_WIPE, 10, 20);
David Bruce's avatar
David Bruce committed
206 207
  /* Make sure background gets drawn (since TransWipe() doesn't */
  /* seem to work reliably as of yet):                          */
208
  SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL);
David Bruce's avatar
David Bruce committed
209 210 211
  SDL_UpdateRect(screen, 0, 0, 0, 0);

  /* --- Pull tux & logo onscreen --- */
212
  for (i = 0; i <= (PRE_ANIM_FRAMES * PRE_FRAME_MULT); i++)
David Bruce's avatar
David Bruce committed
213 214
  {
    start = SDL_GetTicks();
215 216
    SDL_BlitSurface(CurrentBkgd(), &Tuxdest, screen, &Tuxdest);
    SDL_BlitSurface(CurrentBkgd(), &Titledest, screen, &Titledest);
David Bruce's avatar
David Bruce committed
217

218

David Bruce's avatar
David Bruce committed
219 220
    Tuxdest.y -= Tux->frame[0]->h / (PRE_ANIM_FRAMES * PRE_FRAME_MULT);
    Titledest.x -= (screen->w) / (PRE_ANIM_FRAMES * PRE_FRAME_MULT);
221 222 223 224 225
    /* 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
226 227 228 229 230

    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);
231
    SDL_UpdateRect(screen, Titledest.x, Titledest.y, Titledest.w + 40, Titledest.h);
David Bruce's avatar
David Bruce committed
232 233 234 235 236 237 238

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

239
  recalc_rects();
David Bruce's avatar
David Bruce committed
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

  /* 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) 
  {

269
    start = SDL_GetTicks();
David Bruce's avatar
David Bruce committed
270 271 272 273 274 275 276 277 278 279 280 281

    /* ---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)
      {
282
        /* Update "selection" if mouse moves within a menu entry: */
David Bruce's avatar
David Bruce committed
283 284 285 286
        case SDL_MOUSEMOTION:
        {
          cursor.x = event.motion.x;
          cursor.y = event.motion.y;
287 288 289 290

	  for (j = 1; j <= TITLE_MENU_ITEMS; j++)
            if (inRect(menu_button[j], cursor.x, cursor.y))
              key_menu = j;
David Bruce's avatar
David Bruce committed
291 292 293 294 295 296 297 298 299 300 301 302
          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++)
          {
303
            if (inRect(menu_button[j], cursor.x, cursor.y))
David Bruce's avatar
David Bruce committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
            {
              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: */
319
          if (inRect(spkrdest, cursor.x, cursor.y))
David Bruce's avatar
David Bruce committed
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 354 355 356 357 358 359 360 361 362 363 364 365 366 367
          {
            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();
368
              recalc_rects();
David Bruce's avatar
David Bruce committed
369 370 371 372 373 374 375 376 377 378 379
              redraw = 1;
              break;
            }


            /* Toggle menu music: */
            case SDLK_F11:
            {
              if (settings.menu_music)
              {
                MusicUnload( );
380
                settings.menu_music = 0;
David Bruce's avatar
David Bruce committed
381 382 383
              }
              else
              {
384
                settings.menu_music = 1;
David Bruce's avatar
David Bruce committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
                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:
404
	    case SDLK_k:
David Bruce's avatar
David Bruce committed
405 406 407 408 409 410 411 412 413 414 415
            {
              if (settings.menu_sound)
                PlaySound(snd_move);
              key_menu--;
              if (key_menu < 1)
                key_menu = 5;
              break;
            }


            case SDLK_DOWN:
416
	    case SDLK_j:
David Bruce's avatar
David Bruce committed
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 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
            {
              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;
    }


500
    if (menu_opt == EDIT_WORDLIST)
David Bruce's avatar
David Bruce committed
501
    {
502
      ChooseListToEdit();
David Bruce's avatar
David Bruce committed
503 504 505 506 507 508 509 510 511 512 513 514 515
      redraw = 1;
    }


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


    if (menu_opt == LESSONS)
    {
516
//      not_implemented();
517
      SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL);
518
      SDL_Flip(screen);
David Bruce's avatar
David Bruce committed
519 520 521 522 523
      unload_media();

      if (settings.menu_music)
        MusicUnload( );

524
      XMLLesson();
David Bruce's avatar
David Bruce committed
525 526

      load_media();
527

David Bruce's avatar
David Bruce committed
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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
      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)
    {
641 642
//       not_implemented();
       unload_media();
David Bruce's avatar
David Bruce committed
643

644 645 646 647 648
      switch (sub_menu)
      {
        case CASCADE: InstructCascade(); break;
        case LASER:   InstructLaser();   break;
      }
David Bruce's avatar
David Bruce committed
649

650
      load_media();
David Bruce's avatar
David Bruce committed
651

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

      redraw = 1;
    }



660
    if (menu_opt == PHRASE_TYPING)
David Bruce's avatar
David Bruce committed
661
    {
662 663
//      not_implemented();

David Bruce's avatar
David Bruce committed
664
      unload_media();
665

David Bruce's avatar
David Bruce committed
666
      Phrases(NULL);
667

David Bruce's avatar
David Bruce committed
668
      load_media();
669

David Bruce's avatar
David Bruce committed
670 671
      if (settings.menu_music)
        MusicLoad( "tuxi.ogg", -1 );
672

David Bruce's avatar
David Bruce committed
673
      redraw = 1;
David Bruce's avatar
David Bruce committed
674 675 676 677 678 679 680 681 682
    }

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



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

685
      SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL); 
David Bruce's avatar
David Bruce committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
      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++)
      {
711
        text_dst[i].x = screen->w/2 - 70;//290;
David Bruce's avatar
David Bruce committed
712 713
        text_dst[i].w = reg_text[i][menu_depth]->w;
        text_dst[i].h = reg_text[i][menu_depth]->h;
714 715
        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
716 717 718 719 720 721 722 723 724 725
      }


      update_locs = 0;

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

      for (j = 1; j <= TITLE_MENU_ITEMS; j++)
      {
        DOUT(j);
726
        DrawButton(&menu_button[j], 10, REG_RGBA);
David Bruce's avatar
David Bruce committed
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
        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)
    {
755 756 757 758 759 760 761 762 763 764 765
      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
766 767 768 769 770
    }


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

771 772
//    key_menu = 0;
/*
David Bruce's avatar
David Bruce committed
773 774 775 776 777 778 779 780
    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
      }
781
    }*/
David Bruce's avatar
David Bruce committed
782 783 784 785 786 787


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

    if (old_key_menu && (key_menu != old_key_menu))
    {
788 789
      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
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
      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);
      }

806 807
      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
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
      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);

824
    for (i = 1; i < 6; i++)
David Bruce's avatar
David Bruce committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    {
      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)                   */
/*                                                                      */
/************************************************************************/

863 864 865 866 867 868
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);
869

870 871 872 873 874 875
    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
876

877 878 879 880 881 882 883 884 885
      /* 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
886 887
}

888 889 890

/* Renders the menu text for all items and loads the associated */
/* animated sprites, if any.                                    */
David Bruce's avatar
David Bruce committed
891 892
static void load_menu(void)
{
893
  char fn[FNLEN];
David Bruce's avatar
David Bruce committed
894 895 896 897 898 899 900 901 902 903 904 905 906 907
  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",
908
                i, j,  gettext(menu_text[j + 5 * i]));
David Bruce's avatar
David Bruce committed
909 910 911
      }

      /* --- create text surfaces --- */
912 913
      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
914 915 916 917 918 919 920 921 922 923 924 925

      /* (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
  }

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
  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
949 950

  /* --- setup menu item destinations --- */
951
  menu_button[1].x = screen->w/2 - 120; //240;
David Bruce's avatar
David Bruce committed
952 953
  menu_button[1].y = 100;
  menu_button[1].w = menu_width[1];  //calc from width of widest menu item
954
  menu_button[1].h = 50;
David Bruce's avatar
David Bruce committed
955 956 957 958 959 960

  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;

961
  text_dst[1].x = screen->w/2 - 70;//290;
David Bruce's avatar
David Bruce committed
962 963 964 965
  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" */
966
  for (i = 2; i < 6; i++) 
David Bruce's avatar
David Bruce committed
967
  {
968 969 970
    /* --- 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
971 972

    /* --- setup location of button background --- */
973 974 975 976
    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
977 978

    /* --- setup location of animated icon --- */
979 980 981 982
    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
983 984 985 986
  }
}


987

David Bruce's avatar
David Bruce committed
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 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
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 --- */
1035
  LoadBothBkgds("main_bkg.png");
David Bruce's avatar
David Bruce committed
1036 1037 1038
  title = LoadImage( "title1.png", IMG_ALPHA );
  speaker = LoadImage( "sound.png", IMG_ALPHA );
  speakeroff = LoadImage( "nosound.png", IMG_ALPHA );
1039 1040
//  sel = LoadSprite("menu/sel", IMG_ALPHA);
//  reg = LoadSprite("menu/reg", IMG_ALPHA);
David Bruce's avatar
David Bruce committed
1041 1042 1043 1044 1045 1046 1047 1048 1049
  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: */
1050 1051
  if (!CurrentBkgd()
   || !title
David Bruce's avatar
David Bruce committed
1052 1053
   || !speaker
   || !speakeroff
1054
   || !Tux)
David Bruce's avatar
David Bruce committed
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  {
    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 --- */
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
  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
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
  }

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

1106
  FreeBothBkgds();
David Bruce's avatar
David Bruce committed
1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

  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" );

David Bruce's avatar
David Bruce committed
1129 1130 1131
  s1 = BlackOutline( _("Work In Progress!"), DEFAULT_MENU_FONT_SIZE, &white);
  s2 = BlackOutline( _("This feature is not ready yet"), DEFAULT_MENU_FONT_SIZE, &white);
  s3 = BlackOutline( _("Discuss the future of TuxTyping at"), DEFAULT_MENU_FONT_SIZE, &white);
1132
  s4 = BlackOutline( "http://tux4kids.alioth.debian.org/", DEFAULT_MENU_FONT_SIZE, &white);
David Bruce's avatar
David Bruce committed
1133 1134 1135

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

1136
  if (s1 && s2 && s3 && s4 && tux)
David Bruce's avatar
David Bruce committed
1137 1138 1139
  {
    LOG( "NotImplemented() - drawing screen\n" );

1140 1141
    SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL);
    loc.x = screen->w/2 - (s1->w/2); loc.y = 10;
David Bruce's avatar
David Bruce committed
1142
    SDL_BlitSurface( s1, NULL, screen, &loc);
1143
    loc.x = screen->w/2 - (s2->w/2); loc.y = 60;
David Bruce's avatar
David Bruce committed
1144
    SDL_BlitSurface( s2, NULL, screen, &loc);
1145 1146 1147
    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
1148 1149
    SDL_BlitSurface( s4, NULL, screen, &loc);

1150 1151
    loc.x = screen->w/2 - (tux->frame[0]->w/2);
    loc.y = screen->h - 280;
David Bruce's avatar
David Bruce committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
    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);
1179
        SDL_BlitSurface(CurrentBkgd(), &loc, screen, &loc);
David Bruce's avatar
David Bruce committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
        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);
1194
  s1 = s2 = s3 = s4 = NULL;
David Bruce's avatar
David Bruce committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
  FreeSprite(tux);
  tux = NULL;
}





/* 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;
1218 1219 1220 1221
  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
1222 1223 1224 1225 1226 1227 1228 1229 1230

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

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

  /* find the directory to load wordlists from */

1231
  /* First we add the bundled word lists in either NLS or default theme: */
David Bruce's avatar
David Bruce committed
1232
  {
1233
    if (!settings.use_english)  /* Using theme: */
David Bruce's avatar
David Bruce committed
1234
    {
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
      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
1250 1251 1252 1253 1254 1255
    }
  }

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

1256
  DEBUGCODE { fprintf(stderr, "bundled wordPath is: %s\n", wordPath); }
David Bruce's avatar
David Bruce committed
1257 1258 1259


  /* FIXME looks like a place for scandir() - or our own w32_scandir() */
1260
  /* FIXME we also have this block of code repeated three times - DSB  */
David Bruce's avatar
David Bruce committed
1261 1262 1263 1264 1265 1266 1267 1268
  /* create a list of all the .txt files */
  wordsDir = opendir( wordPath );	
  do
  {
    wordsFile = readdir(wordsDir);
    if (!wordsFile)
      break; /* Loop continues until break occurs */

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

David Bruce's avatar
David Bruce committed
1271 1272 1273 1274
    /* must have at least .txt at the end */
    if (strlen(wordsFile->d_name) < 5)
      continue;

1275
    if (strcmp(&wordsFile->d_name[strlen(wordsFile->d_name) -4 ],".txt"))
David Bruce's avatar
David Bruce committed
1276 1277 1278 1279 1280 1281 1282 1283 1284
      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;

1285 1286 1287
    result = fscanf(tempFile, "%[^\n]\n", wordlistName[lists]);
    if (result == EOF)
      continue;
David Bruce's avatar
David Bruce committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297

    /* 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);	
1298

1299 1300 1301
  
  
  /* Adding global custom wordlists ------------------------------------ */
1302
 
1303 1304 1305
  sprintf(wordPath,"%s/words", settings.var_data_path);
  if (!CheckFile(wordPath))
  {
1306
    DEBUGCODE { fprintf(stderr, "chooseWordList() -  path \"%s\" not found\n", wordPath); }
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
  }
  else
  {
    /* If we get to here, we know there is at least a wordlist directory */
    /* but not necessarily any valid files.                              */

    DEBUGCODE { fprintf(stderr, "global custom 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
1319
    {
1320 1321 1322
      wordsFile = readdir(wordsDir);
      if (!wordsFile)
        break; /* Loop continues until break occurs */
1323

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

1326 1327 1328
      /* must have at least .txt at the end */
      if (strlen(wordsFile->d_name) < 5)
        continue;
1329

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

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

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

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

1344 1345 1346
      /* 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';
1347

1348
      lists++;
1349

1350 1351 1352 1353 1354 1355
      fclose(tempFile);
    } while (1); /* Loop continues until break occurs */
    closedir(wordsDir);
  }
  
  /* Now add any lists in the user's personal settings path: ------------ */
1356

1357 1358 1359
  sprintf(wordPath,"%s/words", settings.user_settings_path);
  if (!CheckFile(wordPath))
  {
1360
    DEBUGCODE { fprintf(stderr, "chooseWordList() -  path \"%s\" not found\n", wordPath); }
1361 1362 1363 1364 1365
  }
  else
  {
    /* If we get to here, we know there is at least a wordlist directory */
    /* but not necessarily any valid files.                              */
1366

1367
    DEBUGCODE { fprintf(stderr, "user-specific wordPath is: %s\n", wordPath); }
1368

1369 1370 1371 1372 1373 1374 1375 1376
    /* 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 */
1377

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

1380 1381 1382
      /* must have at least .txt at the end */
      if (strlen(wordsFile->d_name) < 5)
        continue;
1383

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

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

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

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

1398 1399 1400
      /* 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';
1401

1402
      lists++;
David Bruce's avatar
David Bruce committed
1403

1404 1405 1406 1407
      fclose(tempFile);
    } while (1); /* Loop continues until break occurs */
    closedir(wordsDir);
  }
David Bruce's avatar
David Bruce committed
1408

1409
  DEBUGCODE { fprintf(stderr, "Found %d .txt file(s) in words dir\n", lists); }
David Bruce's avatar
David Bruce committed
1410

1411 1412
  
  /* Done scanning for word lists, now display them for user selection: */
David Bruce's avatar
David Bruce committed
1413 1414 1415 1416

  /* Render SDL_Surfaces for list entries: */
  for (i = 0; i < lists; i++)
  {
1417 1418
    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
1419 1420 1421 1422 1423 1424
  }

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

  /* Get out if needed surface not loaded successfully: */
1425
  if (!CurrentBkgd() || !left || !right)
David Bruce's avatar
David Bruce committed
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
  {
    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);
1438
    left = right = NULL;
David Bruce's avatar
David Bruce committed
1439 1440 1441 1442 1443

    return 0;
  }


1444 1445 1446
  leftRect.w = left->w;
  leftRect.h = left->h;
  leftRect.x = screen->w/2 - 80 - (leftRect.w/2);
1447
  leftRect.y = screen->h - 50;
David Bruce's avatar
David Bruce committed
1448

1449 1450 1451 1452
  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
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

  /* 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:
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 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
          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);
          }

1546 1547 1548
          if ((event.key.keysym.sym == SDLK_UP)
	     ||
	      (event.key.keysym.sym == SDLK_k))
1549 1550 1551 1552 1553
          {
            if (loc > 0)
              loc--;
          }

1554 1555 1556
          if ((event.key.keysym.sym == SDLK_DOWN)
	     ||
	      (event.key.keysym.sym == SDLK_j))
1557 1558 1559 1560
          {
            if (loc+1<lists)
              loc++;
          }
David Bruce's avatar
David Bruce committed
1561 1562 1563
      }
    }

1564 1565 1566 1567
    /* Redraw if we have changed location: */
    if (old_loc != loc)
    {
      int start;
David Bruce's avatar
David Bruce committed
1568

1569
      SDL_BlitSurface(CurrentBkgd(), NULL, screen, NULL );
David Bruce's avatar
David Bruce committed
1570

1571
      start = loc - (loc % 8);
David Bruce's avatar
David Bruce committed
1572

1573 1574 1575 1576 1577 1578 1579 1580
      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
1581

1582 1583 1584
      /* --- draw arrow buttons --- */
      if (start > 0)
        SDL_BlitSurface(left, NULL, screen, &leftRect);
David Bruce's avatar
David Bruce committed
1585

1586 1587
      if (start + 8 < lists)
        SDL_BlitSurface(right, NULL, screen, &rightRect);
David Bruce's avatar
David Bruce committed
1588

1589 1590 1591 1592 1593
      SDL_UpdateRect(screen, 0, 0, 0 ,0);
    }

    SDL_Delay(40);
    old_loc = loc;
David Bruce's avatar
David Bruce committed
1594 1595 1596
  }

  /* --- clear graphics before leaving function --- */ 
1597
  for (i = 0; i < lists; i++)
David Bruce's avatar
David Bruce committed
1598 1599 1600 1601 1602 1603 1604 1605
  {
    SDL_FreeSurface(titles[i]);
    SDL_FreeSurface(select[i]);
    titles[i] = select[i] = NULL;
  }

  SDL_FreeSurface(left);
  SDL_FreeSurface(right);
1606
  left = right = NULL; /* Maybe overkill - about to be destroyed anyway */
David Bruce's avatar
David Bruce committed
1607 1608 1609 1610 1611 1612 1613 1614

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

  if (stop == 2)
    return 0;

  return 1;
}