Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conduit style input #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions source/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,25 @@ void camera_ray_pick(struct world* w, float gx0, float gy0, float gz0,
void camera_physics(struct camera* c, float dt) {
assert(c);

float jdx, jdy;
if(input_joystick(dt, &jdx, &jdy)) {
c->rx -= jdx * 2.0F;
c->ry -= jdy * 2.0F;
float px, py, pangle;
const float rotation_c = 20.0F;
if(input_pointer(&px, &py, &pangle)) {
c->rx -= (px - gfx_width() / 2) / (gfx_width() * rotation_c);
c->ry += (py - gfx_height() / 2) / (gfx_height() * rotation_c);
// pangle could be used for rz, but that would be veeery weird :-)
}

float acc_x = 0, acc_y = 0, acc_z = 0;
float speed_c = 40.0F;

float jdx, jdy;
if(input_joystick(dt, &jdx, &jdy)) {
float joy_speed_c = 40.0F * speed_c;
acc_x += (cos(c->rx) * -jdx + sin(c->rx) * sin(c->ry) * jdy) * joy_speed_c;
acc_y += cos(c->ry) * jdy * speed_c;
acc_z += (sin(c->rx) * jdx + cos(c->rx) * sin(c->ry) * jdy) * joy_speed_c;
}

float air_friction = 0.05F;

if(input_held(IB_LEFT)) {
Expand Down
2 changes: 1 addition & 1 deletion source/game/gui/screen_ingame.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <malloc.h>

static void screen_ingame_reset(struct screen* s, int width, int height) {
input_pointer_enable(false);
input_pointer_enable(true);
}

void screen_ingame_render3D(struct screen* s, mat4 view) {
Expand Down
34 changes: 31 additions & 3 deletions source/platform/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern GLFWwindow* window;
static bool input_pointer_enabled;
static double input_old_pointer_x, input_old_pointer_y;
static bool input_key_held[1024];
static int joystick = -1;

void input_init() {
for(int k = 0; k < 1024; k++)
Expand All @@ -40,6 +41,27 @@ void input_init() {
input_pointer_enabled = false;
input_old_pointer_x = 0;
input_old_pointer_y = 0;

// Find joystick
glfwInit();
for(int joy = GLFW_JOYSTICK_1; joy <= GLFW_JOYSTICK_LAST; joy++) {
int num_axes = 0;
const float *axes = glfwGetJoystickAxes(joy, &num_axes);
if(num_axes < 2) continue;

/* Workaround some buggy motherboard which reports the LED controller
* as a joystick; in such cases the controller is reported as having a
* ridiculously high number of axes. See for example:
* https://bbs.archlinux.org/viewtopic.php?id=261161
*/
if (num_axes >= 10) {
fprintf(stderr, "Skipping controller '%s' with %d axes\n",
glfwGetJoystickName(joy), num_axes);
continue;
}
joystick = joy;
break;
}
}

void input_poll() { }
Expand Down Expand Up @@ -103,16 +125,22 @@ bool input_pointer(float* x, float* y, float* angle) {
}

void input_native_joystick(float dt, float* dx, float* dy) {
*dx = 0.0F;
*dy = 0.0F;
if(!input_pointer_enabled) {
double x2, y2;
glfwGetCursorPos(window, &x2, &y2);
*dx = (x2 - input_old_pointer_x) * 0.001F;
*dy = -(y2 - input_old_pointer_y) * 0.001F;
input_old_pointer_x = x2;
input_old_pointer_y = y2;
} else {
*dx = 0.0F;
*dy = 0.0F;
} else if(joystick >= 0) {
int num_axes = 0;
const float *axes = glfwGetJoystickAxes(joystick, &num_axes);
if(axes && num_axes >= 2) {
*dx = axes[0] * dt;
*dy = -axes[1] * dt;
}
}
}

Expand Down