From 5680cffdd4afdb99d39c644a6c397e670e958848 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 4 Aug 2008 22:32:59 +0000 Subject: initial joystick support, added cl_drawkeypress variable --- src/client/Makefile.am | 8 ++--- src/client/client.cc | 2 +- src/client/hud.cc | 0 src/client/hud.h | 11 ------- src/client/input.cc | 56 +++++++++++++++++++++++++++------- src/client/input.h | 4 +++ src/client/joystick.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/client/joystick.h | 25 +++++++++++++++ src/client/keyboard.cc | 28 ++++++++++++++--- src/client/view.cc | 23 +++++++++----- 10 files changed, 200 insertions(+), 39 deletions(-) delete mode 100644 src/client/hud.cc delete mode 100644 src/client/hud.h create mode 100644 src/client/joystick.cc create mode 100644 src/client/joystick.h (limited to 'src') diff --git a/src/client/Makefile.am b/src/client/Makefile.am index 46b8e21..b3261b5 100644 --- a/src/client/Makefile.am +++ b/src/client/Makefile.am @@ -7,12 +7,12 @@ else noinst_LTLIBRARIES = libclient.la endif -libclient_la_SOURCES = action.cc chat.cc client.cc console.cc hud.cc input.cc \ - key.cc keyboard.cc targets.cc video.cc view.cc +libclient_la_SOURCES = action.cc chat.cc client.cc console.cc input.cc \ + joystick.cc key.cc keyboard.cc targets.cc video.cc view.cc libclient_la_CFLAGS = $(LIBSDL_CFLAGS) $(GL_CFLAGS) libclient_la_LDFLAGS = -avoid-version -no-undefined $(GL_LIBS) $(LIBSDL_LIBS) -noinst_HEADERS = action.h chat.h client.h console.h input.h key.h keyboard.h \ - targets.h video.h view.h +noinst_HEADERS = action.h chat.h client.h console.h input.h joystick.h key.h \ + keyboard.h targets.h video.h view.h libclient_la_LIBADD = $(top_builddir)/src/render/librender.la \ $(top_builddir)/src/core/libcore.la diff --git a/src/client/client.cc b/src/client/client.cc index 5d060aa..f67cce0 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -147,7 +147,7 @@ void Client::run() console()->clear_notify(); while (true) { - // elapsed time in microseconds + // current time in microseconds client_current_timestamp = SDL_GetTicks(); // calculate the desired frame length diff --git a/src/client/hud.cc b/src/client/hud.cc deleted file mode 100644 index e69de29..0000000 diff --git a/src/client/hud.h b/src/client/hud.h deleted file mode 100644 index 8b8c07c..0000000 --- a/src/client/hud.h +++ /dev/null @@ -1,11 +0,0 @@ -/* - client/hud.h - This file is part of the Osirion project and is distributed under - the terms and conditions of the GNU General Public License version 2 -*/ - -#ifndef __INCLUDED_HUD_H__ -#define __INCLUDED_HUD_H__ - - -#endif // __INCLUDED_HUD_H__ \ No newline at end of file diff --git a/src/client/input.cc b/src/client/input.cc index b281b8e..6778e9c 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -4,21 +4,23 @@ the terms and conditions of the GNU General Public License version 2 */ -#include "auxiliary/functions.h" +#include "SDL/SDL.h" + #include "audio/audio.h" -#include "core/core.h" -#include "client/client.h" -#include "client/input.h" +#include "auxiliary/functions.h" #include "client/chat.h" +#include "client/client.h" #include "client/console.h" +#include "client/input.h" +#include "client/joystick.h" #include "client/keyboard.h" #include "client/targets.h" #include "client/video.h" -#include "render/camera.h" +#include "core/core.h" #include "math/functions.h" -#include "render/text.h" +#include "render/camera.h" #include "render/draw.h" -#include "SDL/SDL.h" +#include "render/text.h" namespace client { @@ -36,7 +38,9 @@ namespace input Keyboard *keyboard = 0; // keyboard modifiers shift, ctrl, alt etc int keyboard_modifiers = 0; - +// last key pressed +Key *last_key = 0; +float last_key_time = 0; // local controls float local_direction = 0.0f; @@ -73,6 +77,10 @@ int mouse_position_y() { return mouse_y; } +Key *last_key_pressed() { + return last_key; +} + //--- engine functions -------------------------------------------- void func_screenshot(std::string const & args) @@ -231,6 +239,8 @@ void init() SDL_WM_GrabInput(SDL_GRAB_ON); // SDL_EnableUNICODE(1); + Joystick::init(); + input_mousecontrol = core::Cvar::get("input_mousecontrol", 1.0f, core::Cvar::Archive); input_mousecontrol->set_info("[bool] enable or disable mouse control"); @@ -285,6 +295,8 @@ void shutdown() { con_print << "^BShutting down input..." << std::endl; + Joystick::shutdown(); + core::Func::remove("list_actions"); core::Func::remove("list_keys"); core::Func::remove("list_binds"); @@ -551,6 +563,7 @@ void reset() key->key_waspressed = 0; } } + last_key = 0; } void frame(float seconds) @@ -573,11 +586,20 @@ void frame(float seconds) render::reset(); } + /* -- detect joystick changes --------------------- */ + Joystick::frame(); + + /* -- poll SDL keyboard events -------------------- */ SDL_Event event; Key *key = 0; bool pressed = false; + if (last_key_time + 1.0f < client()->time()) { + last_key = 0; + last_key_time = 0; + } + memset(&event, 0, sizeof(SDL_Event)); while (SDL_PollEvent(&event)) { pressed = false; @@ -591,7 +613,6 @@ void frame(float seconds) break; case SDL_MOUSEBUTTONDOWN: - // override for gui mouse down key = keyboard->press(512 + event.button.button); pressed = true; break; @@ -601,6 +622,16 @@ void frame(float seconds) pressed = false; break; + case SDL_JOYBUTTONDOWN: + key = keyboard->press(564 + event.jbutton.button); + pressed = true; + break; + + case SDL_JOYBUTTONUP: + key = keyboard->release(564 + event.jbutton.button); + pressed = false; + break; + case SDL_KEYDOWN: keyboard_modifiers = event.key.keysym.mod; key = keyboard->press(event.key.keysym.sym); @@ -620,10 +651,13 @@ void frame(float seconds) } if (key) { - if (pressed) + if (pressed) { key_pressed(key); - else + last_key = key; // remember the last key that was pressed + last_key_time = client()->time(); + } else { key_released(key); + } } } diff --git a/src/client/input.h b/src/client/input.h index 78b813f..f0c4c9e 100644 --- a/src/client/input.h +++ b/src/client/input.h @@ -7,6 +7,7 @@ #ifndef __INCLUDED_cLIENT_INPUT_H__ #define __INCLUDED_cLIENT_INPUT_H__ +#include "client/key.h" #include "core/cvar.h" namespace client @@ -33,6 +34,9 @@ int mouse_position_x(); /// current mouse y position int mouse_position_y(); +/// the last key that was pressed +Key *last_key_pressed(); + extern bool mouse_deadzone; extern bool mouse_control; diff --git a/src/client/joystick.cc b/src/client/joystick.cc new file mode 100644 index 0000000..cf2270f --- /dev/null +++ b/src/client/joystick.cc @@ -0,0 +1,82 @@ +/* + client/joystick.cc + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#include "SDL/SDL.h" + +#include "auxiliary/functions.h" +#include "client/joystick.h" +#include "core/cvar.h" +#include "sys/sys.h" + +namespace client +{ + +core::Cvar *input_joystick = 0; + +int current_joystick_number = 0; +SDL_Joystick *current_joystick = 0; + +void Joystick::init() +{ + SDL_InitSubSystem(SDL_INIT_JOYSTICK); + + int nbjoysticks = SDL_NumJoysticks(); + if (nbjoysticks) { + for (int i=0; i < nbjoysticks; i++) { + SDL_Joystick *joystick = SDL_JoystickOpen(i); + if (joystick) { + con_print << " joystick " << i+1 << ": " << + SDL_JoystickName(i) << " " << + SDL_JoystickNumAxes(joystick) << " axes " << + SDL_JoystickNumButtons(joystick) << " buttons " << std::endl; + + SDL_JoystickClose(joystick); + } + } + } + + input_joystick = core::Cvar::get("input_joystick", 0.0f, core::Cvar::Archive); + input_joystick->set_info("[int] set joystick number to use"); + + current_joystick_number = 0; + SDL_JoystickEventState(SDL_IGNORE); +} + +void Joystick::shutdown() +{ + if (current_joystick) { + SDL_JoystickEventState(SDL_IGNORE); + SDL_JoystickClose(current_joystick); + current_joystick = 0; + } + + SDL_QuitSubSystem(SDL_INIT_JOYSTICK); +} + +void Joystick::frame() +{ + if (current_joystick_number != (int) input_joystick->value()) + { + if (current_joystick) { + SDL_JoystickEventState(SDL_IGNORE); + SDL_JoystickClose(current_joystick); + current_joystick = 0; + } + + current_joystick_number = (int) input_joystick->value(); + + if (current_joystick_number) { + current_joystick = SDL_JoystickOpen(current_joystick_number -1); + } + + if (current_joystick) { + con_debug << " using joystick " << SDL_JoystickName(current_joystick_number -1) << std::endl; + SDL_JoystickEventState(SDL_ENABLE); + } + } +} + +} diff --git a/src/client/joystick.h b/src/client/joystick.h new file mode 100644 index 0000000..e21a60c --- /dev/null +++ b/src/client/joystick.h @@ -0,0 +1,25 @@ +/* + client/joystick.h + This file is part of the Osirion project and is distributed under + the terms and conditions of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_CLIENT_JOYSTICK_H__ +#define __INCLUDED_CLIENT_JOYSTICK_H__ + +namespace client +{ + +class Joystick +{ +public: + static void init(); + + static void shutdown(); + + static void frame(); +}; + +} + +#endif // __INCLUDED_CLIENT_JOYSTICK_H__ diff --git a/src/client/keyboard.cc b/src/client/keyboard.cc index 5416580..57d99b3 100644 --- a/src/client/keyboard.cc +++ b/src/client/keyboard.cc @@ -205,13 +205,31 @@ Keyboard::Keyboard() add_key("power", SDLK_POWER); add_key("euro", SDLK_EURO); - // mouse key aliases + // mouse button aliases add_key("mouse1", 512 + SDL_BUTTON_LEFT, 0, "+control"); add_key("mouse2", 512 + SDL_BUTTON_RIGHT); add_key("mouse3", 512 + SDL_BUTTON_MIDDLE); add_key("mouse4", 512 + SDL_BUTTON_WHEELUP, 0, "+thrustup"); add_key("mouse5", 512 + SDL_BUTTON_WHEELDOWN, 0, "+thrustdown"); + + // joystick button aliases + add_key("joy0", 564); + add_key("joy1", 565); + add_key("joy2", 566); + add_key("joy3", 567); + add_key("joy4", 568); + add_key("joy5", 569); + add_key("joy6", 570); + add_key("joy7", 571); + add_key("joy8", 572); + add_key("joy9", 573); + add_key("joy10", 574); + add_key("joy11", 575); + add_key("joy12", 576); + add_key("joy13", 577); + add_key("joy14", 578); + add_key("joy15", 579); } Keyboard::~Keyboard() @@ -375,13 +393,13 @@ void Keyboard::bind(std::string const &name, const std::string str) key->assign(modifier, str.c_str()); if (modifier == Key::None) { - con_print << " " << aux::pad_right(key->name(), 6) << " " << key->bind(Key::None) << std::endl; + con_debug << " " << aux::pad_right(key->name(), 6) << " " << key->bind(Key::None) << std::endl; } else if (modifier == Key::Shift) { - con_print << " shift+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Shift) << std::endl; + con_debug << " shift+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Shift) << std::endl; } else if (modifier == Key::Ctrl) { - con_print << " ctrl+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Ctrl) << std::endl; + con_debug << " ctrl+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Ctrl) << std::endl; } else if (modifier == Key::Alt) { - con_print << " alt+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Alt) << std::endl; + con_debug << " alt+" << aux::pad_right(key->name(), 6) << " " << key->bind(Key::Alt) << std::endl; } } else { con_print << "Key '" << name << "' not found." << std::endl; diff --git a/src/client/view.cc b/src/client/view.cc index 391da35..862e394 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -32,6 +32,7 @@ namespace client core::Cvar *draw_ui = 0; core::Cvar *draw_stats = 0; core::Cvar *draw_location = 0; +core::Cvar *draw_keypress = 0; core::Cvar *ui_pointercolor = 0; core::Cvar *ui_pointerhovercolor =0; @@ -58,6 +59,9 @@ void init() draw_ui = core::Cvar::get("draw_ui", "1", core::Cvar::Archive); draw_ui->set_info("[bool] draw the user interface"); + draw_keypress = core::Cvar::get("draw_keypress", "0", core::Cvar::Archive); + draw_keypress->set_info("[bool] draw keypress key names"); + ui_pointercolor = core::Cvar::get("ui_pointercolor", "0 .5 0", core::Cvar::Archive); ui_pointercolor->set_info("[r g b] mouse pointer color"); @@ -177,14 +181,11 @@ void draw_status() Text::draw(4, video::height - Text::fontheight()*2 -4, location); } - if (core::localplayer()->zone()) { - Text::draw(video::width - 4-Text::fontwidth()*24, video::height - Text::fontheight()*3 -4, core::localplayer()->zone()->name()); - } - + float y = 1.0f; core::Entity *entity = targets::current(); if (entity) { std::stringstream target; - target << "^B" << entity->name() << "\n"; + target << "^B" << entity->name() << "\n"; target << "^Ndist ^B"; math::Vector3f v = entity->state()->location() - core::localcontrol()->state()->location(); @@ -194,10 +195,17 @@ void draw_status() else target << " --"; - Text::draw(video::width - 4-Text::fontwidth()*24, video::height - Text::fontheight()*2 -4, target); + Text::draw(video::width - 4-Text::fontwidth()*32, video::height - Text::fontheight()*2 -4, target); + y = 3.0f; } + + Text::draw(video::width-4-Text::fontwidth()*32, video::height-Text::fontheight()*y-4, core::localcontrol()->zone()->name()); } + if (draw_keypress->value() && input::last_key_pressed()) { + Text::setcolor('F'); //set fancy color + Text::draw(video::width-4-Text::fontwidth()*6, video::height-Text::fontheight()-4, input::last_key_pressed()->name()); + } } void draw_cursor() @@ -415,7 +423,8 @@ void frame(float seconds) // draw text elements if (draw_ui->value()) { - Text::setfont("bitmaps/fonts/gui", 16, 24); + //Text::setfont("bitmaps/fonts/gui", 16, 24); + Text::setfont("bitmaps/fonts/gui", 12, 18); // draw the player status draw_status(); -- cgit v1.2.3