/* Shared splitscreen camera 1.0, by Violet CLM 
   http://www.jazz2online.com/snippets/101/shared-splitscreen-camera/ */

bool cameraShared = false;
void onMain() {
	if (jjLocalPlayerCount == 2) {
		jjPLAYER@ player1 = jjLocalPlayers[0];
		jjPLAYER@ player2 = jjLocalPlayers[1];
		float dx = player1.xPos - player2.xPos;
		float dy = player1.yPos - player2.yPos;
		if (abs(dx) < jjResolutionWidth && abs(dy) < jjResolutionHeight) { //might want to tweak the distance requirement to make it a little tighter
			cameraShared = true;
			//nothing to be done about the black line in the middle of the screen...
			if (jjGameTicks & 7 == 0) { //a slight delay, causing the scrolling to be more gradual
				int lx = int(player1.xPos + player2.xPos - jjResolutionWidth) / 2;
				int ty = int(player1.yPos + player2.yPos - jjResolutionHeight) / 2;
				if (lx < 0) lx = 0; //should also add a check for the far right side of the level
				if (ty < 0) ty = 0; //likewise the bottom
				player1.cameraFreeze(lx + player1.subscreenX, ty + player1.subscreenY, false, false);
				player2.cameraFreeze(lx + player2.subscreenX, ty + player2.subscreenY, false, false);
			}
		} else if (cameraShared) {
			cameraShared = false;
			player1.cameraUnfreeze();
			player2.cameraUnfreeze();
		}
	}
}