Added proper analog support.
This commit is contained in:
parent
aa356c6747
commit
13a2f0fba5
|
@ -103,6 +103,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#define BRIEFING_WIDTH 520
|
#define BRIEFING_WIDTH 520
|
||||||
|
|
||||||
|
#define JS_DEADZONE 0.05
|
||||||
|
#define JS_MAX (32767 - JS_DEADZONE*32767)
|
||||||
|
|
||||||
// Object Flags
|
// Object Flags
|
||||||
#define FL_WEAPCO (1L << 0)
|
#define FL_WEAPCO (1L << 0)
|
||||||
#define FL_FRIEND (1L << 1)
|
#define FL_FRIEND (1L << 1)
|
||||||
|
|
|
@ -103,6 +103,9 @@ void engine_init()
|
||||||
for (int i = 0; i < KEY_LAST; i++)
|
for (int i = 0; i < KEY_LAST; i++)
|
||||||
engine.keyState[i] = 0;
|
engine.keyState[i] = 0;
|
||||||
|
|
||||||
|
engine.xaxis = 0;
|
||||||
|
engine.yaxis = 0;
|
||||||
|
|
||||||
engine.eventTimer = 0;
|
engine.eventTimer = 0;
|
||||||
engine.counter2 = 0;
|
engine.counter2 = 0;
|
||||||
engine.timeTaken = 0;
|
engine.timeTaken = 0;
|
||||||
|
|
|
@ -92,7 +92,9 @@ typedef struct Engine_ {
|
||||||
|
|
||||||
char configDirectory[PATH_MAX];
|
char configDirectory[PATH_MAX];
|
||||||
|
|
||||||
char keyState[KEY_LAST];
|
int keyState[KEY_LAST];
|
||||||
|
double xaxis;
|
||||||
|
double yaxis;
|
||||||
|
|
||||||
int cheat; // overall cheat
|
int cheat; // overall cheat
|
||||||
int cheatShield;
|
int cheatShield;
|
||||||
|
|
43
src/game.c
43
src/game.c
|
@ -1339,6 +1339,8 @@ static void game_doPlayer()
|
||||||
int shapeToUse;
|
int shapeToUse;
|
||||||
float cd;
|
float cd;
|
||||||
float cc;
|
float cc;
|
||||||
|
double xm = 0;
|
||||||
|
double ym = 0;
|
||||||
int xmoved = 0;
|
int xmoved = 0;
|
||||||
int ymoved = 0;
|
int ymoved = 0;
|
||||||
char msg[STRMAX];
|
char msg[STRMAX];
|
||||||
|
@ -1444,34 +1446,27 @@ static void game_doPlayer()
|
||||||
LIMIT_ADD(player.reload[0], -1, 0, 999);
|
LIMIT_ADD(player.reload[0], -1, 0, 999);
|
||||||
LIMIT_ADD(player.reload[1], -1, 0, 999);
|
LIMIT_ADD(player.reload[1], -1, 0, 999);
|
||||||
|
|
||||||
if (engine.keyState[KEY_UP])
|
xm = engine.keyState[KEY_RIGHT] - engine.keyState[KEY_LEFT];
|
||||||
{
|
if (!xm)
|
||||||
player.y -= player.speed;
|
xm = engine.xaxis;
|
||||||
engine.ssy += 0.1;
|
|
||||||
ymoved = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (engine.keyState[KEY_DOWN])
|
if (xm)
|
||||||
{
|
{
|
||||||
player.y += player.speed;
|
player.x += xm * player.speed;
|
||||||
engine.ssy -= 0.1;
|
engine.ssx -= xm * 0.1;
|
||||||
ymoved = 1;
|
player.face = (xm < 0 ? 1 : 0);
|
||||||
}
|
|
||||||
|
|
||||||
if (engine.keyState[KEY_LEFT])
|
|
||||||
{
|
|
||||||
player.x -= player.speed;
|
|
||||||
engine.ssx += 0.1;
|
|
||||||
player.face = 1;
|
|
||||||
xmoved = 1;
|
xmoved = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (engine.keyState[KEY_RIGHT])
|
ym = engine.keyState[KEY_DOWN] - engine.keyState[KEY_UP];
|
||||||
|
if (!ym)
|
||||||
|
ym = engine.yaxis;
|
||||||
|
|
||||||
|
if (ym)
|
||||||
{
|
{
|
||||||
player.x += player.speed;
|
player.y += ym * player.speed;
|
||||||
engine.ssx -= 0.1;
|
engine.ssy -= ym * 0.1;
|
||||||
player.face = 0;
|
ymoved = 1;
|
||||||
xmoved = 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (engine.keyState[KEY_ESCAPE])
|
if (engine.keyState[KEY_ESCAPE])
|
||||||
|
@ -1650,6 +1645,8 @@ static void game_doPlayer()
|
||||||
engine.keyState[KEY_DOWN] = 0;
|
engine.keyState[KEY_DOWN] = 0;
|
||||||
engine.keyState[KEY_LEFT] = 0;
|
engine.keyState[KEY_LEFT] = 0;
|
||||||
engine.keyState[KEY_RIGHT] = 0;
|
engine.keyState[KEY_RIGHT] = 0;
|
||||||
|
engine.xaxis = 0;
|
||||||
|
engine.yaxis = 0;
|
||||||
if (CHANCE(1. / 3.))
|
if (CHANCE(1. / 3.))
|
||||||
explosion_add(player.x + RANDRANGE(-10, 10),
|
explosion_add(player.x + RANDRANGE(-10, 10),
|
||||||
player.y + RANDRANGE(-10, 10), SP_BIG_EXPLOSION);
|
player.y + RANDRANGE(-10, 10), SP_BIG_EXPLOSION);
|
||||||
|
@ -2705,6 +2702,8 @@ int game_mainLoop()
|
||||||
engine.keyState[KEY_RIGHT] = 0;
|
engine.keyState[KEY_RIGHT] = 0;
|
||||||
engine.keyState[KEY_FIRE] = 0;
|
engine.keyState[KEY_FIRE] = 0;
|
||||||
engine.keyState[KEY_ALTFIRE] = 0;
|
engine.keyState[KEY_ALTFIRE] = 0;
|
||||||
|
engine.xaxis = 0;
|
||||||
|
engine.yaxis = 0;
|
||||||
LIMIT_ADD(engine.musicVolume, -0.2, 0, 100);
|
LIMIT_ADD(engine.musicVolume, -0.2, 0, 100);
|
||||||
audio_setMusicVolume(engine.musicVolume);
|
audio_setMusicVolume(engine.musicVolume);
|
||||||
}
|
}
|
||||||
|
|
42
src/player.c
42
src/player.c
|
@ -17,6 +17,9 @@ You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "SDL.h"
|
#include "SDL.h"
|
||||||
|
|
||||||
#include "defs.h"
|
#include "defs.h"
|
||||||
|
@ -243,8 +246,7 @@ static enum keys mapkey(int code) {
|
||||||
|
|
||||||
void player_getInput()
|
void player_getInput()
|
||||||
{
|
{
|
||||||
static int prevjoyup, prevjoydown, prevjoyleft, prevjoyright;
|
double val;
|
||||||
int joyup, joydown, joyleft, joyright;
|
|
||||||
static int px = -1, py = -1;
|
static int px = -1, py = -1;
|
||||||
int x, y, w, h;
|
int x, y, w, h;
|
||||||
|
|
||||||
|
@ -315,21 +317,15 @@ void player_getInput()
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_JOYAXISMOTION:
|
case SDL_JOYAXISMOTION:
|
||||||
if (engine.event.jaxis.axis == 1) {
|
val = MIN(1, (double)(abs(engine.event.jaxis.value)) / JS_MAX);
|
||||||
joyup = engine.event.jaxis.value < -16384;
|
if (val < JS_DEADZONE)
|
||||||
joydown = engine.event.jaxis.value >= 16384;
|
val = 0;
|
||||||
if (joyup != prevjoyup)
|
|
||||||
engine.keyState[KEY_UP] = prevjoyup = joyup;
|
if (engine.event.jaxis.axis == 0)
|
||||||
if (joydown != prevjoydown)
|
engine.xaxis = copysign(val, engine.event.jaxis.value);
|
||||||
engine.keyState[KEY_DOWN] = prevjoydown = joydown;
|
else if (engine.event.jaxis.axis == 1)
|
||||||
} else if (engine.event.jaxis.axis == 0) {
|
engine.yaxis = copysign(val, engine.event.jaxis.value);
|
||||||
joyleft = engine.event.jaxis.value < -16384;
|
|
||||||
joyright = engine.event.jaxis.value >= 16384;
|
|
||||||
if (joyleft != prevjoyleft)
|
|
||||||
engine.keyState[KEY_LEFT] = prevjoyleft = joyleft;
|
|
||||||
if (joyright != prevjoyright)
|
|
||||||
engine.keyState[KEY_RIGHT] = prevjoyright = joyright;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_WINDOWEVENT:
|
case SDL_WINDOWEVENT:
|
||||||
|
@ -365,14 +361,18 @@ void player_getInput()
|
||||||
x = screen->w * x / w;
|
x = screen->w * x / w;
|
||||||
y = screen->h * y / h;
|
y = screen->h * y / h;
|
||||||
if (px == x && py == y) {
|
if (px == x && py == y) {
|
||||||
if(engine.keyState[KEY_UP] && engine.cursor_y > 0)
|
if (engine.keyState[KEY_UP])
|
||||||
engine.cursor_y -= 4;
|
engine.cursor_y -= 4;
|
||||||
if(engine.keyState[KEY_DOWN] && engine.cursor_y < screen->h - 4)
|
if (engine.keyState[KEY_DOWN])
|
||||||
engine.cursor_y += 4;
|
engine.cursor_y += 4;
|
||||||
if(engine.keyState[KEY_LEFT] && engine.cursor_x > 0)
|
if (engine.keyState[KEY_LEFT])
|
||||||
engine.cursor_x -= 4;
|
engine.cursor_x -= 4;
|
||||||
if(engine.keyState[KEY_RIGHT] && engine.cursor_x < screen->w - 4)
|
if (engine.keyState[KEY_RIGHT])
|
||||||
engine.cursor_x += 4;
|
engine.cursor_x += 4;
|
||||||
|
if (engine.xaxis)
|
||||||
|
engine.cursor_x += 10 * engine.xaxis;
|
||||||
|
if (engine.yaxis)
|
||||||
|
engine.cursor_y += 10 * engine.yaxis;
|
||||||
} else {
|
} else {
|
||||||
engine.cursor_x = px = x;
|
engine.cursor_x = px = x;
|
||||||
engine.cursor_y = py = y;
|
engine.cursor_y = py = y;
|
||||||
|
|
Loading…
Reference in New Issue