Okay, here's my latest on the joy handling. The archive includes new joy.c, joy.h, and main.c files, along with the joytest binary rom. The main.c is the joytest program source.
I also changed base.c to move the JOY_update to AFTER the callback in the vertical blank, but I hardly need to include the whole file just for that change.
http://www.mediafire.com/?rrq1148961g1446
I made some changes compared to the old code to make it easier to use. Now the JOY_init() determines what kind of controller you have plugged into both ports and sets the support type accordingly. If you have a 3 button, it's set to 3 button; if you have a 6 button, it's set to 6 button. If you have a mouse, it's set to mouse (yes, mice are now supported). The type of controller can be found in the upper nibble of the button info:
Code: Select all
jtype = JOY_readJoypad(JOY_1) >> 12;
for example. The types are defined in joy.h as
Code: Select all
#define JOY_TYPE_PAD3 0x00
#define JOY_TYPE_PAD6 0x01
#define JOY_TYPE_MOUSE 0x02
#define JOY_TYPE_UNKNOWN 0x0F
Unknown means either unsupported or no controller. This is valid for all eight joypads, but since the tap isn't yet supported, JOY_3 to JOY_8 will always return JOY_TYPE_UNKNOWN.
Note that the mouse code emulates a 3 button pad: LMB = A, MMB = B, RMB = C, and START = START. You can either use the regular button defines, or the new mouse button defines if it helps the readability of the code. Moving the mouse up sets BUTTON_UP, moving the mouse down sets BUTTON_DOWN, and left and right similarly. So you can actually just pretend it's not a mouse and use it like a regular 3 button pad.
To get the mouse counters, you call JOY_readJoypad(pad) like normal, but OR a flag with the pad index to tell the function to return the X counter or the Y counter. For example:
Code: Select all
mx = JOY_readJoypad(JOY_MOUSEX + i);
my = JOY_readJoypad(JOY_MOUSEY + i);
The mouse counters are unsigned shorts which are updated when the mouse moves. The mouse uses "Cartesian" coordinates: negative values (which decrease the counters) are left and down, while positive values are right and up. The counters wrap around from 0xFFFF to 0x0000 or the reverse. To get a delta, merely subtract the previous counter value from the current value.
So things are very simple now - you can ignore everything and just treat all controllers like 3 button pads; if you need six buttons, just check if the MSN of the pad data is JOY_TYPE_PAD6, then use the extra buttons; if you need a mouse, you can check the MSN for JOY_TYPE_MOUSE and use the JOY_MOUSEX/Y to get the mouse counters. No need to set the port support or wait or any of that.
Note: For many folks, the above is fine - just leave everything alone and let SGDK take care of the details. However, you occasionally need every last bit of speed possible. Reading the controllers takes time - the mouse in particular.
If your game uses a single 3 button pad for control and you NEED the max time, you should turn off the unused port. Check for a 3 button pad on either port and turn off the other port via JOY_setSupport(port, JOY_SUPPORT_OFF);
If you only find a 6 button pad, change the support to three button for faster handling - it takes more time to read the 6 button fully. So do a JOY_setSupport(port, JOY_SUPPORT_3BTN); to make it faster.
In particular, if you need every last cycle and do NOT use a mouse, turn the port support OFF! The mouse takes MUCH longer to read than either the 3 button or 6 button pads.
Again, that's only a consideration for people trying to get more speed out of their game. Most games won't need to worry about this. So, give it a try and let me know what you think... is this a good way to handle the controllers, or can you think of something better?
