#include <gccore.h>
#include <ogcsys.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gbcore/gb.h"
#include "sound.h"
#include "io.h"
#include "screen.h"
#include "menu.h"
#include "pad.h"

void main_loop(void)
{
	int line;
	int count=0;
	
	sound_start();
	pad_set_mode(PAD_MODE_GAME);

	for(;;) {
		for(line=0; line<154; line++)
			gb_run();
		
		if(isVPAL)
			count++;
		if (count < 5){
			screen_render_ingame();
			skip = 0;
		}
		else{
			skip = 1;
			count = 0;
		}
		
		if(bMenu){
			sound_stop();
			pad_set_mode(PAD_MODE_MENU);
			if(menu_pause()==-1)
				return;
			pad_set_mode(PAD_MODE_GAME);
			sound_start();
			bMenu = 0;
		}
	}
}

int main()
{
	screen_init(); // init the screen
	sound_init(); // init sound system
	config_init(); // init configuration

	pad_init(); //init pad
	io_init(); //init filesystem

	pad_set_mode(PAD_MODE_MENU);

	int menu_option = menu_main();
	if (menu_option==0)
	{
		if (menu_load_rom()) 
			main_loop();
	}
	if (menu_option==2)
		SYS_ResetSystem(SYS_RETURNTOMENU,0,false);
	
	sound_stop();
	pad_deinit();

	return 0;
}
