/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 *
 * Functions to play the introduction of the game
 * 
 * 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/SDL.h>
#if defined(HAVE_SDL_SDL_IMAGE_H)
#include <SDL/SDL_image.h>
#endif
#include <stdlib.h>

#include "image.h"
#include "game.h"
#include "music.h"
#include "music_ym.h"

/**
 *
 */
void
Intro(SDL_Surface *screen, int nosound)
{
  SDL_Surface *image;
  SDL_Event event;
  int x,intensite,start_time;
  SDL_Rect rect;

  image = Image_Load("images/splash.jpg",1);
  if (image==NULL)
    return; /* Skip the intro */

#if ENABLE_SOUND
  if (!nosound)
   {
     YM_Init(0,NULL);
     YM_Load("warp.ym");
     YM_Start();
   }
#endif

  /* Purge the event loop */
  while (SDL_PollEvent(&event))
    ;
 
  //old_event = SDL_EventState(SDL_ALLEVENTS,SDL_QUERY);
  SDL_EventState(SDL_ACTIVEEVENT,SDL_IGNORE);
  
  /* The first part: ???? */
  x=intensite=0;
  rect.x=0;
  rect.y=(screen->h/2)-1;
  rect.h=3;

  trace("Introduction: part1");
  start_time=SDL_GetTicks();

  while (x<screen->w)
   {
     SDL_FillRect(screen,NULL,
	 SDL_MapRGB(screen->format,intensite,intensite,intensite));
     rect.w=x;
     SDL_FillRect(screen,&rect,
	 SDL_MapRGB(screen->format,255-intensite,255-intensite,255-intensite));

     x+=2;

     if (intensite<255)
       intensite++;

     SDL_Flip(screen);
     /* If the user close the window, or press a key get out from this fading */
     SDL_PumpEvents();
     if (SDL_PeepEvents(&event,1,SDL_PEEKEVENT,SDL_QUIT|SDL_KEYUP|SDL_MOUSEBUTTONUP)==1)
       break;
   }
  /* if the first part isn't terminated, draw the entire screen */
  SDL_FillRect(screen,NULL, SDL_MapRGB(screen->format,255,255,255));
  rect.w=screen->w;
  SDL_FillRect(screen,&rect, SDL_MapRGB(screen->format,0,0,0));
  SDL_Flip(screen);

  /* The second part: open the screen */
  trace("Introduction: part2");
  rect.x=0;
  rect.w=screen->w;
  rect.y=(screen->h/2)-1;
  rect.h=3;

  do
   {
     SDL_BlitSurface(image,&rect,screen,&rect);

     rect.y--;
     rect.h+=2;

     SDL_Flip(screen);
     /* If the user close the window, or press a key get out from this fading */
     SDL_PumpEvents();
     if (SDL_PeepEvents(&event,1,SDL_PEEKEVENT,SDL_QUIT|SDL_KEYUP|SDL_MOUSEBUTTONUP)==1)
       break;

   } while (rect.y>0);

  /* if the secnd  part isn't terminated, draw the entire screen */
  SDL_BlitSurface(image,NULL,screen,NULL);
  SDL_Flip(screen);
  SDL_FreeSurface(image);

  /* wait for key or button */
  SDL_EventState(SDL_ACTIVEEVENT,SDL_ENABLE);
  while (1) 
   {
     SDL_WaitEvent(&event);
     if (event.type == SDL_QUIT) 
       Game_Quit();
     if (event.type == SDL_KEYUP || event.type == SDL_MOUSEBUTTONUP)
       break;
   }

#if ENABLE_SOUND
  if (!nosound)
    YM_Release();
#endif

}


