whatever: Added Joystick support

This commit is contained in:
Logen Kain 2016-11-12 22:06:39 -07:00
parent 0d4aa4de22
commit 565d2890be

View File

@ -35,6 +35,12 @@ SDL_Texture* gPlayer;
/* Player coors */ /* Player coors */
SDL_Rect gPlayer_dest = { 0 }; SDL_Rect gPlayer_dest = { 0 };
/* Gamepads ! */
SDL_Joystick* gGameController = NULL;
/* deadzone, we don't want light taps to do anything */
const int JOYSTICK_DEAD_ZONE = 8000;
int main ( int argc, char *argv[] ) int main ( int argc, char *argv[] )
{ {
/* How fast our player moves */ /* How fast our player moves */
@ -61,6 +67,12 @@ int main ( int argc, char *argv[] )
/* quit if gameover is not 0 */ /* quit if gameover is not 0 */
int gameover = 0; int gameover = 0;
/* Normalize directions */
int xDir = 0;
int yDir = 0;
SDL_JoyButtonEvent jbutton;
/* message pump */ /* message pump */
while (!gameover) while (!gameover)
{ {
@ -72,7 +84,64 @@ int main ( int argc, char *argv[] )
gameover = 1; gameover = 1;
} }
/* Handle joy button press */
else if (e.type == SDL_JOYBUTTONDOWN)
{
/* 0 is bottom right? At least that's what it is on my logitec */
/* Changed to number 7 as aparently that's start */
if (e.jbutton.button == 7)
{
gameover = 1;
} }
}
else if (e.type == SDL_JOYAXISMOTION)
{
/* Motion on controller 0 */
if( e.jaxis.axis == 0 )
{
/* Left of dead zone */
if( e.jaxis.value < -JOYSTICK_DEAD_ZONE )
{
xDir = -1;
}
/* Right of dead zone */
else if( e.jaxis.value > JOYSTICK_DEAD_ZONE )
{
xDir = 1;
}
else
{
xDir = 0;
}
}
/* Y axis motion -- Note that this is 1 */
if (e.jaxis.axis == 1 )
{
/*Below the dead zone */
if(e.jaxis.value < -JOYSTICK_DEAD_ZONE)
{
yDir = -1;
}
else if (e.jaxis.value > JOYSTICK_DEAD_ZONE)
{
yDir = 1;
}
else
{
yDir = 0;
}
}
}
}
/* move the player with the above input */
gPlayer_dest.x += xDir;
gPlayer_dest.y += yDir;
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL ); const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
if (currentKeyStates[ SDL_SCANCODE_ESCAPE ] || if (currentKeyStates[ SDL_SCANCODE_ESCAPE ] ||
@ -99,6 +168,8 @@ int main ( int argc, char *argv[] )
} }
/* render the background */ /* render the background */
SDL_RenderCopy(gRenderer, gBackground_texture, NULL, NULL); SDL_RenderCopy(gRenderer, gBackground_texture, NULL, NULL);
@ -133,6 +204,21 @@ int init()
return 1; return 1;
} }
/* Check for joysticks */
if( SDL_NumJoysticks() < 1 )
{
printf( "Warning: No joysticks detected!\n" );
}
else
{
/* Load Joystick */
gGameController = SDL_JoystickOpen( 0 );
if ( gGameController == NULL )
{
printf( "Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
}
}
return 0; return 0;
} }
@ -200,10 +286,23 @@ void close_game()
{ {
/* cleanup SDL */ /* cleanup SDL */
if (gRenderer) SDL_DestroyRenderer(gRenderer);
if (gScreen) SDL_DestroyWindow(gScreen); /* Destroy textures */
if (gBackground_texture) SDL_DestroyTexture(gBackground_texture); if (gBackground_texture) SDL_DestroyTexture(gBackground_texture);
if (gPlayer) SDL_DestroyTexture(gPlayer); if (gPlayer) SDL_DestroyTexture(gPlayer);
gBackground_texture = NULL;
gPlayer = NULL;
/* Destroy window */
if (gRenderer) SDL_DestroyRenderer(gRenderer);
if (gScreen) SDL_DestroyWindow(gScreen);
gRenderer = NULL;
gScreen = NULL;
/* Close game controller */
if (gGameController) SDL_JoystickClose(gGameController);
gGameController = NULL;
/* quit subsystems */
SDL_Quit(); SDL_Quit();
} }