Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » "Endless" mouse

This thread is locked; no one can reply to it. rss feed Print
"Endless" mouse
roger levy
Member #2,513
July 2002

Is it possible to have an "endless" mouse, that isn't constrained to the boundaries of the display? Maybe by warping it every frame?

torhu
Member #2,727
September 2002
avatar

Do you mean just having the pointer appear at the opposite side of the screen of where it exits? I suppose you could just call set_mouse_xy.

roger levy
Member #2,513
July 2002

No, I mean, like hiding the cursor, and using the actual movement of the physical mouse which isn't constrained by the display. As you would in an fps.

torhu
Member #2,727
September 2002
avatar

Right. Not sure about that :-/

Izual
Member #2,756
September 2002
avatar

You can try to emulate it with something like the code below. Its propably not perfect but it works.

Use the dx and dy to determine the mouse movements.

( press G to hide / show the mouse cursor )

#SelectExpand
1#include <stdbool.h> 2#include <stdio.h> 3 4#include "allegro5/allegro.h" 5 6enum 7 { 8 DISPLAY_W = 1280, 9 DISPLAY_H = 960 10 }; 11 12ALLEGRO_DISPLAY *display; 13ALLEGRO_EVENT_QUEUE *event_queue; 14ALLEGRO_EVENT event; 15 16bool repeat; 17bool mouse_hiden; 18 19int main() 20 { 21 al_init(); 22 al_install_mouse(); 23 al_install_keyboard(); 24 25 display = al_create_display( DISPLAY_W, DISPLAY_H ); 26 event_queue = al_create_event_queue(); 27 28 al_register_event_source( event_queue, al_get_keyboard_event_source() ); 29 al_register_event_source( event_queue, al_get_mouse_event_source() ); 30 31 mouse_hiden = false; 32 repeat = true; 33 34 while( repeat ) 35 { 36 al_wait_for_event( event_queue, &event ); 37 38 switch( event.type ) 39 { 40 case ALLEGRO_EVENT_MOUSE_AXES: 41 printf( "MOUSE: x=%d, y=%d, z=%d, dx=%d, dy=%d, dz=%d\n", 42 event.mouse.x, 43 event.mouse.y, 44 event.mouse.z, 45 event.mouse.dx, 46 event.mouse.dy, 47 event.mouse.dz ); 48 49 if( mouse_hiden ) 50 al_set_mouse_xy( display, DISPLAY_W / 2, DISPLAY_H / 2 ); 51 break; 52 53 case ALLEGRO_EVENT_KEY_DOWN: 54 if( event.keyboard.keycode == ALLEGRO_KEY_ESCAPE ) 55 repeat = false; 56 57 if( event.keyboard.keycode == ALLEGRO_KEY_G ) 58 { 59 if( mouse_hiden ) 60 { 61 mouse_hiden = false; 62 al_show_mouse_cursor( display ); 63 al_ungrab_mouse(); 64 } 65 else 66 { 67 mouse_hiden = true; 68 al_grab_mouse( display ); 69 al_hide_mouse_cursor( display ); 70 al_set_mouse_xy( display, DISPLAY_W / 2, DISPLAY_H / 2 ); 71 } 72 } 73 break; 74 75 default: 76 printf( "Unhandled event type %d!\n", event.type ); 77 break; 78 } 79 } 80 81 return 0; 82 }

bamccaig
Member #7,536
July 2006
avatar

Yep, pretty sure FPS's just record the mouse movement and then reset its position to the centre of the screen every frame or thereabouts. Izual's sample code appears to be doing just that.

Elias
Member #358
May 2000

ex_mouse_warp that ships with Allegro also does the same: https://github.com/liballeg/allegro5/blob/master/examples/ex_mouse_warp.c

--
"Either help out or stop whining" - Evert

Go to: