void process_controller_events(gl_t *state, int controller_fd) { struct input_event events[10]; int bytes, i, length; // Read in events bytes = read(controller_fd, events, sizeof(events)); if(bytes > 0) { length = bytes/sizeof(struct input_event); // Process events based on type for(i=0; i<length; i++) { switch(events[i].type) { case EV_KEY: handle_key(state, &events[i]); break; case EV_ABS: handle_joystick(state, &events[i]); break; case EV_REL: handle_mouse(state, &events[i]); break; } } } }
int main() { srand(time(NULL)); aspect::GLWindow window(XRES, YRES); glfwMakeContextCurrent(window.get_window()); glfwSetKeyCallback(window.get_window(), key_callback); glfwSetInputMode(window.get_window(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetCursorPos(window.get_window(), XRES/2, YRES/2); gs.camera.set_position(glm::vec3(+0.0f, +10.0f, +0.0f)); double currentframe = glfwGetTime(); double lastframe = currentframe; aspect::GLProgram monkey_program("shaders/vshader-nm.glsl", "shaders/fshader-nm.glsl"); aspect::Mesh monkey_mesh("models/monkey.dae"); aspect::ModelAsset monkey_asset(&monkey_mesh, &monkey_program); aspect::ModelInstance monkey(&monkey_asset); monkey.offset_position(glm::vec3(40, 6, 60)); monkey.update(); gs.model_instances.push_back(&monkey); aspect::GLProgram chunk_program("shaders/vshader-tex-light.glsl", "shaders/fshader-tex-light.glsl"); for(int x = 0; x < 16; x++) { for(int y = 0; y < 1; y++) { for(int z = 0; z < 16; z++) { aspect::CubeChunk *chunk = new aspect::CubeChunk(&chunk_program, x*CCOUNTX, y*CCOUNTY, z*CCOUNTZ); chunk->update(); gs.cube_chunks.push_back(chunk); } } } glClearColor(0.20, 0.6, 0.7, 0.00); while(!glfwWindowShouldClose(window.get_window())) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); for_each(begin(gs.model_instances), end(gs.model_instances), [](aspect::ModelInstance *instance) { instance->draw(gs.camera.matrix()); walk(instance); }); for_each(begin(gs.cube_chunks), end(gs.cube_chunks), [](aspect::CubeChunk *chunk) { chunk->draw(gs.camera.matrix()); }); handle_mouse(window.get_window()); update_fps_counter(window.get_window()); window.update_fps_counter(); glfwSwapBuffers(window.get_window()); glfwPollEvents(); currentframe = glfwGetTime(); gs.delta_time = currentframe - lastframe; lastframe = currentframe; } return 0; }
int main(int argc, char *argv[]) { int gui_done = 0; scroll_count = 800; selected_menu = ROMS; alpha_up = 1; alpha = 0; mouse_y_pos = 120; selected_option_item = 0; if(!cfg_read("config.txt")) { printf("Fail => cfg_read()\n"); exit(0); } if(!gui_init_sdl()) { printf("Fail => gui_init_sdl()\n"); exit(0); } emu_config_init(); gui_load(); strncpy(now_path, config->roms_path, MAXPATH); filer_read_dir(now_path); SDL_initFramerate( &sixteen ); SDL_setFramerate( &sixteen, 65 ); while(!gui_done) { handle_mouse(); while( SDL_PollEvent( &event ) ) { switch(event.type) { case SDL_QUIT: { gui_clean(); free(emu); exit(0); } break; } } gui_draw(); SDL_Flip(myscreen); SDL_framerateDelay( &sixteen ); } return 0; }
void AllegroShell::run(){ // run the loop unsigned num = 0; ALLEGRO_EVENT ev; al_start_timer(timer); unsigned keyboard_count = 0; unsigned mouse_count = 0; unsigned timer_count = 0; while( run_flag ){ al_wait_for_event(queue,&ev); num++; printf("\r%d",num); if( ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){ run_flag = false; }else if( isKeyboardEvent(&ev)){ // handle keyboard input ++keyboard_count; al_get_keyboard_state(&(keyboard)); handle_keyboard(&ev); }else if( isMouseEvent(&ev) ){ // handle mouse input ++mouse_count; mouse->update(&ev); handle_mouse(&ev); }else if( ev.type == ALLEGRO_EVENT_TIMER){ ++timer_count; // Update the model only if we haven't drawn the scene if(draw_flag == false){ if(step_once_flag){ // always step once if the user requests it model->step(); step_once_flag = false; }else if(step_flag) { model->step(); } } draw_flag = true; }else if (ev.type == USER_VIEW_EVENT){ // handling user events al_unref_user_event(&ev.user); } if( draw_flag && al_event_queue_is_empty(queue)){ draw(); draw_flag = false; } } // end while(run_flag) Textlog::get().log("\nkeyboard_count = %u\n",keyboard_count); Textlog::get().log("mouse_count = %u\n",mouse_count); Textlog::get().log("timer_count = %u\n",timer_count); }
bool FBProcessEvent(bool *quit) { if(pend) { if(pend & 1) handle_mouse(); if(pend & 2) handle_keyboard(); pend = 0; //need to reset, since with repeated call is not updated here, would stuck return true; } return false; }
void enable_mouse(void) { int h = get_output_handle(); /* XXX: Is this all right? -- Miciah */ if (mouse_enabled) return; if (is_xterm()) send_mouse_init_sequence(h); ditrm->mouse_h = handle_mouse(0, (void (*)(void *, unsigned char *, int)) itrm_queue_event, ditrm); mouse_enabled = 1; }
// Check if a USB mouse event is pending and process it if so. static void usb_check_mouse(void) { if (! CONFIG_USB_MOUSE) return; struct usb_pipe *pipe = GET_GLOBAL(mouse_pipe); if (!pipe) return; for (;;) { struct mouseevent data; int ret = usb_poll_intr(pipe, &data); if (ret) break; handle_mouse(&data); } }
void Client::handle_events() { SDL_Event event; while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_MOUSEMOTION: handle_mouse(&event); break; case SDL_KEYDOWN: input->set_key_down(&event.key.keysym.sym); break; case SDL_KEYUP: input->set_key_up(&event.key.keysym.sym); break; default: break; } } }
// // get_event() // Get and handle waiting events // void event_handler::get_event(event &ev) { event *ep; while (!ewaiting) { event_waiting(); if (!ewaiting) { // Sleep for 1 millisecond if there are no events Timer tmp; tmp.WaitMs(1); } } ep = (event *) events.first(); if (ep) { ev = *ep; events.unlink(ep); delete ep; ewaiting = events.first() != NULL; } else { // NOTE : that the mouse status should be known // even if another event has occurred. ev.mouse_move.x = mouse->x(); ev.mouse_move.y = mouse->y(); ev.mouse_button = mouse->button(); // Gather events SDL_Event event; if (SDL_PollEvent(&event)) { // always sort the mouse out handle_mouse(ev); mouse->update(ev.mouse_move.x, ev.mouse_move.y, ev.mouse_button); switch (event.type) { case SDL_QUIT: { exit(0); break; } case SDL_MOUSEBUTTONUP: { switch (event.button.button) { case 4: // Mouse wheel goes up... { ev.key = get_key_binding("b4", 0); ev.type = EV_KEYRELEASE; break; } case 5: // Mouse wheel goes down... { ev.key = get_key_binding("b3", 0); ev.type = EV_KEYRELEASE; break; } } break; } case SDL_MOUSEBUTTONDOWN: { switch (event.button.button) { case 4: // Mouse wheel goes up... { ev.key = get_key_binding("b4", 0); ev.type = EV_KEY; break; } case 5: // Mouse wheel goes down... { ev.key = get_key_binding("b3", 0); ev.type = EV_KEY; break; } } break; } case SDL_KEYDOWN: case SDL_KEYUP: { // Default to EV_SPURIOUS ev.key = EV_SPURIOUS; if (event.type == SDL_KEYDOWN) { ev.type = EV_KEY; } else { ev.type = EV_KEYRELEASE; } switch (event.key.keysym.sym) { case SDLK_DOWN: ev.key = JK_DOWN; break; case SDLK_UP: ev.key = JK_UP; break; case SDLK_LEFT: ev.key = JK_LEFT; break; case SDLK_RIGHT: ev.key = JK_RIGHT; break; case SDLK_LCTRL: ev.key = JK_CTRL_L; break; case SDLK_RCTRL: ev.key = JK_CTRL_R; break; case SDLK_LALT: ev.key = JK_ALT_L; break; case SDLK_RALT: ev.key = JK_ALT_R; break; case SDLK_LSHIFT: ev.key = JK_SHIFT_L; break; case SDLK_RSHIFT: ev.key = JK_SHIFT_R; break; case SDLK_NUMLOCK: ev.key = JK_NUM_LOCK; break; case SDLK_HOME: ev.key = JK_HOME; break; case SDLK_END: ev.key = JK_END; break; case SDLK_BACKSPACE: ev.key = JK_BACKSPACE; break; case SDLK_TAB: ev.key = JK_TAB; break; case SDLK_RETURN: ev.key = JK_ENTER; break; case SDLK_SPACE: ev.key = JK_SPACE; break; case SDLK_CAPSLOCK: ev.key = JK_CAPS; break; case SDLK_ESCAPE: ev.key = JK_ESC; break; case SDLK_F1: ev.key = JK_F1; break; case SDLK_F2: ev.key = JK_F2; break; case SDLK_F3: ev.key = JK_F3; break; case SDLK_F4: ev.key = JK_F4; break; case SDLK_F5: ev.key = JK_F5; break; case SDLK_F6: ev.key = JK_F6; break; case SDLK_F7: ev.key = JK_F7; break; case SDLK_F8: ev.key = JK_F8; break; case SDLK_F9: ev.key = JK_F9; break; case SDLK_F10: ev.key = JK_F10; break; case SDLK_INSERT: ev.key = JK_INSERT; break; case SDLK_KP0: ev.key = JK_INSERT; break; case SDLK_PAGEUP: ev.key = JK_PAGEUP; break; case SDLK_PAGEDOWN: ev.key = JK_PAGEDOWN; break; case SDLK_KP8: ev.key = JK_UP; break; case SDLK_KP2: ev.key = JK_DOWN; break; case SDLK_KP4: ev.key = JK_LEFT; break; case SDLK_KP6: ev.key = JK_RIGHT; break; case SDLK_F11: { // Only handle key down if (ev.type == EV_KEY) { // Toggle fullscreen SDL_WM_ToggleFullScreen(SDL_GetVideoSurface()); } ev.key = EV_SPURIOUS; break; } case SDLK_F12: { // Only handle key down if (ev.type == EV_KEY) { // Toggle grab mouse if (SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON) { the_game->show_help("Grab Mouse: OFF\n"); SDL_WM_GrabInput (SDL_GRAB_OFF); } else { the_game->show_help("Grab Mouse: ON\n"); SDL_WM_GrabInput (SDL_GRAB_ON); } } ev.key = EV_SPURIOUS; break; } case SDLK_PRINT: // print-screen key { // Only handle key down if (ev.type == EV_KEY) { // Grab a screenshot SDL_SaveBMP(SDL_GetVideoSurface(), "screenshot.bmp"); the_game->show_help( "Screenshot saved to: screenshot.bmp.\n"); } ev.key = EV_SPURIOUS; break; } default: { ev.key = (int) event.key.keysym.sym; // Need to handle the case of shift being pressed // There has to be a better way if ((event.key.keysym.mod & KMOD_SHIFT) != 0) { if (event.key.keysym.sym >= SDLK_a && event.key.keysym.sym <= SDLK_z) { ev.key -= 32; } else if (event.key.keysym.sym >= SDLK_1 && event.key.keysym.sym <= SDLK_5) { ev.key -= 16; } else { switch (event.key.keysym.sym) { case SDLK_6: ev.key = SDLK_CARET; break; case SDLK_7: case SDLK_9: case SDLK_0: ev.key -= 17; break; case SDLK_8: ev.key = SDLK_ASTERISK; break; case SDLK_MINUS: ev.key = SDLK_UNDERSCORE; break; case SDLK_EQUALS: ev.key = SDLK_PLUS; break; case SDLK_COMMA: ev.key = SDLK_LESS; break; case SDLK_PERIOD: ev.key = SDLK_GREATER; break; case SDLK_SLASH: ev.key = SDLK_QUESTION; break; case SDLK_SEMICOLON: ev.key = SDLK_COLON; break; case SDLK_QUOTE: ev.key = SDLK_QUOTEDBL; break; default: break; } } } } } break; } } } // No more events ewaiting = 0; } }
void handle_mouse_helper(int x, int y){ handle_mouse(x, y, mouse_speed, &cameraLocation, &lookAtPoint, &upVector); }
int main(int argc, char** argv) { if(argc != 2) usage(argv[0]); FILE* infile = fopen(argv[1], "r"); if(infile == NULL) { perror("Error opening input file"); return EXIT_FAILURE; } System* sys = load_system(infile); if(sys == NULL) { printf("Loading input file failed\n"); return EXIT_FAILURE; } init_simulation(sys); state.sys = sys; state.views = malloc(sys->nplanets * sizeof(PlanetView)); for(int i = 0; i < sys->nplanets; i++) state.views[i].radius = pow(sys->planets[i].mass / DENSITY_FACTOR, 1.0f/3.0f); state.scale = 1.0f; Vector* fst_pos = &sys->planets[0].position; vector_copy(state.pos, *fst_pos); state.pos[1] += 1.1f*get_planet_radius(0); state.pos[0] -= get_planet_radius(0); state.rot_x = 90.0f; state.locked_planet = -1; state.hours_per_sec = DEFAULT_SIMULATION_SPEED; state.time_step = sys->time_step; state.paused = true; state.trails_enabled = true; if(SDL_Init(SDL_INIT_VIDEO) < 0) die("SDL initialization failed"); atexit(SDL_Quit); const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo(); if(!videoInfo) die("Could not get video information"); int videoFlags = SDL_OPENGL | SDL_HWPALETTE | SDL_RESIZABLE | SDL_HWSURFACE | SDL_HWACCEL; SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4); surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags); if(!surface) { printf("Surface creation failed, trying to disable anti-aliasing...\n"); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0); surface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags); } if(!surface) die("Changing video mode failed"); init_gl(); init_viewport(); SDL_ShowCursor(0); SDL_WM_GrabInput(SDL_GRAB_ON); SDL_Event event; while(SDL_PollEvent(&event)) ; /* ignore spurious mouse events at startup */ bool window_is_active = true; int step = 0; while (true) { Uint32 next_update = SDL_GetTicks() + FRAME_INTERVAL; while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_ACTIVEEVENT: window_is_active = event.active.gain; break; case SDL_VIDEORESIZE: surface = SDL_SetVideoMode(event.resize.w, event.resize.h, SCREEN_BPP, videoFlags); if(!surface) die("Lost video surface during resize"); init_viewport(); break; case SDL_KEYDOWN: handle_keypress(&event.key.keysym); break; case SDL_MOUSEMOTION: handle_mouse(&event.motion); break; case SDL_QUIT: goto out; default: break; } } update(); if(window_is_active) { draw_scene(); glFlush(); SDL_GL_SwapBuffers(); } if(!state.paused) { for(int i = 0; i < (state.hours_per_sec * 3600.0f / FRAME_INTERVAL) / state.time_step; i++) { if((step % TRAILS_INTERVAL) == 0) update_trails(); simulate_one_step(sys, step++, state.time_step); } } Sint32 delta = next_update - SDL_GetTicks(); if(delta > 0) SDL_Delay(delta); } out: return 0; }