/*************************************************************************

    基本的シューティンゲーム  ”Ｓｈｏｏｔｉｎｇ　Ｂｌａｓｔｅｒ”

   Game created & copy right by ELECTRIC ORGANISM( Feb. 1995 ).

**************************************************************************/

#include <string.h>
#include <cdrfrb.h>
#include <snd.h>

#include "graphic .h"
#include "calc    .h"
#include "spr_drv .h"
#include "sound   .h"
#include "str_sub .h"

#include "sblaster.h"

int		put_string_s( int x, int y, char *p, int fc, int bc );
int		config_BGM( char *BGMname_p ), CDDA_reset( void );
int		CDDA_prog_play( int start, int rep ), CDDA_pause( void );

/***  メイン  ***/

void game_main( void );

void main( int argc, char *argv[] )
{
	static char fname[][128] = {
		"SB_MAP.S16", "SB_MAP.MPD","SB_CH.S16","SB_GRAD.PAL"
	}, snd_files[] = { "SBLASTER.PMB" };

	static	 char	title[] = "ＳＨＯＯＴＩＮＧ　ＢＬＡＳＴＥＲ";
	static	 char	BGM_name[] = { "OPENING\n" "GAME BGM\n" "GAME OVER\n" };
	static	 int	scr0_para[] = {  0, 0, 320, 240, 0, 0, 2 };
	static	 int	scr1_para[] = { 32, 0, 256, 240, 0,16, 2 };
	int i, hi_score, pad, mos ;

	/*  音声初期化 */
	SND_init( swork ), SND_elevol_init();

	/*  画面初期化 */
	write_page = PAGE0 ;
	screen_init( GRP32K_MODE, SPRITE_MODE, -1 ), sprite_init();
	screen_offset( PAGE0, scr0_para );
	screen_offset( PAGE1, scr1_para );

	/*  ファイルの読み込み  */
	gradpal_ofs = GRADPAL, gradpal_siz = 4, gradpal_div = 14 ;
	for( i = 0 ; i+3 <= argc && i <= 3 ; i++ ) strcpy( fname[i], argv[i+1] );
	if( load_sprite  ( fname[3],   0, 256 ) != 0 ) goto END ;
	if( load_sprite  ( fname[0],   0, 128 ) != 0 ) goto END ;
	if( load_sprite  ( fname[2], 256,   0 ) != 0 ) goto END ;
	if( load_imagemap( fname[1], ON		  ) != 0 ) goto END ;
	if( sound_init   ( snd_files		  ) != 0 ) goto END ;
	bright_level( B_DARK, 32, 0, BLV_ALL, 0 );

	/*  ＣＤ初期化 */
	cdr_mstop(0), cdr_stptime( 0, 0 );
	CDDA_reset(), config_BGM( BGM_name );

	/*  スプライト初期化  */
	sprite_init(), map_init();
	hi_score = 0 ;

	while( 1 ){
		bright_level( B_DARK, 32, 0, BLV_ALL, 0 );
		box_fill( 0, 0, 320, 240, PSET, 0x8000, 0x8000 );

		symbol( 32, 88, title, 1,1, GRB( 0,31, 0), 0, PSET, 3 );
		symbol( 32, 88, title, 1,1, GRB(31,24,31), 0, PSET, 1 );

		put_string_s( 80, 140, "PUSH BUTTON TO START", GRB(31,31,10), cvcl[1]);
		put_string_s( 40, 216, "(C)MCMXCV ELECTRIC ORGANISM.",
			GRB(28,28,28),cvcl[1] );
		put_string_s( 92    , 182, "HI-SCORE", GRB(31,31,31), cvcl[1] );
		put_string_s( 92+9*8, 182, set_num(hi_score,8), GRB(31,31,31),cvcl[1]);
		CDDA_prog_play( 1, 1 );

		do{
			SND_joy_in_2( 0, &pad ), pad = ~pad ;
			SND_joy_in_1( 1, &mos ), mos = ~mos ;
		}while( ( pad & 0xf0 ) == 0 && ( mos & 0x30 ) == 0 );

		if( ( pad & 0x80 )!= 0 ){
			config_BGM( BGM_name ); continue ;
		}
		if( ( mos & 0x30 )!= 0 ) break ;

		CDDA_pause();
		box_fill( 0, 0, 320, 240, PSET, 0x8000, 0x8000 );
		game_main();
		if( hi_score < score ) hi_score = score ;
	}

END:
	sprite_end(), SND_end(), cdr_mstop(0);
}
