diff --git a/sdl/whatever/whatever.c b/sdl/whatever/whatever.c index e112d5c..97c37e0 100644 --- a/sdl/whatever/whatever.c +++ b/sdl/whatever/whatever.c @@ -35,6 +35,12 @@ SDL_Texture* gPlayer; /* Player coors */ 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[] ) { /* How fast our player moves */ @@ -61,6 +67,12 @@ int main ( int argc, char *argv[] ) /* quit if gameover is not 0 */ int gameover = 0; + /* Normalize directions */ + int xDir = 0; + int yDir = 0; + + SDL_JoyButtonEvent jbutton; + /* message pump */ while (!gameover) { @@ -72,8 +84,65 @@ int main ( int argc, char *argv[] ) 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 ); if (currentKeyStates[ SDL_SCANCODE_ESCAPE ] || currentKeyStates[ SDL_SCANCODE_Q ] ) @@ -99,6 +168,8 @@ int main ( int argc, char *argv[] ) } + + /* render the background */ SDL_RenderCopy(gRenderer, gBackground_texture, NULL, NULL); @@ -132,6 +203,21 @@ int init() printf("Unable to set video mode: %s\n", SDL_GetError()); 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; } @@ -200,10 +286,23 @@ void close_game() { /* cleanup SDL */ - if (gRenderer) SDL_DestroyRenderer(gRenderer); - if (gScreen) SDL_DestroyWindow(gScreen); + + /* Destroy textures */ if (gBackground_texture) SDL_DestroyTexture(gBackground_texture); 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(); }