Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Odd event.mouse.x & y values

This thread is locked; no one can reply to it. rss feed Print
Odd event.mouse.x & y values
AceBlkwell
Member #13,038
July 2011
avatar

All,

I'm just starting to work on adding a mouse to my program. The setup seems easy enough on Allegro 5. I added a simple square cursor. When it wouldn't move I started to textf the event.mouse.x, event.mouse.y values for reference. The x value stays 0. The y value is about a 9+ digit number that constantly bounces whether the mouse is being moved or not. It even throws in a negative here and there.

Has anyone ever heard of this before? I don't have a compiler error or warning. The other events from the keyboard work fine. Its just the mouse.

I've installed the mouse, and registered it.
init_confirm(al_install_mouse(), "mouse");
al_register_event_source(queue, al_get_mouse_event_source());

I'm sure I've over looked something, but based on the various code examples and youtube items I've looked up, I don't know what it would be. Especially since I'm not getting error and warnings.

Thanks

torhu
Member #2,727
September 2002
avatar

You probably have to post code :P

Dizzy Egg
Member #10,824
March 2009
avatar

Online example and YouTube videos?? And now you come to us? That’s it, I’m not helping you.

I want a divorce.

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

DanielH
Member #934
January 2001
avatar

where are you getting x/y values of the mouse? Which mouse events?

Just know that the ALLEGRO_EVENT is a union of many different EVENT structs. If you try to get mouse.x and mouse.y values from a non-mouse event, it will be garbage.

Post some code.

AceBlkwell
Member #13,038
July 2011
avatar

Dizzy, I meant no offense ;D. On most other specialized sites I frequent if you ask for help without exhausting ALL other avenues you get hit with a bunch of attitude meant to make you look stupid and lazy. “We answer this all the time” or “When are ppl going start putting forth effort without looking for a quick answer” or “Its all in the manual, that needs to be the first place you look.” Or “ Yes I know all there is to know on the subject. I am the reason they developed XXXXXXX, but don’t be fooled, just because I am on this Help site ALL THE TIME, I’m not here to answer questions!!!!” Ok I might have made up the last one, but you get the idea. I was trying to find it for myself before troubling anyone else. Plus I’ve been asking a lot of questions as of late.

As for code. It’s going to be a little hard to follow because I’m just patching stuff in. I’m trying to get familiar with A5. As I decide I want to learn a certain aspect, I graft it in to my current code which isn’t clear and organized. This could be the issue. I can include the individual functions if need be.

In the Solly’s Matches line, If I change the event.mouse.x/y I get the odd numbers I mentioned earlier, if I replace with pos_x / pos_y they stay at 0. Meaning they aren’t getting updated by the ALLEGRO_EVENT_MOUSE_AXES if.

#SelectExpand
1/**********************INCLUDES ***********************/ 2#include <cstdio> 3#include <cstdlib> 4#include <iostream> 5#include <allegro5/allegro5.h> 6#include <allegro5/allegro_font.h> 7#include <allegro5/allegro_ttf.h> 8//#include <allegro5/allegro_image.h> 9#include <allegro5/allegro_primitives.h> 10#include <allcolor_A5.h> 11#include <string> 12 13/********************* DEFINES ******/ 14#define STX 25 15#define STY 75 16#define CQTY 24 17/********************* STRUCTS ******/ 18struct BLOCKS { 19 float fTX; 20 float fTY; 21 float fBX; 22 float fBY; 23 bool bMatched; 24 bool bFlipped; 25 int iValue; 26 float fCX; 27 float fCY; 28 ALLEGRO_COLOR aFrontColor; 29 ALLEGRO_COLOR aBackColor; 30}; 31 32/********************* FUNCTION DECLARE ******/ 33void a_init(); 34void draw_cards(BLOCKS[], float,int, int); 35void card_setup(BLOCKS[]); 36void match_setup(BLOCKS[]); 37int random_no(int); 38 39/********************* EXTERNALS ******/ 40extern ALLEGRO_TIMER* timer; 41extern ALLEGRO_EVENT_QUEUE* queue; 42extern ALLEGRO_DISPLAY* disp; 43extern ALLEGRO_FONT* font; 44extern ALLEGRO_EVENT event; 45/******************* Main() *****************/ 46int main(int argc, char *argv[]){ 47int iChoiceCount = 0; 48bool bQuit = false; 49int iFirstChoice = 0; 50int iSecondChoice = 0; 51BLOCKS Blocks[CQTY]; 52int iKey = 0; 53int pos_x = 100; 54int pos_y = 100; 55 56 a_init(); 57 card_setup(Blocks); 58 match_setup(Blocks); 59 60while (!bQuit) 61{ 62 63 al_wait_for_event(queue, &event); 64 al_clear_to_color(COLOR::BLACK); 65 al_draw_textf(font, COLOR::BR_CYAN, 10, 20, 0, "Solly's Matches %i %i",event.mouse.x,event.mouse.y); 66 67 68 switch(iChoiceCount<2){ 69 case true: 70 draw_cards(Blocks,0.0,pos_x,pos_y); 71 break; 72 case false: 73 for(int count = 0;count<CQTY;count++){ 74 if(!Blocks[count].bMatched) 75 Blocks[count].bFlipped = false;} // Card Flip Cleared 76 iChoiceCount=0; 77 break; 78 } //end switch for iChoiceCount 79 80 81 if(event.type == ALLEGRO_EVENT_KEY_DOWN) 82 { 83 iKey = event.keyboard.keycode-1; 84 85 if(event.keyboard.keycode >= ALLEGRO_KEY_A && event.keyboard.keycode <= ALLEGRO_KEY_X ){ 86 Blocks[iKey].bFlipped = true; 87 if(iChoiceCount == 0) iFirstChoice = iKey; 88 if(iChoiceCount == 1) iSecondChoice = iKey; 89 iChoiceCount++; 90 } // end if for block choice 91 else if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 92 bQuit = true; 93 94 if(iChoiceCount == 2){ 95 if(Blocks[iFirstChoice].iValue == Blocks[iSecondChoice].iValue ){ 96 Blocks[iFirstChoice].bMatched = true; 97 Blocks[iSecondChoice].bMatched = true; }} 98 99 if((Blocks[iFirstChoice].bFlipped && Blocks[iSecondChoice].bFlipped )&&(Blocks[iFirstChoice].iValue != Blocks[iSecondChoice].iValue )){ 100 draw_cards(Blocks,2.0,pos_x,pos_y); 101 }//end Block Comparison if 102 }// end if event.type keyboard 103 104 else if(event.type == ALLEGRO_EVENT_MOUSE_AXES) 105 { pos_y = event.mouse.y; 106 pos_x = event.mouse.x; 107 } 108 109 draw_cards(Blocks,0.0, pos_x,pos_y); 110 111}// end while() 112 113al_destroy_display(disp); 114al_destroy_event_queue(queue); 115 116 return 0; 117 118} //end main()

