
#include "dkdata.h"

#define MAX_SPRFRAMES			MAX_MD2SKINS

dsprite_t		sprite;
dsprframe_t		frames[MAX_SPRFRAMES];

byte			*byteimage, *lbmpalette;
int				byteimagewidth, byteimageheight;

char			spritename[1024];


void FinishSprite (void);
void Cmd_Spritename (void);



/*
==============
FinishSprite	
==============
*/
void FinishSprite (void)
{
	FILE	*spriteouthandle;
	int			i, curframe;
	dsprite_t	spritetemp;
	char		savename[1024];

	if (sprite.numframes == 0)
		return;

	if (!strlen(spritename))
		Error ("Didn't name sprite file");
		
	sprintf (savename, "%s%s.sp2", gamedir, spritename);

	if (g_release)
	{
		char	name[1024];

		sprintf (name, "%s.sp2", spritename);
		ReleaseFile (name);
		spritename[0] = 0;		// clear for a new sprite
		sprite.numframes = 0;
		return;
	}


	printf ("saving in %s\n", savename);
	CreatePath (savename);
	spriteouthandle = SafeOpenWrite (savename);


//
// write out the sprite header
//
	spritetemp.ident = LittleLong (IDSPRITEHEADER);
	spritetemp.version = LittleLong (SPRITE_VERSION);
	spritetemp.numframes = LittleLong (sprite.numframes);

	SafeWrite (spriteouthandle, &spritetemp, 12);

//
// write out the frames
//
	curframe = 0;

	for (i=0 ; i<sprite.numframes ; i++)
	{
		frames[i].width = LittleLong(frames[i].width);
		frames[i].height = LittleLong(frames[i].height);
		frames[i].origin_x = LittleLong(frames[i].origin_x);
		frames[i].origin_y = LittleLong(frames[i].origin_y);
	}
	SafeWrite (spriteouthandle, frames, sizeof(frames[0])*sprite.numframes);

	fclose (spriteouthandle);
	
	spritename[0] = 0;		// clear for a new sprite
	sprite.numframes = 0;
}


/*
===============
GrabFrame
===============
*/
int SpriteFrame (char *frame)
{		
	int			y, x1, y1, xh, yh, w, h;
	int			ox, oy;
	dsprframe_t	*pframe;
//	char		savename[1024];
	bitmap_t	bitmap;
	byte		*pic, *pal;
	int			len;

	frame = strlwr(frame);

	len = strlen (frame);

	x1 = 0; 
	y1 = 0;

	if (!strcmp(frame+len-4, ".pcx"))
	{
		LoadPCX (frame, &pic, &pal, &w, &h);
	}
	else if (!strcmp(frame+len-4, ".bmp"))
	{
		LoadBMP (frame, &bitmap);
		w = bitmap.width;
		h = bitmap.height;
		FreeBMP (&bitmap);
	}
	else if (!strcmp(frame+len-4, ".tga"))
	{
		LoadTGA (frame, &pic, &w, &h);
	}
	else
	{
		printf ("Skipping unknown file type: %s\n", frame);
		return 1;
	}

	if ((x1 & 0x07) || (y1 & 0x07) || (w & 0x07) || (h & 0x07))
		Error ("Sprite dimensions not multiples of 8\n");

	if (w > 256 || h > 256)
		Error ("Sprite has a dimension longer than 256\n");

	xh = x1 + w;
	yh = y1 + h;

	ox = w / 2;
	oy = h / 2;

	if (sprite.numframes >= MAX_SPRFRAMES)
		Error ("Too many frames; increase MAX_SPRFRAMES\n");

	pframe = &frames[sprite.numframes];
	pframe->width = w;
	pframe->height = h;
	pframe->origin_x = ox;
	pframe->origin_y = oy;

	sprintf (pframe->name, "models/%s", frame);

	printf ("Grabbed: %s\n", pframe->name);

	sprite.numframes++;
	
	return 1;
}

/*
===============
DK_Cmd_Load
===============
*/
void DK_Cmd_Load (void)
{
	printf ("Not yet supported\n");
}


/*
===============
DK_Cmd_SpriteFrame
===============
*/
void DK_Cmd_SpriteFrame (void)
{
	printf ("Not yet supported\n");
}



/*
==============
DK_Cmd_SpriteName
==============
*/
void DK_Cmd_SpriteName (void)
{
	printf ("Not yet supported\n");
}

/*
==============
DK_Cmd_Sprite
==============
*/
void DK_Cmd_Sprite (char *name)
{
	if (name == NULL)
		Error ("Must supply a directory name\n");

	memset (&sprite, 0, sizeof (dsprite_t));
	strcpy (spritename, name);

	if (!DoDirectoryWork (name, "*.*", "--none---", SpriteFrame))
	{
		printf ("lgen : failed to complete task\n");
		return;
	}

	FinishSprite ();
}