diff --git a/src/Globals.h b/src/Globals.h index db09c08..a90369b 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -20,3 +20,6 @@ extern WriteSerializer* gWriteSerializer; struct ReadSerializer; extern ReadSerializer* gReadSerializer; + +struct GuiInputState; +extern GuiInputState* gGuiInputState; diff --git a/src/Utils.h b/src/Utils.h index 16f6c59..edea883 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -5,6 +5,24 @@ #include #include +struct GuiInputState { + int32_t mousedX; + int32_t mousedY; + int32_t mouseX; + int32_t mouseY; + uint8_t mouseButton; + int32_t mouseScroll; + char key; + + GuiInputState() : + mouseX(0), + mouseY(0), + mouseButton(0), + mouseScroll(0), + key(0) { + } +}; + inline void gGetFileModTime (const char* filename, int *sec, int *nsec) { struct stat attr; diff --git a/src/main.cc b/src/main.cc index 5adb71a..9bbb597 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,8 +25,12 @@ GLFWwindow* gWindow = nullptr; RuntimeModuleManager* gModuleManager = nullptr; WriteSerializer* gWriteSerializer = nullptr; ReadSerializer* gReadSerializer = nullptr; +GuiInputState* gGuiInputState = nullptr; double gTimeAtStart = 0; +double mouse_scroll_x = 0.; +double mouse_scroll_y = 0.; + using namespace std; namespace bgfx { @@ -63,6 +67,36 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, glfwSetWindowShouldClose(window, GLFW_TRUE); } +void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { + mouse_scroll_x += xoffset; + mouse_scroll_y += yoffset; +} + +void handle_mouse () { + if (!glfwGetWindowAttrib(gWindow, GLFW_FOCUSED)) { + return; + } + + if (glfwGetMouseButton(gWindow, 1)) { + glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + } else { + glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } + + double mouse_x, mouse_y; + glfwGetCursorPos(gWindow, &mouse_x, &mouse_y); + gGuiInputState->mousedX = mouse_x - gGuiInputState->mouseX; + gGuiInputState->mousedY = mouse_y - gGuiInputState->mouseY; + gGuiInputState->mouseX = mouse_x; + gGuiInputState->mouseY = mouse_y; + gGuiInputState->mouseScroll = mouse_scroll_y; + + gGuiInputState->mouseButton = + glfwGetMouseButton(gWindow, 0) + + (glfwGetMouseButton(gWindow, 1) << 1) + + (glfwGetMouseButton(gWindow, 2) << 2); +} + int main(void) { gTimeAtStart = gGetCurrentTime(); @@ -85,6 +119,9 @@ int main(void) int width, height; glfwGetWindowSize(gWindow, &width, &height); + glfwSetKeyCallback(gWindow, key_callback); + glfwSetScrollCallback (gWindow, mouse_scroll_callback); + std::cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl; std::cout << "GLSL Version : " << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl; @@ -103,6 +140,8 @@ int main(void) // imgui initialization. imguiCreate(); + GuiInputState gui_input_state; + gGuiInputState = &gui_input_state; // Timer Timer timer; @@ -124,10 +163,19 @@ int main(void) // Load modules module_manager.LoadModules(); - glfwSetKeyCallback(gWindow, key_callback); int64_t time_offset = bx::getHPCounter(); while(!glfwWindowShouldClose(gWindow)) { + // Start the imgui frame such that widgets can be submitted + handle_mouse(); + glfwGetWindowSize(gWindow, &width, &height); + imguiBeginFrame (gGuiInputState->mouseX, + gGuiInputState->mouseY, + gGuiInputState->mouseButton, + gGuiInputState->mouseScroll, + width, + height); + static int64_t last = bx::getHPCounter(); int64_t pre_module_check = bx::getHPCounter(); @@ -162,6 +210,9 @@ int main(void) glfwPollEvents(); + // submit the imgui widgets + imguiEndFrame(); + usleep(16000); } diff --git a/src/modules/CharacterModule.cc b/src/modules/CharacterModule.cc index f5e3637..bfcc767 100644 --- a/src/modules/CharacterModule.cc +++ b/src/modules/CharacterModule.cc @@ -185,8 +185,6 @@ static void module_reload(struct module_state *state, void* read_serializer) { } static void module_unload(struct module_state *state, void* write_serializer) { - glfwSetScrollCallback (gWindow, nullptr); - // serialize the state of the entity if (write_serializer != nullptr) { module_serialize(state, static_cast(write_serializer)); diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 987c424..4c33dc3 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -1205,14 +1205,6 @@ void Renderer::initialize(int width, int height) { mLightProbes[LightProbe::Kyoto ].load("kyoto"); mCurrentLightProbe = LightProbe::Bolonga; - // Start the imgui frame such that widgets can be submitted - imguiBeginFrame (inputState.mouseX, - inputState.mouseY, - inputState.mouseButton, - inputState.mouseScroll, - width, - height); - initialized = true; resize (width, height); bgfx::frame(); @@ -1330,9 +1322,6 @@ void Renderer::paintGL() { int num_chars = width / 8; bgfx::dbgTextPrintf(num_chars - 18, 0, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs); - // submit the imgui widgets - imguiEndFrame(); - // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. bgfx::touch(0); @@ -1681,14 +1670,6 @@ void Renderer::paintGL() { // process submitted rendering primitives. bgfx::frame(); - // Start the next imgui frame - imguiBeginFrame (inputState.mouseX, - inputState.mouseY, - inputState.mouseButton, - inputState.mouseScroll, - width, - height); - ImGui::SetNextWindowSize (ImVec2(400.f, 300.0f), ImGuiSetCond_Once); ImGui::SetNextWindowPos (ImVec2(10.f, 300.0f), ImGuiSetCond_Once); diff --git a/src/modules/RenderModule.h b/src/modules/RenderModule.h index 1724f63..f9e8d0b 100644 --- a/src/modules/RenderModule.h +++ b/src/modules/RenderModule.h @@ -13,24 +13,6 @@ struct Entity; -struct InputState { - int32_t mousedX; - int32_t mousedY; - int32_t mouseX; - int32_t mouseY; - uint8_t mouseButton; - int32_t mouseScroll; - char key; - - InputState() : - mouseX(0), - mouseY(0), - mouseButton(0), - mouseScroll(0), - key(0) { - } -}; - struct Camera { Vector3f eye; Vector3f poi; @@ -372,9 +354,6 @@ struct Renderer { uint16_t activeCameraIndex; - // needed to forward inputs to IMGUI - InputState inputState; - Renderer() : initialized(false), drawDebug(false), diff --git a/src/modules/TestModule.cc b/src/modules/TestModule.cc index 9298553..de37689 100644 --- a/src/modules/TestModule.cc +++ b/src/modules/TestModule.cc @@ -20,8 +20,6 @@ using namespace std; -double mouse_scroll_x = 0.; -double mouse_scroll_y = 0.; bool fps_camera = true; // Boilerplate for the module reload stuff @@ -38,11 +36,6 @@ struct module_state { CharacterEntity* character = nullptr; }; -void mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) { - mouse_scroll_x += xoffset; - mouse_scroll_y += yoffset; -} - void handle_mouse (struct module_state *state) { if (!glfwGetWindowAttrib(gWindow, GLFW_FOCUSED)) { return; @@ -54,19 +47,6 @@ void handle_mouse (struct module_state *state) { glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } - double mouse_x, mouse_y; - glfwGetCursorPos(gWindow, &mouse_x, &mouse_y); - gRenderer->inputState.mousedX = mouse_x - gRenderer->inputState.mouseX; - gRenderer->inputState.mousedY = mouse_y - gRenderer->inputState.mouseY; - gRenderer->inputState.mouseX = mouse_x; - gRenderer->inputState.mouseY = mouse_y; - gRenderer->inputState.mouseScroll = mouse_scroll_y; - - gRenderer->inputState.mouseButton = - glfwGetMouseButton(gWindow, 0) - + (glfwGetMouseButton(gWindow, 1) << 1) - + (glfwGetMouseButton(gWindow, 2) << 2); - Camera *active_camera = &gRenderer->cameras[gRenderer->activeCameraIndex]; assert (active_camera != nullptr); Matrix44f camera_view_matrix = SimpleMath::Map(active_camera->mtxView, 4, 4); @@ -82,10 +62,10 @@ void handle_mouse (struct module_state *state) { Vector3f right = camera_rot_inv.block<1,3>(0,0).transpose(); right = view_dir.cross (Vector3f (0.f, 1.f, 0.f)); Matrix33f rot_matrix_y = SimpleMath::GL::RotateMat33( - gRenderer->inputState.mousedY * 0.4f, + gGuiInputState->mousedY * 0.4f, right[0], right[1], right[2]); Matrix33f rot_matrix_x = SimpleMath::GL::RotateMat33( - gRenderer->inputState.mousedX * 0.4f, + gGuiInputState->mousedX * 0.4f, 0.f, 1.f, 0.f); poi = eye + rot_matrix_x * rot_matrix_y * view_dir; @@ -224,22 +204,14 @@ static void module_finalize(struct module_state *state) { static void module_reload(struct module_state *state, void* read_serializer) { std::cout << "Module reload called. State: " << state << std::endl; - // reset mouse scrolling state - mouse_scroll_x = 0; - mouse_scroll_y = 0; - cout << "Creating render entity ..." << endl; // load the state of the entity if (read_serializer != nullptr) { module_serialize(state, static_cast(read_serializer)); } - - glfwSetScrollCallback (gWindow, mouse_scroll_callback); } static void module_unload(struct module_state *state, void* write_serializer) { - glfwSetScrollCallback (gWindow, nullptr); - // serialize the state of the entity if (write_serializer != nullptr) { module_serialize(state, static_cast(write_serializer));