Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » how to use ev.mouse.dx

Credits go to Matthew Leverton for helping out!
This thread is locked; no one can reply to it. rss feed Print
how to use ev.mouse.dx
altalena
Member #13,639
October 2011
avatar

I'm wondering how to get mouse mickeys from allegro 5. I've used

     cursor_x = ev.mouse.dx;
cursor_y = ev.mouse.dy;
al_set_mouse_xy(display,
      al_get_display_width(display)/2,
      al_get_display_height(display)/2);

and its inside an event, but it won't update the mouse position and I end up with a mouse cursor that moves in the rectangle -.5, -.5, .5, .5 of the screen size.

The reason i need this is to make an fps-style noclip mode to travel through a globular star cluster, and the mouse has to not run into the edge of the screen.

I'm basically porting an allegro 4 program i wrote, and the mickeys calculates the distance travelled since the start of the program instead of since the last redraw.

If there is a command to use or a better way to get mouse mickeys please respond with some code that prints how the far the mouse has moved since the last redraw.

If it matters, here's the whole program

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_font.h> 5#include <allegro5/allegro_ttf.h> 6#include <math.h> 7#define M_PIE 3.14159265358979323846 8 9const float FPS = 60; 10const int SCREEN_W = 1920; 11const int SCREEN_H = 1080; 12const int BOUNCER_SIZE = 32; 13const int BOUNCES_SIZE = 32; 14enum MYKEYS { 15 KEY_W, KEY_S, KEY_A, KEY_D 16}; 17 18 19int main(int argc, char **argv) 20{ 21 unsigned long mers_finit[4]={0x123, 0x234, 0x345, 0x456}, mers_ilength=4; 22 mers_init_by_array(mers_finit, mers_ilength); 23 24 ALLEGRO_DISPLAY *display = NULL; 25 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 26 ALLEGRO_TIMER *timer = NULL; 27 ALLEGRO_BITMAP *bouncer = NULL; 28 ALLEGRO_BITMAP *bounces = NULL; 29 float cursor_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; 30 float cursor_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; 31 float bounces_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0; 32 float bounces_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0; 33 bool key[4] = { false, false, false, false }; 34 bool redraw = true; 35 bool doexit = false; 36 37 if(!al_init()) { 38 fprintf(stderr, "failed to initialize allegro!\n"); 39 return -1; 40 } 41 42 al_init_font_addon(); // initialize the font addon 43 al_init_ttf_addon();// initialize the ttf (True Type Font) addon 44 ALLEGRO_FONT *font24 = al_load_ttf_font("VeraMono.ttf",24,0 ); // load the font 45 46 if(!al_init_primitives_addon()) { 47 fprintf(stderr, "failed to initialize primitives!\n"); 48 return -1; 49 } 50 51 if(!al_install_keyboard()) { 52 fprintf(stderr, "failed to initialize the keyboard!\n"); 53 return -1; 54 } 55 56 if(!al_install_mouse()) { 57 fprintf(stderr, "failed to initialize the mouse!\n"); 58 return -1; 59 } 60 61 timer = al_create_timer(1.0 / FPS); 62 if(!timer) { 63 fprintf(stderr, "failed to create timer!\n"); 64 return -1; 65 } 66 67 al_set_new_display_flags(ALLEGRO_FULLSCREEN); 68 display = al_create_display(SCREEN_W, SCREEN_H); 69 if(!display) { 70 fprintf(stderr, "failed to create display!\n"); 71 al_destroy_timer(timer); 72 return -1; 73 } 74 al_hide_mouse_cursor(display); 75 76 bounces = al_create_bitmap(BOUNCES_SIZE, BOUNCES_SIZE); 77 if(!bounces) { 78 fprintf(stderr, "failed to create bounces bitmap!\n"); 79 al_destroy_display(display); 80 al_destroy_timer(timer); 81 return -1; 82 } 83 84 bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE); 85 if(!bouncer) { 86 fprintf(stderr, "failed to create bouncer bitmap!\n"); 87 al_destroy_display(display); 88 al_destroy_timer(timer); 89 return -1; 90 } 91 92 al_set_target_bitmap(bounces); 93 94 al_clear_to_color(al_map_rgb(0, 255, 255)); 95 96 al_set_target_bitmap(bouncer); 97 98 al_clear_to_color(al_map_rgb(255, 0, 255)); 99 100 al_set_target_bitmap(al_get_backbuffer(display)); 101 102 event_queue = al_create_event_queue(); 103 if(!event_queue) { 104 fprintf(stderr, "failed to create event_queue!\n"); 105 al_destroy_bitmap(bouncer); 106 al_destroy_display(display); 107 al_destroy_timer(timer); 108 return -1; 109 } 110 111 al_register_event_source(event_queue, al_get_display_event_source(display)); 112 113 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 114 115 al_register_event_source(event_queue, al_get_keyboard_event_source()); 116 117 al_register_event_source(event_queue, al_get_mouse_event_source()); 118 119 al_clear_to_color(al_map_rgb(0,0,0)); 120 121 al_flip_display(); 122 123 al_start_timer(timer); 124 125 while(!doexit) 126 { 127 ALLEGRO_EVENT ev; 128 al_wait_for_event(event_queue, &ev); 129 130 if(ev.type == ALLEGRO_EVENT_TIMER) { 131 if(key[KEY_W] && bounces_y >= 4.0) { 132 bounces_y -= 4.0; 133 } 134 135 if(key[KEY_S] && bounces_y <= SCREEN_H - BOUNCES_SIZE - 4.0) { 136 bounces_y += 4.0; 137 } 138 139 if(key[KEY_A] && bounces_x >= 4.0) { 140 bounces_x -= 4.0; 141 } 142 143 if(key[KEY_D] && bounces_x <= SCREEN_W - BOUNCES_SIZE - 4.0) { 144 bounces_x += 4.0; 145 } 146 147 redraw = true; 148 } 149 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 150 break; 151 } 152 else if(ev.type == ALLEGRO_EVENT_MOUSE_AXES || 153 ev.type == ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY) { 154 155 if(!al_set_mouse_xy(display, 156 al_get_display_width(display)/2, 157 al_get_display_height(display)/2)) 158 al_draw_textf(font24, al_map_rgb(255,255,255), al_get_display_width(display)/2, 96, 159 ALLEGRO_ALIGN_CENTRE, "fail %f x %f", cursor_x, cursor_y);// draw the text 160 161 cursor_x = ev.mouse.dx; 162 cursor_y = ev.mouse.dy; 163 } 164 else if(ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) { 165 break; 166 } 167 168 if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { 169 switch(ev.keyboard.keycode) { 170 case ALLEGRO_KEY_W: 171 key[KEY_W] = true; 172 break; 173 174 case ALLEGRO_KEY_S: 175 key[KEY_S] = true; 176 break; 177 178 case ALLEGRO_KEY_A: 179 key[KEY_A] = true; 180 break; 181 182 case ALLEGRO_KEY_D: 183 key[KEY_D] = true; 184 break; 185 } 186 } 187 else if(ev.type == ALLEGRO_EVENT_KEY_UP) { 188 switch(ev.keyboard.keycode) { 189 case ALLEGRO_KEY_W: 190 key[KEY_W] = false; 191 break; 192 193 case ALLEGRO_KEY_S: 194 key[KEY_S] = false; 195 break; 196 197 case ALLEGRO_KEY_A: 198 key[KEY_A] = false; 199 break; 200 201 case ALLEGRO_KEY_D: 202 key[KEY_D] = false; 203 break; 204 205 case ALLEGRO_KEY_ESCAPE: 206 doexit = true; 207 break; 208 } 209 } 210 if(redraw && al_is_event_queue_empty(event_queue)) { 211 redraw = false; 212 213 al_clear_to_color(al_map_rgb(0,0,0)); 214 215 al_draw_bitmap(bounces, bounces_x, bounces_y, 0); 216 217 al_draw_filled_triangle( 218 cursor_x, cursor_y, 219 cursor_x+20.0, cursor_y+34.0, 220 cursor_x, cursor_y+40.0, 221 al_map_rgb(184, 22, 22)); 222 223 al_draw_textf(font24, al_map_rgb(255,255,255), al_get_display_width(display)/2, 48, 224 ALLEGRO_ALIGN_CENTRE, "y %f x %f", cursor_x, cursor_y);// draw the text 225 226 al_flip_display(); 227 } 228 } 229 230 al_destroy_bitmap(bouncer); 231 al_destroy_timer(timer); 232 al_destroy_display(display); 233 al_destroy_event_queue(event_queue); 234 235 return 0; 236}

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Matthew Leverton
Supreme Loser
January 1999
avatar

