Tutorial #5: Using PPJOY a.k.a. Yes, you CAN use Glovepie with MAME!
I'll bet you've tried to make a glovepie script for mame right? I'll bet you tried it, it didn't respond and you instantly dismissed the program as useless for mame. Oh how wrong you were. MAME and glovepie get along great, as long as you send it virtual joystick commands and not virtual keystrokes and virtual mouse commands.
You see not too long ago MAME adopted low-level mouse and keyboard reading. The purpose of this was to be able to read individual mice and keyboards so things like spinners and lightguns could be read individually. The only problem is windows HATES it when you do this. Reading, sure enough is easy, but writing (which is what we want glovepie to do), is pretty much impossible in windows xp and beyond.
The solution is PPJOY, which is a dll that creates a virtual (read fake) joystick that glovepie can manipulate. Now PPJOY is a pain in the butt to install on vista/win7 and I won't get into that here, but rest assured it DOES install on those OSes and you only have to install it once. A better solution is irrelevant for our purposes anyway because glovepie only supports ppjoy.
So first you have to download ppjoy and install it. I'll post a good link once I can find one (the official homepage kind of died) but the latest version is 8.4.6(5). On vista/7 you'll have to do crazy things like put your machine into test mode to install it. It's akward, but you only have to do it once and it is well worth the effort. I'll post a link to an install guide later.
Your anit-virus program might complain about one of the files having a virus (the mouse one I think). It's a false positive, but if you don't trust it you can allow your anti-virus to delete the file in question as we won't be using it.
Hold up though... your installation is NOT complete. You've just installed the dll suite, you haven't created any virtual joysticks yet! In your start menu, in the ppjoy folder click on "configure joysticks". A little wizard will pop up. Use the "add" button to add a joystick for each player the default settings are fine for now.
One more thing. Open your mame.ini and in the "core input options" section set the joystick deadzone to 0 and the saturation to 1. Why are we doing this? Well these settings are there to compensate for the slop in a real joystick, aka the deadzone. Our ppjoy joysticks are virtual, so there isn't any slop. If we don't at least set the deadzone to zero, mame will have a tendacy to "snap" the crosshairs to the center in a lightgun game. Since the most common use for glovepie is to use a wiimote for a lightgun, we don't want that.
Of course instead of altering your mame.ini you could add the command line options when launching mame, or write specific inis for different games but this isn't a tutorial of advanced mame usage.

Boy that was a lot of work wasn't it? Well the good news is it's a one time deal... you'll never have to do that again! So yeah a bit of setup is involved but it seems like a bit of setup is involved for ANYTHING revolving around mame, so what else is new?
Ok so now that all the setup is done what can you do with ppjoy? Well anything really. Mame supports joysticks, ppjoy is a joystick. Mame supports both analog and digital controls and joysticks have both. So as long as you understand that within mame you'll be binding things to the joystick and not the keyboard/mouse you are good to go! Enough chatter how about an example script.
Here is a script for turning a wiimote into a lightgun. Note that the script is quite rough, but it's just for the purposes of a working example:
-------------------------------
ppjoy1.Analog0=maprange(wiimote1.pointerx,0,1,-1,1)
ppjoy1.analog1=maprange(wiimote1.pointery,0,1,-1,1)
ppjoy1.Digital0=wiimote1.b
ppjoy1.digital1=wiimote1.a
------------------------------
Really? That's it? Well yes, now that we have a joystick to play with we can simply bind some of the wiimote's functions to the joystick.
The ".analog0" and ".digital1" stuff might confuse you. Because the ppjoy is virtual, you can change the characteristics of the joystick. "analog#" refers to that number analog input and "digital#" refers to that number digital input. These can be changed/looked up via ppjoy's "configure joysticks" icon in your start menu, but by default :
analog0-analog2=X-Axis thru Z-Axis
analog3-analog5=RX thru RZ
digital0-digital15=joystick buttons 1-16
digital16-digital19=pov hat Up, Down, Left and Right (aka the dpad)
So yeah, we are just binding the wiimote's values to the virtual joystick, which works quite well in mame.
Some things to note though are the min and max values of the joystick axis and the wiimote pointer.
First off in many wiimote mouse/lightgun scripts you'll see references to stuff like "dot1.posx" and crazy math and all of that. Those are OLD wiimote scripts! The glovepie author wrote a built in function for the wiimote pointer that works just as well as any of those older methods, namely .pointerx and .pointery. These will return a value not of the screen resolution (which is akward) but of 0 to 1 (which isn't) 0 is the leftmost/topmost postion, 1 is the rightmost/bottommost and anything between is, well anything between.
Joystick axis are a little different. Traditionally they are a value of -255 to 255, with 0 being dead center, but the glovepie author has converted this to the values of -1 to 1. Again, this just makes things easier to work with.
So if left on a wiimote is 0 and on our joystick it's -1 how do we deal with this? Well you could use your math skills, but it's cleaner to just use one of glovepie's built in math functions.
MapRange takes a value that you specify and converts it from it's range (that you specify) to a new range (again that you specify) In our case we want to convert the value of a pointer position, that ranges from 0 to 1, to a joystick, that ranges from -1 to 1... so it looks like this:
maprange(wiimote1.pointerx,0,1,-1,1)
Not too hard is it?
The functions discussed above also have the benefit of greatly simplifying glovepie scripts. Most of the scripts on the net are old, created before these handy functions were added to glovepie. The 4 lines of code we wrote makes a perfectly functioning wiimote joystick/lightgun and has the same functionality as a 2 PAGE script you'll typically find on the net.
So in the case of glovepie, the way to learn the language is NOT to look at other people's code, because most people are still doing it the hard way.
