feuerkraft-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Feuerkraft-CVS] rev 306 - trunk/src/input


From: Ingo Ruhnke
Subject: [Feuerkraft-CVS] rev 306 - trunk/src/input
Date: Sun, 07 Dec 2003 20:54:13 +0100

Author: grumbel
Date: 2003-12-07 20:54:13 +0100 (Sun, 07 Dec 2003)
New Revision: 306

Added:
   trunk/src/input/input_manager_keyboard.cxx
   trunk/src/input/input_manager_keyboard.hxx
Modified:
   trunk/src/input/Makefile.am
   trunk/src/input/input_manager.cxx
   trunk/src/input/input_manager_clanlib.cxx
   trunk/src/input/input_manager_clanlib.hxx
Log:
- added keyboard support

Modified: trunk/src/input/Makefile.am
===================================================================
--- trunk/src/input/Makefile.am 2003-12-07 19:31:19 UTC (rev 305)
+++ trunk/src/input/Makefile.am 2003-12-07 19:54:13 UTC (rev 306)
@@ -8,6 +8,8 @@
   input_manager.cxx \
   input_manager_impl.hxx \
   input_manager_clanlib.hxx \
-  input_manager_clanlib.cxx
+  input_manager_clanlib.cxx \
+  input_manager_keyboard.hxx \
+  input_manager_keyboard.cxx
 
-# EOF #
\ No newline at end of file
+# EOF #

Modified: trunk/src/input/input_manager.cxx
===================================================================
--- trunk/src/input/input_manager.cxx   2003-12-07 19:31:19 UTC (rev 305)
+++ trunk/src/input/input_manager.cxx   2003-12-07 19:54:13 UTC (rev 306)
@@ -18,7 +18,10 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <assert.h>
+#include <iostream>
+#include <ClanLib/Display/joystick.h>
 #include "input_manager_clanlib.hxx"
+#include "input_manager_keyboard.hxx"
 #include "input_manager_impl.hxx"
 #include "input_manager.hxx"
 
@@ -28,9 +31,19 @@
 InputManager::init(InputManagerImpl* arg_impl)
 {
   if (arg_impl)
-    impl = arg_impl;
-  else
-    impl = new InputManagerClanLib();
+    { 
+      impl = arg_impl;
+    }
+  else if (CL_Joystick::get_device_count() > 0)
+    {
+      std::cout << "InputManager: Using joystick" << std::endl;
+      impl = new InputManagerClanLib();
+    }
+  else 
+    {
+      std::cout << "InputManager: Using keyboard" << std::endl;
+      impl = new InputManagerKeyboard();
+    }
 }
 
 void 

Modified: trunk/src/input/input_manager_clanlib.cxx
===================================================================
--- trunk/src/input/input_manager_clanlib.cxx   2003-12-07 19:31:19 UTC (rev 
305)
+++ trunk/src/input/input_manager_clanlib.cxx   2003-12-07 19:54:13 UTC (rev 
306)
@@ -18,6 +18,7 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <iostream>
+#include <stdexcept>
 #include <ClanLib/Display/keyboard.h>
 #include <ClanLib/Display/joystick.h>
 #include <ClanLib/Display/display_iostream.h>
@@ -27,10 +28,20 @@
 
 InputManagerClanLib::InputManagerClanLib()
 {
-  slots.connect(CL_Keyboard::sig_key_down(), this, 
&InputManagerClanLib::on_key_event);
-  slots.connect(CL_Keyboard::sig_key_up(),   this, 
&InputManagerClanLib::on_key_event);
-
-  CL_InputDevice dev = CL_Joystick::get_device(1);
+  if (CL_Joystick::get_device_count() == 1)
+    {
+      dev = CL_Joystick::get_device(0);
+    }
+  else if (CL_Joystick::get_device_count() >= 2)
+    {
+      // FIXME: Hack for localhost...
+      dev = CL_Joystick::get_device(1);
+    }
+  else
+    {
+      throw std::runtime_error("Feuerkraft: No joystick found");
+    }
+  
   slots.connect(dev.sig_axis_move(),this, &InputManagerClanLib::on_axis_move);
   slots.connect(dev.sig_key_down(), this, 
&InputManagerClanLib::on_button_down);
   slots.connect(dev.sig_key_up(),   this, &InputManagerClanLib::on_button_up);
@@ -75,54 +86,6 @@
 }
 
 void
