TCOD_key_t TCOD_sys_wait_for_keypress(bool flush) { SDL_Event ev; TCOD_key_t ret={TCODK_NONE,0}; SDL_PumpEvents(); if ( flush ) { while ( SDL_PollEvent(&ev) ) { TCOD_sys_SDLtoTCOD(&ev,0); } } do { SDL_WaitEvent(&ev); ret = TCOD_sys_SDLtoTCOD(&ev,TCOD_KEY_PRESSED); } while ( ret.vk == TCODK_NONE && ev.type != SDL_QUIT ); return ret; }
TCOD_event_t TCOD_sys_wait_for_event(int eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse, bool flush) { SDL_Event ev; TCOD_event_t retMask=0; if ( eventMask == 0 ) return 0; SDL_PumpEvents(); if ( flush ) { while ( SDL_PollEvent(&ev) ) { TCOD_sys_SDLtoTCOD(&ev,0); } } /* SDL_WaitEvent(&ev); */ tcod_mouse.lbutton_pressed =false; tcod_mouse.rbutton_pressed =false; tcod_mouse.mbutton_pressed =false; tcod_mouse.wheel_up=false; tcod_mouse.wheel_down=false; tcod_mouse.dx=0; tcod_mouse.dy=0; do { SDL_WaitEvent(&ev); retMask=TCOD_sys_handle_event(&ev,eventMask,key,&tcod_mouse); } while ( ev.type != SDL_QUIT && (retMask & TCOD_EVENT_KEY) == 0 ); if (mouse) { *mouse=tcod_mouse; } return retMask; }
TCOD_key_t TCOD_sys_check_for_keypress(int flags) { static TCOD_key_t noret={TCODK_NONE,0}; SDL_Event ev; SDL_PumpEvents(); while ( SDL_PollEvent(&ev) ) { TCOD_key_t tmpretkey=TCOD_sys_SDLtoTCOD(&ev, flags); if ( tmpretkey.vk != TCODK_NONE ) { return tmpretkey; } } return noret; }
static TCOD_event_t TCOD_sys_handle_event(SDL_Event *ev,TCOD_event_t eventMask, TCOD_key_t *key, TCOD_mouse_t *mouse) { TCOD_event_t retMask=0; switch(ev->type) { case SDL_KEYDOWN : { TCOD_key_t tmpKey=TCOD_sys_SDLtoTCOD(ev,TCOD_KEY_PRESSED); if ( (TCOD_EVENT_KEY_PRESS & eventMask) != 0) { retMask|=TCOD_EVENT_KEY_PRESS; if ( key ) *key = tmpKey; return retMask; } } break; case SDL_KEYUP : { TCOD_key_t tmpKey=TCOD_sys_SDLtoTCOD(ev,TCOD_KEY_RELEASED); if ( (TCOD_EVENT_KEY_RELEASE & eventMask) != 0) { retMask|=TCOD_EVENT_KEY_RELEASE; if ( key ) *key = tmpKey; return retMask; } } break; case SDL_MOUSEMOTION : if ( (TCOD_EVENT_MOUSE_MOVE & eventMask) != 0) { SDL_MouseMotionEvent *mme=&ev->motion; int charWidth, charHeight; /*SDL_GetMouseState(&mouse->x,&mouse->y);*/ /*SDL_GetRelativeMouseState(&mouse->dx,&mouse->dy);*/ retMask|=TCOD_EVENT_MOUSE_MOVE; mouse->dx += mme->xrel; mouse->dy += mme->yrel; mouse->x=mme->x; mouse->y=mme->y; TCOD_sys_get_char_size(&charWidth,&charHeight); mouse->cx = (mouse->x - TCOD_ctx.fullscreen_offsetx) / charWidth; mouse->cy = (mouse->y - TCOD_ctx.fullscreen_offsety) / charHeight; mouse->dcx = mouse->dx / charWidth; mouse->dcy = mouse->dy / charHeight; return retMask; } break; case SDL_MOUSEBUTTONDOWN : if ( (TCOD_EVENT_MOUSE_PRESS & eventMask) != 0) { SDL_MouseButtonEvent *mev=&ev->button; retMask|=TCOD_EVENT_MOUSE_PRESS; switch (mev->button) { case SDL_BUTTON_LEFT : mouse->lbutton=mousebl=true; break; case SDL_BUTTON_MIDDLE : mouse->mbutton=mousebm=true; break; case SDL_BUTTON_RIGHT : mouse->rbutton=mousebr=true; break; case SDL_BUTTON_WHEELUP : mouse->wheel_up=true; break; case SDL_BUTTON_WHEELDOWN : mouse->wheel_down=true;break; } return retMask; } break; case SDL_MOUSEBUTTONUP : if ( (TCOD_EVENT_MOUSE_RELEASE & eventMask) != 0) { SDL_MouseButtonEvent *mev=&ev->button; retMask|=TCOD_EVENT_MOUSE_RELEASE; switch (mev->button) { case SDL_BUTTON_LEFT : if (mousebl) mouse->lbutton_pressed = mouse_force_bl=true; mouse->lbutton = mousebl=false; break; case SDL_BUTTON_MIDDLE : if (mousebm) mouse->mbutton_pressed = mouse_force_bm=true; mouse->mbutton = mousebm=false; break; case SDL_BUTTON_RIGHT : if (mousebr) mouse->rbutton_pressed = mouse_force_br=true; mouse->rbutton = mousebr=false; break; } return retMask; } break; case SDL_QUIT : TCOD_console_set_window_closed(); break; case SDL_VIDEOEXPOSE : TCOD_sys_console_to_bitmap(screen,TCOD_console_get_width(NULL),TCOD_console_get_height(NULL),consoleBuffer,prevConsoleBuffer); break; default : break; } return retMask; }