From: <axl...@us...> - 2009-05-17 21:14:20
|
Revision: 268 http://hgengine.svn.sourceforge.net/hgengine/?rev=268&view=rev Author: axlecrusher Date: 2009-05-17 21:14:12 +0000 (Sun, 17 May 2009) Log Message: ----------- Add keyboard input. Move camera around with keyboard input Modified Paths: -------------- Mercury2/src/Camera.cpp Mercury2/src/Camera.h Mercury2/src/MercuryInput.cpp Mercury2/src/MercuryInput.h Mercury2/src/X11Window.cpp Mercury2/src/X11Window.h Modified: Mercury2/src/Camera.cpp =================================================================== --- Mercury2/src/Camera.cpp 2009-05-17 19:16:37 UTC (rev 267) +++ Mercury2/src/Camera.cpp 2009-05-17 21:14:12 UTC (rev 268) @@ -36,6 +36,20 @@ } } +void CameraNode::Update(float dTime) +{ + MercuryVector p = GetPosition(); + + if ( KeyboardInput::IsKeyDown(25) ) p[2] -= dTime*2; + if ( KeyboardInput::IsKeyDown(38) ) p[0] -= dTime*2; + if ( KeyboardInput::IsKeyDown(39) ) p[2] += dTime*2; + if ( KeyboardInput::IsKeyDown(40) ) p[0] += dTime*2; + + SetPosition( p ); + + TransformNode::Update( dTime ); +} + /**************************************************************************** * Copyright (C) 2009 by Joshua Allen * * * Modified: Mercury2/src/Camera.h =================================================================== --- Mercury2/src/Camera.h 2009-05-17 19:16:37 UTC (rev 267) +++ Mercury2/src/Camera.h 2009-05-17 21:14:12 UTC (rev 268) @@ -9,6 +9,7 @@ CameraNode(); virtual void ComputeMatrix(); virtual void HandleMessage(const MString& message, const MessageData* data); + virtual void Update(float dTime); GENRTTI(CameraNode); private: Modified: Mercury2/src/MercuryInput.cpp =================================================================== --- Mercury2/src/MercuryInput.cpp 2009-05-17 19:16:37 UTC (rev 267) +++ Mercury2/src/MercuryInput.cpp 2009-05-17 21:14:12 UTC (rev 268) @@ -15,6 +15,46 @@ POST_MESSAGE( INPUTEVENT_MOUSE, mi, 0 ); } +KeyboardInput::KeyboardInput() + :key(0), isDown(false) +{ + //init initial keymap + if (m_keyStates[0] < 1) + { + m_keyStates[0] = 1; + for (uint16_t i = 1; i < 512; ++i) + m_keyStates[i] = 0; + } +} + +void KeyboardInput::ProcessKeyInput(uint16_t key, bool isDown) +{ + KeyboardInput* ki = new KeyboardInput(); + ki->isDown = isDown; + ki->key = key; + + if (isDown) + { + m_keyStates[key] |= (1 << 1); + printf("press %d\n", key); + } + else + { + m_keyStates[key] &= ~(1 << 1); + printf("release %d\n", key); + } + + POST_MESSAGE( INPUTEVENT_KEYBOARD, ki, 0 ); +} + +bool KeyboardInput::IsKeyDown(uint16_t key) +{ + return (m_keyStates[key] & (1 << 1)) > 0; +} + +uint8_t KeyboardInput::m_keyStates[512]; + + /**************************************************************************** * Copyright (C) 2009 by Joshua Allen * * * Modified: Mercury2/src/MercuryInput.h =================================================================== --- Mercury2/src/MercuryInput.h 2009-05-17 19:16:37 UTC (rev 267) +++ Mercury2/src/MercuryInput.h 2009-05-17 21:14:12 UTC (rev 268) @@ -4,6 +4,7 @@ #include <MessageHandler.h> const MString INPUTEVENT_MOUSE = "MouseInputEvent"; +const MString INPUTEVENT_KEYBOARD = "KeyboardInputEvent"; class MouseInput : public MessageData { @@ -19,6 +20,20 @@ uint8_t buttonMasks; }; +class KeyboardInput : public MessageData +{ + public: + static void ProcessKeyInput(uint16_t key, bool isDown); + static bool IsKeyDown(uint16_t key); + + KeyboardInput(); + + int16_t key; + bool isDown; + private: + static uint8_t m_keyStates[512]; +}; + #endif /**************************************************************************** Modified: Mercury2/src/X11Window.cpp =================================================================== --- Mercury2/src/X11Window.cpp 2009-05-17 19:16:37 UTC (rev 267) +++ Mercury2/src/X11Window.cpp 2009-05-17 21:14:12 UTC (rev 268) @@ -110,7 +110,9 @@ case ClientMessage: { if ( unsigned(event.xclient.data.l[0]) == m_wmDeleteMessage ) + { XDestroyWindow(m_display,m_window); + } break; } case DestroyNotify: @@ -152,15 +154,18 @@ } case KeyPress: { - XKeyEvent* e = (XKeyEvent*)&event; -// unsigned int* keycode = new unsigned int( e->keycode ); -// POST_MESSAGE( "KeyPress", (void*)keycode, 0 ); + //ignore autorepeat +// if ( IsKeyRepeat(&event.xkey) ) break; + + KeyboardInput::ProcessKeyInput(event.xkey.keycode, true); break; } case KeyRelease: { - XKeyEvent* e = (XKeyEvent*)&event; - e->keycode; + //ignore autorepeat +// if ( IsKeyRepeat(&event.xkey) ) break; + + KeyboardInput::ProcessKeyInput(event.xkey.keycode, false); break; } case MotionNotify: @@ -183,6 +188,25 @@ return true; } +bool X11Window::IsKeyRepeat(XKeyEvent* e) +{ + XEvent nEvent; + + if ( XPending(m_display) > 0 ) + { + XPeekEvent(m_display, &nEvent); + if ( (nEvent.type == KeyRelease || nEvent.type == KeyPress) && + nEvent.xkey.keycode == e->keycode && + nEvent.xkey.time == e->time) + { + XNextEvent(m_display, &nEvent); //forget next event + return true; + } + } + + return false; +} + void X11Window::Clear() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Modified: Mercury2/src/X11Window.h =================================================================== --- Mercury2/src/X11Window.h 2009-05-17 19:16:37 UTC (rev 267) +++ Mercury2/src/X11Window.h 2009-05-17 21:14:12 UTC (rev 268) @@ -22,6 +22,8 @@ virtual void Clear(); private: + bool IsKeyRepeat(XKeyEvent* e); + Display* m_display; GLXContext m_renderCtx; Window m_window; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |