Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » mouse y and line y not aligned when ALLEGRO_MENU displayted

This thread is locked; no one can reply to it. rss feed Print
mouse y and line y not aligned when ALLEGRO_MENU displayted
awergh
Member #15,474
January 2014

So I have an editor I've been working on for a project.

Basically I have a grid of 48x48 cells that I can click on with the mouse to set information for each cell. Now I decided to add an ALLEGRO_MENU since I'm using Allegro 5.2 and have access to it. The problem is that when I click on a cell to add a tile it toggles the wrong cell. What it seems is that the lines are being drawn at every 48 pixels but the ev.mouse.x value is not matching to the pixel values for my lines.

Additionally this works perfectly fine when the menu is not displayed its only when the menu is displayed (effectively reducing the usable screen area) that the problem occurs.

I feel there could be something really obvious I am missing but I'm not sure what it is.

Here is some of the important bits of code

#SelectExpand
1//setting mouseX and mouseY 2else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 3{ 4 mouseX = ev.mouse.x; 5 mouseY = ev.mouse.y; 6} 7 8//adding a tile 9addTile(mouseX, mouseY); 10 11//getting tile values from mouse values 12int tileX = mouseXToTileX(mouseX); 13int tileY = mouseYToTileY(mouseY); 14 15 16//mouseYToTileY (mouseY) / (16*3) 17return (mouseY) / (TILE_HEIGHT*scaleFactor_); 18 19 20 21//draw horizontal lines 22for (int i = 0; i < SCREEN_HEIGHT; i += TILE_HEIGHT) 23{ 24float y = i*scaleFactor_; 25 26//wordWidth_ = 160*2*3 27//scaleFactor_ = 3 28//MODE0_X_SCALE = 2 29//SCREEN_HEIGHT = 200 30//looks like x2 is way to big but I don't think thats a cause of my issue 31al_draw_line(0, y, worldWidth_*scaleFactor_*MODE0_X_SCALE, y, al_map_rgb(255, 255, 255), 1); 32}

As an edit I have produced the same behaviour with a subset of the code.
This should make it clearer what I am trying to do and what I might be doing wrong.
This was compiled with Visual Studio 2015 on Windows.

