// 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: playersprite.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 "playersprite.h"
#include "memops.h"
#include "errorhandler.h"

SDL_Surface *SPRITE_Left;
SDL_Surface *SPRITE_Right;
SDL_Surface* sprite;
MOVE_TYPE lastmove = NO_MOVE;
Uint32 spritenum = 0;
Uint32 lastspritenum = 0;
Uint32 spritelasttick;
Uint32 spritetick;

void initPlayerSprite() {
	char fullfname[MAX_FNAME_LENGTH];
	sprintf(fullfname, "%s%s", RESPATH, "sister_moveleft.bmp");
	SDL_Surface* temp = IMG_Load(fullfname);
	if(!temp) {
		DIE(ERROR_IMAGE_READ, fullfname);
	}
	SPRITE_Left = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);

	sprintf(fullfname, "%s%s", RESPATH, "sister_moveright.bmp");
	temp = IMG_Load(fullfname);
	if(!temp) {
		DIE(ERROR_IMAGE_READ, fullfname);
	}
	SPRITE_Right = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);
	
	sprite = SPRITE_Left;
	
	spritetick = spritelasttick = SDL_GetTicks();
	
}


void deInitPlayerSprite() {
	SDL_FreeSurface(SPRITE_Left);
	SDL_FreeSurface(SPRITE_Right);
}

void blitSprite(Uint32 x, Uint32 y, SDL_Surface* src, Uint32 srcoffs) {
	if (SDL_MUSTLOCK(src))
		if (SDL_LockSurface(src) < 0) 
			return;
	
	Uint32 pitch_tmp = src->pitch / 4;
	Uint32 pitch_bg = gScreen->pitch / 4;	
	Uint32 i, j;
	Uint32 pixel;
	
	// Copy the background into the bigger surface
	for(i=0; i < TILESIZE; i++) {
		for(j=0; j < TILESIZE; j++) {
			pixel = ((Uint32 *)src->pixels)[(j + TILESIZE * srcoffs) * pitch_tmp + i];
			
			if(pixel != 0x0000ff00) {
				((Uint32 *)gScreen->pixels)[(j + y) * pitch_bg + i + x] = pixel;
			}
			
		}
	}
	
	// Unlock Surfaces if needed
	if (SDL_MUSTLOCK(src)) 
		SDL_UnlockSurface(src);
}

void showSprite(Uint32 x, Uint32 y, MOVE_TYPE mtype) {
	spritetick = SDL_GetTicks();
	switch (mtype) {
		case MOVE_LEFT:
			sprite = SPRITE_Left;
			spritenum++;
			break;
		case MOVE_RIGHT:
			sprite = SPRITE_Right;
			spritenum++;
			break;
		case JUMP_LEFT:
			break;
		case JUMP_RIGHT:
			break;
		default:
			break;
	}
	if(lastmove != mtype) {
		spritenum = 0;
	} else {
		if(spritenum >= (Uint32)(sprite->h / TILESIZE)) {
			spritenum = 0;
		}
		if(lastspritenum != spritenum) {
			if(spritelasttick < spritetick) {
				spritelasttick = spritetick + SPRITE_SWITCHWAIT;
				lastspritenum = spritenum;
			} else {
				spritenum = lastspritenum;
			}
		}
	}
	
	blitSprite(x, y, sprite, spritenum);	
	
	lastmove = mtype;
	
}

