Struggling with abstractions (cpp)

Summary question: Are there “standard”/best practice ways of dealing with windows and events when using GLFW with C++?


I’m using GLFW and OpenGL through GLAD.

I’m trying to abstract away/OOPify some of my GLFW calls, and I’ve run into the same traps as many others with trying to fit a pear into an apple-shape. I understand the problems, but can’t see any good solutions. It seems like the easiest way to handle Windows and Events are to do it in the main thread and using global functions, but that doesn’t seem very maintainable nor easily workable.

I currently have a Window class that wraps my GLFWwindow, and I am trying to figure out a reasonable way to handle events (resizing windows, key presses, mouse actions). I’ve been spending a little too much time trying to figure out how to deal with the callback functions right now, but I mainly find comments on what not to do, and suggestions on how to deal with what I think is only part of my problem. I found that I can deal with the resizing callback by using static callback functions and by storing a reference to my Window instance as a UserPointer, but that still doesn’t leave me any good options for mouse movements. Optimally, I’d like to deal with resizing in a Window class, but key presses and mouse actions in something like an EventHandler class. I don’t quite know how to do this though, except for possibly by use of something like an AppManager class (which would manage both the Window and the EventHandler) which I store a user pointer to, but I would then basically only have this class for that purpose.

I feel like there must be a better solution for dealing with this sort of problem, but I quite simply can’t find it.

I use GLFW with C++ but haven’t felt the need to write any form of abstraction for the C API.

I did find this C++ abstraction on github:

This abstraction seems overkill to me. My experience of writing C and C++ code in the games industry for the last two decades has been to keep things simple. Some abstraction can be handy - for example I do have a simple wrapper for posting callbacks to UI subsystems, however these are application specific since they handle the separate modes of the application.

1 Like