/*-
 * 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"pie.h"

static int Cx,Cy;
static int Font_base;

static unsigned Frame[14*18*8];

static void init_screen( int font )
{
	static unsigned buf[28*18];
	unsigned *p;
	int i;
	int x,y;

	Font_base = font;

	p = Frame;
	for( i = 0 ; i < 14*18*8 ; i++ )
		*p++ = 0;
	font_set_colordata(Font_base,14*18,(void far*)Frame);

	p = buf;
	for( y = 0 ; y < 18 ; y++ ){
		for( x = 0 ; x < 28 ; x+= 2 ){
			*p++ = 8<<9 | font;
			*p++ = 9<<9 | font;
			font++;
		}
	}
	screen_set_char(0,0,0,28,18,(void far*)buf);
}



static void draw_char( int code )
{
	int font;
	int i;
	unsigned char glyph[8];
	unsigned *addr;
	unsigned char *p;

	if( Cy >= 18 )
		return;

	addr = Frame + (Cy*14+Cx/2)*8;

	p = (unsigned char*)addr;
	if( !(Cx & 1) )
		p++;

	text_get_fontdata(code,(void far*)glyph);
	for( i = 0 ; i < 8 ; i++ ){
		*p = glyph[i];
		p+= 2;
	}

	font = Cy*14 + Cx/2 + Font_base;
	font_set_colordata(font,1,(void far*)addr);

	Cx++;
	if( Cx == 28 ){
		Cx = 0;
		Cy++;
	}
}



static void draw_str( unsigned char *str )
{
	int code;


	while( *str ){
		if( *str & 0x80 ){
			code = (int)str[0] << 8 | str[1];
			str += 2;
		}else{
			code = *str++;
		}
		draw_char(code);
	}
}


static void backspace( void )
{
	int x,y;

	if( Cx == 0 && Cy == 0 )
		return;

	Cx--;
	if( Cx < 0 ){
		Cx = 27;
		Cy--;
	}
	x = Cx;
	y = Cy;
	draw_char(' ');
	Cx = x;
	Cy = y;
}


static void cursor( void )
{
	static unsigned buf[2];
	static int count = 0;

	if( count < 30 ){
		buf[0] = 2<<9;
		buf[1] = (Cx*8)<<8 | Cy*8;
		sprite_set_data(0,1,(void far*)buf);
		sprite_set_range(0,1);
	}else{
		sprite_set_range(0,0);
	}
	count++;
	if( count >= 60 )
		count = 0;
}


int main( int ac, char **av )
{
	unsigned k;
	int font;
	static pie_param_t param;
	unsigned char *str;
	int x,y;
	int prev;

	lcd_set_color(0x8acf,0x0246);
	screen_set_scroll(0,0,0);
	screen_set_scroll(1,0,0);
	display_control(DCM_SCR2|DCM_SCR1|DCM_SPR|7<<8);
	sprite_set_range(0,0);

	palette_set_color(8,0x0077);
	palette_set_color(9,0x0707);
	palette_set_color(10,0x0002);

	param.screen = 1;
	param.x = 0;
	param.y = 0;
	param.mode = 0;
	param.fore = 0;
	param.back = 5;
	param.shade = 3;

	font = PIE_init(0,0);
	screen_fill_char(1,0,0,32,32,4<<9|0);

	Cx = 0;
	Cy = 0;
	init_screen(font);

	PIE_start(&param);

	k = 0;
	for(;;){
		sys_wait(1);
		cursor();
		prev = k;
		k = key_press_check();
		if( k & KEY_START )
			break;

		if( (k & KEY_RIGHT2) && !(prev & KEY_RIGHT2)  ){
			draw_char(' ');
			PIE_reset();
		}else if( (k & KEY_LEFT2) && !(prev & KEY_LEFT2)  ){
			backspace();
			PIE_reset();
		}else{
			str = PIE_input(k);
			if( str )
				draw_str(str);
		}
		x = Cx-7;
		if( x < 0 )
			x = 0;
		else if( x > 14 )
			x = 14;
		if( Cy < 9 )
			y = Cy + 1;
		else
			y = Cy - 9;

		x = -x*8 & 255;
		y = -y*8 & 255;

		screen_set_scroll(1,x,y);
	}
}

