rudimentary gamepad controls
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 16 Nov 2016 08:00:14 +0000 (10:00 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Wed, 16 Nov 2016 08:00:14 +0000 (10:00 +0200)
src/app.cc
src/app.h
src/main.cc

index 4fd3690..59c8f07 100644 (file)
@@ -42,6 +42,8 @@ static bool have_headtracking, should_swap;
 static int prev_mx, prev_my;
 static bool bnstate[8];
 static bool keystate[256];
+static Vec2 joy_move, joy_look;
+static float joy_deadzone = 0.1;
 
 static Mat4 view_matrix, mouse_view_matrix, proj_matrix;
 static TextureSet texman;
@@ -166,6 +168,29 @@ static void update(float dt)
        float speed = walk_speed * dt;
        Vec3 dir;
 
+       // joystick
+       float jdeadsq = joy_deadzone * joy_deadzone;
+       float jmove_lensq = length_sq(joy_move);
+       float jlook_lensq = length_sq(joy_look);
+
+       if(jmove_lensq > jdeadsq) {
+               float len = sqrt(jmove_lensq);
+               jmove_lensq -= jdeadsq;
+
+               dir.x += joy_move.x / len * speed;
+               dir.z += joy_move.y / len * speed;
+       }
+       if(jlook_lensq > jdeadsq) {
+               float len = sqrt(jlook_lensq);
+               jlook_lensq -= jdeadsq;
+
+               cam_theta += joy_look.x / len * mouse_speed * 2.0;
+               cam_phi += joy_look.y / len * mouse_speed * 1.0;
+               if(cam_phi < -90.0f) cam_phi = -90.0f;
+               if(cam_phi > 90.0f) cam_phi = 90.0f;
+       }
+
+       // keyboard move
        if(keystate[(int)'w']) {
                dir.z -= speed;
        }
@@ -467,3 +492,26 @@ void app_mouse_delta(int dx, int dy)
                mouse_look(dx * mouse_speed, dy * mouse_speed);
        }
 }
+
+void app_gamepad_axis(int axis, float val)
+{
+       switch(axis) {
+       case 0:
+               joy_move.x = val;
+               break;
+       case 1:
+               joy_move.y = val;
+               break;
+
+       case 2:
+               joy_look.x = val;
+               break;
+       case 3:
+               joy_look.y = val;
+               break;
+       }
+}
+
+void app_gamepad_button(int bn, bool pressed)
+{
+}
index 09b1c82..d81b848 100644 (file)
--- a/src/app.h
+++ b/src/app.h
@@ -26,6 +26,9 @@ void app_mouse_button(int bn, bool pressed, int x, int y);
 void app_mouse_motion(int x, int y);
 void app_mouse_delta(int dx, int dy);
 
+void app_gamepad_axis(int axis, float val);
+void app_gamepad_button(int bn, bool pressed);
+
 // the following functions are implemented by the backend (main.cc)
 void app_quit();
 void app_swap_buffers();
index 4efca60..58e01de 100644 (file)
@@ -17,11 +17,13 @@ static bool quit;
 static unsigned int start_time;
 static unsigned int modkeys;
 
+SDL_GameController *gamepad;
+
 static int scale_factor = 1;
 
 int main(int argc, char **argv)
 {
-       if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
+       if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
                fprintf(stderr, "failed to initialize SDL\n");
                return 1;
        }
@@ -55,6 +57,17 @@ int main(int argc, char **argv)
        SDL_GL_GetDrawableSize(win, &win_width, &win_height);
        win_aspect = (float)win_width / (float)win_height;
 
+       printf("detected %d joysticks\n", SDL_NumJoysticks());
+       for(int i=0; i<SDL_NumJoysticks(); i++) {
+               if(SDL_IsGameController(i)) {
+                       if(!(gamepad = SDL_GameControllerOpen(i))) {
+                               fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
+                               continue;
+                       }
+                       printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
+               }
+       }
+
        if(!init(argc, argv)) {
                SDL_Quit();
                return 1;
@@ -186,6 +199,10 @@ static void process_event(SDL_Event *ev)
                        app_reshape(win_width, win_height);
                }
                break;
+
+       case SDL_CONTROLLERAXISMOTION:
+               app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 65535.0f);
+               break;
        }
 }