/*	=================================================================================================
		プリンメーカー	for WonderSwan

					Programed By K.Tadamori		2000/12/23 - 

	-------------------------------------------------------------------------------------------------
		MainTitle.c		<タイトル>
			
			タイトルの処理を行います。

	=================================================================================================	*/



/*	-------------------------------------------------------------------------------------------------
 *		構築
	-------------------------------------------------------------------------------------------------	*/
#include <sys/bios.h>

#include "title.h"
#include "common.h"
#include "sprite.h"
#include "key.h"
#include "bg.h"
#include "palet.h"
#include "game.h"
#include "Ranking.h"

/*	-------------------------------------------------------------------------------------------------
 *		グローバル変数
	-------------------------------------------------------------------------------------------------	*/
extern MAIN_REG	g_Reg;										/*	ゲーム全体で使うグローバルな変数		*/
extern void	(*g_GameProc)(void);							/*	ゲームの進行状況を制御します。			*/
extern unsigned char ucChrLogo[][16];
extern unsigned char ucChrTitle[][16];

/*	-------------------------------------------------------------------------------------------------
 *		ローカル定数宣言
	-------------------------------------------------------------------------------------------------	*/
enum {
	TITLE_PHASE_LOGO = 0,
	TITLE_PHASE_TITLE,
	TITLE_PHASE_CONFIG
};

#define LOGO_X			7
#define LOGO_Y			5
#define LOGO_WIDTH		13
#define LOGO_HEIGHT		7
#define LOGO_TOPCHR		256
#define LOGO_PARAM		BG_PALc							/*	パレット設定等								*/

#define TITLE_X			4
#define TITLE_Y			4
#define TITLE_WIDTH		21
#define TITLE_HEIGHT	6
#define TITLE_TOPCHR	256
#define TITLE_PARAM		BG_PAL8

#define TITLE_SEL_NUMBER	3
#define TITLE_SEL_START		0
#define TITLE_SEL_SCORE		1
#define TITLE_SEL_EXIT		2

/*	-------------------------------------------------------------------------------------------------
 *		ローカル構造体宣言
	-------------------------------------------------------------------------------------------------	*/
typedef struct{
	int iWait;
} LOGO_REG;

typedef struct{
	int iSelecter;										/*	メニューの選択されている物					*/
} TITLE_REG;


/*	-------------------------------------------------------------------------------------------------
 *		ローカル変数
	-------------------------------------------------------------------------------------------------	*/
TITLE_REG	l_title;
LOGO_REG	l_logo;

/*	-------------------------------------------------------------------------------------------------
 *		ローカル関数
	-------------------------------------------------------------------------------------------------	*/
void Title_Logo_Preparetion();
void Title_Title_Preparation();
void Title_Config_Preparation();

/*	-------------------------------------------------------------------------------------------------
 *		[概要]
 *			初期化の為、一度だけ呼ぶ関数です。
 *		[IN]
 *
 *		[OUT]
 *
 *		[RETURN]
 *
	-------------------------------------------------------------------------------------------------	*/
void Title_Init()
{
	Title_Logo_Preparetion();							/*	ロゴから開始するように設定					*/
}

/*	-------------------------------------------------------------------------------------------------
 *		[概要]
 *			タイトルへ移行する時にコールします。
 *
 *		[IN]
 *
 *		[OUT]
 *
 *		[RETURN]
 *
	-------------------------------------------------------------------------------------------------	*/
void Title_Start()
{
	if( g_GameProc != Title_Logo ){						/*	ロゴ以外の時はタイトルへ移行するように・・	*/
		Title_Title_Preparation();
	}
}

/*	=================================================================================================
		ロゴ処理
	=================================================================================================	*/

/*	-------------------------------------------------------------------------------------------------
 *		ロゴの初期化
 *	-------------------------------------------------------------------------------------------------	*/
void Title_Logo_Preparetion()
{
	int i, j;

	/*	
	 *	変数の初期化
	 */
	g_GameProc = Title_Logo;							/*	メインループ指定							*/
	l_logo.iWait = 180;
	key_WithoutRepeat();

	/*	
	 *	グラフィック設定
	 */
	sprite_Off();
	bg_Off( SCREEN1 );
	bg_Off( SCREEN2 );

	bg_SetOffset( SCREEN1, 0, 0 );
	bg_SetOffset( SCREEN2, 0, 0 );

	lcd_set_color(uiLCDColorL, uiLCDColorH);			/* パレット設定 */
	sprite_SetCharacter( LOGO_TOPCHR, LOGO_WIDTH * LOGO_HEIGHT, ucChrLogo );
	bg_Clear( SCREEN1, 0 );

	for( i = 0; i < LOGO_WIDTH; i++ ){
		for( j = 0; j < LOGO_HEIGHT; j++ ){
			bg_Set( SCREEN1, i + LOGO_X, j + LOGO_Y, i + j * LOGO_WIDTH + LOGO_TOPCHR | LOGO_PARAM );
		}
	}
	bg_Text( SCREEN1, 20, 16, "Ver1.0", BG_PALd );

	bg_On( SCREEN1 );									/*	表示開始									*/
}