#SelectExpand
1#include <stdio.h> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_native_dialog.h> 5 6const int WIDTH_ = 800; 7const int HEIGHT_ = 600; 8const int TILE_SIZE_ = 64; 9const int CELLS_X_ = WIDTH_ / TILE_SIZE_; 10const int CELLS_Y_ = HEIGHT_ / TILE_SIZE_; 11 12char grid_[CELLS_X_][CELLS_Y_] = { {0} }; 13 14void drawGridLines(); 15void drawCells(); 16const int mousePositionToTilePosition(long int mousePosition); 17void updateCell(long int mouseX, long int mouseY); 18 19int main() 20{ 21 if (!al_init()) 22 { 23 printf("allegro init failed"); 24 return -1; 25 } 26 27 ALLEGRO_DISPLAY* display = al_create_display(WIDTH_, HEIGHT_); 28 29 if (!display) 30 { 31 printf("create display failed"); 32 return -2; 33 } 34 35 al_init_native_dialog_addon(); 36 al_init_primitives_addon(); 37 38 bool mouseDown = false; 39 bool running = true; 40 long int mouseX = 0; 41 long int mouseY = 0; 42 43 ALLEGRO_MENU* menu = al_create_menu(); 44 al_append_menu_item(menu, "File", 0, 0, NULL, NULL); 45 al_set_display_menu(display, menu); 46 47 al_install_mouse(); 48 ALLEGRO_EVENT_QUEUE* eventQueue = al_create_event_queue(); 49 al_register_event_source(eventQueue, al_get_mouse_event_source()); 50 al_register_event_source(eventQueue, al_get_display_event_source(display)); 51 al_register_event_source(eventQueue, al_get_default_menu_event_source()); 52 53 ALLEGRO_EVENT ev; 54 55 while (running) 56 { 57 al_wait_for_event(eventQueue, &ev); 58 59 if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 60 { 61 running = false; 62 } 63 else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) 64 { 65 mouseDown = true; 66 } 67 else if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) 68 { 69 mouseDown = false; 70 } 71 else if (ev.type == ALLEGRO_EVENT_MOUSE_AXES) 72 { 73 mouseX = ev.mouse.x; 74 mouseY = ev.mouse.y; 75 } 76 77 if (mouseDown) 78 { 79 updateCell(mouseX, mouseY); 80 } 81 82 al_clear_to_color(al_map_rgb(0, 0, 0)); 83 drawCells(); 84 drawGridLines(); 85 al_flip_display(); 86 } 87 88 al_set_display_menu(display, NULL); 89 al_destroy_menu(menu); 90 91 al_uninstall_mouse(); 92 al_shutdown_native_dialog_addon(); 93 al_shutdown_primitives_addon(); 94 al_destroy_display(display); 95 return 0; 96} 97 98void drawGridLines() 99{ 100 //draw vertical lines 101 for (int i = 0; i < WIDTH_; i += TILE_SIZE_) 102 { 103 al_draw_line(i, 0, i, HEIGHT_, al_map_rgb(255, 255, 255), 1); 104 } 105 106 //draw horizontal lines 107 for (int i = 0; i < HEIGHT_; i += TILE_SIZE_) 108 { 109 al_draw_line(0, i, WIDTH_, i, al_map_rgb(255, 255, 255), 1); 110 } 111} 112 113void drawCells() 114{ 115 for (int i = 0; i < CELLS_X_; ++i) 116 { 117 for (int j = 0; j < CELLS_Y_; ++j) 118 { 119 if (grid_[i][j]) 120 { 121 int x = i*TILE_SIZE_; 122 int y = j*TILE_SIZE_; 123 124 al_draw_filled_rectangle(x, y, x + TILE_SIZE_, y + TILE_SIZE_, al_map_rgb(255, 255, 0)); 125 } 126 } 127 } 128} 129 130const int mousePositionToTilePosition(long int mousePosition) 131{ 132 return mousePosition / TILE_SIZE_; 133} 134 135void updateCell(long int mouseX, long int mouseY) 136{ 137 int tileX = mousePositionToTilePosition(mouseX); 138 int tileY = mousePositionToTilePosition(mouseY); 139 140 int value = grid_[tileX][tileY]; 141 142 if (value) 143 { 144 value = 0; 145 } 146 else 147 { 148 value = 1; 149 } 150 151 grid_[tileX][tileY] = value; 152}

utz000
Member #16,715
July 2017

Try initializing your addons before creating the display.

awergh
Member #15,474
January 2014

