/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 *
 * Routine to start a new game, do transition between each turns ...
 * 
 * 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.
 * 
 * This program 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

#include "stonage.h"

#include <SDL.h>
#if defined(HAVE_SDL_SDL_IMAGE_H)
#include <SDL/SDL_image.h>
#endif

#include "game.h"
#include "screen.h"
#include "level.h"
#include "level_io.h"
#include "level_code.h"
#include "image.h"
#include "music.h"


/*
 * Cette fonction permet de gérer l'entrée d'un nouveau niveau.
 * @return the level of the game, 0 if the password is incorrect.
 */
int 
Game_EnterLevel(SDL_Surface *screen)
{
  SDL_Surface *image, *font;
  SDL_Event event;
  SDL_Rect rs,rd;
  char level_password[7];
  int  nbchar=0;
  int new_level;

  image = Image_Load("images/enterlevel.jpg",1);
  if (image==NULL)
   {
     fprintf(stderr,_("Can't load images/enterlevel.jpg\n"));
     return 0;
   }
  font = Image_Load("images/font_enterlevel.png",1);
  if (font==NULL)
   {
     fprintf(stderr,_("Can't load images/font_enterlevel.png\n"));
     return 0;
   }

  Screen_Fade(screen,image);
  SDL_FreeSurface(image);

  /* wait for key or button */
  while (1) 
   {
     SDL_WaitEvent(&event);
     if (event.type == SDL_QUIT) 
       Game_Quit();
     if (event.type == SDL_KEYUP)
      {
	if (event.key.keysym.sym>=SDLK_a && event.key.keysym.sym<=SDLK_z)
	 {
	   /* Draw the letter */
	   rd.y=204;
	   rd.x=254+nbchar*36;
	   rd.w=rd.h=32;

	   rs.x=1+(event.key.keysym.sym-SDLK_a)*32;
	   rs.y=0;
	   rs.w=rs.h=32;

	   SDL_BlitSurface(font,&rs,screen,&rd);
	   SDL_Flip(screen);

	   level_password[nbchar++]=event.key.keysym.sym - 'a' + 'A';
	   if (nbchar>=6)
	     break;
	 }
      }
   }
  level_password[6]=0;
  new_level = level_decode(level_password);
  if (new_level)
    game.level=new_level;

  SDL_FreeSurface(font);
  return new_level;
}



