瀏覽代碼

Moving to not SDL input

Panagiotis Christopoulos Charitos 13 年之前
父節點
當前提交
76001c1171
共有 7 個文件被更改,包括 197 次插入127 次删除
  1. 6 0
      include/anki/core/NativeWindow.h
  2. 14 33
      include/anki/input/Input.h
  3. 8 3
      src/input/CMakeLists.txt
  4. 3 91
      src/input/Input.cpp
  5. 12 0
      src/input/InputDummy.cpp
  6. 149 0
      src/input/InputX11.cpp
  7. 5 0
      testapp/Main.cpp

+ 6 - 0
include/anki/core/NativeWindow.h

@@ -36,6 +36,12 @@ public:
 	{}
 	~NativeWindow();
 
+	NativeWindowImpl& getNative()
+	{
+		ANKI_ASSERT(isCreated());
+		return *impl;
+	}
+
 	/// @name Public interface
 	/// Don't implement them in .h files
 	/// @{

+ 14 - 33
include/anki/input/Input.h

@@ -3,50 +3,41 @@
 
 #include "anki/math/Math.h"
 #include "anki/util/Singleton.h"
-#include <SDL/SDL_scancode.h>
-#include <array>
+#include "anki/util/Array.h"
 
 namespace anki {
 
-/// Handle the SDL input
+class NativeWindow;
+
+/// Handle the input and other events
 class Input
 {
 public:
 	Input()
-	{
-		init();
-	}
+	{}
+	~Input()
+	{}
 
 	/// @name Acessors
 	/// @{
-	uint32_t getKey(uint32_t i) const
+	U32 getKey(U32 i) const
 	{
 		return keys[i];
 	}
 
-	uint32_t getMouseBtn(uint32_t i) const
+	U32 getMouseBtn(U32 i) const
 	{
 		return mouseBtns[i];
 	}
-
-	bool getWarpMouse() const
-	{
-		return warpMouseFlag;
-	}
-	bool& getWarpMouse()
-	{
-		return warpMouseFlag;
-	}
-	void setWarpMouse(const bool x)
-	{
-		warpMouseFlag = x;
-	}
 	/// @}
 
+	void init(NativeWindow* nativeWindow);
 	void reset();
 	void handleEvents();
 
 private:
+	NativeWindow* nativeWindow = nullptr;
+
 	/// @name Keys and btns
 	/// @{
 
@@ -54,23 +45,13 @@ private:
 	/// - 0 times: unpressed
 	/// - 1 times: pressed once
 	/// - >1 times: Kept pressed 'n' times continuously
-	std::array<uint32_t, SDL_NUM_SCANCODES> keys;
+	Array<U32, 128> keys;
 
 	/// Mouse btns. Supporting 3 btns & wheel. @see keys
-	std::array<uint32_t, 8> mouseBtns;
+	Array<U32, 8> mouseBtns;
 	/// @}
 
-	bool warpMouseFlag = false;
-
-	// mouse stuff
 	Vec2 mousePosNdc; ///< The coords are in the NDC space
-	/// The coords are in the window space. (0, 0) is in the upper left
-	/// corner
-	Vec2 mousePos;
-	Vec2 mouseVelocity;
-	bool hideCursor = false;
-
-	void init();
 };
 
 typedef Singleton<Input> InputSingleton;

+ 8 - 3
src/input/CMakeLists.txt

@@ -1,4 +1,9 @@
-FILE(GLOB ANKI_I_SOURCES *.cpp)
-FILE(GLOB ANKI_I_HEADERS *.h)
+FILE(GLOB ANKI_INPUT_SOURCES Input.cpp)
 
-ADD_LIBRARY(ankiinput ${ANKI_I_SOURCES} ${ANKI_I_HEADERS})
+IF(ANKI_WINDOW_BACKEND STREQUAL "GLXX11" OR ANKI_WINDOW_BACKEND STREQUAL "EGLX11")
+	SET(ANKI_INPUT_SOURCES ${ANKI_INPUT_SOURCES} InputX11.cpp)
+ELSE()
+	MESSAGE(FATAL "Unrecognized ANKI_WINDOW_BACKEND: ${ANKI_WINDOW_BACKEND}")
+ENDIF()
+
+ADD_LIBRARY(ankiinput ${ANKI_INPUT_SOURCES})

+ 3 - 91
src/input/Input.cpp

@@ -1,102 +1,14 @@
-#include "anki/core/App.h"
-#include "anki/core/Logger.h"
 #include "anki/input/Input.h"
-#include <SDL/SDL.h>
+#include <cstring>
 
 namespace anki {
 
-//==============================================================================
-void Input::init()
-{
-	ANKI_LOGI("Initializing input...");
-	reset();
-	ANKI_LOGI("Input initialized");
-}
-
 //==============================================================================
 void Input::reset(void)
 {
-	memset(&keys[0], 0, keys.size() * sizeof(uint32_t));
-	memset(&mouseBtns[0], 0, mouseBtns.size() * sizeof(uint32_t));
+	memset(&keys[0], 0, keys.getSize() * sizeof(U32));
+	memset(&mouseBtns[0], 0, mouseBtns.getSize() * sizeof(U32));
 	mousePosNdc = Vec2(0.0);
-	mouseVelocity = Vec2(0.0);
-}
-
-//==============================================================================
-void Input::handleEvents()
-{
-	// add the times a key is bying pressed
-	for(uint32_t& k : keys)
-	{
-		if(k)
-		{
-			++k;
-		}
-	}
-	for(int x=0; x<8; x++)
-	{
-		if(mouseBtns[x]) ++mouseBtns[x];
-	}
-
-	mouseVelocity = Vec2(0.0);
-
-	SDL_Event event_;
-	while(SDL_PollEvent(&event_))
-	{
-		switch(event_.type)
-		{
-		case SDL_KEYDOWN:
-			keys[event_.key.keysym.scancode] = 1;
-			break;
-
-		case SDL_KEYUP:
-			keys[event_.key.keysym.scancode] = 0;
-			break;
-
-		case SDL_MOUSEBUTTONDOWN:
-			mouseBtns[event_.button.button] = 1;
-			break;
-
-		case SDL_MOUSEBUTTONUP:
-			mouseBtns[event_.button.button] = 0;
-			break;
-
-		case SDL_MOUSEMOTION:
-		{
-			Vec2 prevMousePosNdc(mousePosNdc);
-
-			mousePos.x() = event_.button.x;
-			mousePos.y() = event_.button.y;
-
-			mousePosNdc.x() = (2.0 * mousePos.x()) /
-				(float)AppSingleton::get().getWindowWidth() - 1.0;
-			mousePosNdc.y() = 1.0 - (2.0 * mousePos.y()) /
-				(float)AppSingleton::get().getWindowHeight();
-
-			if(warpMouseFlag)
-			{
-				// the SDL_WarpMouse pushes an event in the event queue.
-				// This check is so we wont process the event of the
-				// SDL_WarpMouse function
-				if(mousePosNdc == Vec2(0.0))
-				{
-					break;
-				}
-
-				uint w = AppSingleton::get().getWindowWidth();
-				uint h = AppSingleton::get().getWindowHeight();
-				SDL_WarpMouse(w / 2, h / 2);
-			}
-
-			mouseVelocity = mousePosNdc - prevMousePosNdc;
-			break;
-		}
-
-		case SDL_QUIT:
-			AppSingleton::get().quit(1);
-			break;
-		}
-	}
 }
 
 } // end namespace

+ 12 - 0
src/input/InputDummy.cpp

@@ -0,0 +1,12 @@
+#include "anki/input/Input.h"
+
+namespace anki {
+
+//==============================================================================
+void Input::handleEvents()
+{
+	// You are dummy... do nothing
+}
+
+} // end namespace anki
+

+ 149 - 0
src/input/InputX11.cpp

@@ -0,0 +1,149 @@
+#include "anki/input/Input.h"
+#include "anki/core/Logger.h"
+#if ANKI_WINDOW_BACKEND_GLXX11
+#	include "anki/core/NativeWindowGlxX11.h"
+#elif ANKI_WINDOW_BACKEND_EGLX11
+#	include "anki/core/NativeWindowEglX11.h"
+#else
+#	error "See file"
+#endif
+
+namespace anki {
+
+//==============================================================================
+static Bool eventsPending(Display* display)
+{
+	XFlush(display);
+	if(XEventsQueued(display, QueuedAlready)) 
+	{
+		return true;
+	}
+
+	static struct timeval zero_time;
+	int x11_fd;
+	fd_set fdset;
+
+	x11_fd = ConnectionNumber(display);
+	FD_ZERO(&fdset);
+	FD_SET(x11_fd, &fdset);
+	if(select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) 
+	{
+		return XPending(display);
+	}
+
+	return false;
+}
+
+//==============================================================================
+void Input::init(NativeWindow* nativeWindow_)
+{
+	ANKI_ASSERT(nativeWindow == nullptr);
+	nativeWindow = nativeWindow_;
+	XSelectInput(nativeWindow->getNative().xDisplay, 
+		nativeWindow->getNative().xWindow, 
+		ExposureMask | ButtonPressMask | KeyPressMask);
+	reset();
+}
+
+//==============================================================================
+void Input::handleEvents()
+{
+	ANKI_ASSERT(nativeWindow != nullptr);
+
+	// add the times a key is being pressed
+	for(U32& k : keys)
+	{
+		if(k)
+		{
+			++k;
+		}
+	}
+	for(U32& k : mouseBtns)
+	{
+		if(k)
+		{
+			++k;
+		}
+	}
+
+	NativeWindowImpl& win = nativeWindow->getNative();
+	Display* disp = win.xDisplay;
+	while(eventsPending(disp))
+	{
+		XEvent event;
+		XNextEvent(disp, &event);
+
+		switch(event.type)
+		{
+		case KeyPress:
+			ANKI_LOGI("Key pressed " << rand());
+			break;
+		default:
+			ANKI_LOGW("Unknown X event");
+			break;
+		}
+	}
+
+#if 0
+
+	SDL_Event event_;
+	while(SDL_PollEvent(&event_))
+	{
+		switch(event_.type)
+		{
+		case SDL_KEYDOWN:
+			keys[event_.key.keysym.scancode] = 1;
+			break;
+
+		case SDL_KEYUP:
+			keys[event_.key.keysym.scancode] = 0;
+			break;
+
+		case SDL_MOUSEBUTTONDOWN:
+			mouseBtns[event_.button.button] = 1;
+			break;
+
+		case SDL_MOUSEBUTTONUP:
+			mouseBtns[event_.button.button] = 0;
+			break;
+
+		case SDL_MOUSEMOTION:
+		{
+			Vec2 prevMousePosNdc(mousePosNdc);
+
+			mousePos.x() = event_.button.x;
+			mousePos.y() = event_.button.y;
+
+			mousePosNdc.x() = (2.0 * mousePos.x()) /
+				(F32)AppSingleton::get().getWindowWidth() - 1.0;
+			mousePosNdc.y() = 1.0 - (2.0 * mousePos.y()) /
+				(F32)AppSingleton::get().getWindowHeight();
+
+			if(warpMouseFlag)
+			{
+				// the SDL_WarpMouse pushes an event in the event queue.
+				// This check is so we wont process the event of the
+				// SDL_WarpMouse function
+				if(mousePosNdc == Vec2(0.0))
+				{
+					break;
+				}
+
+				uint w = AppSingleton::get().getWindowWidth();
+				uint h = AppSingleton::get().getWindowHeight();
+				SDL_WarpMouse(w / 2, h / 2);
+			}
+
+			mouseVelocity = mousePosNdc - prevMousePosNdc;
+			break;
+		}
+
+		case SDL_QUIT:
+			AppSingleton::get().quit(1);
+			break;
+		}
+	}
+#endif
+}
+
+} // end namespace anki

+ 5 - 0
testapp/Main.cpp

@@ -184,6 +184,8 @@ void mainLoopExtra()
 	static Movable* mover = SceneSingleton::get().getActiveCamera().getMovable();
 	Input& in = InputSingleton::get();
 
+	mover->moveLocalZ(-0.02);
+
 	if(in.getKey(SDL_SCANCODE_1))
 	{
 		mover = &SceneSingleton::get().getActiveCamera();
@@ -321,6 +323,9 @@ void initSubsystems(int argc, char* argv[])
 	win = new NativeWindow;	
 	win->create(nwinit);
 
+	// Input
+	InputSingleton::get().init(win);
+
 	// Main renderer
 	RendererInitializer initializer;
 	initializer.ms.ez.enabled = true;