Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » weird mouse issue

Credits go to Edgar Reynaldo, Izual, and Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
weird mouse issue
bill99
Member #11,913
May 2010

Ive added mouse functionality to my game and a strange thing happens, any time i move the mouse outside the game display window for more then a second or two the program exits. It doesn't crash, it just exits with a normal status zero. (obviously thats without grab_mouse) but when i use grab_mouse the same thing occurs if i have the mouse at any of the corners for the same length of time. I have no code which should allow this to occur... any help would be very appreciated.

Edgar Reynaldo
Member #8,592
May 2007
avatar

You should show code and also try to produce a minimal example that reproduces this. Also, you ran it from a console and it is returning 0? On what conditions do you return from your program? Link the the allegro debug libraries and when your program runs, look at the allegro.log file created for clues.

And what versions are you using... compiler, allegro, and what os

bill99
Member #11,913
May 2010

Here is a minimal example that reproduces the effect:

#SelectExpand
1#include <iostream> 2 3#include"allegro5/allegro.h" 4#include"allegro5/allegro_image.h" 5#include"allegro5/debug.h" 6using namespace std; 7 8int main() 9{ 10 //initialize allegro 11 al_init(); 12 al_init_image_addon(); 13 // initialize display 14 ALLEGRO_DISPLAY_MODE DisplayData; 15 al_get_display_mode(al_get_num_display_modes() - 1, &DisplayData); 16 unsigned short ScreenHeight = 640; 17 unsigned short ScreenWidth = 640; 18 ALLEGRO_DISPLAY * Display = NULL; 19 Display = al_create_display(ScreenHeight, ScreenWidth); 20 // initialize Keyboard 21 al_install_keyboard(); 22 // initialize mouse 23 al_install_mouse(); 24 // Initalize events 25 ALLEGRO_EVENT_QUEUE * EventQueue = al_create_event_queue(); 26 al_register_event_source(EventQueue, al_get_mouse_event_source()); 27 al_register_event_source(EventQueue, al_get_keyboard_event_source()); 28 ALLEGRO_EVENT Events; 29 30 bool Run = true; 31 while(Run == true) 32 { 33 al_wait_for_event(EventQueue, & Events); 34 if(Events.keyboard.keycode == ALLEGRO_KEY_ESCAPE) // exit on escape 35 { 36 Run = false; 37 } 38 if(Events.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 39 { 40 std::cout << "Mouse Event Detected. \n"; // to verify mouse events work 41 } 42 43 } 44 45 46 al_destroy_display(Display); 47 return 0; 48}

in order to get the exit to happen just wiggle the cursor around your desktop both on and off of the display. i added the debug.h file but could find no log.

i am running windows 7 home premium sp 1 64 bit, allegro 5 (the most recent stable release), mingw gcc 4.8.1

Izual
Member #2,756
September 2002
avatar

Change this part of your code:

#SelectExpand
1if(Events.keyboard.keycode == ALLEGRO_KEY_ESCAPE) // exit on escape 2{ 3 Run = false; 4}

To this:

#SelectExpand
1if(Events.type == ALLEGRO_EVENT_KEY_DOWN) 2{ 3 if(Events.keyboard.keycode == ALLEGRO_KEY_ESCAPE) // exit on escape 4 { 5 Run = false; 6 } 7}

You have to check first what kind of event you have got from the queue and then process is.

At first glance it seems that when the mouse leaves window area the ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY is fired, and the values of that event match the ALLEGRO_KEY_ESCAPE condition you are checking for ( without the check for event type ).
And that makes your program exit, just like you wanted. :)

Thomas Fjellstrom
Member #476
June 2000
avatar

One thing to know is that allegro stores the event data in a "shared" data type. It shares space in the same structure for the different event types. So you ALWAYS want to check what type the event is when checking for the actual events, otherways odd things can and will happen.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

bill99
Member #11,913
May 2010

Yup, That did it, thanks! And I'll be sure to remember that in the future! 8-)

Go to: