Sign in to follow this  

SDL Event Frenzy

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I'm new to SDL and have been playing around with it and now I've got a little problem and hope to get a little more help from you pros out there. I have an event loop to check for input from the user. Within this event loop when the enter key is pressed i wish to enable another event loop (or something else if there is an alternative) so it can take in a value and store it in a variable. I have the other event loop written, so that when the enter key in the first event loop is pressed, the second event loop will only check for KEYDOWN events and store them to a string which i can then convert to an integer and strore in my variable. The problem: It appears that the first loop still runs while the second loop is running, so when i press enter on the first event loop i can enter a value but the key presses that are meant for the second loop are effected in the first loop too! If this helps I'll show the small section of code that does this: From the first event loop: (the handle_input() function contains the second loop) if (event.key.keysym.sym == SDLK_RETURN) { //If the user hasn't entered a value yet if( valueEntered == false ) { SDL_FreeSurface( instr_text ); instr_text = TTF_RenderText_Solid( betfont, "Enter value for square: ", Orange); DrawSprite( gScreen, instr_text , 560, 180); SDL_Flip(gScreen); //Get user input handle_input(); The function then runs as follows so you can see the way the loop is set up: void handle_input(){ while(SDL_PollEvent(&InEvent)) { if ( valueEntered == false ) { //If a key was pressed if( InEvent.type == SDL_KEYDOWN ) { //Keep a copy of the current version of the string std::string temp = str; ...etc. etc. I hope this doesnt sound too confusing, and really appreciate any help! Thanks

Share this post


Link to post
Share on other sites
Try decoupling input/processing and rendering code. Only do light processing in input loops like setting a state. This should help you simplify your program flow logic. Good Luck.

Consider this incomplete snippet:

#include <all_the_header_foo.h>
#include <boost>
#include <ml>
//Don't use globals
SDL_Surface* instr_text = NULL;
int state = 0;
bool bDone = false;
string_foo input;
int key_foo=0;
bool bHaveKey = false;

void IncrState(void)
{
state = state + 1;
}
void StateMachineFoo(void)
{

if(0==state){
//Setup
instr_text = TTF_RenderText_Solid( betfont, "Enter value for square:",Orange);
IncrState():
}else if(1==state){
//Get Info
if(!bHaveKey )return;
bHaveKey = false;//reset flag
if(SDL_RETURN == key_foo){
if(0==input.textlenght_foo())return;//force user to enter something
int value = input.to_integer_foo();
IncrState();
}else{
input+=SDL_2_ASCIICHAR_FOO(key_foo);//add char to string
}
}else if(2==state){
//Cleanup
FreeSurface(instr_text)
bDone = true;
state = 42;//top secret state,hush hush ,loose lips sink ships
}
}
HandleInput_foo()
{
while(SDL_PollEvent(&InEvent))
{
//if key down event
switch(event.type.foo)
{
case SDL_KEYDOWN_FOO:
key_foo = InEvent.input.keyboard.sys.ibm.pc.key4real.symb.foo;
bHaveKey = true;
break;
}
}
}
//Process state events
Process_foo()
{
StateMachineFoo();
}
//do cool graphics
Render_foo()
{
DrawSprite( gScreen, instr_text , 560, 180);
SDL_Flip(gScreen);
}
main_foo(){

//Initialize
Init_Libraries_Foo();
StateMachineFoo();
//Loopy
while(!bDone){
HandleInput_foo();
Process_foo();
Render_foo();
}
//Cleanup
StateMachineFoo();
Close_Libraries_Foo();
Watch_Kung_Foo();
}

Share this post


Link to post
Share on other sites

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this