/*-
 * Copyright (c) 2000
 * Tatsuya Kudoh(CDR/TK),ROYALPANDA.    All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include<stdlib.h>
#include<sys/bios.h>
#include"jump.h"

#include"fontdata.h"


int DISP_wait_vblank( void )
{
	sys_wait(0);
}


static void create_ascii_font( void )
{
	unsigned char font[9];
	unsigned buf[8];
	int i,j;


	font[0] = 0;
	for( i = 0 ; i < 128 ; i++ ){
		text_get_fontdata(i,(void far*)(font+1));
		for( j = 0 ; j < 8 ; j++ ){
			buf[j] =  (unsigned)font[j+1] << 9
				| (unsigned)font[j+1] << 1
				| (unsigned)font[j];
			font_set_colordata(i,1,(void far*)buf);
		}
	}
}


void DISP_disable( void )
{
	display_control(7<<8);
}


void DISP_enable( void )
{
	display_control(DCM_SCR1|DCM_SCR2|DCM_SPR|7<<8);
}

void DISP_init( void )
{
	int i;
	int x,y;

	DISP_disable();

	lcd_set_color(0x8acf,0x0246);
	palette_set_color(12,0x7400);	/* for Graphics */
	palette_set_color(13,0x7650);	/* for background */
	palette_set_color(14,0x3300);	/* for Strings */

	create_ascii_font();

	font_set_colordata(128,92,(void far*)Fontdata);

	/* draw background */

	for( y = 0 ; y < 32 ; y+=2 )
		for( x = 0 ; x < 32 ; x+=2 )
			screen_set_char(0,x,y,2,2,(void far*)CT_back);

	screen_fill_char(1,1,0,30,32,12<<9|32);

	for( y = 0 ; y < 32 ; y+=2 ){
		screen_set_char(1,0,y,1,2,(void far*)CT_wall[0]);
		screen_set_char(1,31,y,1,2,(void far*)CT_wall[1]);
	}

	/* sprite */

	sprite_set_range(0,128);
	for( i = 0 ; i < 128 ; i++ )
		sprite_set_location(i,224,224);

	/* scroll */

	screen_set_scroll(0,0,0);
	screen_set_scroll(1,0,0);

	DISP_wait_vblank();
	DISP_enable();
}

void DISP_put_str( int x, int y, char far *str )
{
	unsigned buf[32];
	int i;

	for( i = 0 ; i < 32 ; i++){
		if( !*str )
			break;
		if( x > 31 )
			break;
		buf[i] = 14<<9 | *str;
		str++;
	}
	if( i )
		screen_set_char(1,x,y,i,1,buf);
}


void DISP_clear( void )
{
	int i;

	for( i = 0 ; i < 128 ; i++ )
		sprite_set_location(i,224,224);

	screen_set_scroll(0,16,0);
	screen_set_scroll(1,8,0);
	screen_fill_char(1,1,0,30,32,12<<9|32);
}
