
#include "global.h"
#include "main.h"
#include "overlays.h"
#include "menu.h"
#include "gametext.h"
#include "sound.h"
#include "options.h"
#include "inventory.h"
#include "gallery.h"

extern void optionsDraw(void);
extern void optionsCycle(void);
extern unsigned short justquitflag;
extern unsigned char optionstate;
extern unsigned char menuclosing;
extern int menuwidth;
//extern SfxSampleType *Clik2;

#ifdef _WINDOWS
char *gallery_filenames[] =
{
	"GALLERY\\1.BMP",
	"GALLERY\\2.BMP",
	"GALLERY\\3.BMP",
	"GALLERY\\4.BMP",
	"GALLERY\\5.BMP",
	"GALLERY\\6.BMP",
	"GALLERY\\7.BMP",
	"GALLERY\\8.BMP",
	"GALLERY\\9.BMP",
	"GALLERY\\10.BMP"
};

#else

char *gallery_filenames[] =
{
	"GALLERY\\1.TIM",
	"GALLERY\\2.TIM",
	"GALLERY\\3.TIM",
	"GALLERY\\4.TIM",
	"GALLERY\\5.TIM",
	"GALLERY\\6.TIM",
	"GALLERY\\7.TIM",
	"GALLERY\\8.TIM",
	"GALLERY\\9.TIM",
	"GALLERY\\10.TIM"
};
#endif

#ifdef DREAMCAST
TIM_HEADER *galleryPic = NULL;
void galleryPicLoad(int n)
{
	if (galleryPic)
		dcTim_Unload(&galleryPic);

	dcTim_Load(&galleryPic, gallery_filenames[n]);
}
#else

	#ifdef _WINDOWS
		void galleryPicLoad(int n)
		{
			//ULONG *tim;
			sound_XA_PauseForLoading();

			//tim=(ULONG *)fileLoad(gallery_filenames[n],0);

			sound_XA_Resume(-1);

			LoadBackdrop1 ( gallery_filenames[n] );
			//galleryUploadTimToVRAM(tim);

			//FREE(tim);			// Remember to free the memory
		}
	#else
		void galleryPicLoad(int n)
		{
			ULONG *tim;
			sound_XA_PauseForLoading();

			tim=(ULONG *)fileLoad(gallery_filenames[n],0);

			sound_XA_Resume(-1);

			galleryUploadTimToVRAM(tim);

			FREE(tim);			// Remember to free the memory
		}
	#endif
#endif

void galleryEnd()
{
#ifdef DREAMCAST
	if (galleryPic)
		dcTim_Unload(&galleryPic);
	emptyInitRenderTexture();
#endif

	displayPage[0].drawenv.isbg = 1;
	displayPage[1].drawenv.isbg = 1;

	optionstate=GALLERY_MAIN;
	draw_routine = optionsDraw;
	cycle_routine = optionsCycle;
	menuwidth=400;
}

void galleryDraw()
{
	int bgwidth;

	VSync(0);
	bgwidth = 10+fontExtentW(font,TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT));
	fontPrint(font,2+(-bgwidth/2),100,TEXTSTR(TEXTSTR_INVENTORY_TRITOQUIT), 128,128,128);
	optionsAlphaRectangle( -bgwidth/2, 95,bgwidth, 18, 0,0x003000);
#ifdef DREAMCAST
	dcTim_Draw(0, 0, 639, 479, galleryPic);
#endif

#ifdef _WINDOWS
	DrawBackdrop();
#endif
}

void galleryCycle()
{
#ifdef DREAMCAST
	if(padData.debounce[0] & PAD_CIRCLE)
#else
	if(padData.debounce[0] & PAD_TRIANGLE)
#endif
	{
		soundPlaySample(optscndata->Clik2,0xffff,1000);
		galleryEnd();
	}
}

void galleryStart(int pic)
{
	draw_routine = galleryDraw;
	cycle_routine = galleryCycle;
	displayPage[0].drawenv.isbg = 0;		// Stop it being cleared each frame!
	displayPage[1].drawenv.isbg = 0;

#ifdef DREAMCAST
	emptyDestroyRenderTexture();
#endif

	galleryPicLoad(pic);
}

#ifndef DREAMCAST
void galleryUploadTimToVRAM(ULONG *addr)
{
#ifndef _WINDOWS
	RECT rect1;
	GsIMAGE tim1;
#else
	RECTPS rect1;
#endif

	// The inventory lives in the bottom bit of vram at the moment, so it needs releasing
	if(streamed_bank)
	{
		textureUnloadBank(streamed_bank);
		streamed_bank = NULL;
	}

	/* get TIM data info. */	
	/* skip and pass file header*/
#ifndef _WINDOWS
	GsGetTimInfo(addr+1, &tim1);	
#endif

	// Hmm ok, loading this directly into the frame buffer as there are textures in the bottom half of VRAM now
	rect1.x=0;
	rect1.y=256;
	rect1.w=512;
	rect1.h=256;

#ifndef _WINDOWS
	LoadImage(&rect1,tim1.pixel);
#endif
	
	DrawSync(0);

#ifndef _WINDOWS
	// Copy page 0 over page 1 to stop that annoying flickeriness
	{
		static RECT rect = {0,256,512,256};
		MoveImage(&rect,0,0);	
		DrawSync(0);
	}
#endif
}
#endif
