Skip to content

Commit 6263310

Browse files
committed
examples: add joystick support to gl2gears
So we can rotate the gears on the console, too.
1 parent c2f4b59 commit 6263310

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

examples/sdl2/opengl20/gl2gears.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static SDL_Window * window = NULL;
114114
static SDL_GLContext gl_context = NULL;
115115
/** Should the main loop be running? */
116116
static SDL_bool app_running = SDL_TRUE;
117+
static SDL_Joystick *joystick = NULL;
117118

118119
#if !defined(__GLIBC__) && !defined(__NEWLIB__)
119120
/**
@@ -619,6 +620,11 @@ gears_event(SDL_Event * event)
619620
}
620621
break;
621622
}
623+
624+
case SDL_JOYDEVICEADDED:
625+
/* Of course, we should dispose the old one, etc etc :-) */
626+
joystick = SDL_JoystickOpen(event->jdevice.which);
627+
break;
622628
}
623629
}
624630

@@ -652,6 +658,15 @@ gears_idle(void)
652658
tRate0 = t;
653659
frames = 0;
654660
}
661+
662+
if (joystick) {
663+
float amount = dt * 100;
664+
Uint8 hat = SDL_JoystickGetHat(joystick, 0);
665+
if (hat & SDL_HAT_LEFT) view_rot[1] += amount;
666+
if (hat & SDL_HAT_RIGHT) view_rot[1] -= amount;
667+
if (hat & SDL_HAT_UP) view_rot[0] += amount;
668+
if (hat & SDL_HAT_DOWN) view_rot[0] -= amount;
669+
}
655670
}
656671

657672
static const char vertex_shader[] =
@@ -779,7 +794,7 @@ main(int argc, char *argv[])
779794
SDL_SetMainReady();
780795
}
781796

782-
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
797+
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) != 0) {
783798
SDL_Log("Unable to initialize SDL video subsystem: %s\n", SDL_GetError());
784799
return 1;
785800
}

0 commit comments

Comments
 (0)