void analog_test(void){ joystick_set_new_center(); JOY_pos_t joy_pos; SLI_pos_t sli_pos; while(1){ sli_pos = slider_position(); printf_P(PSTR("Slider position: (Left:%d, Right:%d)\n"), sli_pos.L, sli_pos.R); printf_P(PSTR("Slider buttons: (%s, %s)\n"), (slider_left_button() ? "LEFT" : "Left"), (slider_right_button() ? "RIGHT" : "Right") ); joy_pos = joystick_position(); printf_P(PSTR("pos_x: %4d | pos_y: %4d\n"), joy_pos.x, joy_pos.y); printf_P(PSTR("Joy dirn: %d\n"), joystick_direction()); printf_P(PSTR("Joy button: %d\n\n\n"), joystick_button()); _delay_ms(1000); } }
void EventHandler::handle_events() { SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_VIDEORESIZE: resize_window(event.resize.w, event.resize.h); break; case SDL_JOYAXISMOTION: joystick_motion(&event.jaxis); break; case SDL_JOYBUTTONDOWN: if (nJoystickDlg) joystickDlg->joystickDlgButton(&event.jbutton); else joystick_button(&event.jbutton); break; case SDL_MOUSEMOTION: /** * GUI_MOUSE_MOTION_WORKAROUND * The GUI sometimes returns that it used the event, although it is not visible anymore! * Therefore we need to check whether it is visible at all. * Where it works: * -ESC to show GUI * -open dialog (for example video) * -close dialog * -ESC to hide GUI * Everything is fine, the mouse-motions are not used by the GUI. * -ESC to show GUI * -toggle something from the menu bar (for example Verbosity) * -ESC to hide GUI * Now the GUI still uses the mouse motions, although it is invisible! */ if (Global::TXInterface->inputMethod() == T_TX_Interface::eIM_mouse) { // always update the mouse interface mouse_motion(&event.motion); if (Global::gui) Global::gui->mouseMotionHandler(event.motion.x, event.motion.y); } else if (!Global::gui || !Global::gui->isVisible() || !Global::gui->mouseMotionHandler(event.motion.x, event.motion.y)) { // GUI did not use the event, pass it to the simulation itself mouse_motion(&event.motion); } break; case SDL_MOUSEBUTTONDOWN: #if TEST_WITHOUT_JOYSTICK > 0 if (nJoystickDlg && event.button.button != SDL_BUTTON_LEFT) { SDL_JoyButtonEvent jevent; jevent.type = SDL_JOYBUTTONDOWN; jevent.state = SDL_PRESSED; jevent.which = 0; switch (event.button.button) { case SDL_BUTTON_MIDDLE: jevent.button = 0; break; case SDL_BUTTON_RIGHT: jevent.button = 1; break; default: jevent.button = 2; break; } joystickDlg->joystickDlgButton(&jevent); } #endif if (!Global::gui || !Global::gui->mouseButtonDownHandler(event.button.button, event.button.x, event.button.y)) { // GUI did not use the event, pass it to the simulation itself mouse_button(&event.button); } break; case SDL_MOUSEBUTTONUP: if (Global::gui) Global::gui->mouseButtonUpHandler(event.button.button, event.button.x, event.button.y); break; case SDL_KEYDOWN: if (!Global::gui || !Global::gui->keyDownEventHandler(event.key.keysym)) { // GUI did not use the event, pass it to the simulation itself key_down(&event.key.keysym); } break; case SDL_KEYUP: if (Global::gui) Global::gui->keyUpEventHandler(event.key.keysym); break; case SDL_QUIT: if (Global::gui) Global::gui->doQuitDialog(); else Global::Simulation->quit(); break; } } }
/* Intro screen eventloop */ static int intro_event_loop (void) { Uint8 needupdate, rval, isevent; SDL_Event Event; Sint8 command; Uint32 delay, lasttime; while (1) { if (game_settings.mbg_anim) { needupdate = 1; isevent = SDL_PollEvent (&Event); } else { needupdate = 0; isevent = 1; if (!SDL_WaitEvent (&Event)) { printf ("Error occured while waiting for an event: %s\n", SDL_GetError ()); exit (1); } } if (isevent) switch (Event.type) { case SDL_KEYDOWN: command = -1; if (intr_message.show) { intr_message.show = 0; if (intr_message.setkey) { *intr_message.setkey = Event.key.keysym.sym; intr_message.setkey = NULL; return 0; } needupdate = 1; } else { if (Event.key.keysym.sym == SDLK_F11) screenshot (); else if (Event.key.keysym.sym == SDLK_UP) command = MNU_UP; else if (Event.key.keysym.sym == SDLK_DOWN) command = MNU_DOWN; else if (Event.key.keysym.sym == SDLK_LEFT) command = MNU_LEFT; else if (Event.key.keysym.sym == SDLK_RIGHT) command = MNU_RIGHT; else if (Event.key.keysym.sym == SDLK_RETURN) { if((Event.key.keysym.mod & (KMOD_LALT|KMOD_RALT))) toggle_fullscreen(); else command = MNU_ENTER; } else if (Event.key.keysym.sym == SDLK_ESCAPE) command = MNU_BACK; } if (command >= 0) { if (command != MNU_ENTER && command != MNU_BACK) playwave (WAV_BLIP); else playwave (WAV_BLIP2); rval = menu_control (&intro_menu, command); if (rval == INTRO_RVAL_STARTGAME || rval == INTRO_RVAL_EXIT) return rval; needupdate = 1; } break; case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: joystick_button (&Event.jbutton); break; case SDL_JOYAXISMOTION: joystick_motion (&Event.jaxis, 0); break; case SDL_QUIT: exit(0); default: break; } if (!(game_settings.mbg_anim && isevent)) { lasttime = SDL_GetTicks (); if (needupdate) draw_intro_screen (); if (game_settings.mbg_anim) { delay = SDL_GetTicks () - lasttime; if (delay >= 60) delay = 0; else delay = 60 - delay; SDL_Delay (delay); } } } /* Never reached */ return 0; }
/* or 0 if selection was cancelled. */ int select_weapon(struct LevelFile *level) { int i; memset(screen->pixels,0,screen->pitch*screen->h); draw_header(screen,level->settings->mainblock.name); for(i=0;i<4;i++) if(players[i].state != INACTIVE) draw_weapon_bar(screen,i); SDL_UpdateRect(screen,0,0,0,0); while(1) { SDL_Event event; SDL_WaitEvent(&event); /* Handle event */ if(event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_F11) screenshot (); else if(event.key.keysym.sym == SDLK_RETURN) { if((event.key.keysym.mod & (KMOD_LALT|KMOD_RALT))) toggle_fullscreen(); else return 1; } else if(event.key.keysym.sym == SDLK_ESCAPE) break; else for(i=0;i<4;i++) { int update = 0; if(players[i].state == INACTIVE) continue; if(event.key.keysym.sym == game_settings.controller[i].keys[2]) { players[i].specialWeapon--; update=1; } else if (event.key.keysym.sym == game_settings.controller[i].keys[3]) { players[i].specialWeapon++; update=1; } else if (event.key.keysym.sym == game_settings.controller[i].keys[1]) { players[i].standardWeapon--; update=1; } else if (event.key.keysym.sym == game_settings.controller[i].keys[0]) { players[i].standardWeapon++; update=1; } if(update) { SDL_Rect rect; playwave(WAV_BLIP); if ((int)players[i].specialWeapon < 0) players[i].specialWeapon = special_weapon_count() - 1; else if (players[i].specialWeapon == special_weapon_count() ) players[i].specialWeapon = 0; if ((int) players[i].standardWeapon < 0) players[i].standardWeapon = normal_weapon_count() - 1; else if ((int) players[i].standardWeapon == normal_weapon_count()) players[i].standardWeapon = 0; rect = draw_weapon_bar(screen,i); SDL_UpdateRect(screen,rect.x,rect.y,rect.w,rect.h); } } } else if(event.type == SDL_JOYBUTTONDOWN) joystick_button(&event.jbutton); else if(event.type == SDL_JOYAXISMOTION) joystick_motion(&event.jaxis,1); else if(event.type == SDL_QUIT) exit(0); } return 0; }
/* Level selection screen. Returns the selected level or NULL if cancelled */ struct LevelFile *select_level(int fade) { struct dllist *levels; struct LevelFile *selection=NULL; SDL_Event event; int loop=1,animate=0; Uint32 lasttime=0; char roundstr[32]; levels = randomize_level(get_level_thumbnails()); /* Draw the screen */ sprintf(roundstr,"Round %d of %d",game_status.total_rounds + 1, game_settings.rounds + game_status.total_rounds); if(luola_options.mbg_anim && fade) { SDL_Surface *tmp = make_surface(screen,0,0); draw_header(tmp,roundstr); draw_level_bar(tmp,levels,0); fade_from_black(tmp); SDL_FreeSurface(tmp); } else { memset(screen->pixels,0,screen->pitch*screen->h); draw_header(screen,roundstr); draw_level_bar(screen,levels,0); SDL_UpdateRect(screen,0,0,0,0); } /* Level selection event loop */ while(loop) { enum {DO_NOTHING,MOVE_LEFT,MOVE_RIGHT,CHOOSE,CANCEL} cmd = DO_NOTHING; if(animate) { if(SDL_PollEvent(&event)==0) event.type = SDL_NOEVENT; } else { SDL_WaitEvent(&event); } /* Handle event */ switch(event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_F11) screenshot (); else if (event.key.keysym.sym == SDLK_LEFT) cmd = MOVE_LEFT; else if (event.key.keysym.sym == SDLK_RIGHT) cmd = MOVE_RIGHT; else if (event.key.keysym.sym == SDLK_ESCAPE) cmd = CANCEL; else if(event.key.keysym.sym == SDLK_RETURN) { if((event.key.keysym.mod & (KMOD_LALT|KMOD_RALT))) toggle_fullscreen(); else cmd = CHOOSE; } break; case SDL_JOYBUTTONUP: case SDL_JOYBUTTONDOWN: joystick_button (&event.jbutton); break; case SDL_JOYAXISMOTION: joystick_motion (&event.jaxis, 0); break; case SDL_QUIT: exit(0); default: break; } /* Handle action */ switch(cmd) { case DO_NOTHING: break; case MOVE_LEFT: if(levels->prev) { if(luola_options.mbg_anim) animate -= level_width(levels->data) + THUMBNAIL_SEP; levels = levels->prev; } else cmd = DO_NOTHING; break; case MOVE_RIGHT: if(levels->next) { levels = levels->next; if(luola_options.mbg_anim) animate += level_width(levels->data) + THUMBNAIL_SEP; } else cmd = DO_NOTHING; break; case CHOOSE: selection = ((struct LevelThumbnail*)levels->data)->file; loop=0; break; case CANCEL: selection = NULL; loop=0; break; } if(cmd == MOVE_LEFT || cmd==MOVE_RIGHT || animate) { if(animate!=0) { int delta=abs(animate)/16+1; lasttime = SDL_GetTicks(); if(animate<0) animate += delta; else animate -= delta; } draw_level_bar(screen,levels,animate); SDL_UpdateRect(screen,0,screen->h/2 - BAR_HEIGHT/2, screen->w,BAR_HEIGHT); } if (animate) { Uint32 delay = SDL_GetTicks () - lasttime; if (delay >= GAME_SPEED) delay = 0; else delay = GAME_SPEED - delay; SDL_Delay (delay); } } dllist_free(levels,free_level_thumbnail); return selection; }