The issue I'm having is interesting. The function KeyHit() works now, but it appears to not always get the key input for some reason. By pressing q, it should negate the boolean renderQuuad. It does this, but only about half the time that I press q..
#pragma once
#ifndef SDLKeys_h
#define SDLKeys_h
#include <SDL.h>
class SDLKeys
{
private:
Uint8 *mPreviousKeys;
Uint8 *mCurrentKeys;
int *mMouseX;
int *mMouseY;
public:
SDLKeys(void);
~SDLKeys(void);
void UpdateKeys();
void UpdateCursor();
int GetMouseX() const;
int GetMouseY() const;
bool KeyHit( int index ) const;
bool KeyDown( int index ) const;
};
#endif // SDLKeys_h
There are various problems with this code, the one that is propably causing your error, as far as I can see, should be this:
The pointer returned by SDL_GetKeyState points to an internal array used by SDL, it remains valid during the whole lifetime of your application and is updated at each call to SDL_PumpEvents. You should only read this array and there is no need to call SDL_GetKeyState more than once, it will return the same pointer each time. In your first call to UpdateKeys() you swap your internal array pointer with the one returned by SDL_GetKeyState() and overwrite the contents of your own array. This is more or less ok, when you call it again, however, mCurrentKeys is a pointer to your own array, mPreviousKeys belongs to SDL. You swap the two and copy the array belonging to SDL over itself. Not only is this a bad thing because you should never modify SDLs internal key array, but it will obviously not work as expected.
Another part of your code, that will undoubtedly cause some kind of a problem is your usage of SDL_GetMouseState(). This function, unlike SDL_GetKeyState, will not return something that remains valid. It expects you to pass in pointers to existing int variables and will store the current position of the cursor in those. It also accepts NULL pointers, which is the reason your program does not crash(yet).Once you call the GetMouseX() or GetMouseY() methods you will dereference a NULL pointer, which is a very very bad thing...
Furthermore, you never free the array you allocate in SDLKeys constructor and should not need to use new and delete for your SDLKeys object in main.
Some of this information might not be up to date or correct, since I havent used SDL at all for about 2 years, so if any of my explanations above are fundamentally wrong I beg your forgiveness.
Well, as far as I can see it, the contents of mPreviousKeys and mCurrentKeys seem to be the same after each call to UpdateKeys(), because you copy the contents after they are updated. You should propably put your memcpy before you call SDL_PumpEvents().
Hmm. Thanks for all help thus far :P. That doesn't appear to work though.
EDIT: It works! I did have to call the memcpy first, the reason it didn't work at first after changing it is because I was calling it in main, after SDL_PollEvents.
Thanks much for your help!
Last edited by MadPumpkin on Sat May 18, 2013 10:48 pm, edited 1 time in total.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
I presume you still call your UpdateKeys() and UpdateMouse() methods after invoking SDL_PollEvent()? I was a little unsure about this point, so I checked it(http://sdl.beuc.net/sdl.wiki/SDL_PollEvent) and SDL_PollEvent calls SDL_PumpEvents, so the state of the key array is updated before you even call your UpdateKeys