1

For the past week I have been trying to learn C++ (no previous programming experience) and am currently playing with the SDL2 and SDL_TTF libraries using Visual Studio 2015. So far I have been able to solve most issues I've come up against with some Googling and experimentation. However this particular issue has me stumped.

I am trying to make a basic RPG-esque text box based dialogue system that renders text in a typewriter style with SDL_TTF. The text rendering all works beautifully however I am having trouble with handling inputs to allow the player to advance the text.

In this basic setup I want player input to be disabled until all the text has rendered and then the player can either press a mouse button or either SPACE or RETURN to advance to the next box of dialogue.

I have the basic functionality working however there is a problem where if the player presses SPACE or RETURN before the text has finished rendering it will advance the instant the text has rendered, as if the key press event has been queued up.

To counter this I have added the SDL_FlushEvent functions after the text rendering function in an attempt to clear the event queue of any key presses that have been entered whilst the text was rendering. However if the player presses a key more than once it still seems to queue up these inputs and then release them as soon as the text rendering function finishes.

Oddly the same functionality with the mouse button press works perfectly as intended - all mouse button inputs are ignored until the text has finished rendering.

Why does the mouse button queue flush correctly but the key press queue seems to still store values?

My code for this particular section is below:

bool quit = false;    
bool inputOn = false;
int eventcheck = 0;

    // render and blit text

    renderText(currentLine, currentChar);

    // blit Text Box Arrow
    SDL_BlitSurface(textBoxArrow, NULL, screenSurface, &textboxrect);
    SDL_UpdateWindowSurface(window);

    // start input handling

    inputOn = true;
    SDL_PumpEvents();
    SDL_FlushEvent(SDL_MOUSEBUTTONDOWN);
    SDL_FlushEvent(SDL_KEYDOWN);

    while ((quit == false) && (inputOn == true) && (SDL_WaitEvent(&sdlEvent))) {
        switch (sdlEvent.type) {
        case SDL_QUIT:
            quit = true;
            inputOn = false;
            break;
        case SDL_MOUSEBUTTONDOWN:
            eventcheck = 1;
            std::cout << "Mouse button pressed! Eventcheck is: " << eventcheck << std::endl;
            inputOn = false;
            break;
        case SDL_KEYDOWN:
            if ((sdlEvent.key.keysym.sym == SDLK_SPACE || sdlEvent.key.keysym.sym == SDLK_RETURN) && sdlEvent.key.repeat == 0) {
                eventcheck = 1;
                inputOn = false;
                std::cout << "SPACE or RETURN button pressed! Eventcheck is: " << eventcheck << std::endl;
            }
            if (sdlEvent.key.keysym.sym == SDLK_q) {
                quit = true;
                inputOn = false;
                std::cout << "ESC button pressed! Eventcheck is: " << eventcheck << std::endl;
            }
            if ((sdlEvent.key.keysym.sym == SDLK_LALT || sdlEvent.key.keysym.sym == SDLK_RALT) && sdlEvent.key.keysym.sym == SDLK_F4) {
                quit = true;
                inputOn = false;
                std::cout << "ALT-F4 button pressed! Eventcheck is: " << eventcheck << std::endl;
            }
            break;
        }
        std::cout << "In event handle loop" << std::endl;
    }
JRX88
  • 11
  • 3
  • Possible duplicate of [A way to make keyboard event queue both responsive and not take whole CPU power](http://stackoverflow.com/questions/9845491/a-way-to-make-keyboard-event-queue-both-responsive-and-not-take-whole-cpu-power) – Paul Sweatte Nov 23 '15 at 07:01

0 Answers0