Archived

This topic is now archived and is closed to further replies.

ChickenMcOwnage

SDL - events for turn-based game

Recommended Posts

hello, I''m using the SDL library for C++, writing a graphical rogue-like game. It is completely turnbased, so absolutely no processing should be done while waiting for input from the player. Right now I am using... if (SDL_WaitEvent(&event)!=0) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym==SDLK_LEFT) { //move the player ...etc. etc. the problem is, it''s not even waiting for an event. Monsters and NPCs are moving when the program should be sitting and waiting for input. I''ve tried SDL_PollEvent, but that works the same way. What''s the best way to get my desired effect? many thanks!

Share this post


Link to post
Share on other sites
This uses if statements instead of swithc statements, but it should work.


SDL_Event event;
for(;;)
{
SDL_WaitEvent(&event);
if(event.type==SDL_KEYDOWN)
{
//do key press events here

}
)


[edited by - nodeg on October 20, 2003 12:24:44 AM]

[edited by - nodeg on October 20, 2003 12:26:49 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You don''t show where you do your monster updating.

I''d suggest something like the following:


while( true ) {
bool doMonsters = false;
if( SDL_WaitEvent(&event)!=0 ) {
switch( event.type ) {
case SDL_KEYDOWN:
doMonsters = HandleKeyDown( event );
break;
...
default:
break;
}
}
if( doMonsters ) {
MoveMonsters();
}
DisplayScene();
}


The key is that waitevent may return for all kinds of events (updates, mouse movement, timers, idle, etc) and you need to be able to deal with that, only updating monster state when your player state updates. In the example code, whether this happens is returned by HandleKeyDown().

In the future, I suggest setting a breakpoint in your code (such as right after waitevent) and tracing through the code, looking at variable values, to figure out what the problem is.

Share this post


Link to post
Share on other sites