Initialising the addons before creating the display did not make any difference. :(

Kev Callahan
Member #6,022
July 2005
avatar

Tried your second source code and it's pretty much pixel perfect under Linux X11..
(Apart from the half-squares at the bottom select the top ones)..

btw, why do you have variables with a trailing underscore..?! :'(

My Makefile

#SelectExpand
1 2STATICLIBS=/usr/local/lib/liballegro_acodec-static.a \ 3 /usr/local/lib/liballegro_font-static.a \ 4 /usr/local/lib/liballegro_memfile-static.a \ 5 /usr/local/lib/liballegro_ttf-static.a \ 6 /usr/local/lib/liballegro_audio-static.a \ 7 /usr/local/lib/liballegro_image-static.a \ 8 /usr/local/lib/liballegro_dialog-static.a \ 9 /usr/local/lib/liballegro_primitives-static.a \ 10 /usr/local/lib/liballegro_color-static.a \ 11 /usr/local/lib/liballegro_main-static.a \ 12 /usr/local/lib/liballegro-static.a 13 14DYNAMICLIBS=-lfreetype -lpng16 -ljpeg -lGL -lGLU -lX11 -lXcursor -lXrandr -lgthread-2.0 -lXinerama -lXi 15 16test_pc: Makefile test.cc 17 g++ -Wno-write-strings -fpermissive -DINTEL -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 \ 18 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lm -lcairo \ 19 -lpango-1.0 -lfreetype -lfontconfig -lgobject-2.0 -lgmodule-2.0 20 -lgthread-2.0 -lrt -lglib-2.0 -lpthread \ 21 test.cc \ 22 -o test_pc \ 23 -I/usr/local/include/allegro5 \ 24 $(STATICLIBS) $(DYNAMICLIBS)

Chris Katko
Member #1,881
January 2002
avatar

A quick glance shows they're all globals. Not a bad idea to highlight variables that have side-effects outside your functions.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

awergh
Member #15,474
January 2014

I was wondering if it would be platform specific.
I was in the process of "quickly" spinning up a Debian Stretch vm so I could be lazy and just install the allegro 5.2 package (my other Debian installs are all Jessie still) but my internet being dreadful means I probably won't have it installed until tomorrow.

Thanks for the makefile.

As for the trailing underscore this is because of a particular project at uni where we had a coding standard that included using a trailing underscore for member variables.
I found it convenient and so it stuck however I've never been quite sure what to do with it in my main or the top of a c/cpp file. I try to be consistent but I'm not always as consistent as I should be.

Its effectively the equivalent of doing m_foo as opposed to foo_ or _foo is another option.

Kev Callahan
Member #6,022
July 2005
avatar

Hehe, fair enough - never seen such a convention before.
Being _ inconsistent is fine, that's why the words consistent_ & inconsistent_ exist :)

Yeah, maybe it is platform specific - the native_dialog stuff can be a bit of a bitch in my experience on different platforms.

Peter Hull
Member #1,136
March 2001

I also couldn't reproduce your issue on Windows.

Clicking the bottom row/right column goes out of the bounds of the array (no checks on tileX/tileY in updateCell) - I suppose just because it's test code, but it might explain Kev's observation.

If I added some submenus to that menu (see code below), then I did see an issue. If I open up the menu and move the mouse off it, the first click closes the menu, and the next click is registered at a spot that's beneath where the menu was. Is that what you're referring to?

Pete

[edit] When you call updateCell you should take the mouse X,Y from the event that caused the mouse down, not a previous mouse move. When you open the menu and move off it, you don't get any mouse move events until you click (to close the menu) and move the mouse again. That explains what I saw.

  ALLEGRO_MENU *menu = al_create_menu();
  ALLEGRO_MENU *sub = al_create_menu();
  al_append_menu_item(sub, "Open", 0, 0, 0, 0);
  al_append_menu_item(sub, "Save", 0, 0, 0, 0);
  al_append_menu_item(sub, "Destroy Universe", 0, 0, 0, 0);
  al_append_menu_item(menu, "File", 0, 0, NULL, sub);
  al_set_display_menu(display, menu);

awergh
Member #15,474
January 2014

Yeah its supposed to be the minimum needed to show what I was experiencing, so no bounds checking.

I've attached what happens when I click the mouse at the position in the grid where the cell above is activated instead of the one I expected.
As I get further down the grid the amount it is off gets bigger.

So effectively my mouseX and mouseY variables are redundant and I should use the ones from the event? It doesn't fix my issue but I'll keep that in mind.

Dizzy Egg
Member #10,824
March 2009
avatar

It's not a rounding issue is it?

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

Peter Hull
Member #1,136
March 2001

OK, yes I see it now. It's like a sort of scaling the client area to the area including the menu (or something)
{"name":"610992","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/b\/9be93d840ce36d70df67589d1dd946d0.jpg","w":801,"h":627,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/b\/9be93d840ce36d70df67589d1dd946d0"}610992
SiegeLord might know why, possibly due to the HiDPI scaling code?

[edit] Possibly I am going mad. Can you take some screenshots and measure them with MSPaint or something? Basically it looks like, if you request an 800x600 display then put a menu onto it, the client area will get squashed a little bit in the Y direction. On my system, the client area goes to something like 582 pixels, so your grid squares are no longer quite square. Is this in the documentation? I haven't checked.

Kev Callahan
Member #6,022
July 2005
avatar

Yeah I thought when you opened a menu everything shifts down a bit.. It doesn't draw over it.
Is this what you're seeing?

I usually get round this by resizing the draw to the display when the menu is enabled.

Guess you'd have to convert mouse coordinates as well.

Peter Hull
Member #1,136
March 2001

I think this is a bug and you should file an issue on it:
https://github.com/liballeg/allegro5/issues

Pete

Go to: