I have a generic problem I suppose.
I`m currently learning C++ and SDL 2.0. SDL provides a function which returns a pointer to a const uint * containing all the keystates.
These are the variables I would like to use:
const Uint8* oldKeyState;
const Uint8* currentKeyState;
In the construction of my input.cpp:
currentKeyState = SDL_GetKeyboardState(&this->length);
oldKeyState = currentKeyState;
And in the Update() method I use:
oldKeyState = currentKeyState;
currentKeyState = SDL_GetKeyboardState(NULL);
However, instead of copying over the last values, all I do is giving a pointer to the oldKeyState, which in turn points to the current keystates..
So how do I go about copying the actual values from the variable's pointer to the current and old keystate? I don't want the pointer in my old keystate since I will not be able to check whether the previous state was UP and the new state is DOWN.
The problem you have here is that you are trying to copy the pointer to a const array, which never changes. As a result, you will see that both pointers go to the same memory address, and you never have two copies of the input state which allows you to check for pressed keys.
Instead, you should use memcpy to copy one array to the other. But in order to do so, you should change the type of oldKeyState to just
Uint8*
, or else you will get an error for copying into a const array.So, the code should end up like this: