jeffchannell.com

Dirty Mednafen GUI

Posted in Linux
2009-03-28 19:57:26 +0000 UTC

A while back, I had a need for a GUI frontend for the game emulator mednafen, so here's a quick and dirty bash script I wrote. It requires mednafen and Zenity.

#!/bin/bash

# NES Rom Browser
# Jeff Channell, 5.07.2006

medgui_conf=~/.medgui

function medgui_start ()
{
	touch $medgui_conf
	zenity --question --title="Default Rom Directory?" --text="Press OK to select a default rom folder, or hit Cancel to always use your Home directory."
	case $? in
		0)
			ROMPATH=`zenity --file-selection --directory --title="Select Default ROM Directory"`
			;;
		*)
			ROMPATH=~/
			;;
	esac
	echo "ROMPATH=$ROMPATH" >> $medgui_conf
}

if [ -e "$medgui_conf" ]
then
	ROMPATH=`sed -n '/^ROMPATH=/p' $medgui_conf | sed -e 's/^ROMPATH=//'` 
else
	zenity --question --text="No config file found. Would you like to create one?"
	case $? in
		0)
			echo "Creating file"
			medgui_start
			;;
		1)
			echo "Error: Cannot find default config!"
			exit 2
			;;
	esac
fi
cd $ROMPATH
SELECTEDROM=`zenity --file-selection --title="Select An NES Rom File..."`
case $? in
	0)
		if [ -e "$SELECTEDROM" ]
		then
			file "$SELECTEDROM" | grep NES
			case $? in
				0)
					mednafen -nes.no8lim 1 -nes.stretch 1 -nes.xres 1024 -nes.yres 768 "$SELECTEDROM"
				;;
				*)
					zenity --error --text="The selected file does not appear to be an NES rom file!"
					exit 1
				;;
			esac
		else
			zenity --error --text="Error: File Does Not Exist!"
			exit 1
		fi
		;;
	1)
		echo "No rom selected. Exiting...";;
	-1)
    echo "No rom selected. Exiting...";;
esac

As it stands, this script does checks to see if the ROM file loaded is an NES ROM. If you're planning on playing other types of ROMs, you'll have to change the script to your tastes, as I only had NES ROMs to test this with.