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 JS_DEADZONE 0.05
|
||||
#define JS_MAX (32767 - JS_DEADZONE*32767)
|
||||
|
||||
// Object Flags
|
||||
#define FL_WEAPCO (1L << 0)
|
||||
#define FL_FRIEND (1L << 1)
|
||||
|
|
|
@ -103,6 +103,9 @@ void engine_init()
|
|||
for (int i = 0; i < KEY_LAST; i++)
|
||||
engine.keyState[i] = 0;
|
||||
|
||||
engine.xaxis = 0;
|
||||
engine.yaxis = 0;
|
||||
|
||||
engine.eventTimer = 0;
|
||||
engine.counter2 = 0;
|
||||
engine.timeTaken = 0;
|
||||
|
|
|
@ -92,7 +92,9 @@ typedef struct Engine_ {
|
|||
|
||||
char configDirectory[PATH_MAX];
|
||||
|
||||
char keyState[KEY_LAST];
|
||||
int keyState[KEY_LAST];
|
||||
double xaxis;
|
||||
double yaxis;
|
||||
|
||||
int cheat; // overall cheat
|
||||
int cheatShield;
|
||||
|
|
43
src/game.c
43
src/game.c
|
@ -1339,6 +1339,8 @@ static void game_doPlayer()
|
|||
int shapeToUse;
|
||||
float cd;
|
||||
float cc;
|
||||
double xm = 0;
|
||||
double ym = 0;
|
||||
int xmoved = 0;
|
||||
int ymoved = 0;
|
||||
char msg[STRMAX];
|
||||
|
@ -1444,34 +1446,27 @@ static void game_doPlayer()
|
|||
LIMIT_ADD(player.reload[0], -1, 0, 999);
|
||||
LIMIT_ADD(player.reload[1], -1, 0, 999);
|
||||
|
||||
if (engine.keyState[KEY_UP])
|
||||
{
|
||||
player.y -= player.speed;
|
||||
engine.ssy += 0.1;
|
||||
ymoved = 1;
|
||||
}
|
||||
xm = engine.keyState[KEY_RIGHT] - engine.keyState[KEY_LEFT];
|
||||
if (!xm)
|
||||
xm = engine.xaxis;
|
||||
|
||||
if (engine.keyState[KEY_DOWN])
|
||||
if (xm)
|
||||
{
|
||||
player.y += player.speed;
|
||||
engine.ssy -= 0.1;
|
||||
ymoved = 1;
|
||||
}
|
||||
|
||||
if (engine.keyState[KEY_LEFT])
|
||||
{
|
||||
player.x -= player.speed;
|
||||
engine.ssx += 0.1;
|
||||
player.face = 1;
|
||||
player.x += xm * player.speed;
|
||||
engine.ssx -= xm * 0.1;
|
||||
player.face = (xm < 0 ? 1 : 0);
|
||||
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;
|
||||
engine.ssx -= 0.1;
|
||||
player.face = 0;
|
||||
xmoved = 1;
|
||||
player.y += ym * player.speed;
|
||||
engine.ssy -= ym * 0.1;
|
||||
ymoved = 1;
|
||||
}
|
||||
|
||||
if (engine.keyState[KEY_ESCAPE])
|
||||
|
@ -1650,6 +1645,8 @@ static void game_doPlayer()
|
|||
engine.keyState[KEY_DOWN] = 0;
|
||||
engine.keyState[KEY_LEFT] = 0;
|
||||
engine.keyState[KEY_RIGHT] = 0;
|
||||
engine.xaxis = 0;
|
||||
engine.yaxis = 0;
|
||||
if (CHANCE(1. / 3.))
|
||||
explosion_add(player.x + RANDRANGE(-10, 10),
|
||||
player.y + RANDRANGE(-10, 10), SP_BIG_EXPLOSION);
|
||||
|
@ -2705,6 +2702,8 @@ int game_mainLoop()
|
|||
engine.keyState[KEY_RIGHT] = 0;
|
||||
engine.keyState[KEY_FIRE] = 0;
|
||||
engine.keyState[KEY_ALTFIRE] = 0;
|
||||
engine.xaxis = 0;
|
||||
engine.yaxis = 0;
|
||||
LIMIT_ADD(engine.musicVolume, -0.2, 0, 100);
|
||||
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/>.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "SDL.h"
|
||||
|
||||
#include "defs.h"
|
||||
|
@ -243,8 +246,7 @@ static enum keys mapkey(int code) {
|
|||
|
||||
void player_getInput()
|
||||
{
|
||||
static int prevjoyup, prevjoydown, prevjoyleft, prevjoyright;
|
||||
int joyup, joydown, joyleft, joyright;
|
||||
double val;
|
||||
static int px = -1, py = -1;
|
||||
int x, y, w, h;
|
||||
|
||||
|
@ -315,21 +317,15 @@ void player_getInput()
|
|||
break;
|
||||
|
||||
case SDL_JOYAXISMOTION:
|
||||
if (engine.event.jaxis.axis == 1) {
|
||||
joyup = engine.event.jaxis.value < -16384;
|
||||
joydown = engine.event.jaxis.value >= 16384;
|
||||
if (joyup != prevjoyup)
|
||||
engine.keyState[KEY_UP] = prevjoyup = joyup;
|
||||
if (joydown != prevjoydown)
|
||||
engine.keyState[KEY_DOWN] = prevjoydown = joydown;
|
||||
} else if (engine.event.jaxis.axis == 0) {
|
||||
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;
|
||||
}
|
||||
val = MIN(1, (double)(abs(engine.event.jaxis.value)) / JS_MAX);
|
||||
if (val < JS_DEADZONE)
|
||||
val = 0;
|
||||
|
||||
if (engine.event.jaxis.axis == 0)
|
||||
engine.xaxis = copysign(val, engine.event.jaxis.value);
|
||||
else if (engine.event.jaxis.axis == 1)
|
||||
engine.yaxis = copysign(val, engine.event.jaxis.value);
|
||||
|
||||
break;
|
||||
|
||||
case SDL_WINDOWEVENT:
|
||||
|
@ -365,14 +361,18 @@ void player_getInput()
|
|||
x = screen->w * x / w;
|
||||
y = screen->h * y / h;
|
||||
if (px == x && py == y) {
|
||||
if(engine.keyState[KEY_UP] && engine.cursor_y > 0)
|
||||
if (engine.keyState[KEY_UP])
|
||||
engine.cursor_y -= 4;
|
||||
if(engine.keyState[KEY_DOWN] && engine.cursor_y < screen->h - 4)
|
||||
if (engine.keyState[KEY_DOWN])
|
||||
engine.cursor_y += 4;
|
||||
if(engine.keyState[KEY_LEFT] && engine.cursor_x > 0)
|
||||
if (engine.keyState[KEY_LEFT])
|
||||
engine.cursor_x -= 4;
|
||||
if(engine.keyState[KEY_RIGHT] && engine.cursor_x < screen->w - 4)
|
||||
if (engine.keyState[KEY_RIGHT])
|
||||
engine.cursor_x += 4;
|
||||
if (engine.xaxis)
|
||||
engine.cursor_x += 10 * engine.xaxis;
|
||||
if (engine.yaxis)
|
||||
engine.cursor_y += 10 * engine.yaxis;
|
||||
} else {
|
||||
engine.cursor_x = px = x;
|
||||
engine.cursor_y = py = y;
|
||||
|
|
Loading…
Reference in New Issue