Commit c1b3814a authored by alistert's avatar alistert

Improved input handling.

parent 4a9aa467
......@@ -10,7 +10,7 @@
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
......@@ -70,7 +70,7 @@
#define MTL_L_STAGE 3
#define MTL_P_ANIMS (PANIMS + 3)
#define MTL_P_TEMP 45
#define MTL_P_TEMP 46
#define BUFFER_LENGTH 255 /* Should always be big enough to hold any message */
......
......@@ -8,7 +8,7 @@
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
......@@ -46,6 +46,7 @@ Controls::Controls () {
keys[C_JUMP].key = SDLK_SPACE;
keys[C_FIRE].key = SDLK_LALT;
#endif
keys[C_SWIM].key = keys[C_JUMP].key;
keys[C_CHANGE].key = SDLK_RCTRL;
keys[C_ENTER].key = SDLK_RETURN;
keys[C_ESCAPE].key = SDLK_ESCAPE;
......@@ -77,6 +78,8 @@ Controls::Controls () {
buttons[C_STATS].button = -1;
buttons[C_PAUSE].button = -1;
#endif
buttons[C_SWIM].button = buttons[C_JUMP].button;
axes[C_UP].axis = 1;
axes[C_UP].direction = false;
axes[C_DOWN].axis = 1;
......@@ -86,6 +89,7 @@ Controls::Controls () {
axes[C_RIGHT].axis = 0;
axes[C_RIGHT].direction = true;
axes[C_JUMP].axis = -1;
axes[C_SWIM].axis = -1;
axes[C_FIRE].axis = -1;
axes[C_CHANGE].axis = -1;
axes[C_ENTER].axis = -1;
......@@ -95,6 +99,10 @@ Controls::Controls () {
for (count = 0; count < CONTROLS; count++) {
keys[count].state = false;
buttons[count].state = false;
axes[count].state = false;
controls[count].time = 0;
controls[count].state = false;
......@@ -108,6 +116,7 @@ Controls::Controls () {
void Controls::setKey (int control, int key) {
keys[control].key = key;
keys[control].state = false;
return;
......@@ -117,6 +126,7 @@ void Controls::setKey (int control, int key) {
void Controls::setButton (int control, int button) {
buttons[control].button = button;
buttons[control].state = false;
return;
......@@ -127,6 +137,7 @@ void Controls::setAxis (int control, int axis, bool direction) {
axes[control].axis = axis;
axes[control].direction = direction;
axes[control].state = false;
return;
......@@ -171,97 +182,63 @@ int Controls::update (SDL_Event *event, int type) {
case SDL_KEYDOWN:
for (count = 0; count < CONTROLS; count++)
if (event->key.keysym.sym == keys[count].key) {
if (type == KEY_LOOP) return event->key.keysym.sym;
for (count = 0; count < CONTROLS; count++)
if (event->key.keysym.sym == keys[count].key)
keys[count].state = true;
break;
}
if (type == KEY_LOOP) return event->key.keysym.sym;
break;
case SDL_KEYUP:
for (count = 0; count < CONTROLS; count++)
if (event->key.keysym.sym == keys[count].key) {
if (event->key.keysym.sym == keys[count].key)
keys[count].state = false;
break;
}
break;
case SDL_JOYBUTTONDOWN:
for (count = 0; count < CONTROLS; count++)
if (event->jbutton.button == buttons[count].button) {
if (type == JOYSTICK_LOOP) return JOYSTICKB | event->jbutton.button;
for (count = 0; count < CONTROLS; count++)
if (event->jbutton.button == buttons[count].button)
buttons[count].state = true;
break;
}
if (type == JOYSTICK_LOOP) return JOYSTICKB | event->jbutton.button;
break;
case SDL_JOYBUTTONUP:
for (count = 0; count < CONTROLS; count++)
if (event->jbutton.button == buttons[count].button) {
if (event->jbutton.button == buttons[count].button)
buttons[count].state = false;
break;
}
break;
case SDL_JOYAXISMOTION:
for (count = 0; count < CONTROLS; count++)
if (event->jaxis.axis == axes[count].axis) {
if (!axes[count].direction &&
(event->jaxis.value < -16384)) {
axes[count].state = true;
break;
}
else if (axes[count].direction &&
(event->jaxis.value > 16384)) {
axes[count].state = true;
break;
}
else axes[count].state = false;
}
if (type == JOYSTICK_LOOP) {
if (event->jaxis.value < -16384)
return JOYSTICKANEG | event->jaxis.axis;
if (event->jaxis.value > 16384)
else if (event->jaxis.value > 16384)
return JOYSTICKAPOS | event->jaxis.axis;
}
for (count = 0; count < CONTROLS; count++)
if (event->jaxis.axis == axes[count].axis) {
if (!axes[count].direction && (event->jaxis.value < -16384))
axes[count].state = true;
else if (axes[count].direction && (event->jaxis.value > 16384))
axes[count].state = true;
else
axes[count].state = false;
}
break;
}
......
......@@ -8,7 +8,7 @@
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
......@@ -36,14 +36,15 @@
#define C_LEFT 2
#define C_RIGHT 3
#define C_JUMP 4
#define C_FIRE 5
#define C_CHANGE 6 /* Change weapon */
#define C_ENTER 7
#define C_ESCAPE 8
#define C_STATS 9
#define C_PAUSE 10
#define C_SWIM 5
#define C_FIRE 6
#define C_CHANGE 7 /* Change weapon */
#define C_ENTER 8
#define C_ESCAPE 9
#define C_STATS 10
#define C_PAUSE 11
// Size of those arrays
#define CONTROLS 11
#define CONTROLS 12
// Time interval
#define T_KEY 200
......
......@@ -9,7 +9,7 @@
* Part of the OpenJazz project
*
*
* Copyright (c) 2005-2009 Alister Thomson
* Copyright (c) 2005-2010 Alister Thomson
*
* OpenJazz is distributed under the terms of
* the GNU General Public License, version 2.0
......@@ -144,6 +144,7 @@ int DemoLevel::play () {
localPlayer->setControl(C_FIRE, macroPoint & 16);
localPlayer->setControl(C_CHANGE, macroPoint & 32);
localPlayer->setControl(C_JUMP, macroPoint & 64);
localPlayer->setControl(C_SWIM, macroPoint & 64);
......
......@@ -811,20 +811,26 @@ int Level::load (char *fileName, unsigned char diff, bool checkpoint) {
// The players' coordinates
if (!checkpoint && game) {
x = file->loadShort();
y = file->loadShort() + 1;
x = file->loadShort();
y = file->loadShort() + 1;
game->setCheckpoint(x, y);
if (!checkpoint && game) game->setCheckpoint(x, y);
} else file->seek(4, false);
// Set the players' initial values
if(game) {
if (game) {
for (count = 0; count < nPlayers; count++)
game->resetPlayer(players + count);
} else {
localPlayer->reset();
localPlayer->setPosition(TTOF(x), TTOF(y));
}
// Next level
x = file->loadChar();
y = file->loadChar();
......
......@@ -182,7 +182,7 @@ int loadMain (int argc, char *argv[]) {
}
// Check that the config file was opened, and has the correct version
if (file && (file->loadChar() == 1)) {
if (file && (file->loadChar() == 2)) {
// Read video settings
screenW = file->loadShort();
......@@ -480,7 +480,7 @@ void freeMain () {
if (file) {
// Write the version number
file->storeChar(1);
file->storeChar(2);
// Write video settings
file->storeShort(screenW);
......
......@@ -37,10 +37,8 @@
int Menu::setupKeyboard () {
const char *options[7] = {"up", "down", "left", "right", "jump", "fire",
"weapon"};
const char *options[PCONTROLS] = {"up", "down", "left", "right", "jump", "swim up", "fire", "weapon"};
int progress, count, character;
bool used;
progress = 0;
......@@ -50,27 +48,25 @@ int Menu::setupKeyboard () {
if (character == E_QUIT) return E_QUIT;
if (character > 0) {
used = false;
// Check if key is already in use
for (count = 0; count < CONTROLS; count++)
if (character == controls.getKey(count)) {
if (character == controls.getKey(C_ESCAPE)) return E_NONE;
if (count != progress) used = true;
}
if (character > 0) {
// If not, assign it to the current control
// If this is a navigation controls (up, down, or enter),
// make sure it's not the same as other navigation controls
if (!used) {
if (((progress != C_UP) &&
(progress != C_DOWN) &&
(progress != C_ENTER)) ||
(controls.getKey(progress) == character) ||
((controls.getKey(C_UP) != character) &&
(controls.getKey(C_DOWN) != character) &&
(controls.getKey(C_ENTER) != character))) {
controls.setKey(progress, character);
progress++;
if (progress == 7) {
if (progress == PCONTROLS) {
// If all controls have been assigned, return
......@@ -85,13 +81,11 @@ int Menu::setupKeyboard () {
}
if (controls.release(C_ESCAPE)) return E_NONE;
SDL_Delay(T_FRAME);
clearScreen(0);
for (count = 0; count < 7; count++) {
for (count = 0; count < PCONTROLS; count++) {
if (count < progress)
fontmn2->showString("okay", (screenW >> 2) + 176,
......@@ -122,10 +116,8 @@ int Menu::setupKeyboard () {
int Menu::setupJoystick () {
const char *options[7] = {"up", "down", "left", "right", "jump", "fire",
"weapon"};
const char *options[PCONTROLS] = {"up", "down", "left", "right", "jump", "swim up", "fire", "weapon"};
int progress, count, control;
bool used;
progress = 0;
......@@ -139,20 +131,16 @@ int Menu::setupJoystick () {
case JOYSTICKB:
used = false;
// Check if the button is already in use
for (count = 0; count < CONTROLS; count++)
if ((control & 0xFF) == controls.getButton(count)) {
if (count != progress) used = true;
}
// If not, assign it to the current control
// If this is a navigation controls (up, down, or enter),
// make sure it's not the same as other navigation controls
if (!used) {
if (((progress != C_UP) &&
(progress != C_DOWN) &&
(progress != C_ENTER)) ||
(controls.getButton(progress) == (control & 0xFF)) ||
((controls.getButton(C_UP) != (control & 0xFF)) &&
(controls.getButton(C_DOWN) != (control & 0xFF)) &&
(controls.getButton(C_ENTER) != (control & 0xFF)))) {
controls.setButton(progress, control & 0xFF);
progress++;
......@@ -173,21 +161,16 @@ int Menu::setupJoystick () {
case JOYSTICKANEG:
used = false;
// Check if the arrow is already in use
for (count = 0; count < CONTROLS; count++)
if (((control & 0xFF) == controls.getAxis(count)) &&
!controls.getAxisDirection(count)) {
if (count != progress) used = true;
}
// If not, assign it to the current control
// If this is a navigation controls (up, down, or enter),
// make sure it's not the same as other navigation controls
if (!used) {
if (((progress != C_UP) &&
(progress != C_DOWN) &&
(progress != C_ENTER)) ||
((controls.getAxis(progress) == (control & 0xFF)) && !controls.getAxisDirection(progress)) ||
(((controls.getAxis(C_UP) != (control & 0xFF)) || controls.getAxisDirection(C_UP)) &&
((controls.getAxis(C_DOWN) != (control & 0xFF)) || controls.getAxisDirection(C_DOWN)) &&
((controls.getAxis(C_ENTER) != (control & 0xFF)) || controls.getAxisDirection(C_ENTER)))) {
controls.setAxis(progress, control & 0xFF, false);
progress++;
......@@ -208,21 +191,16 @@ int Menu::setupJoystick () {
case JOYSTICKAPOS:
used = false;
// Check if the arrow is already in use
for (count = 0; count < CONTROLS; count++)
if (((control & 0xFF) == controls.getAxis(count)) &&
controls.getAxisDirection(count)) {
if (count != progress) used = true;
}
// If not, assign it to the current control
// If this is a navigation controls (up, down, or enter),
// make sure it's not the same as other navigation controls
if (!used) {
if (((progress != C_UP) &&
(progress != C_DOWN) &&
(progress != C_ENTER)) ||
((controls.getAxis(progress) == (control & 0xFF)) && controls.getAxisDirection(progress)) ||
(((controls.getAxis(C_UP) != (control & 0xFF)) || !controls.getAxisDirection(C_UP)) &&
((controls.getAxis(C_DOWN) != (control & 0xFF)) || !controls.getAxisDirection(C_DOWN)) &&
((controls.getAxis(C_ENTER) != (control & 0xFF)) || !controls.getAxisDirection(C_ENTER)))) {
controls.setAxis(progress, control & 0xFF, true);
progress++;
......@@ -424,12 +402,9 @@ int Menu::setupResolution () {
int Menu::setup () {
const char *setupOptions[4] = {"character", "keyboard", "joystick",
"resolution"};
const char *setupCharacterOptions[5] = {"name", "fur", "bandana", "gun",
"wristband"};
const char *setupCharacterColOptions[8] = {"white", "red", "orange",
"yellow", "green", "blue", "animation 1", "animation 2"};
const char *setupOptions[4] = {"character", "keyboard", "joystick", "resolution"};
const char *setupCharacterOptions[5] = {"name", "fur", "bandana", "gun", "wristband"};
const char *setupCharacterColOptions[8] = {"white", "red", "orange", "yellow", "green", "blue", "animation 1", "animation 2"};
const unsigned char setupCharacterCols[8] = {PC_WHITE, PC_RED, PC_ORANGE,
PC_YELLOW, PC_LGREEN, PC_BLUE, PC_SANIM, PC_LANIM};
int ret;
......
......@@ -787,6 +787,7 @@ void Player::send (unsigned char *data) {
data[42] = (y >> 16) & 255;
data[43] = (y >> 8) & 255;
data[44] = y & 255;
data[45] = pcontrols[C_SWIM];
return;
......@@ -795,7 +796,7 @@ void Player::send (unsigned char *data) {
void Player::receive (unsigned char *buffer) {
// Interpret data recieved from client/server
// Interpret data received from client/server
switch (buffer[1]) {
......@@ -812,6 +813,7 @@ void Player::receive (unsigned char *buffer) {
pcontrols[C_LEFT] = buffer[5];
pcontrols[C_RIGHT] = buffer[6];
pcontrols[C_JUMP] = buffer[7];
pcontrols[C_SWIM] = buffer[45];
pcontrols[C_FIRE] = buffer[8];
pcontrols[C_CHANGE] = false;
......
......@@ -148,7 +148,7 @@
// General
#define PANIMS 38 /* Number of player animations. Is probably higher. */
#define PCONTROLS 7 /* Number of player controls. */
#define PCONTROLS 8 /* Number of player controls. */
// Classes
......
......@@ -158,7 +158,7 @@ void Player::control (unsigned int ticks, int msps) {
} else if (y + PYO_MID > level->getWaterLevel()) {
if (pcontrols[C_JUMP]) {
if (pcontrols[C_SWIM]) {
// Swim upwards
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment