diff --git a/libraries/tinyblocks-sdl2/src/tb_sdl2.c b/libraries/tinyblocks-sdl2/src/tb_sdl2.c index 3e905ed..b4c532b 100644 --- a/libraries/tinyblocks-sdl2/src/tb_sdl2.c +++ b/libraries/tinyblocks-sdl2/src/tb_sdl2.c @@ -502,6 +502,74 @@ static void* be_window_open(const char* title, int w, int h) { return sw; } +/* Evento que nao coube no quadro anterior (segunda tecla, texto alem do + buffer, clique repetido): e o primeiro a ser aplicado no proximo quadro, + assim nada se perde ao digitar rapido ou colar texto. */ +static SDL_Event s_pending; +static bool s_has_pending = false; + +/* Aplica um evento a entrada da sua janela. false = nao cabe neste quadro. */ +static bool be_apply(SDL_Event* ev) { + SdlWin* w = NULL; + switch (ev->type) { + case SDL_WINDOWEVENT: + if ((w = win_by_id(ev->window.windowID)) && ev->window.event == SDL_WINDOWEVENT_CLOSE) + w->in.close = 1; + break; + case SDL_MOUSEMOTION: + if ((w = win_by_id(ev->motion.windowID))) { + w->in.mouse_x = (float)ev->motion.x; + w->in.mouse_y = (float)ev->motion.y; + } + break; + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + if ((w = win_by_id(ev->button.windowID))) { + bool down = ev->type == SDL_MOUSEBUTTONDOWN; + w->in.mouse_x = (float)ev->button.x; + w->in.mouse_y = (float)ev->button.y; + /* clique repetido no mesmo quadro: espera o proximo */ + if (ev->button.button == SDL_BUTTON_LEFT && (down ? w->in.l_down : w->in.l_up)) return false; + if (ev->button.button == SDL_BUTTON_LEFT) { if (down) w->in.l_down = 1; else w->in.l_up = 1; } + if (ev->button.button == SDL_BUTTON_RIGHT) { if (down) w->in.r_down = 1; else w->in.r_up = 1; } + } + break; + case SDL_MOUSEWHEEL: + if ((w = win_by_id(ev->wheel.windowID))) { + w->in.scroll_dx += (float)ev->wheel.x; + w->in.scroll_dy += (float)ev->wheel.y; + } + break; + case SDL_KEYDOWN: + if ((w = win_by_id(ev->key.windowID))) { + if (w->in.key) return false; /* uma tecla por quadro */ + w->in.key = map_key(ev->key.keysym.scancode); + w->in.key_mod = map_mod(ev->key.keysym.mod); + w->in.key_repeat = ev->key.repeat ? 1 : 0; + } + break; + case SDL_TEXTINPUT: + if ((w = win_by_id(ev->text.windowID))) { + size_t cur = strlen(w->in.text), add = strlen(ev->text.text); + if (cur + add >= sizeof(w->in.text)) { + if (cur > 0) return false; /* nao cabe: vai no proximo quadro */ + add = sizeof(w->in.text) - 1; /* sozinho e maior que o buffer: corta */ + } + memcpy(w->in.text + cur, ev->text.text, add); + w->in.text[cur + add] = 0; + } + break; + case SDL_DROPFILE: + if ((w = win_by_id(ev->drop.windowID)) && ev->drop.file) + snprintf(w->in.drop_file, sizeof(w->in.drop_file), "%s", ev->drop.file); + SDL_free(ev->drop.file); + break; + default: + break; + } + return true; +} + /* Coleta os eventos do SO e distribui para a janela de cada evento */ static void be_poll(void) { for (SdlWin* w = s_wins; w; w = w->next) { @@ -514,57 +582,13 @@ static void be_poll(void) { in->close = 0; } + if (s_has_pending) { + s_has_pending = false; + if (!be_apply(&s_pending)) s_has_pending = true; + } SDL_Event ev; - while (SDL_PollEvent(&ev)) { - SdlWin* w = NULL; - switch (ev.type) { - case SDL_WINDOWEVENT: - if ((w = win_by_id(ev.window.windowID)) && ev.window.event == SDL_WINDOWEVENT_CLOSE) - w->in.close = 1; - break; - case SDL_MOUSEMOTION: - if ((w = win_by_id(ev.motion.windowID))) { - w->in.mouse_x = (float)ev.motion.x; - w->in.mouse_y = (float)ev.motion.y; - } - break; - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: - if ((w = win_by_id(ev.button.windowID))) { - bool down = ev.type == SDL_MOUSEBUTTONDOWN; - w->in.mouse_x = (float)ev.button.x; - w->in.mouse_y = (float)ev.button.y; - if (ev.button.button == SDL_BUTTON_LEFT) { if (down) w->in.l_down = 1; else w->in.l_up = 1; } - if (ev.button.button == SDL_BUTTON_RIGHT) { if (down) w->in.r_down = 1; else w->in.r_up = 1; } - } - break; - case SDL_MOUSEWHEEL: - if ((w = win_by_id(ev.wheel.windowID))) { - w->in.scroll_dx += (float)ev.wheel.x; - w->in.scroll_dy += (float)ev.wheel.y; - } - break; - case SDL_KEYDOWN: - if ((w = win_by_id(ev.key.windowID))) { - w->in.key = map_key(ev.key.keysym.scancode); - w->in.key_mod = map_mod(ev.key.keysym.mod); - w->in.key_repeat = ev.key.repeat ? 1 : 0; - } - break; - case SDL_TEXTINPUT: - if ((w = win_by_id(ev.text.windowID))) { - size_t cur = strlen(w->in.text), add = strlen(ev.text.text); - if (cur + add < sizeof(w->in.text)) memcpy(w->in.text + cur, ev.text.text, add + 1); - } - break; - case SDL_DROPFILE: - if ((w = win_by_id(ev.drop.windowID)) && ev.drop.file) - snprintf(w->in.drop_file, sizeof(w->in.drop_file), "%s", ev.drop.file); - SDL_free(ev.drop.file); - break; - default: - break; - } + while (!s_has_pending && SDL_PollEvent(&ev)) { + if (!be_apply(&ev)) { s_pending = ev; s_has_pending = true; } } /* Estado contínuo: botões segurados só valem na janela com o foco do mouse */