From 13cb522b404a97172255df898d7674fe6b16cedd Mon Sep 17 00:00:00 2001 From: Eugenio Favalli Date: Fri, 15 Jul 2005 08:21:06 +0000 Subject: Enabling support for joypads: - Only <=6 buttons are supported - Only the first jopad is used (if you have more than one) - Axes need to be tuned - You can only move and attack with joypad Small fix in the updating system --- src/game.cpp | 194 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 143 insertions(+), 51 deletions(-) (limited to 'src/game.cpp') diff --git a/src/game.cpp b/src/game.cpp index 178bfb87..3fa3977f 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -71,6 +71,7 @@ int startX = 0, startY = 0; int gameTime = 0; Being *autoTarget = NULL; Engine *engine = NULL; +SDL_Joystick *joypad; /**< Joypad object */ OkDialog *deathNotice = NULL; ConfirmDialog *exitConfirm = NULL; @@ -137,48 +138,6 @@ int get_elapsed_time(int start_time) } } -void do_init(); - -void game() -{ - engine = new Engine(); - do_init(); - - gameTime = tick_time; - - while (state != EXIT) - { - // Handle all necessary game logic - while (get_elapsed_time(gameTime) > 0) - { - do_input(); - engine->logic(); - gui->logic(); - gameTime++; - } - - gameTime = tick_time; - - // Update the screen when application is active, delay otherwise - if (SDL_GetAppState() & SDL_APPACTIVE) - { - engine->draw(); - graphics->updateScreen(); - } - else - { - SDL_Delay(10); - } - - // Handle network stuff and flush it - do_parse(); - flush(); - } - - delete engine; - close_session(); -} - void do_init() { std::string path(map_path); @@ -187,7 +146,7 @@ void do_init() // Try .tmx.gz map file pathDir.insert(pathDir.size(), ".tmx.gz"); Map *tiledMap = MapReader::readMap(pathDir); - + if (!tiledMap) { logger->error("Could not find map file!"); @@ -220,10 +179,78 @@ void do_init() add_node(player_node); remove("packet.list"); + + // Initialize joypad + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + //SDL_JoystickEventState(SDL_ENABLE); + int num_joy = SDL_NumJoysticks(); + logger->log("%i joysticks/gamepads found", num_joy); + for (int i = 0; i < num_joy; i++) + logger->log("- %s", SDL_JoystickName(i)); + // TODO: The user should be able to choose which one to use + // Open the first device + if (num_joy > 0) + { + joypad = SDL_JoystickOpen(0); + if (joypad == NULL) + { + logger->log("Couldn't open joystick: %s", SDL_GetError()); + } + else { + logger->log("Axes: %i ", SDL_JoystickNumAxes(joypad)); + logger->log("Balls: %i", SDL_JoystickNumBalls(joypad)); + logger->log("Hats: %i", SDL_JoystickNumHats(joypad)); + logger->log("Buttons: %i", SDL_JoystickNumButtons(joypad)); + } + } +} + +void game() +{ + engine = new Engine(); + do_init(); + + gameTime = tick_time; + + while (state != EXIT) + { + // Handle all necessary game logic + while (get_elapsed_time(gameTime) > 0) + { + do_input(); + engine->logic(); + gui->logic(); + gameTime++; + } + + gameTime = tick_time; + + // Update the screen when application is active, delay otherwise + if (SDL_GetAppState() & SDL_APPACTIVE) + { + engine->draw(); + graphics->updateScreen(); + } + else + { + SDL_Delay(10); + } + + // Handle network stuff and flush it + do_parse(); + flush(); + } + + delete engine; + close_session(); } void do_exit() { + if (joypad != NULL) + { + SDL_JoystickClose(joypad); + } } void do_input() @@ -231,6 +258,40 @@ void do_input() // Get the state of the keyboard keys Uint8* keys; keys = SDL_GetKeyState(NULL); + + // Get the state of the joypad buttons + // TODO: Only 6- buttons joypads are allowed + bool joy[10]; + for (int i=0; i<10; i++) + { + joy[i] = false; + } + if (joypad != NULL) + { + if (SDL_JoystickGetAxis(joypad, 0) > 100) + { + joy[JOY_RIGHT] = true; + } + if (SDL_JoystickGetAxis(joypad, 0) < -100) + { + joy[JOY_LEFT] = true; + } + if (SDL_JoystickGetAxis(joypad, 1) < -100) + { + joy[JOY_UP] = true; + } + if (SDL_JoystickGetAxis(joypad, 1) > 100) + { + joy[JOY_DOWN] = true; + } + for (int i=0; i<6; i++) + { + if (SDL_JoystickGetButton(joypad, i) == 1) + { + joy[JOY_BTN0 + i] = true; + } + } + } // Events SDL_Event event; @@ -371,7 +432,7 @@ void do_input() // Picking up items on the floor else if ((keysym.sym == SDLK_g || keysym.sym == SDLK_z) && - !chatWindow->isFocused()) + !chatWindow->isFocused()) { unsigned short x = player_node->x; unsigned short y = player_node->y; @@ -527,7 +588,38 @@ void do_input() popupMenu->showPopup(mx, my); } } - + /* + // Joystick events + else if (event.type == SDL_JOYBUTTONDOWN) + { + // Attacking monsters + if (player_node->action == STAND) + { + if (event.jbutton.which == 0 && event.jbutton.button == 1) + { + Being *monster = attack(player_node->x, + player_node->y, + player_node->direction); + } + } + logger->log("Joystick button %d", event.jbutton.which); + } + else if (event.type == SDL_JOYAXISMOTION) + { + if (event.jaxis.axis == 0) { + if (event.jaxis.value > 100) + { + walk(player_node->x + 1, 0, EAST); + player_node->setDestination(player_node->x + 1, player_node->y); + } + else if (event.jaxis.value < -100){ + walk(player_node->x - 1, 0, WEST); + player_node->setDestination(player_node->x - 1, player_node->y); + } + } + logger->log("Axis %d moved of %d", event.jaxis.axis, event.jaxis.value); + }*/ + // Quit event else if (event.type == SDL_QUIT) { @@ -540,7 +632,7 @@ void do_input() } } // End while - + // Moving player around if ((player_node->action != DEAD) && (current_npc == 0) && !chatWindow->isFocused()) @@ -552,25 +644,25 @@ void do_input() int Direction = DIR_NONE; // Translate pressed keys to movement and direction - if (keys[SDLK_UP] || keys[SDLK_KP8]) + if (keys[SDLK_UP] || keys[SDLK_KP8] || joy[JOY_UP]) { yDirection = -1; if (player_node->action != WALK) Direction = NORTH; } - if (keys[SDLK_DOWN] || keys[SDLK_KP2]) + if (keys[SDLK_DOWN] || keys[SDLK_KP2] || joy[JOY_DOWN]) { yDirection = 1; if (player_node->action != WALK) Direction = SOUTH; } - if (keys[SDLK_LEFT] || keys[SDLK_KP4]) + if (keys[SDLK_LEFT] || keys[SDLK_KP4] || joy[JOY_LEFT]) { xDirection = -1; if (player_node->action != WALK) Direction = WEST; } - if (keys[SDLK_RIGHT] || keys[SDLK_KP6]) + if (keys[SDLK_RIGHT] || keys[SDLK_KP6] || joy[JOY_RIGHT]) { xDirection = 1; if (player_node->action != WALK) @@ -642,7 +734,7 @@ void do_input() // Attacking monsters if (player_node->action == STAND) { - if (keys[SDLK_LCTRL]) + if (keys[SDLK_LCTRL] || joy[JOY_BTN0]) { Being *monster = attack(x, y, player_node->direction); if (monster == NULL && autoTarget != NULL) -- cgit v1.2.3-60-g2f50