Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro windows show up on incorrect monitor (continued)

This thread is locked; no one can reply to it. rss feed Print
Allegro windows show up on incorrect monitor (continued)
Schyfis
Member #9,752
May 2008
avatar

Continuing from here, I'm still trying to figure out how to get Allegro to spawn its window on my primary monitor (the monitor with the Windows taskbar on it) by default.

I'm using the following code to set up the display-

I've got 4 monitors, so as expected, it crashes for values of n outside of 0, 1, 2, and 3.

I don't have a way to get the ID of the primary monitor yet, but assuming I did, passing the value to al_set_new_display_adapter still won't work right.
Only values of 0 and 1 work, and 2 and 3 spawn the window in unknown space- not on any monitor. I can see the thumbnail of the window if I hover over the icon on the taskbar, but the window itself isn't visible anywhere.

Any ideas? If I'm making a professional game, I definitely want the window to spawn in valid space, and on the primary monitor if possible.

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

OnlineCop
Member #7,919
October 2006
avatar

Are you able to return the screen coordinates of the spawned window? Then you can know whether one was spawned above where you expected it instead of to the right, etc.

Schyfis
Member #9,752
May 2008
avatar

I figured out how to retrieve the window position, and the result was completely unexpected.

For values of n, this is the window position I got.

0: 2160, 135 (valid, center of non-primary monitor)
1: -1680, 135 (valid, center of non-primary monitor)
2: -720, 32792 (invalid)
3: -720, 32792 (invalid)

Not sure what's going on here, but it looks like something in Allegro overflowed.
Fortunately, I learned I can also use al_set_window_position to put the window in valid space, since it appears that 0, 0 will always be the upper-left coordinate of the primary monitor.

However, I'd also like to note here that al_get_window_position doesn't return the same value that you previously set it to in al_set_window_position. If I set the window position to 100, 100, and then retrieve the window position again, I get 103, 125. I'm using Allegro 5.0.7, so I'm not sure if this has been fixed in the latest release. (I think this was reported once already.)

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

Peter Wang
Member #23
April 2000

Easiest way to get it fixed is to send a patch.

Schyfis
Member #9,752
May 2008
avatar

What's the proper way to submit a patch? I've never done it before, so for now I guess I'll drop it here.

I made a change to _al_win_set_window_position in wwindow.c, making it account for the window border. Now al_set_window_position and al_get_window_position are consistent with each other, both referring to the upper left corner of the client area instead of the upper left corner of the window itself.

#SelectExpand
1void _al_win_set_window_position(HWND window, int x, int y) 2{ 3 RECT client_rect; 4 WINDOWINFO wi; 5 6 wi.cbSize = sizeof(WINDOWINFO); 7 GetWindowInfo(window, &wi); 8 9 client_rect.left = x; 10 client_rect.top = y; 11 AdjustWindowRectEx(&client_rect, wi.dwStyle, false, wi.dwExStyle); 12 13 SetWindowPos( 14 window, 15 HWND_TOP, 16 client_rect.left, 17 client_rect.top, 18 0, 19 0, 20 SWP_NOSIZE | SWP_NOZORDER); 21}

Tomorrow I'll try and see what's up with those other weird coordinates regarding the display adapter.

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

Trent Gamblin
Member #261
April 2000
avatar

I tested your patch and with the code below the window flies off screen (up and to the left).