/*	-------------------------------------------------------------------------------------------------
 *		ロゴメイン
 *	-------------------------------------------------------------------------------------------------	*/
void Title_Logo()
{
	int iKey;

	l_logo.iWait--;
	if( l_logo.iWait == 0 ){
		Title_Title_Preparation();
	} else if( l_logo.iWait < 40 ){
		palet_SetFade( uiLCDColorL, uiLCDColorH, 0x0000, 0x0000, l_logo.iWait / 5 );
	}

	iKey = key_PushDown();
	if( iKey & KEY_A ){
		Title_Title_Preparation();
	}
}

/*	=================================================================================================
		タイトル処理
	=================================================================================================	*/
void Title_Title_Preparation()
{
	int i, j;	
	/*	
	 *	変数の初期化
	 */
	g_GameProc = Title_Title;
	l_title.iSelecter = TITLE_SEL_START;
	key_WithoutRepeat();

	/*	
	 *	グラフィック設定
	 */
	sprite_Off();
	bg_Off( SCREEN1 );
	bg_Off( SCREEN2 );

	bg_SetOffset( SCREEN1, 0, 0 );
	bg_SetOffset( SCREEN2, 0, 0 );

	lcd_set_color(uiLCDColorL, uiLCDColorH);			/* パレット設定 */
	sprite_SetCharacter( TITLE_TOPCHR, TITLE_WIDTH * TITLE_HEIGHT, ucChrTitle );
	bg_Clear( SCREEN1, 0 );

	for( i = 0; i < TITLE_WIDTH; i++ ){
		for( j = 0; j < TITLE_HEIGHT; j++ ){
			bg_Set( SCREEN1, i + TITLE_X, j + TITLE_Y, i + j * TITLE_WIDTH + TITLE_TOPCHR | TITLE_PARAM );
		}
	}
	bg_Text( SCREEN1, 5, 16, "2000/01/31 HAMHAM.", BG_PALd );

	bg_On( SCREEN1 );									/*	表示開始									*/
}

void Title_Title()
{
	int iKey;
	int iParamStart, iParamScore, iParamConfig, iParamExit;
	iParamStart = BG_PALd;
	iParamScore = BG_PALd;
	iParamConfig = BG_PALd;
	iParamExit = BG_PALd;

	/*	選択されている物の色を変える	*/
	switch(l_title.iSelecter){
		case	TITLE_SEL_START:
			iParamStart = BG_PALb;
			break;
		case	TITLE_SEL_SCORE:
			iParamScore = BG_PALb;
			break;
		case	TITLE_SEL_EXIT:
			iParamExit = BG_PALb;
			break;
	}

	/*	選択肢を描画					*/
	bg_Text( SCREEN1, 11, 11, "START ", iParamStart );
	bg_Text( SCREEN1, 11, 12, "SCORE ", iParamScore );
	bg_Text( SCREEN1, 11, 14, "EXIT  ", iParamExit );

	/*	マスコットの描画				*/

	/*	キー判定						*/
	iKey = key_PushDown();
	if( iKey & KEY_START || iKey & KEY_A ){ 
		switch(l_title.iSelecter){
			case	TITLE_SEL_START:
				Game_Start();
				break;
			case	TITLE_SEL_SCORE:
				Ranking_Start();
				break;
			case	TITLE_SEL_EXIT:
				g_Reg.iGameLoop = FALSE;
				break;
		}
	} else if( iKey & KEY_UP1 ){
		l_title.iSelecter--;
		if( l_title.iSelecter < 0 ){
			l_title.iSelecter = TITLE_SEL_NUMBER - 1;
		}
	} else if( iKey & KEY_DOWN1 ){
		l_title.iSelecter++;
		if( l_title.iSelecter >= TITLE_SEL_NUMBER ){
			l_title.iSelecter = 0;
		}
	}
}

/*	=================================================================================================
		コンフィグ処理
	=================================================================================================	*/
void Title_Config_Preparation()
{
}

void Title_Config()
{
}