-InputManagerClanLib::on_key_event(const CL_InputEvent& event)
-{
-  switch (event.type)
-    {
-    case CL_InputEvent::released:
-      //std::cout << "Release: " << event.id << std::endl;
-      break;
-
-    case CL_InputEvent::pressed:
-      //std::cout << "Press: " << event.id << std::endl;
-      switch (event.id)
-        {
-          // Steering
-        case CL_KEY_DOWN:
-          add_axis_event(ACCELERATE_AXIS, 1.0f);
-          break;
-        case CL_KEY_UP:
-          add_axis_event(ACCELERATE_AXIS, -1.0f);
-          break;
-        case CL_KEY_RIGHT:
-          add_axis_event(ORIENTATION_AXIS, 1.0f);
-          break;
-        case CL_KEY_LEFT:
-          add_axis_event(ORIENTATION_AXIS, -1.0f);
-          break;
-
-        case CL_KEY_O:
-          add_axis_event(STRAFE_AXIS, -1.0f);
-          break;
-        case CL_KEY_U:
-          add_axis_event(STRAFE_AXIS, 1.0f);
-          break;
-
-        case CL_KEY_E:
-          add_button_event(PRIMARY_FIRE_BUTTON, true);
-          break;
-        default:
-          std::cout << "Unknown keypress: " << event << std::endl;
-          break;
-        }
-      break;
-
-    default:
-      break;
-    }
-}
-
-void
 InputManagerClanLib::add_axis_event(AxisName name, float pos)
 {
   InputEvent event;
@@ -145,51 +108,8 @@
 void
 InputManagerClanLib::update(float delta)
 {
-#if 0
-  if (CL_Keyboard::get_keycode(CL_KEY_LEFT))
-    {
-      if (CL_Keyboard::get_keycode(CL_KEY_RIGHT))
-        controller.set_axis_state(ORIENTATION_AXIS, 0);
-      else
-        controller.set_axis_state(ORIENTATION_AXIS, -1.0);
-    }
-  else if (CL_Keyboard::get_keycode(CL_KEY_RIGHT))
-    {
-      controller.set_axis_state(ORIENTATION_AXIS, 1.0);
-    }
-  else
-    {
-      controller.set_axis_state(ORIENTATION_AXIS, 0);
-    }
-
-  if (CL_Keyboard::get_keycode(CL_KEY_UP))
-    {
-      if (CL_Keyboard::get_keycode(CL_KEY_DOWN))
-        controller.set_axis_state(ACCELERATE_AXIS, 0);
-      else
-        controller.set_axis_state(ACCELERATE_AXIS, -1.0);
-    }
-  else if (CL_Keyboard::get_keycode(CL_KEY_DOWN))
-    {
-      controller.set_axis_state(ACCELERATE_AXIS, 1.0);
-    }
-  else
-    {
-      controller.set_axis_state(ACCELERATE_AXIS, 0);
-    }
-
-  if (CL_Keyboard::get_keycode(CL_KEY_O) && 
!CL_Keyboard::get_keycode(CL_KEY_U))
-    controller.set_axis_state(STRAFE_AXIS, -1.0f);
-  else if (CL_Keyboard::get_keycode(CL_KEY_U) && 
!CL_Keyboard::get_keycode(CL_KEY_O))
-    controller.set_axis_state(STRAFE_AXIS, 1.0f);
-  else
-    controller.set_axis_state(STRAFE_AXIS, 0.0f);
-
-  controller.set_button_state(PRIMARY_FIRE_BUTTON, 
-                              CL_Keyboard::get_keycode(CL_KEY_E));
-#endif 
-
   CL_InputDevice dev = CL_Joystick::get_device(1);
+
   controller.set_axis_state(ORIENTATION_AXIS, dev.get_axis(0));
   controller.set_axis_state(ACCELERATE_AXIS,  dev.get_axis(1));
   controller.set_axis_state(STRAFE_AXIS,      dev.get_axis(2));

Modified: trunk/src/input/input_manager_clanlib.hxx
===================================================================
--- trunk/src/input/input_manager_clanlib.hxx   2003-12-07 19:31:19 UTC (rev 
305)
+++ trunk/src/input/input_manager_clanlib.hxx   2003-12-07 19:54:13 UTC (rev 
306)
@@ -22,6 +22,7 @@
 
 #include <ClanLib/Signals/slot.h>
 #include <ClanLib/Signals/slot_container.h>
+#include <ClanLib/Display/input_device.h>
 #include "controller.hxx"
 #include "input_manager_impl.hxx"
 
@@ -31,18 +32,20 @@
 class InputManagerClanLib : public InputManagerImpl
 {
 private:
+  CL_InputDevice dev;
+
   Controller controller;
   InputEventLst events;
 
   CL_SlotContainer slots;
   
-  void on_key_event(const CL_InputEvent& event);
   void on_axis_move(const CL_InputEvent& event);
   void on_button_down(const CL_InputEvent& event);
   void on_button_up(const CL_InputEvent& event);
 
   void add_axis_event(AxisName name, float pos);
   void add_button_event(ButtonName name, bool down);
+
 public:
   InputManagerClanLib();
   virtual ~InputManagerClanLib();

Added: trunk/src/input/input_manager_keyboard.cxx
===================================================================
--- trunk/src/input/input_manager_keyboard.cxx  2003-12-07 19:31:19 UTC (rev 
305)
+++ trunk/src/input/input_manager_keyboard.cxx  2003-12-07 19:54:13 UTC (rev 
306)
@@ -0,0 +1,170 @@
+//  $Id: input_manager_clanlib.cxx,v 1.10 2003/10/31 23:24:41 grumbel Exp $
+//
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#include <iostream>
+#include <ClanLib/Display/keyboard.h>
+#include <ClanLib/Display/joystick.h>
+#include <ClanLib/Display/display_iostream.h>
+#include <ClanLib/Display/keys.h>
+#include <ClanLib/Display/input_event.h>
+#include "input_manager_keyboard.hxx"
+
+InputManagerKeyboard::InputManagerKeyboard()
+{
+  slots.connect(CL_Keyboard::sig_key_down(), this, 
&InputManagerKeyboard::on_key_event);
+  slots.connect(CL_Keyboard::sig_key_up(),   this, 
&InputManagerKeyboard::on_key_event);
+}
+
+InputManagerKeyboard::~InputManagerKeyboard()
+{
+}
+
+void
+InputManagerKeyboard::on_key_event(const CL_InputEvent& event)
+{
+  switch (event.type)
+    {
+    case CL_InputEvent::released:
+      break;
+
+    case CL_InputEvent::pressed:
+      switch (event.id)
+        {
+          // Steering
+        case CL_KEY_DOWN:
+          add_axis_event(ACCELERATE_AXIS, 1.0f);
+          break;
+        case CL_KEY_UP:
+          add_axis_event(ACCELERATE_AXIS, -1.0f);
+          break;
+        case CL_KEY_RIGHT:
+          add_axis_event(ORIENTATION_AXIS, 1.0f);
+          break;
+        case CL_KEY_LEFT:
+          add_axis_event(ORIENTATION_AXIS, -1.0f);
+          break;
+
+        case CL_KEY_O:
+          add_axis_event(STRAFE_AXIS, -1.0f);
+          break;
+        case CL_KEY_U:
+          add_axis_event(STRAFE_AXIS, 1.0f);
+          break;
+
+        case CL_KEY_E:
+          add_button_event(PRIMARY_FIRE_BUTTON, true);
+          break;
+        default:
+          std::cout << "Unknown keypress: " << event << std::endl;
+          break;
+        }
+      break;
+
+    default:
+      break;
+    }
+}
+
+void
+InputManagerKeyboard::add_axis_event(AxisName name, float pos)
+{
+  InputEvent event;
+  event.type = AXIS_EVENT;
+  event.axis.name = name;
+  event.axis.pos  = pos;
+  events.push_back(event);
+}
+
+void
+InputManagerKeyboard::add_button_event(ButtonName name, bool down)
+{
+  InputEvent event;
+  event.type = BUTTON_EVENT;
+  event.button.name = name;
+  event.button.down = down;
+  events.push_back(event);
+}
+
+void
+InputManagerKeyboard::update(float delta)
+{
+  if (CL_Keyboard::get_keycode(CL_KEY_LEFT))
+    {
+      if (CL_Keyboard::get_keycode(CL_KEY_RIGHT))
+        controller.set_axis_state(ORIENTATION_AXIS, 0);
+      else
+        controller.set_axis_state(ORIENTATION_AXIS, -1.0);
+    }
+  else if (CL_Keyboard::get_keycode(CL_KEY_RIGHT))
+    {
+      controller.set_axis_state(ORIENTATION_AXIS, 1.0);
+    }
+  else
+    {
+      controller.set_axis_state(ORIENTATION_AXIS, 0);
+    }
+
+  if (CL_Keyboard::get_keycode(CL_KEY_UP))
+    {
+      if (CL_Keyboard::get_keycode(CL_KEY_DOWN))
+        controller.set_axis_state(ACCELERATE_AXIS, 0);
+      else
+        controller.set_axis_state(ACCELERATE_AXIS, -1.0);
+    }
+  else if (CL_Keyboard::get_keycode(CL_KEY_DOWN))
+    {
+      controller.set_axis_state(ACCELERATE_AXIS, 1.0);
+    }
+  else
+    {
+      controller.set_axis_state(ACCELERATE_AXIS, 0);
+    }
+
+  if (CL_Keyboard::get_keycode(CL_KEY_O) && 
!CL_Keyboard::get_keycode(CL_KEY_U))
+    controller.set_axis_state(STRAFE_AXIS, -1.0f);
+  else if (CL_Keyboard::get_keycode(CL_KEY_U) && 
!CL_Keyboard::get_keycode(CL_KEY_O))
+    controller.set_axis_state(STRAFE_AXIS, 1.0f);
+  else
+    controller.set_axis_state(STRAFE_AXIS, 0.0f);
+
+  controller.set_button_state(PRIMARY_FIRE_BUTTON, 
+                              CL_Keyboard::get_keycode(CL_KEY_E));
+}
+
+InputEventLst
+InputManagerKeyboard::get_events()
+{
+  InputEventLst old_events = events;
+  return old_events;
+}
+
+Controller
+InputManagerKeyboard::get_controller()
+{
+  controller.events = events;
+  return controller;
+}
+
+void
+InputManagerKeyboard::clear()
+{
+  events.clear();
+}
+
+/* EOF */

Added: trunk/src/input/input_manager_keyboard.hxx
===================================================================
--- trunk/src/input/input_manager_keyboard.hxx  2003-12-07 19:31:19 UTC (rev 
305)
+++ trunk/src/input/input_manager_keyboard.hxx  2003-12-07 19:54:13 UTC (rev 
306)
@@ -0,0 +1,59 @@
+//  $Id: input_manager_clanlib.hxx,v 1.5 2003/10/31 23:24:41 grumbel Exp $
+// 
+//  Pingus - A free Lemmings clone
+//  Copyright (C) 2002 Ingo Ruhnke <address@hidden>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+#ifndef HEADER_INPUT_MANAGER_KEYBOARD_HXX
+#define HEADER_INPUT_MANAGER_KEYBOARD_HXX
+
+#include <ClanLib/Signals/slot.h>
+#include <ClanLib/Signals/slot_container.h>
+#include "controller.hxx"
+#include "input_manager_impl.hxx"
+
+class CL_InputEvent;
+
+/** */
+class InputManagerKeyboard : public InputManagerImpl
+{
+private:
+  Controller controller;
+  InputEventLst events;
+
+  CL_SlotContainer slots;
+  
+  void on_key_event(const CL_InputEvent& event);
+
+  void add_axis_event(AxisName name, float pos);
+  void add_button_event(ButtonName name, bool down);
+
+public:
+  InputManagerKeyboard();
+  virtual ~InputManagerKeyboard();
+
+  void update(float delta);
+  InputEventLst get_events();
+  Controller get_controller();
+  void clear();
+private:
+  InputManagerKeyboard (const InputManagerKeyboard&);
+  InputManagerKeyboard& operator= (const InputManagerKeyboard&);
+};
+
+#endif
+
+/* EOF */





reply via email to

[Prev in Thread] Current Thread [Next in Thread]