• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
DillonBroadus

Problems with SDL Input

9 posts in this topic

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!
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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)
1

Share this post


Link to post
Share on other sites
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]
0

Share this post


Link to post
Share on other sites
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[i] == HIT ){
keyboard.key_state[i] = 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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0