Skip to content

Commit 53ceb0a

Browse files
committed
Add support for OpenGL Example keyboard input (Windows only) by creating a Win32 Subclass proc to retrieve native keyboard messages. Who wants to do the same thing for macOS?
1 parent bc0f165 commit 53ceb0a

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,11 @@ if (BUILD_EXAMPLES)
407407
opengl-example
408408
dullahan
409409
opengl32
410+
comctl32
410411
${CEF_LIBRARY}
411412
${CEF_DLL_LIBRARY}
412413
${THIRD-PARTY-PREFIX}/${GLFW_FOLDER}/${GLFW_LIB_PATH}
413414
)
414-
415415
SET_PROPERTY(DIRECTORY PROPERTY VS_STARTUP_PROJECT "opengl-example")
416416
elseif(IS_MACOS)
417417
find_library (OPENGL_FRAMEWORK OpenGL)
@@ -447,6 +447,10 @@ if (BUILD_EXAMPLES)
447447
endif()
448448
endif()
449449

450+
if(CMAKE_CONFIGURATION_TYPES)
451+
set(CMAKE_CONFIGURATION_TYPES "Release")
452+
endif()
453+
450454
# Install the Dullahan library and host executable
451455

452456
if( USE_SPOTIFY_CEF )

examples/opengl-example/src/opengl-example.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,10 @@ void openglExample::resizeCallback(int width, int height)
7474
mDullahan->setSize(width, height);
7575
}
7676
}
77+
7778
void openglExample::handleKeyEvent(int key, int scancode, int action, int mods)
7879
{
79-
// Dullahan only has keyboard functions for native OS events
80-
// I don't know if you can get msg, wparam, lparam etc. in GLFW
81-
// Might have to forego keyboard input for the moment
82-
83-
// keey the ESC key to exit as well as File -> Quit since it's useful
80+
// keep the ESC key to exit as well as File -> Quit since it's useful
8481
if (action == GLFW_PRESS)
8582
{
8683
if (key == GLFW_KEY_ESCAPE)
@@ -201,6 +198,21 @@ void openglExample::mouseScrollCallback(double xoffset, double yoffset)
201198

202199
}
203200

201+
#if defined(WIN32)
202+
// Windows subclass procedure for handling keyboard events using native
203+
// Windows messages and parameters which is what Dullahan requires.
204+
LRESULT CALLBACK openglExample::keyEventSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
205+
{
206+
if (uMsg == WM_CHAR || uMsg == WM_KEYDOWN || uMsg == WM_KEYUP)
207+
{
208+
openglExample* parent = (openglExample*)dwRefData;
209+
parent->mDullahan->nativeKeyboardEventWin(uMsg, (uint32_t)wParam, lParam);
210+
}
211+
212+
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
213+
}
214+
#endif
215+
204216
bool openglExample::init()
205217
{
206218
if (! glfwInit())
@@ -224,6 +236,13 @@ bool openglExample::init()
224236
// store this to so the static callbacks can get to an instance
225237
glfwSetWindowUserPointer(mWindow, this);
226238

239+
// Create a Windows subclass procedure for handling keyboard events using
240+
// native Windows messages and parameters which is what Dullahan requires.
241+
#if defined(WIN32)
242+
HWND hwnd = glfwGetWin32Window(mWindow);
243+
SetWindowSubclass(hwnd, keyEventSubClassProc, 0x01, (DWORD_PTR)this);
244+
#endif
245+
227246
glfwSetErrorCallback(errorCallback);
228247
glfwMakeContextCurrent(mWindow);
229248
gladLoadGL();

examples/opengl-example/src/opengl-example.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,21 @@
3535
#include "imgui_impl_opengl2.h"
3636

3737
#include <glad/glad.h>
38+
#if defined(WIN32)
39+
#undef APIENTRY
40+
#define GLFW_EXPOSE_NATIVE_WIN32
41+
#include <GLFW/glfw3.h>
42+
#include <GLFW/glfw3native.h>
43+
#include <commctrl.h>
44+
#elif
3845
#define GLFW_INCLUDE_NONE
3946
#include <GLFW/glfw3.h>
47+
#endif
4048

4149
class dullahan;
4250

43-
class openglExample {
51+
class openglExample
52+
{
4453
public:
4554
openglExample();
4655

@@ -96,20 +105,29 @@ class openglExample {
96105
void generatePickTexture();
97106
bool mousePosToTexturePos(int* tx, int* ty);
98107

99-
// Used to marshall staticfunction callbacks to a instance of the app class
100-
static void resizeCallbackStatic(GLFWwindow* window, int width, int height) {
108+
// Used to marshall static function callbacks to a instance of the app class
109+
static void resizeCallbackStatic(GLFWwindow* window, int width, int height)
110+
{
101111
static_cast<openglExample*>(glfwGetWindowUserPointer(window))->resizeCallback(width, height);
102112
}
103-
static void keyCallbackStatic(GLFWwindow* window, int key, int scancode, int action, int mods) {
113+
static void keyCallbackStatic(GLFWwindow* window, int key, int scancode, int action, int mods)
114+
{
104115
static_cast<openglExample*>(glfwGetWindowUserPointer(window))->handleKeyEvent(key, scancode, action, mods);
105116
}
106-
static void mouseButtonCallbackStatic(GLFWwindow* window, int button, int action, int mods) {
117+
static void mouseButtonCallbackStatic(GLFWwindow* window, int button, int action, int mods)
118+
{
107119
static_cast<openglExample*>(glfwGetWindowUserPointer(window))->mouseButtonCallback(button, action, mods);
108120
}
109-
static void mouseMoveCallbackStatic(GLFWwindow* window, double xpos, double ypos) {
121+
static void mouseMoveCallbackStatic(GLFWwindow* window, double xpos, double ypos)
122+
{
110123
static_cast<openglExample*>(glfwGetWindowUserPointer(window))->mouseMoveCallback(xpos, ypos);
111124
}
112-
static void mouseScrollCallbackStatic(GLFWwindow* window, double xoffset, double yoffset) {
125+
static void mouseScrollCallbackStatic(GLFWwindow* window, double xoffset, double yoffset)
126+
{
113127
static_cast<openglExample*>(glfwGetWindowUserPointer(window))->mouseScrollCallback(xoffset, yoffset);
114128
}
129+
130+
#if defined(WIN32)
131+
static LRESULT CALLBACK keyEventSubClassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
132+
#endif
115133
};

0 commit comments

Comments
 (0)