void event_poll(Uint32 mask) { SDL_Event event; #if TO_EVENT_POLL time_t t0 = SDL_GetTicks (); time_t _t = t0; #endif /* if (!mask) mask = SDL_ALLEVENTS; else mask |= SDL_ACTIVEEVENTMASK | SDL_QUITMASK | SDL_SYSWMEVENTMASK; while (PollEvent(&event, mask)) { */ while (SDL_PollEvent (&event)) { #if TO_EVENT_POLL if (!gameOpts->legacy.bInput) _t = SDL_GetTicks (); #endif switch(event.type) { case SDL_KEYDOWN: key_handler((SDL_KeyboardEvent *)&event); break; case SDL_KEYUP: key_handler((SDL_KeyboardEvent *)&event); break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: mouse_button_handler((SDL_MouseButtonEvent *)&event); break; case SDL_MOUSEMOTION: mouse_motion_handler((SDL_MouseMotionEvent *)&event); break; #ifndef USE_LINUX_JOY // stpohle - so we can choose at compile time.. case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: joy_button_handler((SDL_JoyButtonEvent *)&event); break; case SDL_JOYAXISMOTION: joy_axis_handler((SDL_JoyAxisEvent *)&event); break; case SDL_JOYHATMOTION: joy_hat_handler((SDL_JoyHatEvent *)&event); break; case SDL_JOYBALLMOTION: break; #endif case SDL_QUIT: { quit_request(); } break; } #if TO_EVENT_POLL if (!gameOpts->legacy.bInput && (_t - t0 >= TO_EVENT_POLL)) break; #endif } }
/************************************************************************** SDL-client main loop. **************************************************************************/ Uint16 gui_event_loop(void *pData, void (*loop_action)(void *pData), Uint16 (*key_down_handler)(SDL_keysym Key, void *pData), Uint16 (*key_up_handler)(SDL_keysym Key, void *pData), Uint16 (*mouse_button_down_handler)(SDL_MouseButtonEvent *pButtonEvent, void *pData), Uint16 (*mouse_button_up_handler)(SDL_MouseButtonEvent *pButtonEvent, void *pData), Uint16 (*mouse_motion_handler)(SDL_MouseMotionEvent *pMotionEvent, void *pData)) { Uint16 ID; static struct timeval tv; static fd_set civfdset; Uint32 t_current, t_last_unit_anim, t_last_map_scrolling; Uint32 real_timer_next_call; static int result, schot_nr = 0; static char schot[32]; ID = ID_ERROR; t_last_map_scrolling = t_last_unit_anim = real_timer_next_call = SDL_GetTicks(); while (ID == ID_ERROR) { /* ========================================= */ /* net check with 10ms delay event loop */ if ((net_socket >= 0) || (ggz_socket >= 0)) { FD_ZERO(&civfdset); if (net_socket >= 0) { FD_SET(net_socket, &civfdset); } if (ggz_socket >= 0) { FD_SET(ggz_socket, &civfdset); } tv.tv_sec = 0; tv.tv_usec = 10000;/* 10ms*/ result = fc_select(MAX(net_socket, ggz_socket) + 1, &civfdset, NULL, NULL, &tv); if (result < 0) { if (errno != EINTR) { break; } else { continue; } } else { if (result > 0) { if ((net_socket >= 0) && FD_ISSET(net_socket, &civfdset)) { SDL_PushEvent(pNet_User_Event); } if ((ggz_socket >= 0) && FD_ISSET(ggz_socket, &civfdset)) { SDL_PushEvent(pGGZ_User_Event); } } } } else { /* if connection is not establish */ SDL_Delay(10); } /* ========================================= */ t_current = SDL_GetTicks(); if (t_current > real_timer_next_call) { real_timer_next_call = t_current + (real_timer_callback() * 1000); } if ((t_current - t_last_unit_anim) > UNITS_TIMER_INTERVAL) { if (autoconnect) { widget_info_counter++; SDL_PushEvent(pAnim_User_Event); } else { SDL_PushEvent(pAnim_User_Event); } t_last_unit_anim = SDL_GetTicks(); } if (is_map_scrolling) { if ((t_current - t_last_map_scrolling) > MAP_SCROLL_TIMER_INTERVAL) { SDL_PushEvent(pMap_Scroll_User_Event); t_last_map_scrolling = SDL_GetTicks(); } } else { t_last_map_scrolling = SDL_GetTicks(); } if (widget_info_counter > 0) { SDL_PushEvent(pInfo_User_Event); widget_info_counter = 0; } /* ========================================= */ if (loop_action) { loop_action(pData); } /* ========================================= */ while (SDL_PollEvent(&Main.event) == 1) { switch (Main.event.type) { case SDL_QUIT: return MAX_ID; break; case SDL_KEYUP: switch (Main.event.key.keysym.sym) { /* find if Shifts are released */ case SDLK_RSHIFT: RSHIFT = FALSE; break; case SDLK_LSHIFT: LSHIFT = FALSE; break; case SDLK_LCTRL: LCTRL = FALSE; break; case SDLK_RCTRL: RCTRL = FALSE; break; case SDLK_LALT: LALT = FALSE; break; default: if(key_up_handler) { ID = key_up_handler(Main.event.key.keysym, pData); } break; } break; case SDL_KEYDOWN: switch(Main.event.key.keysym.sym) { case SDLK_PRINT: fc_snprintf(schot, sizeof(schot), "fc_%05d.bmp", schot_nr++); log_normal(_("Making screenshot %s"), schot); SDL_SaveBMP(Main.screen, schot); break; case SDLK_RSHIFT: /* Right Shift is Pressed */ RSHIFT = TRUE; break; case SDLK_LSHIFT: /* Left Shift is Pressed */ LSHIFT = TRUE; break; case SDLK_LCTRL: /* Left CTRL is Pressed */ LCTRL = TRUE; break; case SDLK_RCTRL: /* Right CTRL is Pressed */ RCTRL = TRUE; break; case SDLK_LALT: /* Left ALT is Pressed */ LALT = TRUE; break; default: if(key_down_handler) { ID = key_down_handler(Main.event.key.keysym, pData); } break; } break; case SDL_MOUSEBUTTONDOWN: if(mouse_button_down_handler) { ID = mouse_button_down_handler(&Main.event.button, pData); } break; case SDL_MOUSEBUTTONUP: if(mouse_button_up_handler) { ID = mouse_button_up_handler(&Main.event.button, pData); } break; case SDL_MOUSEMOTION: if(mouse_motion_handler) { ID = mouse_motion_handler(&Main.event.motion, pData); } break; case SDL_USEREVENT: switch(Main.event.user.code) { case NET: input_from_server(net_socket); break; case GGZ: input_from_ggz(ggz_socket); break; case ANIM: update_button_hold_state(); animate_mouse_cursor(); draw_mouse_cursor(); break; case SHOW_WIDGET_INFO_LABBEL: draw_widget_info_label(); break; case TRY_AUTO_CONNECT: if (try_to_autoconnect()) { pInfo_User_Event->user.code = SHOW_WIDGET_INFO_LABBEL; autoconnect = FALSE; } break; case FLUSH: unqueue_flush(); break; case MAP_SCROLL: scroll_mapview(scroll_dir); break; case EXIT_FROM_EVENT_LOOP: return MAX_ID; break; default: break; } break; } } if (ID == ID_ERROR) { if (callbacks && callback_list_size(callbacks) > 0) { struct callback *cb = callback_list_get(callbacks, 0); callback_list_remove(callbacks, cb); (cb->callback)(cb->data); free(cb); } } } return ID; }
void process_events() // Check and respond to any events in the event queue. { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: { // char* keyname = SDL_GetKeyName(event.key.keysym.sym); // printf("The %s key was pressed!\n", keyname); int key = event.key.keysym.sym; if (key == SDLK_q || key == SDLK_ESCAPE) { // Quit on 'q'. exit(0); } if (key == SDLK_w) { // Toggle wireframe mode. wireframe_mode = !wireframe_mode; printf("wireframe %s\n", wireframe_mode ? "on" : "off"); } if (key == SDLK_b) { // Cycle through rendering of { geo, geo + boxes, boxes, nothing } if ( render_opt.show_geometry ) { if ( render_opt.show_box ) { render_opt.show_geometry = false; } else { render_opt.show_box = true; } } else { if ( render_opt.show_box ) { render_opt.show_box = false; } else { render_opt.show_geometry = true; } } printf("geo %s, box %s\n", render_opt.show_geometry ? "on" : "off", render_opt.show_box ? "on" : "off"); } if (key == SDLK_m) { // Toggle rendering of vertex morphing. render_opt.morph = !render_opt.morph; printf("morphing %s\n", render_opt.morph ? "on" : "off"); } if (key == SDLK_u) { // Toggle LOD update. Useful for inspecting low LOD meshes up close. enable_update = ! enable_update; printf("LOD update %s\n", enable_update ? "on" : "off"); } if (key == SDLK_f) { // Toggle viewpoint motion. move_forward = ! move_forward; printf("forward motion %s\n", move_forward ? "on" : "off"); } if (key == SDLK_LEFTBRACKET || key == SDLK_KP_MINUS) { // Decrease max pixel error. max_pixel_error -= 0.5; if (max_pixel_error < 0.5) { max_pixel_error = 0.5; } printf("new max_pixel_error = %f\n", max_pixel_error); } if (key == SDLK_RIGHTBRACKET || key == SDLK_KP_PLUS) { //Increase max pixel error. max_pixel_error += 0.5; printf("new max_pixel_error = %f\n", max_pixel_error); } if (key == SDLK_PERIOD || key == SDLK_g) { // Speed up. speed++; if (speed > 15) speed = 15; printf( "move speed = %d\n", speed ); } if (key == SDLK_COMMA || key == SDLK_d) { // Slow down. speed--; if (speed < 1) speed = 1; printf( "move speed = %d\n", speed ); } if (key == SDLK_p) { // Toggle performance monitor. measure_performance = ! measure_performance; if ( measure_performance ) { // Reset counters & timer. frame_count = 0; total_triangle_count = frame_triangle_count = 0; performance_timer = 0; } printf("perf monitor %s\n", measure_performance ? "on" : "off"); } if (key == SDLK_2) { s_two_phase_render = ! s_two_phase_render; printf("two-phase rendering %s\n", s_two_phase_render ? "on" : "off"); } if (key == SDLK_t) { // Toggle background loader thread... s_use_loader_thread = ! s_use_loader_thread; printf("loader thread %s\n", s_use_loader_thread ? "on" : "off"); } if (key == SDLK_QUESTION || key == SDLK_F1 || key == SDLK_h) { print_usage(); } break; } case SDL_MOUSEMOTION: { mouse_motion_handler(event.motion.xrel, event.motion.yrel, event.motion.state); break; } case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: { // mouse_button_handler(event.button.button, event.button.state == SDL_PRESSED ? true : false); break; } case SDL_QUIT: exit(0); } } }
void event_poll() { SDL_Event event; int clean_uniframe=1; window *wind = window_get_front(); int idle = 1; // If the front window changes, exit this loop, otherwise unintended behavior can occur // like pressing 'Return' really fast at 'Difficulty Level' causing multiple games to be started while ((wind == window_get_front()) && SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: case SDL_KEYUP: if (clean_uniframe) memset(unicode_frame_buffer,'\0',sizeof(unsigned char)*KEY_BUFFER_SIZE); clean_uniframe=0; key_handler((SDL_KeyboardEvent *)&event); idle = 0; break; case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: mouse_button_handler((SDL_MouseButtonEvent *)&event); idle = 0; break; case SDL_MOUSEMOTION: mouse_motion_handler((SDL_MouseMotionEvent *)&event); idle = 0; break; case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: joy_button_handler((SDL_JoyButtonEvent *)&event); idle = 0; break; case SDL_JOYAXISMOTION: if (joy_axis_handler((SDL_JoyAxisEvent *)&event)) idle = 0; break; case SDL_JOYHATMOTION: joy_hat_handler((SDL_JoyHatEvent *)&event); idle = 0; break; case SDL_JOYBALLMOTION: break; case SDL_QUIT: { d_event qevent = { EVENT_QUIT }; call_default_handler(&qevent); idle = 0; } break; } } // Send the idle event if there were no other events if (idle) { d_event ievent; ievent.type = EVENT_IDLE; event_send(&ievent); } else event_reset_idle_seconds(); mouse_cursor_autohide(); }