// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See LICENSE for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
					CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/16 10:47:53 $
$Name: rel-0-3-patches $
$Revision: 1.11 $
$State: Exp $

$Log: intro.cpp,v $
Revision 1.11  2006/03/16 10:47:53  cavac
Changes some .bmp to .jpg for smaller downloads
Re-compressed sisters_orient.ogg for smaller download
Updated .cpp files to reflect changes

Revision 1.10  2006/03/02 13:03:42  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.9  2006/02/25 16:48:31  dauti
Replaced while loop by a simple calculation

Revision 1.8  2006/02/17 10:49:42  cavac
Added error-handling

Revision 1.7  2006/02/13 09:53:51  cavac
Added BOSS-Key (ESC is instant exit)

Revision 1.6  2006/02/10 18:56:17  cavac
Code Audit: Fixed many 64Bit based problems (mostly untested)

Revision 1.5  2006/02/10 17:50:26  cavac
Added ActionCapture feature (define ALLOW_ACTIONCAPTURE to allow this
resource-hungry extra)

Revision 1.4  2006/01/27 17:04:02  cavac
Updated VC6 workspace to current release
Added ogg/vorbis support for Win32
Added SDL_Image and SDL_ttf support
Changed build- and installer-system for easier maintenance

Revision 1.3  2006/01/18 01:22:39  cavac
Added SDL_Image to load additional graphics formats
Updated osdef* and xcode-project to to add SDL_Image
Updated all image-loader functions to use SDL_Image
Added level6 as temporary testlevel
Added code to display start- and goal-points with an animation (needs some fixing)
Removed temporary "marker"-tiles from level-definitions, as we have now an
animated display for that.

Revision 1.2  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include "globals.h"
#include "intro.h"
#include "sound.h"
#include "showtext.h"
#include "drawprimitives.h"
#include "debug.h"
#include "background.h"
#include "tiles.h"
#include "errorhandler.h"

SDL_Surface *introFG;

Uint32 bgoffs = 0;
Uint32 tmpbgoffs;
Uint32 fgtransp;

bool blinkOn = true;


void initIntro()
{
	SDL_Surface *temp = IMG_Load(RESPATH "intro.bmp");
	if(!temp) {
		DIE(ERROR_IMAGE_READ, RESPATH "intro.bmp");
	}
	introFG = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);
		
	// Looking for the transparent color in Intro-Foreground
	// For simplicity, take the uppermost left pixel (e.g. the first pixels)
	// as an indicator for the transparent one
	
	// Lock surface if needed
	if (SDL_MUSTLOCK(introFG))
		if (SDL_LockSurface(introFG) < 0) 
			return;
	fgtransp = ((Uint32*)introFG->pixels)[0];
	
	// Unlock if needed
	if (SDL_MUSTLOCK(introFG)) 
		SDL_UnlockSurface(introFG);
	
	gLastTick = SDL_GetTicks();
	
	initBackground("introbg.jpg");
	initTiles("bathtiles.bmp");
	
	soundStartMusic("introMusic.ogg");
}

void deInitIntro ()
{
	soundStopMusic();
	SDL_FreeSurface(introFG);
	deInitBackground();
	deInitTiles();
}


void renderIntro()
{
	
	// Ask SDL for the time in milliseconds
	Uint32 tick = SDL_GetTicks();
	
	/* NOT NEEDED ?
	if (tick <= gLastTick) 
	{
		SDL_Delay(1);
		return;
	}
	*/

	
	// Physics-Engine
	int cnt = (tick - gLastTick) / (1000 / PHYSICSFPS) ;
	bgoffs += cnt ;
	gLastTick += cnt * 1000 / PHYSICSFPS ;
	if(bgoffs >= INTRO_BGWIDTH) {
		bgoffs -= INTRO_BGWIDTH;
	}
	//bgoffs++;
	
	
	Uint32 i;
	Uint32 j;
	Uint32 pitch = gScreen->pitch / 4;
	Uint32 pitchFG = introFG->pitch / 4;

	// This intro is quite simply made, so the foreground bitmap must be EXACTLY the same
	// size as our screen!
	
	// Lock screen if needed
	if (SDL_MUSTLOCK(gScreen))
		if (SDL_LockSurface(gScreen) < 0) 
			return;

	// Background
	drawBackground(bgoffs, 0);	
	
	// Foreground
	// Lock foreground if needed
	if (SDL_MUSTLOCK(introFG))
		if (SDL_LockSurface(introFG) < 0) 
			return;

	// Draw non-transparent parts of FG
	for(i=0; i < SCR_WIDTH; i++) {
		for(j=0; j < SCR_HEIGHT; j++) {
			if(((Uint32 *)introFG->pixels)[j * pitchFG + i] != fgtransp) {
				((Uint32 *)gScreen->pixels)[j * pitch + i] = 
						((Uint32 *)introFG->pixels)[j * pitchFG + i];
			}
		}
	}
	
	 
	
	// Unlock foreground if needed
	if (SDL_MUSTLOCK(introFG)) 
		SDL_UnlockSurface(introFG);
	

	// Draw Version String and something blinking
	drawrect(40,455,300,20,0x00000000);
	drawstring(45, 460, LONGVERSION, gScreen);
	
	drawstring(150, 370, "Press Space to start", gScreen);
	
#ifndef CAVAC_RELEASEMODE
	Uint32 tmpcol;
	if(blinkOn) {
		tmpcol = 0x00FFFFFF;
	} else {
		tmpcol = 0x00000000;
	}
	blinkOn = !blinkOn;

	drawcircle(20,460,15,tmpcol);

#endif // CAVAC_RELEASEMODE

#ifdef SHOWFRAMERATE
	displayFPSCounter();
#endif //
	// Unlock screen if needed
	if (SDL_MUSTLOCK(gScreen)) 
		SDL_UnlockSurface(gScreen);
	
	// Tell SDL to update the whole screen
	SDL_UpdateRect(gScreen, 0, 0, 640, 480);
	
}



void displayIntro()
{
	
	initIntro();
	
	bool displayIntro = true;
	while (displayIntro)
	{
		// Render stuff
		renderIntro();
		
		// Poll for events, and handle the ones we care about.
		SDL_Event event;
		while (SDL_PollEvent(&event) && displayIntro) 
		{
			switch (event.type) 
			{
				case SDL_KEYDOWN:
					break;
				case SDL_KEYUP:
					// If escape is pressed, return (and thus, quit)
					if (event.key.keysym.sym == SDLK_SPACE) {
						displayIntro = false;

#ifdef ALLOW_BOSSKEY
					} else if (event.key.keysym.sym == SDLK_ESCAPE) {
						exit(0);
#endif // ALLOW_BOSSKEY
					}
					break;
				case SDL_QUIT:
					exit(0);
			}
		}
	}
	
	deInitIntro();
}

