// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See License.txt for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
					CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/02 13:03:43 $
$Name: rel-0-3-patches $
$Revision: 1.6 $
$State: Exp $

$Log: showpicture.cpp,v $
Revision 1.6  2006/03/02 13:03:43  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.5  2006/02/17 10:49:42  cavac
Added error-handling

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

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 "showpicture.h"
#include "memops.h"
#include "errorhandler.h"

void showPicture(char *fname, Uint32 ticks) {
	Uint32 lasttick = SDL_GetTicks() + ticks;
	
	char fullfname[MAX_FNAME_LENGTH];
	sprintf(fullfname, "%s%s", RESPATH, fname);
	SDL_Surface* temp = IMG_Load(fullfname);
	if(!temp) {
		DIE(ERROR_IMAGE_READ, fullfname);
	}
	SDL_Surface* temp2 = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);
	
	// Lock Surfaces if needed
	if (SDL_MUSTLOCK(temp2))
		if (SDL_LockSurface(temp2) < 0) 
			return;
	if (SDL_MUSTLOCK(gScreen))
		if (SDL_LockSurface(gScreen) < 0) 
			return;
	
	Uint32 pitch_tmp = temp2->pitch / 4;
	Uint32 pitch_bg = gScreen->pitch / 4;	
	Uint32 i, j;
	
	// Copy the background into the bigger surface
	for(i=0; i < (Uint32)temp2->w; i++) {
		for(j=0; j < (Uint32)temp2->h; j++) {
			((Uint32 *)gScreen->pixels)[j * pitch_bg + i] = 
			((Uint32 *)temp2->pixels)[j * pitch_tmp + i];
		}
	}
	
	// Unlock Surfaces if needed
	if (SDL_MUSTLOCK(gScreen)) 
		SDL_UnlockSurface(gScreen);
	if (SDL_MUSTLOCK(temp2)) 
		SDL_UnlockSurface(temp2);
	
	// Tell SDL to update the whole screen
	SDL_UpdateRect(gScreen, 0, 0, 640, 480);
	
	// Wait until time is up
	while(SDL_GetTicks() < lasttick) {
		SDL_Delay(10);
	}
	
	
	SDL_FreeSurface(temp2);
	
}
