Esempio n. 1
0
void nst_load(const char *filename) {
	// Load a Game ROM
	Machine machine(emulator);
	Sound sound(emulator);
	Nes::Result result;
	char *rom;
	int romsize;
	char patchname[512];
	
	// Pause play before pulling out a cartridge
	if (playing) { nst_pause(); }
	
	// Pull out any inserted cartridges
	nst_unload();
	
	// Handle the file as an archive if it is one
	#ifdef _GTK
	char reqname[256];
	bool isarchive = gtkui_archive_handle(filename, reqname, sizeof(reqname));

	if (isarchive) {
		nst_archive_handle(filename, &rom, &romsize, reqname);
	#else
	if (nst_archive_handle(filename, &rom, &romsize, NULL)) {
	#endif
		// Convert the malloc'd char* to an istream
		std::string rombuf(rom, romsize);
		std::istringstream file(rombuf);
		result = machine.Load(file, nst_default_system());
	}
	// Otherwise just load the file
	else {
		std::ifstream file(filename, std::ios::in|std::ios::binary);
		
		// Set the file paths
		nst_set_paths(filename);
		
		if (nst_find_patch(patchname)) { // Load with a patch if there is one
			std::ifstream pfile(patchname, std::ios::in|std::ios::binary);
			Machine::Patch patch(pfile, false);
			result = machine.Load(file, nst_default_system(), patch);
		}
		else { result = machine.Load(file, nst_default_system()); }
	}
	
	if (NES_FAILED(result)) {
		switch (result) {
			case Nes::RESULT_ERR_INVALID_FILE:
				fprintf(stderr, "Error: Invalid file\n");
				break;

			case Nes::RESULT_ERR_OUT_OF_MEMORY:
				fprintf(stderr, "Error: Out of Memory\n");
				break;

			case Nes::RESULT_ERR_CORRUPT_FILE:
				fprintf(stderr, "Error: Corrupt or Missing File\n");
				break;

			case Nes::RESULT_ERR_UNSUPPORTED_MAPPER:
				fprintf(stderr, "Error: Unsupported Mapper\n");
				break;

			case Nes::RESULT_ERR_MISSING_BIOS:
				fprintf(stderr, "Error: Missing Fds BIOS\n");
				break;

			default:
				fprintf(stderr, "Error: %d\n", result);
				break;
		}
		return;
	}
	
	// Deal with any DIP Switches
	nst_dipswitch();
	
	// Set the region
	nst_set_region();
	
	if (machine.Is(Machine::DISK)) {
		Fds fds(emulator);
		fds.InsertDisk(0, 0);
		nst_fds_info();
	}
	
	// Check if sound distortion should be enabled
	sound.SetGenie(conf.misc_genie_distortion);
	
	// note that something is loaded
	loaded = 1;
	
	// Set the title
	video_set_title(nstpaths.gamename);
	#ifdef _GTK
	if (!conf.misc_disable_gui) { gtkui_opengl_start(); gtkui_set_title(nstpaths.gamename); }
	#endif

	// power on
	machine.Power(true); // false = power off
	
	nst_play();
}

int main(int argc, char *argv[]) {
	// This is the main function
	
	static SDL_Event event;
	void *userData = (void*)0xDEADC0DE;

	// Set up directories
	nst_set_dirs();
	
	// Set default config options
	config_set_default();
	
	// Read the config file and override defaults
	config_file_read();
	
	// Exit if there is no CLI argument
	#ifdef _GTK
	if (argc == 1 && conf.misc_disable_gui) {
	#else
	if (argc == 1) {
	#endif
		cli_show_usage();
		return 0;
	}
	
	// Handle command line arguments
	cli_handle_command(argc, argv);
	
	// Initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	// Detect Joysticks
	input_joysticks_detect();
	
	// Set default input keys
	input_set_default();
	
	// Read the input config file and override defaults
	input_config_read();
	
	// Set up the video parameters
	video_set_params();
	
	// Create the window
	#ifdef _GTK
	conf.misc_disable_gui ? video_create() : gtkui_init(argc, argv);
	if (conf.video_fullscreen) { video_create(); }
	#else
	video_create();
	#endif
	
	// Set up the callbacks
	Video::Output::lockCallback.Set(VideoLock, userData);
	Video::Output::unlockCallback.Set(VideoUnlock, userData);
	
	Sound::Output::lockCallback.Set(SoundLock, userData);
	Sound::Output::unlockCallback.Set(SoundUnlock, userData);
	
	User::fileIoCallback.Set(nst_cb_file, userData);
	User::logCallback.Set(nst_cb_log, userData);
	User::eventCallback.Set(nst_cb_event, userData);
	
	// Initialize and load FDS BIOS and NstDatabase.xml
	nstdb = NULL;
	fdsbios = NULL;
	nst_load_db();
	nst_load_fds_bios();

	// Load a rom from the command line
	if (argc > 1) {
		nst_load(argv[argc - 1]);
		
		if (!loaded) {
			fprintf(stderr, "Fatal: Could not load ROM\n");
			exit(1);
		}
	}
	
	// Start the main loop
	nst_quit = 0;
	
	while (!nst_quit) {
		#ifdef _GTK
		while (gtk_events_pending()) {
			gtk_main_iteration_do(FALSE);
		}
		#endif
		if (playing) {
			while (SDL_PollEvent(&event)) {
				switch (event.type) {
					case SDL_QUIT:
						nst_quit = 1;
						break;
					
					case SDL_KEYDOWN:
					case SDL_KEYUP:
					case SDL_JOYHATMOTION:
					case SDL_JOYAXISMOTION:
					case SDL_JOYBUTTONDOWN:
					case SDL_JOYBUTTONUP:
					case SDL_MOUSEBUTTONDOWN:
					case SDL_MOUSEBUTTONUP:
						input_process(cNstPads, event);
						break;
					default: break;
				}	
			}
			
			if (NES_SUCCEEDED(Rewinder(emulator).Enable(true))) {
				Rewinder(emulator).EnableSound(true);
			}
			
			audio_play();
			
			if (updateok) {
				// Pulse the turbo buttons
				input_pulse_turbo(cNstPads);
				
				// Check if it's time to skip a frame
				frameskip = timing_frameskip();
				
				// Execute a frame
				if (!frameskip) {
					emulator.Execute(cNstVideo, cNstSound, cNstPads);
				}
				else { emulator.Execute(NULL, cNstSound, cNstPads); }
				
				// Prevent insane speeds when vsync is turned off...
				if (!altspeed) { updateok = false; }
				
				// ...but only use the limiter when enabled
				if (!conf.timing_limiter) { updateok = true; }
			}
		}
	}
	
	// Remove the cartridge and shut down the NES
	nst_unload();
	
	// Unload the FDS BIOS and NstDatabase.xml
	if (nstdb) { delete nstdb; nstdb = NULL; }
	if (fdsbios) { delete fdsbios; fdsbios = NULL; }
	
	// Deinitialize audio
	audio_deinit();
	
	// Deinitialize joysticks
	input_joysticks_close();
	
	// Write the input config file
	input_config_write();
	
	// Write the config file
	config_file_write();

	return 0;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
	static SDL_Event event;
	int i;
	void* userData = (void*) 0xDEADC0DE;
	char dirname[1024], savedirname[1024], *home;

	// read the key/controller mapping
	ctl_defs = parse_input_file();

	if (!ctl_defs)
	{
		std::cout << "~/.nestopia/nstcontrols not found, creating a new one.\n";
		
		// make sure the output directory exists
		home = getenv("HOME");
		sprintf(dirname, "%s/.nestopia/", home);
		sprintf(savedirname, "%ssave/", dirname);
		mkdir(dirname, 0700);
		mkdir(savedirname, 0700);
		create_input_file();
		
		ctl_defs = parse_input_file();
		if (!ctl_defs)
		{
			std::cout << "Reading ~/.nestopia/nstcontrols file: FAIL\n";
			return -1;
		}
	} 

	playing = 0;
	intbuffer = NULL;

	auxio_init();

	sSettings = new Settings;
	sCheatMgr = new CheatMgr;
	
	gtk_init(&argc, &argv);
	
	get_screen_res();

	UIHelp_Init(argc, argv, sSettings, sCheatMgr, cur_Rwidth, cur_Rheight);
	
	// setup video lock/unlock callbacks
	Video::Output::lockCallback.Set( VideoLock, userData );
	Video::Output::unlockCallback.Set( VideoUnlock, userData );

	// misc callbacks (for others, see NstApuUser.hpp)
	User::fileIoCallback.Set( DoFileIO, userData );
	User::logCallback.Set( DoLog, userData );

	// try to load the FDS BIOS
	auxio_set_fds_bios();

	// and the NST database
	auxio_load_db();

	// attempt to load and autostart a file specified on the commandline
	if (argc > 1)
	{
		NstLoadGame(argv[1]);

		if (loaded)
		{
			if (nsf_mode)
			{
				on_nsfplay_clicked(NULL, NULL);
			}
			else
			{
				on_playbutton_clicked(NULL, NULL);
			}
		}
	}

	nst_quit = 0;
	while (!nst_quit)
	{
		while (gtk_events_pending())
		{
			gtk_main_iteration();
		}
		
		if (playing)
		{
				gtk_main_iteration_do(FALSE);

			 	while (SDL_PollEvent(&event))
				{
					switch (event.type)
					{
						case SDL_QUIT:
							schedule_stop = 1;
							break;

						case SDL_KEYDOWN:
						case SDL_KEYUP:
							// ignore num lock, caps lock, and "mode" (whatever that is)
							event.key.keysym.mod = (SDLMod)((int)event.key.keysym.mod & (~(KMOD_NUM | KMOD_CAPS | KMOD_MODE)));
							
							// (intentional fallthrough)
						case SDL_JOYHATMOTION:
						case SDL_JOYAXISMOTION:
						case SDL_JOYBUTTONDOWN:
						case SDL_JOYBUTTONUP:
							nst_dispatch(cNstPads, event);
							break;
					}	
				}
				//}

				if (NES_SUCCEEDED(Rewinder(emulator).Enable(true)))
				{
					Rewinder(emulator).EnableSound(true);
				}

			m1sdr_TimeCheck();
			if (updateok)
			{
				emulator.Execute(cNstVideo, cNstSound, cNstPads);
				updateok = 0;
			}

			if (state_save)
			{
				kill_video_if_fs();
				auxio_do_state_save();
				state_save = 0;
				cleanup_after_io();
			}

			if (state_load)
			{
				kill_video_if_fs();
				auxio_do_state_load();
				state_load = 0;
				cleanup_after_io();
			}

			if (movie_load)
			{
				kill_video_if_fs();
				auxio_do_movie_load();
				movie_load = 0;
				cleanup_after_io();
			}

			if (movie_save)
			{
				kill_video_if_fs();
				auxio_do_movie_save();
				movie_load = 0;
				cleanup_after_io();
			}

			if (movie_stop)
			{
				movie_stop = 0;
				auxio_do_movie_stop();
			}

			if (schedule_stop)
			{
				NstStopPlaying();
			}
		}
		else
		{
			gtk_main_iteration_do(TRUE);
		}
	}

	nst_unload();

	auxio_shutdown();

	delete sSettings;
	delete sCheatMgr;

	write_output_file(ctl_defs);
	free(ctl_defs);

	return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
	static SDL_Event event;
	int i;
	void* userData = (void*) 0xDEADC0DE;
	char dirname[1024], savedirname[1024], *home;
	
	// read the config file
	read_config_file();

	if (argc == 1 && conf->misc_disable_gui) {
		// Show usage and free config 
		cli_show_usage();
		config_file_free();
		return 0;
	}

	cli_handle_command(argc, argv);
	
	playing = 0;
	intbuffer = NULL;

	// Initialize File input/output routines
	fileio_init();
	
	// Initialize SDL
	if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
		fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
		return 1;
	}
	
	// Initialize input and read input config
	input_init();
	input_read_config();
	
	// Set up the video parameters
	video_set_params();
	
	// Initialize GTK+
	gtk_init(&argc, &argv);
	
	if (conf->misc_disable_gui) {
		// do nothing at this point
	}
	// Don't show a GUI if it has been disabled in the config
	else {
		gtkui_init(argc, argv, rendersize.w, rendersize.h);
	}

	// Create the game window
	video_create();
	
	// setup video lock/unlock callbacks
	Video::Output::lockCallback.Set( VideoLock, userData );
	Video::Output::unlockCallback.Set( VideoUnlock, userData );

	// misc callbacks (for others, see NstApuUser.hpp)
	User::fileIoCallback.Set( DoFileIO, userData );
	User::logCallback.Set( DoLog, userData );

	// try to load the FDS BIOS
	fileio_set_fds_bios();

	// and the NST database
	fileio_load_db();

	// attempt to load and autostart a file specified on the commandline
	if (argc > 1)
	{
		NstLoadGame(argv[argc - 1]);

		if (loaded)
		{
			if (nsf_mode)
			{
				on_nsfplay_clicked(NULL, NULL);
			}
			else
			{
				on_playbutton_clicked(NULL, NULL);
			}
		}
	}

	nst_quit = 0;
	while (!nst_quit)
	{
		while (gtk_events_pending())
		{
			gtk_main_iteration();
		}
		
		if (playing)
		{
				gtk_main_iteration_do(FALSE);

			 	while (SDL_PollEvent(&event))
				{
					switch (event.type)
					{
						case SDL_QUIT:
							schedule_stop = 1;
							return 0; // FIX
							break;
						
						case SDL_KEYDOWN:
						case SDL_KEYUP:
							// ignore num lock, caps lock, and "mode" (whatever that is)
							//event.key.keysym.mod = (SDLMod)((int)event.key.keysym.mod & (~(KMOD_NUM | KMOD_CAPS | KMOD_MODE)));
							
							// (intentional fallthrough)
						case SDL_JOYHATMOTION:
						case SDL_JOYAXISMOTION:
						case SDL_JOYBUTTONDOWN:
						case SDL_JOYBUTTONUP:
							input_process(cNstPads, event);
							break;
					}	
				}
				//}

				if (NES_SUCCEEDED(Rewinder(emulator).Enable(true)))
				{
					Rewinder(emulator).EnableSound(true);
				}

			m1sdr_TimeCheck();
			if (updateok)
			{
				emulator.Execute(cNstVideo, cNstSound, cNstPads);
				//emulator.Execute(cNstVideo, NULL, cNstPads);
				updateok = 0;
			}
			

			if (state_save)
			{
				fileio_do_state_save();
				state_save = 0;
				cleanup_after_io();
			}

			if (state_load)
			{
				fileio_do_state_load();
				state_load = 0;
				cleanup_after_io();
			}

			if (movie_load)
			{
				fileio_do_movie_load();
				movie_load = 0;
				cleanup_after_io();
			}

			if (movie_save)
			{
				fileio_do_movie_save();
				movie_load = 0;
				cleanup_after_io();
			}

			if (movie_stop)
			{
				movie_stop = 0;
				fileio_do_movie_stop();
			}

			if (schedule_stop)
			{
				NstStopPlaying();
			}
		}
		else
		{
			gtk_main_iteration_do(TRUE);
		}
	}

	nst_unload();

	fileio_shutdown();
	
	input_deinit();
	
	write_config_file();
	input_write_config();

	return 0;
}