I'm not exactly sure if this is what you want, but you can do this:

cursor_x += ev.mouse.dx;
cursor_y += ev.mouse.dy;

if (cursor_x < 0) cursor_x = 0;
else if (cursor_x >= SCREEN_W) cursor_x = SCREEN_W - 1;

if (cursor_y < 0) cursor_y = 0;
else if (cursor_y >= SCREEN_H) cursor_y = SCREEN_H - 1;

The mouse will never leave the display window.

Unrelated note: you should load fonts after creating the display.

altalena
Member #13,639
October 2011
avatar

I tried that without the code to keep the cursor on the screen, but what happened is i got huge numbers for cursor_x and cursor_y because ev.mouse.dx and ev.mouse.dy is not giving me the mouse difference since the last redraw, but the distance the mouse has travelled since the program loop began. Is there a way to get ev.mouse.dx to give me the distance the mouse has travelled since the last redraw?
I've attached the complete code including the mersenne twister that i had to change all the variable names for to get it to work.

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Matthew Leverton
Supreme Loser
January 1999
avatar

The code works on OS X. Maybe the Allegro 5 Windows mouse driver is buggy when you change mouse positions.

altalena
Member #13,639
October 2011
avatar

Could someone write a short program that redraws at 60 fps and displays how far the mouse has moved since the last redraw? I tried al_grab_mouse together with al_set_mouse_xy to move the mouse cursor but it didn't work.

edit- I'm using allegro 5.0.4

edit-
http://www.allegro.cc/forums/thread/608627

...At the briefest instant following creation all the matter of the universe was concentrated in a very small place, no larger than a grain of mustard. The matter at this time was very thin, so intangible, that it did not have real substance. It did have, however, a potential to gain substance and form and to become tangible matter. From the initial concentration of this intangible substance in its minute location, the substance expanded, expanding the universe as it did so. As the expansion progressed, a change in the substance occurred. This initially thin noncorporeal substance took on the tangible aspects of matter as we know it. From this initial act of creation, from this ethereally thin pseudosubstance, everything that has existed, or will ever exist, was, is, and will be formed. - the RaMBaN, 1194 - 1270

ג וּשְׁאַבְתֶּם-מַיִם, בְּשָׂשׂוֹן, מִמַּעַיְנֵי, הַיְשׁוּעָה. - Yeshayahu 12:3

Go to: