diff options
author | Eugenio Favalli <elvenprogrammer@gmail.com> | 2005-07-15 08:21:06 +0000 |
---|---|---|
committer | Eugenio Favalli <elvenprogrammer@gmail.com> | 2005-07-15 08:21:06 +0000 |
commit | 13cb522b404a97172255df898d7674fe6b16cedd (patch) | |
tree | b9cd647a456349984ea038e9b172bff24b7bd5f4 /src | |
parent | c44f66d94c9a26e2a13e59334abe18c63f925b74 (diff) | |
download | mana-13cb522b404a97172255df898d7674fe6b16cedd.tar.gz mana-13cb522b404a97172255df898d7674fe6b16cedd.tar.bz2 mana-13cb522b404a97172255df898d7674fe6b16cedd.tar.xz mana-13cb522b404a97172255df898d7674fe6b16cedd.zip |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 194 | ||||
-rw-r--r-- | src/game.h | 19 | ||||
-rw-r--r-- | src/gui/updatewindow.cpp | 4 | ||||
-rw-r--r-- | src/main.cpp | 6 |
4 files changed, 169 insertions, 54 deletions
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) @@ -58,6 +58,25 @@ extern int server_tick; extern bool displayPathToMouse; extern int startX, startY; +enum { + JOY_UP, + JOY_DOWN, + JOY_LEFT, + JOY_RIGHT, + JOY_BTN0, + JOY_BTN1, + JOY_BTN2, + JOY_BTN3, + JOY_BTN4, + JOY_BTN5, + JOY_BTN6, + JOY_BTN7, + JOY_BTN8, + JOY_BTN9, + JOY_BTN10, + JOY_BTN11 +}; + /** * Main game loop */ diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index 73efc102..42a68afa 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -158,6 +158,10 @@ int updateProgress(void *ptr, { std::string labelString(currentFile); float progress = d/t; + if (d < 0) + { + d = 0.0f; + } std::stringstream progressString(""); progressString << ((int)(progress*100)); labelString += " (" + progressString.str() + "%)"; diff --git a/src/main.cpp b/src/main.cpp index 0778bc82..0417be56 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -83,8 +83,8 @@ volatile int framesToDraw = 0; Sound sound; Music *bgm; -Configuration config; /**< Xml file configuration reader */ -Logger *logger; /**< Log object */ +Configuration config; /**< Xml file configuration reader */ +Logger *logger; /**< Log object */ ItemManager *itemDb; /**< Item database object */ /** @@ -313,7 +313,7 @@ void init_engine() new OkDialog("Sound Engine", err, &initWarningListener); logger->log("Warning: %s", err); } - + // Set frame counter when using fps limit int fpsLimit = (int)config.getValue("fpslimit", 0); if (fpsLimit) |