void TEST_move_mouse() { char *routine = "TEST_move_mouse"; printf(testing, routine); //First we test with no window int before_x = mouse_x(0); int before_y = mouse_y(0); assert(move_mouse(0, 0, 0)); int after_x = mouse_x(0); int after_y = mouse_y(0); assert(after_x == 0 && after_y == 0); //Now we try it with a valid window Window win = active_window(); activate_window(win); assert(move_mouse(10, 10, win)); after_x = mouse_x(win); after_y = mouse_y(win); assert(after_x == 10 && after_y == 10); //Finally, let's make sure it doesn't bomb with an invalid window //The correct action is to leave the mouse where it is assert(move_mouse(20, 20, 20) == FALSE); assert(mouse_x(0) != 20 && mouse_y(0) != 20); move_mouse(before_x, before_y, 0); printf(done, routine); }
virtual i4_bool mouse_drag() { //{{{ Handle Moving if (buttons() & moving) { i4_float theta = m1_render_window->theta + 0.01f*(mouse_x() - last_x()); i4_float phi = m1_render_window->phi - 0.01f*(mouse_y() - last_y()); if (theta<0.0) { theta += i4_pi()*2; } else if (theta>i4_pi()*2) { theta -= i4_pi()*2; } if (phi<0.0) { phi += i4_pi()*2; } else if (phi>i4_pi()*2) { phi -= i4_pi()*2; } m1_render_window->theta = theta; m1_render_window->phi = phi; m1_render_window->recalc_view(); return i4_T; } //}}} //{{{ Handle Zooming if (buttons() & zooming) { //This is not really that time critical, only done several times per second. //otherwise, it would be bad style... m1_render_window->dist *= (i4_float) exp(i4_float(mouse_y() - last_y())/height() * log(4.0f)); m1_render_window->recalc_view(); return i4_T; } //}}} //{{{ Handle Panning if (buttons() & zooming) { i4_float p_x = i4_float(mouse_x() - last_x())/width(); i4_float p_y = i4_float(mouse_y() - last_y())/height(); m1_render_window->pan(p_x,p_y); m1_render_window->recalc_view(); return i4_T; } //}}} return i4_F; }
int main() { while ( !button_pressed() ) { setup_demo( buf, synth ); // base frequency and amplitude on mouse position int period = mouse_x() * 100 + 10; int amplitude = mouse_y() * 9 + 1; // generate alternating signs of square wave, spaced by period int time = 0; while ( time < 1000 ) { amplitude = -amplitude; synth.update( time, amplitude ); time += period; } buf.end_frame( 1000 ); show_buffer( buf ); } return 0; }
virtual i4_bool mouse_down() { ax = mouse_x(); ay = mouse_y(); return i4_F; }
int Textbox::mouse_position() { if (mouse_x() <= area.x() + font_size) return 0; if (mouse_x() >= area.x() + font_size + al_get_text_width(font(font_size),text.c_str())) return text.size(); for (int i = 0; i < text.size(); i++) { if (mouse_x() > area.x() + font_size + al_get_text_width(font(font_size),text.substr(0, i).c_str()) && mouse_x() < area.x() + font_size + al_get_text_width(font(font_size),text.substr(0, i+1).c_str())) { if (al_get_text_width(font(font_size),text.substr(i, 1).c_str())/2 > mouse_x() - (area.x() + font_size + al_get_text_width(font(font_size),text.substr(0, i).c_str()))) { return i; } else { return i+1; } } } }
int main() { long sample_rate = 44100; // Sample rate sets how many samples are generated per second if ( buf.set_sample_rate( sample_rate ) ) return 1; // out of memory buf.bass_freq( 0 ); // keep waveforms perfectly flat // Setup synth synth.output( &buf ); synth.volume( 0.50 ); while ( !button_pressed() ) { // Mouse sets clock rate, higher to the right. The higher the clock // rate, the more packed the waveform becomes. long rate = sample_rate * (mouse_x() * 10 + 1); // Clock rate sets how many time units there are per second buf.clock_rate( rate ); // Generate random waveform, with each transition spaced 50 clocks apart. srand( 1 ); buf.clear(); for ( int time = 0; time < 500; time += 50 ) synth.update( time, rand() % 20 - 10 ); buf.end_frame( 600 ); show_buffer_unscaled( buf ); } return 0; }
void event_logic(){ for(ALLEGRO_EVENT event; al_get_next_event(engine_event_queue,&event);){ //Default event handlers switch(event.type){ case ALLEGRO_EVENT_DISPLAY_CLOSE: quit_at_next_frame(); break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if(event.mouse.button==1){ for(std::list<renderable*>::iterator iter = ENGINE_SCREEN_MANAGER.buttons.begin(); iter != ENGINE_SCREEN_MANAGER.buttons.end(); ++iter){ if( (*iter)->active){ (*iter)->calc_bounds(); if(in_range((*iter)->bounds[0][0],(*iter)->bounds[0][1],mouse_x()) && in_range((*iter)->bounds[1][0],(*iter)->bounds[1][1],mouse_y())){ ((button*)(*iter))->on_click->exec(); } } } } break; case ALLEGRO_EVENT_TIMER: if(event.timer.source==timer) draw_now=true; break; } game_event_hooks(event); } al_rest(0.001); }
BOOL _normalize_wincoords(int *x, int *y, unsigned long win) { if (!_check_init()) { return FALSE; } XWindowAttributes *attribs = calloc(1, sizeof(XWindowAttributes)); if(attribs == NULL) { fprintf(stderr, "Unable to allocate space for XWindowAttributes"); return FALSE; } XSetErrorHandler(_invalid_window_error_handler); int status = XGetWindowAttributes(defaults->display, win, attribs); XSetErrorHandler(NULL); BOOL ret; if (status) { int winx = window_x(win); int winy = window_y(win); *x = winx + *x; *y = winy + *y; logit(LOG_LEVEL_VERBOSE, "Window at %d X %d\n", winx, winy); logit(LOG_LEVEL_VERBOSE, "Relative location %d X %d\n", *x, *y); ret = TRUE; } else { fprintf(stderr, "Window handle %lu appears to be invalid\n", win); fprintf(stderr, "Not moving the mouse\n"); *x = mouse_x(0); *y = mouse_y(0); ret = FALSE; } free(attribs); //In case the window is partially off the screen _normalize_coords(x, y); return ret; }
void image_example_logic(){ for(int i = 0; i < 10; ++i){ if(sprite[i]->auto_update){ sprite[i]->x_vel+=(mouse_x()-sprite[i]->x)/(40.0f-2.0f*i)-sprite[i]->x_vel/(20.0f+2.0f*i); sprite[i]->y_vel+=(mouse_y()-sprite[i]->y)/(40.0f-2.0f*i)-sprite[i]->y_vel/(20.0f+2.0f*i); } } }
void particle2_example_logic(){ for(int i = 0; i < 40; ++i){ if(particle2[i]->auto_update){ particle2[i]->x_vel+=(mouse_x()-particle2[i]->x)/(40.0f-0.5f*i)-particle2[i]->x_vel/(20.0f+0.5f*i); particle2[i]->y_vel+=(mouse_y()-particle2[i]->y)/(40.0f-0.5f*i)-particle2[i]->y_vel/(20.0f+0.5f*i); } } }
void TEST_mouse_x() { char *routine = "TEST_mouse_x"; printf(testing, routine); //There's no way to know if the values we receive are "correct", //so we can only test to make sure it doesn't cause problems //First with no window mouse_x(0); //Then with a valid window Window win = active_window(); mouse_x(win); //Finally with an invalid window - which should return INT_MIN int invalid_x = mouse_x(20); assert(invalid_x == INT_MIN); printf(done, routine); }
int particle2_example_init(){ for(int i = 0; i < 40; ++i){ particle2[i]=new particle_circ(5.0f); particle2[i]->deactivate_at_edge=false; particle2[i]->auto_render=true; particle2[i]->auto_update=true; particle2[i]->tracer=true; int tmp=(rand()%256)|128; particle2[i]->set( double(mouse_x()),double(mouse_y()), al_map_rgba(tmp/2,0,tmp,(rand()%256)|255)); } return 0; }
virtual i4_bool mouse_up() { g1_quad_object_class * obj = m1_info.obj; g1_vert_class * src_vert = obj->get_verts(m1_info.current_animation, m1_info.current_frame); r1_vert v; i4_float tmp; px = mouse_x(); py = mouse_y(); if (ax>px) { tmp=ax; ax=px; px=tmp; } if (ay>py) { tmp=ay; ay=py; py=tmp; } if (!i4_current_app->get_window_manager()->shift_pressed()) { li_call("select_none"); } for (int i=0; i<obj->num_quad; i++) { g1_quad_class * q = 0; q= &(obj->quad[i]); i4_bool out=i4_F; for (int j=0; j<q->num_verts() && !out; j++) { m1_render_window->project_point(src_vert[q->vertex_ref[j]].v, v); if (v.px<ax || v.px>px || v.py<ay || v.py>py) { out = i4_T; } } if (!out) { q->set_flags(g1_quad_class::SELECTED); } } m1_render_window->restore_state(); m1_render_window->request_redraw(i4_F); m1_st_edit->edit_poly_changed(); return i4_T; }
int main() { // Setup left buffer and wave int left_time = 0; int left_amp = 0; setup_demo( left, left_synth ); left.clock_rate( left.sample_rate() * 100 ); // Setup right buffer and wave int right_time = 0; int right_amp = 0; setup_demo( right, right_synth ); right.clock_rate( right.sample_rate() * 100 ); while ( !button_pressed() ) { blip_time_t length = 100000; // mouse sets frequency of left wave int period = 1000 + 6 * mouse_x(); // Add saw wave to left buffer do { left_synth.update( left_time, left_amp = (left_amp + 1) % 10 ); } while ( (left_time += period) < length ); left.end_frame( length ); left_time -= length; // Add saw wave of slightly lower pitch to right buffer do { right_synth.update( right_time, right_amp = (right_amp + 1) % 10 ); } while ( (right_time += 1000) < length ); right.end_frame( length ); right_time -= length; // buffer to read samples into int const buf_size = 2048; static blip_sample_t samples [buf_size]; // Read left channel into even samples, right channel into odd samples: // LRLRLRLRLR... long count = left.read_samples( samples, buf_size / 2, 1 ); right.read_samples( samples + 1, count, 1 ); play_stereo_samples( samples, count * 2 ); } return 0; }
BOOL move_mouse(int newX, int newY, Window win) { int loop = 0; if (!_check_init()) { return FALSE; } BOOL success = TRUE; logit(LOG_LEVEL_VERBOSE, "Window handle: %lu\n", win); if (win) { success = _normalize_wincoords(&newX, &newY, win); if(success == FALSE) { return FALSE; } } else { _normalize_coords(&newX, &newY); } if (defaults->mouse_move_delay == ZERO) { return _do_movement(newX, newY); } else { int oldX = mouse_x(0L); int oldY = mouse_y(0L); while (oldX != newX || oldY != newY) { int movementX, movementY; _gradientMovement(newX, newY, &movementX, &movementY); // 1:1 is a little slow if ((fabs(movementX) == 1 && fabs(movementY) == 1) && (fabs(newX - oldX) > 1 && fabs(newY - oldY) > 1) ) { movementX *= 2; movementY *= 2; } oldX += movementX; oldY += movementY; success &= _do_movement(oldX, oldY); if (!success) { fprintf(stderr, "Simulation of mouse movement failed.\n"); break; } usleep(50 * defaults->mouse_move_delay); loop++; if (loop > 10000) { return FALSE; } //Probably something is wrong } } return success; }
void _gradientMovement(int targX, int targY, int *xResp, int *yResp) { int currX = mouse_x(0L); int currY = mouse_y(0L); int diffX = (int) (fabs(targX - currX)); int diffY = (int) (fabs(targY - currY)); if (diffX == 0 && diffY == 0) { //No movement at all - why are we here? //Theoretically this shouldn't happen, but at least it won't explode fprintf(stderr, "gradientMovement - target and destination" " coordinates are the same\n"); *xResp = 0; *yResp = 0; } else if (diffX == 0) { //Up-down movement only *xResp = 0; *yResp = diffY; } else if (diffY == 0) { //Left-right movement only *xResp = diffX; *yResp = 0; } else { //Want a movement based on ratio if (diffX == diffY) { *xResp = 1; *yResp = 1; } else if (diffX > diffY) { //The following line is cast for clarity sake - I know I want int *xResp = (int) (diffX / diffY); *yResp = 1; } else { *xResp = 1; //The following line is cast for clarity sake - I know I want int *yResp = (int) (diffY / diffX); } } // Multiply by -1 if moving up or left if (targX < currX) { *xResp *= -1; } if (targY < currY) { *yResp *= -1; } }
void IOController::trigger_events(const BBox& playing_area) { // Loop over all possible keys to find active key states for (int i = SDLK_FIRST + 1; i < SDLK_LAST; i++) { IOEventTrigger::trigger_t notrigger = IOEventTrigger::NONE; if (iostate.key_press_states[i]) { // Trigger key press events __trigger_events(notrigger, SDLKey(i), iostate.keymod, false); } else if (iostate.key_down_states[i]) { // Trigger key hold events __trigger_events(notrigger, SDLKey(i), iostate.keymod, true); } } if (playing_area.contains(mouse_x(), mouse_y())) { if (mouse_left_click() || mouse_left_down()) { __trigger_events(IOEventTrigger::MOUSE_LEFT_CLICK, SDLKey(0), iostate.keymod, !mouse_left_click()); } if (mouse_right_click() || mouse_right_down()) { __trigger_events(IOEventTrigger::MOUSE_RIGHT_CLICK, SDLKey(0), iostate.keymod, !mouse_left_click()); } } if (iostate.mouse_middleclick || iostate.mouse_middledown) { __trigger_events(IOEventTrigger::MOUSE_MIDDLE_CLICK, SDLKey(0), iostate.keymod, !iostate.mouse_middleclick); } if (mouse_upwheel()) { __trigger_events(IOEventTrigger::MOUSE_WHEEL_UP, SDLKey(0), iostate.keymod, false); } if (mouse_downwheel()) { __trigger_events(IOEventTrigger::MOUSE_WHEEL_DOWN, SDLKey(0), iostate.keymod, false); } if (mouse_downwheel()) { __trigger_events(IOEventTrigger::MOUSE_WHEEL_DOWN, SDLKey(0), iostate.keymod, false); } }
int delta_mouse_x(void) const noexcept { return mouse_x() - mouse_x(_old); }