// 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.7 $
$State: Exp $

$Log: showtext.cpp,v $
Revision 1.7  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.6  2006/02/17 10:49:42  cavac
Added error-handling

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

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

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 "showtext.h"
#include "blending.h"
#include "errorhandler.h"

// Surface that contains the font
SDL_Surface *gFont;

// Offset of character data per character
Uint32 gFontOfs[256];
// Width of character data per character
Uint32 gFontWidth[256];

void initShowText()
{
	// Load the font surface
	printf(RESPATH "font.bmp\n");
	SDL_Surface *temp = IMG_Load(RESPATH "font.bmp");
	if(!temp) {
		DIE(ERROR_IMAGE_READ, RESPATH "font.bmp");
	}
	gFont = SDL_ConvertSurface(temp, gScreen->format, SDL_SWSURFACE);
	SDL_FreeSurface(temp);
	
	// -- Calculate Font width
	if (SDL_MUSTLOCK(gFont))
		if (SDL_LockSurface(gFont) < 0)
			return;
	
	Sint32 i, j, k;
	for (k = 0; k < gFont->h / gFont->w; k++)
	{
		gFontOfs[k] = gFont->w;
		gFontWidth[k] = 0;
		for (i = 0; i < gFont->w; i++)
		{
			for (j = 0; j < gFont->w; j++)
			{
				if (((Uint32*)gFont->pixels)[(i + k * gFont->w) *
					(gFont->pitch / 4) + j] != 0)
				{
					if (j < (Sint32)gFontOfs[k])
						gFontOfs[k] = j;
					
					if (j > (Sint32)gFontWidth[k])
						gFontWidth[k] = j;
				}
			}
		}
		gFontWidth[k] -= gFontOfs[k];
	}
	
	// Unlock if needed
	if (SDL_MUSTLOCK(gFont))
		SDL_UnlockSurface(gFont);
	
	
	
}

void drawcharacter(Uint32 x, Uint32 y, Uint32 character, Sint32 blend, SDL_Surface* dest)
{
	if (character == 32)
		return;
	
	Uint32 pitch = dest->pitch / 4;
	
	character -= 33; // our font does not have the first 33 characters.
	
	// Lock surface if needed
	if (SDL_MUSTLOCK(gFont))
		if (SDL_LockSurface(gFont) < 0)
			return;
	
	Sint32 i, j;
	for (i = 0; i < gFont->w; i++) {
		Uint32 screenofs = x + (y + i) * pitch;
		Uint32 charofs = (i + character * gFont->w) *
			(gFont->pitch / 4) + gFontOfs[character];
		for (j = 0; j < (Sint32)gFontWidth[character]; j++) {
			Uint32 pixel = ((Uint32*)gFont->pixels)[charofs];
			if (pixel != 0) {
				Uint32 oldpixel = ((Uint32*)dest->pixels)[screenofs];
				switch (blend) {
					case -1:
						pixel = blend_mul(pixel ^ 0xffffff, oldpixel);
						break;
					case 0:
						pixel = blend_avg(pixel, oldpixel);
						break;
					case 1:
						pixel = blend_add(pixel, oldpixel);
						break;
				}
				((Uint32*)dest->pixels)[screenofs] = pixel;
			}
			screenofs++;
			charofs++;
		}
	}
	
	// Unlock if needed
	if (SDL_MUSTLOCK(gFont))
		SDL_UnlockSurface(gFont);
}

void drawstring(Uint32 x, Uint32 y, char *s, SDL_Surface* dest)
{
	if(!dest) {
		dest = gScreen;
	}
	
	while (*s != 0) {
		drawcharacter(x + 1, y + 1, *s, -1, dest);
		drawcharacter(x, y, *s, 0, dest);
		if (*s == 32)
			x += gFont->w / 3;
		else
			x += gFontWidth[*s - 33];
		
		// Add a little space between characters
		x += 3;
		
		s++;
	}
}
