Problems with SDL Input

Started by
8 comments, last by rip-off 11 years, 8 months ago
So, I've been working on a bit of game code on and off and I had the simplest operations working nicely, those being image loading and blitting, as well as some keyboard input to move a dummy character around. What I sometimes like to do is, while developing something, I'll turn my main.cpp file into a sandbox, until the stuff I'm doing works correctly and then I'll take the code, split it up and organize it into nice little files. Well, after such an organization, I found that the keyboard input was no longer continuous, even though my switch case is supposed to handle SDL_KEYDOWN and SDL_KEYUP. I've tried moving a bunch of things around, but I'm not able to get it to work correctly. Anyone have any ideas as to why?

game loop:

[source lang="cpp"] while (!quit)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
Chronos.GetInput(event);
if(event.type == SDL_QUIT)
{
quit = true;
}
}

//Chronos.Move();

ColorRect(screen, 255, 255, 255);

Chronos.Show();

Blit(15, 15, img, screen);

Update(screen);
}[/source]

Chronos.GetInput():
[source lang="cpp"]void Player::GetInput(SDL_Event event)
{
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_UP:
{
y -= 5;
direction = FACEUP;
break;
}
case SDLK_DOWN:
{
y += 5;
direction = FACEDOWN;
break;
}
case SDLK_RIGHT:
{
x += 5;
direction = FACERIGHT;
break;
}
case SDLK_LEFT:
{
x -= 5;
direction = FACELEFT;
break;
}
break;
}

case SDL_KEYUP:
switch(event.key.keysym.sym)
{
case SDLK_UP:
{
y -= 5;
direction = FACEUP;
break;
}
case SDLK_RIGHT:
{
x += 5;
direction = FACERIGHT;
break;
}
case SDLK_DOWN:
{
y += 5;
direction = FACEDOWN;
break;
}
case SDLK_LEFT:
{
x -= 5;
direction = FACELEFT;
break;
}
}
break;
}
}
[/source]

Using Task Manager, I also noticed that my program seems to slowly add more memory to its Working Set getting upwards of 800k KB for no apparent reason and, if unchecked, would keep going. Does anyone know what a possible cause of this could be?

I'm sure the former is just a novice mistake, however the latter seems strange and possibly more serious.

Thanks in advance for any and all help. It is much appreciated!
Advertisement
You shouldn't call SDL_PollEvent in Player::GetInput, as you already call it before you call Player::GetInput so it will throw away some events. I can't see anything else wrong with the code you provided.
Ah, well this is very strange. Polling the event inside the function or outside the function provides no differences and the program still continuously amasses a large quantity of memory it doesn't need. I'm not sure why this program works perfectly when everything's in the same source file and yet it starts acting funky when classes and multiple source files are involved.
You need to poll in exactly one place. If other areas want to be notified of events, then you'll want to handle that separately. A simple mechanism is to pass the resulting SDL_Event object to subsystems that might need to react to it.

As for the memory, it is likely another problem. Things that tend to cause excessive memory usage include per-frame allocation/loading of images, of TTF_Fonts, rendering text, and in some cases naive string management (e.g. trying to use character arrays and pointers instead of std::string).

We cannot give concrete advice about the memory leak without seeing more code.
I have made it to where it only polls once and just passes the event along. However, the input still isn't continuous and in fact it has some weird effect where when one key is pressed, instead of moving the character a continuous amount of units in that certain direction, it goes 5 units in the direction while the key is down and goes 5 again when the key is up. And also, I figure it may be a memory leak caused by maybe pointers or something, however, after a quick glance, it doesn't appear that anything funky is being done with pointers. I would show more code, however, not knowing where the problem may be, I'm not sure what portion of the code to show. However, these are the image functions which use more pointers than any other part:

[source lang="cpp"]SDL_Surface * LoadImage(std::string path)
{
SDL_Surface * loadedImage = NULL;

SDL_Surface * optimizedImage = NULL;

loadedImage = IMG_Load( path.c_str() );

if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);

SDL_FreeSurface(loadedImage);

if(optimizedImage != NULL)
{
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 111, 111, 111);

SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
}
}
return optimizedImage;
}

void Blit(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
Rect coords;

coords.x = x;
coords.y = y;

SDL_BlitSurface( source, NULL, destination, &coords );
}

void ColorRect(SDL_Surface* surface, int x, int y, int z)
{
SDL_FillRect(surface, 0, SDL_MapRGB(surface->format, x, y, z));
}

void Update(SDL_Surface* surface)
{
SDL_Flip(surface);
}
[/source]
Well, the problem with the memory is probably in Player::Show. Regarding the keyboard events, you need to use SDL_EnableKeyRepeat to make SDL emit keydown events repeatedly (obviously if you do this you shouldn't check for keyup events)
Well thank you very much for that tip on the SDL_EnableKeyRepeat. Here's that player::show() function. See anything strange?

[source lang="cpp"]void Player::Show()
{
character = LoadImage(direction);
Blit(x, y, character, screen);
}[/source]
I typically handle input like this:

[source lang="cpp"]
static void update(){

SDL_Event event;


const short key_count = 322;
short i = 0;

for ( ; i < key_count; ++i){
if ( keyboard.key_state == HIT ){
keyboard.key_state = DOWN;
}
}


while ( SDL_PollEvent(&event) ){

switch (event.type){

case SDL_KEYDOWN:
keyboard.key_state[ event.key.keysym.sym ] = HIT;
break;

case SDL_KEYUP:
keyboard.key_state[ event.key.keysym.sym ] = UP;
break;

case SDL_QUIT:
printf("Quitting\n");
exit(0);
}
}

}
[/source]

There are 322 possible key combinations, so just create an array of 322 bytes (char), and use it to store key state, as received from SDL.

This gives you a nice, persistent view of overall keyboard state, without any need for SDL_EnableKeyRepeat (which, in my experience, doesn't work very reliably across systems).

Then you can check key state like so:
[source lang="cpp"]if (key_state[SDLK_UP] == HIT):

// do something[/source]

I have yet to find a better system.

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+
With regards to Player::Show, since it is called every time the loop runs it will overwrite the character surface with a new surface so the old surface is not freed. You can either free the surface in character before loading or load all the character pictures at the beginning and assign them to the character variable as needed.
The correct answer to RulerOfNothing's question is to load all the character pictures at the beginning. Ideally, load each picture exactly once and free them at the end of your program. But for the moment loading a distinct copy of the same picture will be an improvement.

This topic is closed to new replies.

Advertisement