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

	menu.c:			Simple little menu thing
******************************************************************************************/

#include "global.h"

#include "main.h"
#include "viewer.h"
#include "platform.h"
#include "collide.h"
#include "inventory.h"
#include "enemies.h"
#include "scenics.h"
#include "charactr.h"

#include "map_view.h"
#include "map_play.h"

#include "stilts.h"
#include "station.h"
#include "catapult.h"
#include "startup.h"
#include "credits.h"
#include "fireworks.h"
#include "gametext.h"
#include "fma.h"
#include "options.h"
#include "fxtest.h"
#include "seesaw.h"
#include "dogchase.h"
#include "crate.h"
#include "engine.h"
#include "language.h"
#include "sound.h"
#include "demo.h"
#include "menu.h"
#include "card.h"

char startup_choice = 0;
unsigned long startup_colour;
unsigned long startup_colouradd;

TextureType *plank = 0;
TextureBankType *bd = 0;

void startupDraw();
void startupCycle();


// RFW - I've put this in because I can't honestly see the point of the start banner screen
// (everything there's on Fowler's anyway. Note that the start banner *has* to always go to fowlers, because
// TRC says you've got to be able to access the options before the first rumble happens)

// And also because with Fowler's the way it was there was actually no way of "loading & continuing" a game.
// (coz the door on fowler's was "exit back to banner" rather than "continue"

//#define STARTUP_DIRECT_TO_FOWLERS


void startupContinue()
{

// RFW - I've moved the TIM load to "StartupLoad", so there's no loading between the language screen
// and the startup sceen (Which means that the music can keep going)

// RFW - And I've moved the default speed-trial times to "cardInitialGameState()"
// RFW - Same with the initial single map-piece...


/*
		ULONG *tim;

		sound_XA_PauseForLoading();	// It'll be playing the intro, followed by the daytime loop
			tim=(ULONG *)fileLoad("SHELL\\BACKDROP.TIM",0);
			uploadTimToVRAM(tim);
			FREE(tim);
		sound_XA_Resume(-1);
*/

#ifdef STARTUP_DIRECT_TO_FOWLERS
// We want to start on the *Door* of fowler's hut if we've loaded a gamestate
// If we didn't load a gamestate, we want to start either there or on the bomb.
		optionsStart(OPTIONS_DOOR_START | OPTIONS_FROM_TITLE);
#else
		startup_choice = 0;
		startup_colour = 64;
		startup_colouradd = 8;

		draw_routine = startupDraw;
		cycle_routine = startupCycle;
#endif
}

void startupLoad(void)
{
	bd = textureLoadBank("FRONTEND\\FRONTEND.SPT");
	textureDownloadBank(bd);							
	textureDestroyBank(bd);							
	plank = textureFindInAllBanks("64X64PLANK2");		
	ASSERT(plank);

#ifndef STARTUP_DIRECT_TO_FOWLERS
	{
		ULONG *tim;
		tim=(ULONG *)fileLoad("SHELL\\BACKDROP.TIM",0);
		uploadTimToVRAM(tim);
		FREE(tim);
	}
#endif

}

void startupStart()
{
	startupLoad();
	startupContinue();
}

void startupEnd(void)
{
	textureUnloadBank(bd);							
	bd = NULL;

	switch(startup_choice)
	{
		case 0:
			optionsStart(OPTIONS_DOOR_START | OPTIONS_FROM_TITLE);	//BOMB_START);
			break;
		case 1:
			optionsStart(OPTIONS_BED_START | OPTIONS_FROM_TITLE);
			break;
		case 2:
			startupEndLoad();
			break;
		default:
			break;
	}
}

// Called by the language->cheat mode switch
void startupFreeStart()
{
	textureUnloadBank(bd);
	bd = NULL;
}


void startupEndStart()
{
//	DB("start new game\n");
	mapplayStart("A1_HUT17", 0);
}


void startupEndLoad()
{
#ifdef GAME_SHOW_DEMO
	optionsStart(OPTIONS_BOMB_START | OPTIONS_FROM_TITLE);

#else
//	DB("load \n");
	menuStart();
#endif
}


void startupDraw()
{
	drawBGTim();

	if(startup_choice==0)
	{
		fontPrintCentred(font,0,30,TEXTSTR(TEXTSTR_FRONT_START),startup_colour, startup_colour, startup_colour);
		fontPrintCentred(font,0,46,TEXTSTR(TEXTSTR_FRONT_LOAD),48,48,48);
#if defined (GAME_SHOW_DEMO)
		fontPrintCentred(font,0,62,"LEVEL SELECT",48,48,48);
#elif defined (DEBUG)
		fontPrintCentred(font,0,62,"DEBUG MENU",48,48,48);
#endif

	}
	else if(startup_choice==1)
	{
		fontPrintCentred(font,0,30,TEXTSTR(TEXTSTR_FRONT_START),48,48,48);
		fontPrintCentred(font,0,46,TEXTSTR(TEXTSTR_FRONT_LOAD),startup_colour,startup_colour,startup_colour);
#if defined (GAME_SHOW_DEMO)
		fontPrintCentred(font,0,62,"LEVEL SELECT",48,48,48);
#elif defined (DEBUG)
		fontPrintCentred(font,0,62,"DEBUG MENU",48,48,48);
#endif
	}
	else
	{
		fontPrintCentred(font,0,30,TEXTSTR(TEXTSTR_FRONT_START),48,48,48);
		fontPrintCentred(font,0,46,TEXTSTR(TEXTSTR_FRONT_LOAD),48,48,48);
#if defined (GAME_SHOW_DEMO)
		fontPrintCentred(font,0,62,"LEVEL SELECT",startup_colour,startup_colour,startup_colour);
#elif defined (DEBUG)
		fontPrintCentred(font,0,62,"DEBUG MENU",startup_colour,startup_colour,startup_colour);
#endif
	}

}


void startupCycle()
{
	if(pReadPad->debounce[0] & (PAD_START | PAD_CROSS))
		startupEnd();

	if(pReadPad->debounce[0] & PAD_UP)
		startup_choice--;
	if(pReadPad->debounce[0] & PAD_DOWN)
		startup_choice++;

#if defined (DEBUG) || defined (GAME_SHOW_DEMO)
	if((signed char)startup_choice<0)
		startup_choice = 2;

	if(startup_choice>2)
		startup_choice = 0;
#else
	if((signed char)startup_choice<0)
		startup_choice = 1;

	if(startup_choice>1)
		startup_choice = 0;
#endif

	startup_colour+=startup_colouradd;
	if((startup_colour==160)||(startup_colour==56))
		startup_colouradd = -startup_colouradd;
}


