#include "mrc_base.h"
#include "mrc_exb.h"
#include "debug.h"
#include "fontread.h"
#include "ts_font16.h"
#include "fileLog.h"

const static char gbText[] = 
{
	"话说天下大势，分久必合，合久必分。周末七国分争，并入于秦。及秦灭之后，楚、汉 \
	分争，又并入于汉。汉朝自高祖斩白蛇而起义，一统天下，后来光武中兴，传至献帝，遂分 \
	为三国。推其致乱之由，殆始于桓、灵二帝。桓帝禁锢善类，崇信宦官。及桓帝崩，灵帝即 \
	位，大将军窦武、太傅陈蕃共相辅佐。时有宦官曹节等弄权，窦武、陈蕃谋诛之，机事不 \
	密，反为所害，中涓自此愈横。"
};

const static char uniText[] = 
{
	"sdf"
};

#define TEXT1 	\
	"\x6d\x4b\x8b\xd5\x00\x54\x00\x53\x5b\x57\x4f\x53\xff\x1a\x00\x0d\x00\x0a\x04\x14\x04\x15\x04\x01\x04\x16\x04\x17\x04\x18\x04\x19\x00\x0d\x00\x0a\x8b\xdd\x8b\xf4\x59\x29\x4e\x0b\x4e\x4b\x52\xbf\x52\x06\x4e\x45\x5f\xc5\x54\x08\x54\x08\x4e\x45\x5f\xc5\x52\x06\x30\x02\x00\x0d\x00\x0a\x31\x05\x31\x06\x31\x07\x31\x08\x31\x09\x31\x0a\x31\x0b\x00\x0d\x00\x0a\x54\x68\x67\x2b\x4e\x03\x56\xfd\x52\x06\x4e\x89\xff\x0c\x5e\x76\x51\x65\x4e\x8e\x79\xe6\x30\x02\x00\x0d\x00\x0a\x25\xcf\x25\xce\x25\xc7\x25\xc6\x25\xa1\x25\xa0\x25\xb3\x25\xb2\x00\x0d\x00\x0a\x53\xca\x79\xe6\x70\x6d\x4e\x4b\x54\x0e\xff\x0c\x69\x5a\x30\x01\x6c\x49\x52\x06\x4e\x89\xff\x0c\x53\xc8\x5e\x76\x51\x65\x4e\x8e\x6c\x49\x30\x02\x00\x0d\x00\x0a\x25\x45\x25\x46\x25\x47\x25\x48\x25\x49\x25\x4a\x25\x4b\x00\x00"
#define TEXT2	\
	"\x6d\x4b\x8b\xd5\x53\x3a\x57\xdf\x7e\xd8\x52\x36\x00\x00"
#define TEXT_YW	\
	"\x00\x6d\x00\x61\x00\x72\x00\x76\x00\x65\x00\x6c\x00\x3d\x00\x6e\x00\x2e\x59\x47\x8f\xf9\xff\x1b\x60\xca\x59\x47\x00\x20\x00\x20\x00\x76\x00\x74\x00\x2e\x60\xca\x59\x47\x00\x0d\x00\x0a\x00\x6d\x00\x61\x00\x73\x00\x63\x00\x75\x00\x6c\x00\x69\x00\x6e\x00\x65\x00\x3d\x00\x61\x00\x2e\x75\x37\x60\x27\x76\x84\xff\x1b\x5f\x3a\x58\xee\x76\x84\x00\x0d\x00\x0a\x00\x6d\x00\x61\x00\x73\x00\x73\x00\x61\x00\x63\x00\x72\x00\x65\x00\x3d\x00\x6e\x00\x2e\x59\x27\x5c\x60\x67\x40\xff\x0c\x6b\x8b\x67\x40\x00\x0d\x00\x0a\x00\x6d\x00\x61\x00\x73\x00\x73\x00\x69\x00\x76\x00\x65\x00\x3d\x00\x61\x00\x2e\x7c\x97\x59\x27\x76\x84\xff\x1b\x59\x27\x80\x0c\x91\xcd\x76\x84\x00\x0d\x00\x0a\x00\x6d\x00\x61\x00\x73\x00\x74\x00\x3d\x00\x6e\x00\x2e\x68\x45\x67\x46\xff\x1b\x67\x46\x00\x20\x00\x20\x00\x76\x00\x74\x00\x2e\x62\x6f\x5e\x06\x00\x00"
#define TEXT_SG	\
	"\x6e\xda\x6e\xda\x95\x7f\x6c\x5f\x4e\x1c\x90\x1d\x6c\x34\xff\x0c\x6d\x6a\x82\xb1\x6d\xd8\x5c\x3d\x82\xf1\x96\xc4\x30\x02\x00\x0d\x00\x0a\x66\x2f\x97\x5e\x62\x10\x8d\x25\x8f\x6c\x59\x34\x7a\x7a\x30\x02\x00\x20\x00\x0d\x00\x0a\x97\x52\x5c\x71\x4f\x9d\x65\xe7\x57\x28\xff\x0c\x51\xe0\x5e\xa6\x59\x15\x96\x33\x7e\xa2\x30\x02\x00\x0d\x00\x0a\x76\x7d\x53\xd1\x6e\x14\x6a\x35\x6c\x5f\x6e\x1a\x4e\x0a\xff\x0c\x60\xef\x77\x0b\x79\xcb\x67\x08\x66\x25\x98\xce\x30\x02\x00\x20\x00\x0d\x00\x0a\x4e\x00\x58\xf6\x6d\x4a\x91\x52\x55\x9c\x76\xf8\x90\x22\x30\x02\x00\x0d\x00\x0a\x53\xe4\x4e\xca\x59\x1a\x5c\x11\x4e\x8b\xff\x0c\x90\xfd\x4e\xd8\x7b\x11\x8c\x08\x4e\x2d\x30\x02\x00\x20\x00\x0d\x00\x0a\x30\x00\x30\x00\x30\x00\x30\x00\x30\x00\x30\x00\x20\x14\x20\x14\x8c\x03\x5b\xc4\x30\x0a\x4e\x34\x6c\x5f\x4e\xd9\x30\x0b\x00\x20\x00\x00"

static int i;
static int16 font;

/*
void drawFont()
{
	mr_colourSt clr = {30, 144, 0};
	mr_screenRectSt rect = {5,0,230,320};
	int t1,t2;
	char buf[50] = {0};

	mrc_clearScreen(0, 0, 0);
	if(i)
	{
		//uni换行绘制测试
		t1 = mrc_getUptime();
		_drawTextLeft((char*)uniText,
			5, 10, rect, clr, 
			MR_SET_UNICODE, font);
		t2 = mrc_getUptime();
		sprintf(buf, "字库uni换行绘制耗时:%dms", t2-t1);

	}else{
		//uni换行绘制测试
		t1 = mrc_getUptime();
		mrc_drawTextLeft((char*)uniText,
			5, 10, rect, clr, 
			3, font);
		t2 = mrc_getUptime();

		sprintf(buf, "系统uni换行绘制耗时:%dms", t2-t1);
	}

	font = (font + 1)%3;
	mrc_drawText(buf, 0, 300, 30,144,250, 0, MR_FONT_MEDIUM);
	mrc_refreshScreen(0,0,240,320);
}
*/
void * texts[5];
void draw()
{
	mr_colourSt clr = {0, 250, 0};
	mr_screenRectSt rect = {10,10,220,300};
	int t1,t2;
	char buf[50] = {0};
	int32 fw, fh, lines;
	// = {TEXT1, TEXT2, TEXT_SG, TEXT_YW};

	mrc_clearScreen(0, 0, 0);
	
	writePaintLog(0, "tsf_textWidthHeightLines");
	tsf_textWidthHeightLines((uint8*)texts[i], rect.w,
		&fw, &fh, &lines);
	writePaintLog(1, "tsf_textWidthHeightLines");
	mrc_printf("fw = %d fh = %d ines = %d", fw, fh, lines);

	t1 = mrc_getUptime();
	writePaintLog(0, "tsf_drawTextLeft");
	tsf_drawTextLeft((uint8*)texts[i], rect.x, rect.y,
		rect, clr, TS_FT_NORMAL|TS_FT_CRLFNEWLINE|TS_FT_AUTONEWLINE);
	//TS_FT_NORMAL|
	writePaintLog(1, "tsf_drawTextLeft");
	t2 = mrc_getUptime();

	rect.w = 80;
	//tsf_drawTextLeft((uint8*)TEXT2,  rect.x, 100,
	//	rect, clr, TS_FT_NORMAL);

	sprintf(buf, "time = %d ms", t2-t1);
	mrc_drawText(buf, 0, 300, 30,144,250, 0, MR_FONT_MEDIUM);

	mrc_refreshScreen(0,0,240,320);
}

int32 MRC_EXT_INIT(void)
{
	i = 1; 
	font = 0;
	//FontModuleInit(1, 1, 1);
	//drawFont();

	texts[0] = TEXT1, texts[1] = TEXT2, texts[2] = TEXT_SG, texts[3] = TEXT_YW;

	openLogFile();

	//tsf_loadTSF("宋体16.tsf");
	tsf_loadTSF("宋体16.tsf");
	draw();

	return MR_SUCCESS;
}



//Mythroad平台退出函数
int32 MRC_EXT_EXIT(void)
{       
	closeLogFile();

	//FontModuleRelease();
    return MR_SUCCESS;
}

//Mythroad平台事件处理函数
#ifdef SDK_MOD
int32 mrc_appEvent(int32 code, int32 param0, int32 param1)
#else
int32 mrc_event(int32 code, int32 param0, int32 param1)
#endif
{
	if(code == MR_MOUSE_DOWN)
	{
		i = (i+1)%4;
		//drawFont();
		draw();
	}	
	
	return MR_SUCCESS;
}

//Mythroad平台外部事件中断函数（电话、SMS、闹钟等）
#ifdef SDK_MOD
int32 mrc_appPause()
#else
int32 mrc_pause()
#endif
{
     return MR_SUCCESS;       
}

//Mythroad平台外部事件结束恢复函数
#ifdef SDK_MOD
int32 mrc_appResume()
#else
int32 mrc_resume()
#endif
{
     return MR_SUCCESS;
}

#ifndef SDK_MOD
#include "mrc_base_i.h"

int32 mrc_extRecvAppEvent(int32 app, int32 code, int32 param0, int32 param1){
	return MR_SUCCESS;
}

int32 mrc_extRecvAppEventEx(int32 code, int32 p0, int32 p1, int32 p2, int32 p3, int32 p4, int32 p5){
	return MR_SUCCESS;
}
#endif
//int32 mrc_extR