From 4fd0714c121c9f26fdaec9cfab86fe9503bfc848 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Sun, 16 Dec 2007 16:05:47 +0100 Subject: [PATCH] Joystick support (via command line); better keyboard controls Enable more keyboard controls for Player 1 (arrow keys, space, left control) and enable the usage of a joystick when the user specifies the name of the joystick as command line argument. This even works with applesmc on Linux, so you can play and control Tennix by tilting your Apple MacBook and pressing the left control key, which is now mapped to Player 1 swing racket. --- game.c | 42 ++++++++++++++++++++++++++++++++++++------ game.h | 6 +++++- graphics.c | 1 - input.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ input.h | 16 ++++++++++++++++ makefile | 4 ++-- tennix.c | 34 ++++++++++++++++++++++++++++------ 7 files changed, 136 insertions(+), 16 deletions(-) diff --git a/game.c b/game.c index 8dec55f..dfddfb8 100644 --- a/game.c +++ b/game.c @@ -53,7 +53,10 @@ void game( bool singleplayer) { false, { { { 0 } } }, 0.0, - SOUND_MAX + SOUND_MAX, + 0.0, + 0.0, + 0 }; strcpy( s.game_score_str, format_game( &s)); @@ -233,6 +236,20 @@ bool step( GameState* s) { SDL_PollEvent( &e); keys = SDL_GetKeyState( NULL); + switch(e.type) { + case SDL_JOYAXISMOTION: + if (e.jaxis.axis == JOYSTICK_Y_AXIS) { + s->joystick_y = JOYSTICK_PERCENTIZE(e.jaxis.value); + } else if (e.jaxis.axis == JOYSTICK_X_AXIS) { + s->joystick_x = JOYSTICK_PERCENTIZE(e.jaxis.value); + } + break; + case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: + if (e.jbutton.button == JOYSTICK_BUTTON_A) { + s->joystick_a = (e.jbutton.state == SDL_PRESSED); + } + break; + } if( keys['c'] && s->time%50==0) { s->court_type++; @@ -243,13 +260,17 @@ bool step( GameState* s) { if( !is_fading() && !s->is_over) { if( s->player1.type == PLAYER_TYPE_HUMAN) { - input_human( &s->player1, keys['w'], keys['s'], keys['d']); + input_human( &s->player1, + keys['w'] || keys[SDLK_UP] || s->joystick_y < -JOYSTICK_TRESHOLD, + keys['s'] || keys[SDLK_DOWN] || s->joystick_y > JOYSTICK_TRESHOLD, + keys['d'] || keys[SDLK_SPACE] || keys[SDLK_LCTRL] || s->joystick_a, + s); } else { input_ai( &s->player1, &s->ball, &s->player2, s); } if( s->player2.type == PLAYER_TYPE_HUMAN) { - input_human( &s->player2, keys['o'], keys['l'], keys['k']); + input_human( &s->player2, keys['o'], keys['l'], keys['k'], s); } else { input_ai( &s->player2, &s->ball, &s->player1, s); } @@ -413,13 +434,22 @@ float get_move_y( GameState* s, unsigned char player) { return (y_len*move_x)/(x_len); } -void input_human( Player* player, bool up, bool down, bool hit) { +void input_human( Player* player, bool up, bool down, bool hit, GameState* s) { + int diff = PLAYER_MOVE_Y; + + if (fabsf(s->joystick_y) > JOYSTICK_TRESHOLD) { + diff = PLAYER_MOVE_Y*fabsf(s->joystick_y)/40.0; + if (diff > PLAYER_MOVE_Y) { + diff = PLAYER_MOVE_Y; + } + } + if( up) { - player->y -= 6; + player->y -= diff; } if( down) { - player->y += 6; + player->y += diff; } if( hit) { diff --git a/game.h b/game.h index f840a0a..373a887 100644 --- a/game.h +++ b/game.h @@ -106,6 +106,9 @@ typedef struct { unsigned char ngram[NGRAM_STEPS][NGRAM_STEPS][NGRAM_STEPS]; float ngram_prediction; sound_id play_sound; + float joystick_y; + float joystick_x; + unsigned char joystick_a; } GameState; #define PI 3.1415 @@ -153,6 +156,7 @@ typedef struct { #define IS_NEAR_X(px,bx) (fabsf(px-bx), and ", "player 2 moves with , and ", "switch court in game with , pause with

", + "" /* joystick help text comes here */, }; /* Number of lines in help_text */ #define HELP_LINES 4 /* Height (in pixels) of the help text scroller */ -#define HELP_PHASE 50 +#define HELP_PHASE 85 #ifdef WIN32 @@ -136,6 +137,24 @@ int main( int argc, char** argv) { else if (OPTION_SET("--help", "-h")) { do_help = true; } + else if (OPTION_SET("--list-joysticks", "-J")) { + SDL_Init(SDL_INIT_JOYSTICK); + joystick_list(); + return 0; + } + else if (OPTION_SET("--joystick", "-j")) { + SDL_Init(SDL_INIT_JOYSTICK); + if (OPTION_VALUE == NULL) { + fprintf(stderr, "Error: You need to specify the name of the joystick as parameter.\n"); + do_help = true; + break; + } + if (joystick_open(OPTION_VALUE)==0) { + fprintf(stderr, "Warning: Cannot find joystick \"%s\" - Ignored.\n", OPTION_VALUE); + break; + } + OPTION_VALUE_PROCESSED; + } else { fprintf(stderr, "Ignoring unknown option: %s\n", argv[i]); } @@ -143,7 +162,7 @@ int main( int argc, char** argv) { } if (do_help == true) { - fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h]\n", argv[0]); + fprintf(stderr, "Usage: %s [--fullscreen|-f] [--help|-h] [--list-joysticks|-J] [--joystick|-j] [joystick name]\n", argv[0]); return 0; } #endif @@ -168,7 +187,7 @@ int main( int argc, char** argv) { elektrons[el].new_size = 5+rand()%10; } - if( SDL_Init( SDL_INIT_VIDEO) == -1) { + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) == -1) { fprintf( stderr, "Can't init SDL: %s\n", SDL_GetError()); exit( 1); } @@ -185,6 +204,8 @@ int main( int argc, char** argv) { init_sound(); init_graphics(); + init_joystick(); + help_text[3] = get_joystick_help(); play_sample_background(SOUND_BACKGROUND); @@ -274,10 +295,10 @@ int main( int argc, char** argv) { show_image( GR_MENU, WIDTH/2-get_image_width( GR_MENU)/2, 0, 255); font_draw_string( GR_DKC2_FONT, copyright_line, (WIDTH-copyright_line_width)/2, HEIGHT-35, i, ((i/150)%ANIMATION_COUNT)); for( help=0; help