How I handle multiple joysticks (+demo)

ZX80, ZX 81, ZX Spectrum, TS2068 and other clones
Post Reply
Timmy
Well known member
Posts: 392
Joined: Sat Mar 10, 2012 4:18 pm

How I handle multiple joysticks (+demo)

Post by Timmy »

I promised some source code on how to handle multiple joysticks on the Spectrum. Here it is.

Usage:
* Save it to a file called joys.c and compile it using: "zcc +zx -vn -startup=31 joys.c -o joys -create-app"
* Press Fire to choose a joystick. (C for cursor) This code works for qaopm, qaop-space, kempston, sinclair 1+2 and cursor.
(Although I only wrote the cursor part today so it might need a bit more testing.)
* Press Up/Down/Left/Right/Fire to see the first byte on the screen changed.
* Note: This is still for the "old" lib with sccz80. You might have to adapt it for newlib or sdcc.
* If you want to use it in your own games, just copy the first part of the code (up to the part where it says "demo").

Code: Select all

// zcc +zx -vn -startup=31 joys.c -o joys -create-app

#include <input.h>
#include <spectrum.h>
unsigned char joys;

uint (*joyfunc)(struct in_UDK *) __z88dk_fastcall;
struct in_UDK joykeys;

void setupkeyboard() 
{
	joykeys.fire  = in_LookupKey(' ');
	joykeys.left  = in_LookupKey('o');
	joykeys.right = in_LookupKey('p');
	joykeys.up    = in_LookupKey('q');
	joykeys.down  = in_LookupKey('a');

	joyfunc = in_JoyKeyboard;
}

void setupcursor() 
{
	joykeys.fire  = in_LookupKey('0');
	joykeys.left  = in_LookupKey('5');
	joykeys.right = in_LookupKey('8');
	joykeys.up    = in_LookupKey('7');
	joykeys.down  = in_LookupKey('6');

	joyfunc = in_JoyKeyboard;
}

unsigned char kempston_available = 0;

void choosejoystick() 
{
	kempston_available = zx_kempston();
	while (1)
	{
		joyfunc = in_JoyKeyboard;
		joykeys.fire  = in_LookupKey(' ');
		joys = (joyfunc) (&joykeys);
		if (in_FIRE & joys) return;
		joykeys.fire  = in_LookupKey('m');
		joys = (joyfunc) (&joykeys);
		if (in_FIRE & joys) return;
		joykeys.fire  = in_LookupKey('c');
		joys = (joyfunc) (&joykeys);
		if (in_FIRE & joys) { setupcursor(); return; }
		joyfunc = in_JoySinclair1;
		joys = (joyfunc) (&joykeys);
		if (in_FIRE & joys) return;
		joyfunc = in_JoySinclair2;
		joys = (joyfunc) (&joykeys);
		if (in_FIRE & joys) return;
		if (kempston_available)
		{
			joyfunc = in_JoyKempston;
			joys = (joyfunc) (&joykeys);
			if (in_FIRE & joys) return;
		}
	}
}

// Here is a small demo program.

unsigned char *screen = 16384;

main()
{
	int i;
	
	joys = 0;
	setupkeyboard();
	
	// This is just a delay to make sure emulators don't see my enter key to load games as an input.
	// You probably won't need this in your game.
	// You'll likely put code here to play a tune instead.
	for (i=0; i<100; i++)
	{
		#asm
		halt
		#endasm
	}
	
	choosejoystick();
	
	while (1)
	{
		joys = (joyfunc) (&joykeys);
		*(screen) = joys;
	}

	return 0;
}
-- End of code --
Post Reply