I am trying to update my camera's parameters based on my mouse movement in OpenGL. I am using GLFW. There is an predefined event handler in GLFW
glfwSetCursorPosCallback(window, mouseMoveCallback);
that i'm using to register the mouseMoveCallback() function. I have declared my Camera in a class and an instance of the Camera class is used in the program. The members in the Camera class are non-static but the mouseMoveCallback function is static. I know it is not possible for a static function to access non-static members. What is the best way I can update the members of the non-static instance of Camera's class?
I am able to gain the required functionality by using globals and using the mouseMoveCallback() function to update the globals and then reading the updated global values from the non-static member functions. But this can get ugly real quick when I want to add more parameters to read from, like keyboard inputs, etc. So I wanted to know if there is a better way of doing this
You can use glfwSet/GetWindowUserPointer to associate a user-defined pointer with a GLFWwindow object. Let's say you create a GLFWwindow pointed to by win, and a Camera object pointed to by cam - setup with:
...
glfwSetWindowUserPointer(win, cam);
glfwSetCursorPosCallback(win, mouseMoveCallback);
...
Since mouseMoveCallback is called from a C library, it has C linkage:
extern "C" void mouseMoveCallback (GLFWwindow *win, double x, double y)
{
// get the associated user-data:
Camera *cam = static_cast<Camera *>(glfwGetWindowUserPointer(win));
// manipulate the associated Camera object:
...
}
Your question requires you to decide which design to use to communicate to your object instances.
One design could be to create a global std::map of window handles as the key and object instance as the data. When the handle comes in, you search the map for the window handle and you can retrieve the object instance from there.
Another design is to see if the callback allows a "user parameter". If so, then you can attach the pointer to the instance to the user parameter. When the callback is invoked, you retrieve the instance from the "user parameter".
These are just two methods. There are others that I won't mention, but can be researched by doing a search for ways of "making an object-oriented wrapper for a 'C' interface".
Related
A friend and I are working on a simple synthesizer in C++. We have working oscillator, filter and envelope classes and are able to output some sounds using Portaudio libraries.
We are now working on the graphic interface with Qt Creator but are quite confused with how to organise everything. Indeed, portaudio uses a callback function to fill the buffer and to do so, it needs to access the getValue() method of the ocsillator or the process() method of the filter. But at the moment, we declared our oscillator object and our filter object as pointers inside the MainWindow class. We thus cannot access their methods in the callback function.
More informations on the callback function of portaudio can be found here.
Here are 2 solutions that I thought of :
Putting all the synthesiser objects (osc, filter etc) as globals, so that I can access them in my callback function
Putting the callback function and all the sound-related functions in main.cpp instead of mainwindow.cpp and having the MainWindow object as global, so that I can access its attributes (i.e. the oscillator etc. objects) in the callback function.
I don't really know which solution is the best, or if there is any that would work better...
So I've been using this game engine for quite some time, I can create a game using that with either the built-in events or I can use C++ but I've recently tried to embed Lua 5.3 to it, but I'm having problems on how to register a certain C++ class to Lua, example:
In the game engine's C++, I would change the background color like this:
#include "GDCpp/RuntimeScene.h"
void changeBackground(RuntimeScene & scene)
{
scene.SetBackgroundColor(250,100,85)
}
But my problem is, how can I do that in Lua ? how can I register that function and class in Lua 5.3?
Hava a look on this example.
You create/request a metatable with the class name, push member functions (except constructor) into him and register a constructor function which returns your class as userdata, linked with the metatable.
Setting field "__index" on self table is required that later member access leads to the metatable, not userdata. "__gc" happens on garbage collection - your destructor. Because Lua is written in C, allocating userdata memory doesn't invoke constructors, so the class instance was put on the heap and the address passed to Lua.
I'm developing a game using OpenGL. I have a Game class that contains all the environment variables (by environment, I mean things like gravity or tile sets). There's only one Game object. I also have another class called Entity, which contains properties to display objects on the screen.
I'm finding myself needing access to more and more Game variables in my Entity class. At the moment i'm just using parameters to pass data into each function, but I'm considering just passing a pointer to the Game class? Is there anything wrong with that? Is there a better way?
I think this is good practice. It is good idea to replace a group of parameters with a parameter object.
Just make sure that Game remains cohesive. The variables contained within Game should be related.
Make Entity a Friend of the Game class.
Please see
http://msdn.microsoft.com/en-us/library/465sdshe.aspx
Note: If this is ever done in C#, there isn't a friend keyword or exact equivalent.
This question already has answers here:
How to pass a class method as a GLUT callback?
(5 answers)
Closed 10 years ago.
I am trying to pass a function to GLUT call back glutSpecialFunc.
It's working perfectly when I try to pass a static function(specialKeyProcessor) to it.
When I moved this function to a class(KeyBoardMovement) specialized in processing keyboard-related functions it does not seem to work:
....
KeyboardMovement keyboard;
....
glutSpecialFunc(keyboard.specialKeyProcessor);
The error pops out: Reference to non-static member function must be called.
I dont understand this error because I cant see any difference between the same function placed in different places.
The glutSpecialFunc function sets a per-window callback. If you are using OpenGLUT, it also allows you to associate some data with each window using glutSetWindowData.
Therefore, you can make a global shim function like this:
void specialKeyProcessor(int key, int x, int y) {
KeyboardMovement *keyboard = static_cast<KeyboardMovement *>(glutGetWindowData());
keyboard->specialKeyProcessor(key, x, y);
}
and associate your keyboard handler using glutGetWindowData:
glutSetWindowData(static_cast<void *>(&keyboard));
If you need to store more than one piece of window data, you can store a pointer to a window-specific struct with fields for each window-specific data.
If you aren't using OpenGLUT, you can consider making a std::map mapping window IDs to your global data. Then you can take a similar approach to the above to have window-specific data.
C++ doesn't support closures, which means it can not turn a class instance, together with a method into a function pointer that when called will execute the method of the class instance.
Unfortunately GLUT doesn't support data aware callbacks, like most other C callbacks API do. So it's either write a wrapper function, that will call on a global instance of your class, or well, employ some really dirty tricks using a library called "ffcall" that can practically create new functions at runtime, which can be parametized and act as a closure you can pass to GLUT. But that's really dark stuff and the right thing would be to ditch GLUT.
I am designing an application, where multiple geometric primitive types (all inheriting the base Primitive class) are stored in an object of class Scene.
Instead of having a function in my main() that programmatically creates a scene by constructing the primitives and then calling Scene::add(...), I'd like to have some sort of configuration file that is read at runtime.
This would save me from having to recompile every time I change something about the scene, and seems like a general good idea (in case someone else that can't program might need to use my program sometime)
I've devised a little scheme that defines the scene through a .ini file like so:
[primitivefoo]
type = sphere
pos = 10 20 30
radius = 5.5
[primitivebar]
type = triangle
vertexA = 10 10 -10
vertexB = ...
...
You get the idea.
My plan is to have each subclass of Primitive register their own method interpretINI(...) in the class Scene. Scene would need some sort of map that maps string->void* (...), so that I know which type-string from the .ini file corresponds to which subclass of Primitive.
If this entire approach is bad design, and there is already a much more elegant way to achieve what I want to achieve, I would love to hear it. If not, I would be very grateful if someone could help me realize my design. I'm stuck on how to iterate over all Primitive subclasses to have them register themselves in Scene...
Your approach sounds good to me, with the exception that the names should map to a function returning Primitive* (instead of void*). This is a factory pattern (on two levels)
One limitation of this approach is that the factory functions registered will have to accept the same number/type of parameters. [Update: upon reading your comment to your question, this should not be a problem in your case].
I do not know of any automatic way of enumerating subclasses of a particular class, you will need to manually register the known classes (in main, or a special register function)
Update: you could get around the subclass enumeration by using static variables (in-class or otherwise) that will run a "register" function specific to the class-in-question:
class MyShape : public Shape
{
static int reg;
// appropriate constructor
};
int registerMyShape();
int MyShape::reg = registerMyShape(); // this will be called at program startup
The only problem is, there is no guarantee about the static initialization order in separate compilation units. Maybe if you can make the shapes module-like and "load" them after the registry class is fully initialized?