c++ - SDL Text Input returning white squares -
i created event class , wanted text it. unfortunately text isn't reading correctly...
i've set text gotten output command line when new key input, isn't working.
here's "eventmanager" code update function:
void eventmanager::update(){ while(sdl_pollevent(&e)){ switch(e.type){ case sdl_quit: running = false; break; case sdl_mousebuttondown: mousepressed = true; break; case sdl_keydown: if(shouldcollecttext && e.key.keysym.sym == sdlk_backspace && currentcollectedtext.length() > 0){ currentcollectedtext.pop_back(); }else if(shouldcollecttext && e.key.keysym.sym != sdlk_backspace){ currentcollectedtext += e.text.text; //the problem std::cout << currentcollectedtext << std::endl; } } } }
i followed the lazy foo tutorials, , can't find problem.
some other things note:
i start text input in "main.cpp" class:
int main( int argc, char *argv[] ) { game *game = new game(); game->init(); sdl_starttextinput(); while(game->isrunning()){ game->handleevents(); game->update(); game->render(); } game->close(); sdl_stoptextinput(); return 0; }
i create of variables in "eventmanager.h":
bool shouldcollecttext; std::string currentcollectedtext;
and define them in "init()" function:
shouldcollecttext = false; currentcollectedtext = "";
your problem using wrong event type. take second @ lazy foo's tutorial , here:
e.text.text
reserved text input events , nothing else. being sdl_event union of structures, overlaps may occur in used memory. see in terminal output.
remove sdl_keydown
event , try instead:
case sdl_textinput: currentcollectedtext += e.text.text; break;
i'm not sure std::cout << currentcollectedtext << std::endl;
though. depends on when want output of text content.
if want use sdl_keydown
have access keysym tables , maybe calculate letter correspond , print out. say, approach more complicated want achieve.
so in conclusion e.text.text
work if e.type
has sdl_textinput
value event. other settings in code event should triggered , may text type in.
so complete example this:
void eventmanager::update(){ while(sdl_pollevent(&e)){ switch(e.type){ case sdl_quit: running = false; break; case sdl_mousebuttondown: mousepressed = true; break; case sdl_textinput: if(shouldcollecttext && currentcollectedtext.length() > 0){ currentcollectedtext.pop_back(); }else if(shouldcollecttext) { currentcollectedtext += e.text.text; //the problem std::cout << currentcollectedtext << std::endl; } break; default: break; } } }
Comments
Post a Comment