I am working on a project where you can play a piano. I want to have the option to switch octaves using the arrow keys (octave stored as an int variable) and have that change what sound is played using another library (miniaudio). I have to be able to switch what happens when each key is pressed based on the current octave, but I can’t figure out how to do this.
Here is the code to play the note A5
input_manager.register_hotkey(GLFW_KEY_A, hotkey_type::key_down, [](int, float){
ma_sound_start(&a5);
});
If I change the octave in the program, I want this to switch so the part inside the manager is ma_sound_start(&a6)
My advice would be to look into user pointers. In my engine I have my windows user pointer point to an EngineData struct, and it has handles for any instances of systems that I want to interact with. For instance, as a very rough example with a good bit missing:
struct EngineData {
InputManager* inputManager
int octave
}
Then, whenever you need it you could grab it from your window with glfwGetWindowPointer(). At that point, it’s inside your code, so you can store it away somewhere you can get to it - i.e. pass it down the chain from your top level glfwKeyCallback.
EDIT:
Disregard above, it’s probably not quite what you need. Rather than using function pointers for callbacks, if you’re in c++ you might want to use std::function or similar, and then provide a lambda with a capture. Provided that you’re not passing that callback straight to glfw (I don’t think you are, i dont recognise the signature at least) you should be able to use your existing dispatch mechanism.