/******************************************************************************************
	CRTEST PS   (c) 1999-2001 ISL

	powerbar.c		- powerbars and stuff for sub-games
******************************************************************************************/

#include "global.h"
#include "main.h"
#include "menu.h"
#include "map_draw.h"
#include "bff_load.h"
#include "collide.h"
#include "debris.h"
#include "lights.h"
#include "timer.h"
#include "maths.h"
#include "overlays.h"


#define POWERBAR_FIREWORKS_WIDTH 140
#define POWERBAR_FIREWORKS_HEIGHT 16



TextureBankType *powerbar_textures;
int powerbar_limit, powerbar_power;


// ==========================================================================================
// Powerbar stuff for the fireworks, catapult and seesaw sub-games
// ==========================================================================================

void powerbar_fireworks_Init(void)
{
	powerbar_textures = textureLoadBank("SHELL\\powerbar.spt");
	textureDownloadBank(powerbar_textures);
}


void powerbar_fireworks_Close(void)
{
	textureUnloadBank(powerbar_textures);
	textureDestroyBank(powerbar_textures);	
}


void powerbar_fireworks_DrawOverlay(int x, int y)
{
TextureType *overlay;

	overlay = textureFindCRCInBank(powerbar_textures, utilStr2CRC("POWRBARBMP"));
	ASSERT(overlay != NULL);	
	overlaySprite(overlay, x, y, 0, 0x808080);
}


void powerbar_fireworks_DrawBarPoly(int x, int y, int w, int h, int size, int curPos, char r0, char g0, char b0,
									char r1, char g1, char b1)
{
POLY_G4 *barPoly;
long redInc, greenInc, blueInc, endRed, endGreen, endBlue;
int actualWidth;

	actualWidth = (((w  << 16) / size) * curPos) >> 16;
//	printf("Actual width = %i\n", actualWidth);
	
	// =====
	// Calculate the end RGB values for the gouraud values
	// =====
	redInc		= ((r1 - r0) << 16);
	greenInc	= ((g1 - g0) << 16);
	blueInc		= ((b1 - b0) << 16);
	redInc = redInc / w;
	greenInc = greenInc / w;
	blueInc = blueInc / w;
	endRed = ((redInc * actualWidth) >> 16) + r0;
	endGreen = ((greenInc * actualWidth) >> 16) + g0;
	endBlue = ((blueInc * actualWidth) >> 16) + b0;

	BEGINPRIM(barPoly, POLY_G4);
	setPolyG4(barPoly);

	barPoly->x0 = x;					// Setup the powerbar draw rectangle
	barPoly->y0 = y;					//
	barPoly->x1 = x + actualWidth;
	barPoly->y1 = y;

	barPoly->x2 = x;
	barPoly->y2 = y + h-1;
	barPoly->x3 = x + actualWidth;
	barPoly->y3 = y + h-1;

	barPoly->r0 = r0;	// Setup the RGB values for the left edge 
	barPoly->g0 = g0;	// of the poly
	barPoly->b0 = b0;	//
	barPoly->r2 = r0;	//
	barPoly->g2 = g0;	//
	barPoly->b2 = b0;	//

	barPoly->r1 = endRed;					// Setup the RGB values for the right edge 
	barPoly->g1 = endGreen;					// of the poly
	barPoly->b1 = endBlue;					//
	barPoly->r3 = endRed;					//
	barPoly->g3 = endGreen;					//
	barPoly->b3 = endBlue;					//
	
	ENDPRIM(barPoly, 0, POLY_G4);
}


void powerbar_fireworks_Draw(int x, int y)
{
int limit1;
	
	if ((limit1 = powerbar_power) >= powerbar_limit / 2)
		limit1 = powerbar_limit / 2;

	powerbar_fireworks_DrawBarPoly(x + 51, y + 5,  POWERBAR_FIREWORKS_WIDTH / 2,  POWERBAR_FIREWORKS_HEIGHT,
									powerbar_limit / 2,limit1, 0, 128, 0, 128, 128, 0);

	if (powerbar_power >= powerbar_limit /2)
		powerbar_fireworks_DrawBarPoly(x + 50 +  POWERBAR_FIREWORKS_WIDTH / 2, y + 5,  
									POWERBAR_FIREWORKS_WIDTH / 2,  POWERBAR_FIREWORKS_HEIGHT,
									powerbar_limit / 2,powerbar_power - powerbar_limit/2, 128, 128, 0, 128, 0, 0);

	powerbar_fireworks_DrawOverlay(x, y);
}