Thanks for looking it over.

Dizzy Egg
Member #10,824
March 2009
avatar

Haha that's ok, I was only joking! :)

We love helping out here 8-)

Your mouse event line on 102 is else-if'd with the checking of blocks; I'm guessing it should just be:

if(event.type == ALLEGRO_EVENT_MOUSE_AXES)
{
    pos_y = event.mouse.y;
    pos_x = event.mouse.x;
}

Without the else

EDIT: Oh no, that's wrong - I can see now it's else-if'd with the other event!

Back to the drawing board....

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

AceBlkwell
Member #13,038
July 2011
avatar

Well it's working now. Not sure why. I moved my text print line to my ALLEGRO_EVENT_MOUSE_AXES if section and it worked. The numbers were accurate to my mouse location and the "cursor" moved around with the system cursor. So I figure my drawing and flipping were conflicting somehow. So I put it back (uncommented it out) and it still works. Craziest thing. I've had this happen on rare occasions. Could be the IDE. Next time I'll do a CLEAN first and then build.

In any case. Thanks a lot. I'm sure I'll be back.

Dizzy Egg
Member #10,824
March 2009
avatar

Oh well, glad it's working! 8-)

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

DanielH
Member #934
January 2001
avatar

else is a special keyword. It links to the most recent if. In your case, you assumed it's the if for ALLEGRO_KEY_ESCAPE. However, it's linking to the if right above it. Fix it with braces or move it right beneath the if

#SelectExpand
1 if(event.type == ALLEGRO_EVENT_KEY_DOWN) 2 { 3 iKey = event.keyboard.keycode-1; 4 5 if(event.keyboard.keycode >= ALLEGRO_KEY_A && event.keyboard.keycode <= ALLEGRO_KEY_X ){ 6 Blocks[iKey].bFlipped = true; 7 if(iChoiceCount == 0) iFirstChoice = iKey; 8 if(iChoiceCount == 1) iSecondChoice = iKey; 9 iChoiceCount++; 10 } // end if for block choice 11 else if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 12 bQuit = true; 13 14 else if(event.type == ALLEGRO_EVENT_MOUSE_AXES) 15 { pos_y = event.mouse.y; 16 pos_x = event.mouse.x; 17 } 18 19 if(iChoiceCount == 2){ 20 if(Blocks[iFirstChoice].iValue == Blocks[iSecondChoice].iValue ){ 21 Blocks[iFirstChoice].bMatched = true; 22 Blocks[iSecondChoice].bMatched = true; }} 23 24 if((Blocks[iFirstChoice].bFlipped && Blocks[iSecondChoice].bFlipped )&&(Blocks[iFirstChoice].iValue != Blocks[iSecondChoice].iValue )){ 25 draw_cards(Blocks,2.0,pos_x,pos_y); 26 }//end Block Comparison if 27 }// end if event.type keyboard

****************************************

my current code which isn’t clear and organized

One way to help organize is have better indentation (line things up).
also, if you have multiple if/else statements, use a switch statement. By the time you get to the nth if/else, you've needlessly checked the every if before.

Actually you can't with your keykode checks. No need to write every case statement from A to X.

I like to line up by braces like this:

#SelectExpand
1 if(event.type == ALLEGRO_EVENT_KEY_DOWN) 2 { 3 | iKey = event.keyboard.keycode-1; 4 | 5 | if(event.keyboard.keycode >= ALLEGRO_KEY_A && event.keyboard.keycode <= ALLEGRO_KEY_X ) 6 | { 7 | | Blocks[iKey].bFlipped = true; 8 | | 9 | | if(iChoiceCount == 0) iFirstChoice = iKey; 10 | | if(iChoiceCount == 1) iSecondChoice = iKey; 11 | | iChoiceCount++; 12 | } // end if for block choice 13 | else if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 14 | { 15 | | bQuit = true; 16 | } 17 }

AceBlkwell
Member #13,038
July 2011
avatar

Thanks Daniel,

Typically I do a little better job making my code more easily readable. Maybe not as nice as you have it but I do align connected code. I also comment better. Again in this case I was just patch working. I didn't expect anyone to be looking at it ;D.

Thanks again.

Go to: