/***************************************************************************

	misc.c

    PSP miscellaneous functions.

***************************************************************************/

#include "psp.h"


/***************************************************************************
	Global variables
 **************************************************************************/

int psp_cpuclock;


/*------------------------------------------------------
	Set PSP CPU clock
 -----------------------------------------------------*/

void set_cpu_clock(void)
{
	switch (psp_cpuclock)
	{
	case 1:  scePowerSetClockFrequency(266, 266, 133); break;
	case 2:  scePowerSetClockFrequency(333, 333, 166); break;
	default: scePowerSetClockFrequency(222, 222, 111); break;
	}
}


/*------------------------------------------------------
	Wait msec.
 -----------------------------------------------------*/

void wait(int msec)
{
	TICKER curr = ticker();
	TICKER target, prev;

	target = curr + msec * (TICKS_PER_SEC / 1000);

	while (curr - target < 0)
	{
		prev = curr;
		curr = ticker();
		if (!bLoop) break;
	}
}


/*------------------------------------------------------
	Load background image
 -----------------------------------------------------*/

void load_background(void)
{
	char path[MAX_PATH];

#ifdef SUPPORT_BMP
	sprintf(path, "%s%s", launchDir, "logo.png");
	if (!load_png(path))
	{
		sprintf(path, "%s%s", launchDir, "logo.bmp");
		if (!load_bmp(path))
		{
			boxfill(0,  0, 479,  23, MAKECOL15( 76, 76, 76));
			boxfill(0, 23, 479,  44, MAKECOL15(113,113,113));
			boxfill(0, 45, 479, 271, MAKECOL15(100,100,100));

			large_icon(4, 0, MAKECOL15(220,220,220), ICON_L_RESET);
			font_print(40, 2, MAKECOL15(220,220,220), "NEOGEO CDZ Emulator for PSP");

			video_copy_rect(draw_frame, 2, &full_rect, &full_rect);
		}
	}
#else
	sprintf(path, "%s%s", launchDir, "logo.png");
	if (!load_png(path))
	{
		boxfill(0,  0, 479,  23, MAKECOL15( 76, 76, 76));
		boxfill(0, 23, 479,  44, MAKECOL15(113,113,113));
		boxfill(0, 45, 479, 271, MAKECOL15(100,100,100));

		large_icon(4, 0, MAKECOL15(220,220,220), ICON_L_RESET);
		font_print(40, 2, MAKECOL15(220,220,220), "NEOGEO CDZ Emulator for PSP");

		video_copy_rect(draw_frame, 2, &full_rect, &full_rect);
	}
#endif
}


/*------------------------------------------------------
	Show background image
 -----------------------------------------------------*/

void show_background(void)
{
	video_copy_rect(2, draw_frame, &full_rect, &full_rect);
}


/*------------------------------------------------------
	Draw battery status
 -----------------------------------------------------*/

void draw_battery_status(int show_warning)
{
	int width, icon;
	int bat = scePowerGetBatteryLifePercent();
	int charging = scePowerIsBatteryCharging();
	static u32 counter = 0;
	char bat_left[8];

	if (bat > 66) icon = ICON_S_BATTERY1;
	else if (bat > 33) icon = ICON_S_BATTERY2;
	else if (bat >= 10) icon = ICON_S_BATTERY3;
	else icon = ICON_S_BATTERY4;

	counter++;

	if ((charging && (counter % 60 < 40)) || !charging)
	{
		small_icon_shadow(434-26+1, 2, COLOR_WHITE, icon, 4);
		small_icon(434-26, 1, COLOR_WHITE, icon);
	}

	font_print_shadow(432, 0, COLOR_WHITE, "[", 4);
	font_print_shadow(462, 0, COLOR_WHITE, "%]", 4);

	if (bat >= 0 && bat <= 100)
		sprintf(bat_left, "%3d", bat);
	else
		strcpy(bat_left, "---");

	width = font_get_string_width(bat_left);
	font_print_shadow(462 - width, 0, COLOR_WHITE, bat_left, 4);

	if (!charging && bat < 10)
	{
		if (show_warning  && (counter % 120 < 40))
		{
			int sx, sy, ex, ey;
			char warning[128];

			sprintf(warning, "Warning: Battery is low (%d%%). Please charge battery!", bat);
			width = font_get_string_width(warning);

			sx = (SCR_WIDTH - width) >> 1;
			sy = (SCR_HEIGHT - 16) >> 1;
			ex = sx + width;
			ey = sy + 16;

			draw_dialog(sx - 6, sy - 4, ex + 6, ey + 8);
			font_print_shadow_center(sy, MAKECOL15(255,128,128), warning, 4);
		}
	}
}


#if 0
/*------------------------------------------------------
	Draw clock
 -----------------------------------------------------*/

void draw_clock(void)
{
	pspTime t;
	char buf[32], buf2[32];
	int width;
	extern int neogeo_region;

	sceRtcGetCurrentClockLocalTime(&t);

	switch (neogeo_region)
	{
	case 0:
	case 1:	sprintf(buf, "%d/%d  ", t.month, t.day); break;
	case 2: sprintf(buf, "%d/%d  ", t.day, t.month); break;
	}

	sprintf(buf2, "%d:%d", t.hour, t.minutes);
	strcat(buf, buf2);

	width = font_get_string_width(buf);

	font_print_shadow(400 - width + 1, 1, COLOR_WHITE, buf, 4);
}

#endif