#SelectExpand
1 2#include <stdio.h> 3#include <stdlib.h> 4#include "allegro5/allegro.h" 5#include "allegro5/allegro_image.h" 6 7#include "common.c" 8 9int main(int argc, const char *argv[]) 10{ 11 const char *filename; 12 ALLEGRO_DISPLAY *display; 13 ALLEGRO_BITMAP *bitmap; 14 ALLEGRO_TIMER *timer; 15 ALLEGRO_EVENT_QUEUE *queue; 16 bool redraw = true; 17 double zoom = 1; 18 double t0; 19 double t1; 20 21 if (argc > 1) { 22 filename = argv[1]; 23 } 24 else { 25 filename = "data/mysha.pcx"; 26 } 27 28 if (!al_init()) { 29 abort_example("Could not init Allegro.\n"); 30 } 31 32 if (argc > 2) { 33 al_set_new_display_adapter(atoi(argv[2])); 34 } 35 36 al_install_mouse(); 37 al_install_keyboard(); 38 39 al_init_image_addon(); 40 41 display = al_create_display(640, 480); 42 if (!display) { 43 abort_example("Error creating display\n"); 44 } 45 46 al_set_window_title(display, filename); 47 48 t0 = al_get_time(); 49 bitmap = al_load_bitmap(filename); 50 t1 = al_get_time(); 51 if (!bitmap) { 52 abort_example("%s not found or failed to load\n", filename); 53 } 54 55 printf("Loading took %.4f seconds\n", t1 - t0); 56 57 timer = al_create_timer(1.0 / 30); 58 queue = al_create_event_queue(); 59 al_register_event_source(queue, al_get_keyboard_event_source()); 60 al_register_event_source(queue, al_get_display_event_source(display)); 61 al_register_event_source(queue, al_get_timer_event_source(timer)); 62 al_start_timer(timer); 63 64 // * 65 al_set_window_position(display, 100, 100); 66 67 while (1) { 68 69 // * 70 int x, y; 71 al_get_window_position(display, &x, &y); 72 al_set_window_position(display, x, y); 73 printf("x=%d y=%d\n", x, y); 74 75 ALLEGRO_EVENT event; 76 al_wait_for_event(queue, &event); 77 if (event.type == ALLEGRO_EVENT_DISPLAY_ORIENTATION) { 78 int o = event.display.orientation; 79 if (o == ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES) { 80 printf("0 degrees\n"); 81 } 82 else if (o == ALLEGRO_DISPLAY_ORIENTATION_90_DEGREES) { 83 printf("90 degrees\n"); 84 } 85 else if (o == ALLEGRO_DISPLAY_ORIENTATION_180_DEGREES) { 86 printf("180 degrees\n"); 87 } 88 else if (o == ALLEGRO_DISPLAY_ORIENTATION_270_DEGREES) { 89 printf("270 degrees\n"); 90 } 91 else if (o == ALLEGRO_DISPLAY_ORIENTATION_FACE_UP) { 92 printf("Face up\n"); 93 } 94 else if (o == ALLEGRO_DISPLAY_ORIENTATION_FACE_DOWN) { 95 printf("Face down\n"); 96 } 97 } 98 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) 99 break; 100 if (event.type == ALLEGRO_EVENT_KEY_CHAR) { 101 if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) 102 break; 103 if (event.keyboard.unichar == '1') 104 zoom = 1; 105 if (event.keyboard.unichar == '+') 106 zoom *= 1.1; 107 if (event.keyboard.unichar == '-') 108 zoom /= 1.1; 109 if (event.keyboard.unichar == 'f') 110 zoom = (double)al_get_display_width(display) / 111 al_get_bitmap_width(bitmap); 112 } 113 if (event.type == ALLEGRO_EVENT_TIMER) 114 redraw = true; 115 116 if (redraw && al_is_event_queue_empty(queue)) { 117 redraw = false; 118 al_clear_to_color(al_map_rgb_f(0, 0, 0)); 119 if (zoom == 1) 120 al_draw_bitmap(bitmap, 0, 0, 0); 121 else 122 al_draw_scaled_rotated_bitmap( 123 bitmap, 0, 0, 0, 0, zoom, zoom, 0, 0); 124 al_flip_display(); 125 } 126 } 127 128 al_destroy_bitmap(bitmap); 129 130 return 0; 131} 132 133/* vim: set sts=4 sw=4 et: */

It actually looks like this was fixed the opposite way in git already. It uses to top left corner of the window decoration. So your patch just mismatched with get... in git.

Thomas Fjellstrom
Member #476
June 2000
avatar

Yeah, I was pretty sure the get/set window position was already fixed in git. The other problem however I'm not so sure.

--
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

Erin Maus
Member #7,537
July 2006
avatar

I have yet to test the following code (I only have one monitor) but, in theory, it should position a misplaced window properly:

#SelectExpand
1static void CenterDisplay(ALLEGRO_DISPLAY* display, bool fullscreen) 2{ 3 /* MinGW doesn't have MonitorFromWindow right now (!!!) so import it. */ 4 typedef HMONITOR (* WINAPI Func)(HWND hwnd, DWORD dwFlags); 5 HMODULE user32 = LoadLibrary("user32.dll"); 6 Func MonitorFromWindow = (Func)GetProcAddress(user32, "MonitorFromWindow"); 7 8 /* Now the display can be centered! */ 9 HWND handle = al_get_win_window_handle(display); 10 HMONITOR monitor = MonitorFromWindow(handle, 2 /* MONITOR_DEFAULTTONEAREST */); 11 MONITORINFO info; 12 RECT windowSize; 13 int x, y; 14 int monitorWidth, monitorHeight; 15 int windowWidth, windowHeight; 16 17 info.cbSize = sizeof(MONITORINFO); 18 GetMonitorInfo(monitor, &info); 19 20 GetWindowRect(handle, &windowSize); 21 22 monitorWidth = info.rcMonitor.right - info.rcMonitor.left; 23 monitorHeight = info.rcMonitor.bottom - info.rcMonitor.top; 24 25 windowWidth = windowSize.right - windowSize.left; 26 windowHeight = windowSize.bottom - windowSize.top; 27 28 x = info.rcMonitor.left + (monitorWidth - windowWidth) / 2; 29 y = info.rcMonitor.top + (monitorHeight - windowHeight) / 2; 30 31 if (!fullscreen) 32 { 33 SetWindowPos(handle, HWND_TOP, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); 34 } 35 else 36 { 37 SetWindowPos(handle, HWND_TOP, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER); 38 al_resize_display(display, monitorWidth, monitorHeight); 39 } 40}

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

Schyfis
Member #9,752
May 2008
avatar

Trent:
The code you posted combined with the patch I posted does not move the window for me.
Maybe one of the Windows functions is returning something unexpected on different versions of Windows? (I'm on Windows 7 Pro)

Aaron:
I tried your code, but I got the following error. I'm not sure what it means.
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

Side note- I had to change HMODULE user32 = LoadLibrary("user32.dll"); to HMODULE user32 = LoadLibrary((LPCWSTR)L"user32.dll"); because my IDE was yelling at me about it. Not sure if that's what's causing the problem or not.

________________________________________________________________________________________________________
[freedwill.us]
[unTied Games]

Erin Maus
Member #7,537
July 2006
avatar

If you're using MSVC you should be able to just use MonitorFromWindow directly. The extra fluff I have in that method can then be ignored...

As far as the ESP problem, it looks like a calling convention problem. Why it does that, I'm not sure. Just try it using MonitorFromWindow directly instead of using LoadLibrary and company.

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

Go to: