From 8884f4cc36201cf91908d42920dd60974bd56607 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Mon, 13 Oct 2014 17:24:11 +0200 Subject: [PATCH] Started initial performance testing on Lua-based keyboard controller --- ext/ghoul | 2 +- scripts/default_keybinding.lua | 4 +++ src/engine/openspaceengine.cpp | 3 ++- src/interaction/interactionhandler.cpp | 22 ++++++++++++----- src/interaction/keyboardcontroller.cpp | 34 ++++++++++++++++++++++++-- 5 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 scripts/default_keybinding.lua diff --git a/ext/ghoul b/ext/ghoul index 1f1386215e..7553b63a5b 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit 1f1386215e517179f7a4e80e64693fd3ebc2b339 +Subproject commit 7553b63a5b78374726682997e3138584bda22675 diff --git a/scripts/default_keybinding.lua b/scripts/default_keybinding.lua new file mode 100644 index 0000000000..9ebf558a60 --- /dev/null +++ b/scripts/default_keybinding.lua @@ -0,0 +1,4 @@ +return { + w = function() print("w") end, + s = function() print("s") end +} diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index f528652f9c..44601aa0d1 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -309,7 +309,8 @@ bool OpenSpaceEngine::initialize() { //DeviceIdentifier::init(); //DeviceIdentifier::ref().scanDevices(); - _interactionHandler.setKeyboardController(new interaction::KeyboardControllerFixed); + //_interactionHandler.setKeyboardController(new interaction::KeyboardControllerFixed); + _interactionHandler.setKeyboardController(new interaction::KeyboardControllerLua); _interactionHandler.setMouseController(new interaction::TrackballMouseController); // Run start up scripts diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index bceb05d861..926a1b858f 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -24,6 +24,12 @@ #include +#include + +namespace { + const std::string _loggerCat = "InteractionHandler"; +} + namespace openspace { namespace interaction { @@ -91,24 +97,28 @@ const Camera* const InteractionHandler::camera() const { } void InteractionHandler::keyboardCallback(int key, int action) { - if (_keyboardController) - _keyboardController->keyPressed(KeyAction(action), Keys(key)); + if (_keyboardController) { + auto start = ghoul::HighResClock::now(); + _keyboardController->keyPressed(KeyAction(action), Keys(key)); + auto end = ghoul::HighResClock::now(); + LINFO("Keyboard timing: " << std::chrono::duration_cast(end - start).count() << "ns"); + } } void InteractionHandler::mouseButtonCallback(int button, int action) { if (_mouseController) - _mouseController->button(MouseAction(action), MouseButton(button)); + _mouseController->button(MouseAction(action), MouseButton(button)); } void InteractionHandler::mousePositionCallback(int x, int y) { if (_mouseController) - // TODO Remap screen coordinates to [0,1] - _mouseController->move(float(x), float(y)); + // TODO Remap screen coordinates to [0,1] + _mouseController->move(float(x), float(y)); } void InteractionHandler::mouseScrollWheelCallback(int pos) { if (_mouseController) - _mouseController->scrollWheel(float(pos)); + _mouseController->scrollWheel(float(pos)); } } // namespace interaction diff --git a/src/interaction/keyboardcontroller.cpp b/src/interaction/keyboardcontroller.cpp index 6aeb91d383..ec3ba81399 100644 --- a/src/interaction/keyboardcontroller.cpp +++ b/src/interaction/keyboardcontroller.cpp @@ -27,6 +27,11 @@ #include #include +#include +#include +#include +#include + namespace openspace { namespace interaction { @@ -143,8 +148,33 @@ void KeyboardControllerFixed::keyPressed(KeyAction action, Keys key) { */ } -void KeyboardControllerLua::keyPressed(KeyAction action, Keys key) -{ +void KeyboardControllerLua::keyPressed(KeyAction action, Keys key) { + std::string _loggerCat = "KeyboardControllerLua"; + + lua_State* s = luaL_newstate(); + luaL_openlibs(s); + + int status = luaL_loadfile(s, absPath("${SCRIPTS}/default_keybinding.lua").c_str()); + if (status != LUA_OK) { + LERROR("Error loading script: '" << lua_tostring(s, -1) << "'"); + return; + } + + if (lua_pcall(s, 0, LUA_MULTRET, 0)) { + LERROR("Error executing script: " << lua_tostring(s, -1)); + return; + } + + auto start = ghoul::HighResClock::now(); + + + lua_pushstring(s, "w"); + lua_gettable(s, -2); + lua_pcall(s, 0, 0, 0); + + auto end = ghoul::HighResClock::now(); + LINFO("Keyboard timing: " << std::chrono::duration_cast(end - start).count() << "ns"); + }