Getting Allegro to Play Nice with Windows Scaling Settings


I noticed this issue when demoing a program I wrote on another person's machine. Took me a while to figure out why seemingly everything was wrong with the size of the window/coordinates etc.

So Windows appears to scale everything with this setting and it makes my program go offscreen etc was wondering what the usual counter to this is.

Although I don't have access to a development environment right now I pulled this code off an archive of my project:

1#include <allegro5/allegro_native_dialog.h> 2#include "../gamedata.h" 3#include "../../resources/resources.h" 4#include "../../defines/game_def.h" 5 6bool gamedata :: init_screen() 7{ 8 WindowWidth = GAME_WIDTH * WidthScale; 9 WindowHeight = GAME_HEIGHT * HeightScale; 10 11 /* Monitor info */ 12 13 al_get_monitor_info(0, &info); 14 DeviceWidth = info.x2 - info.x1; 15 DeviceHeight = info.y2 - info.y1; 16 17 Screen = al_create_display(WindowWidth, WindowHeight); 18 if(Screen == nullptr) 19 { 20 al_show_native_message_box(Screen, "Error!", "", "Failed to create the display.", nullptr, ALLEGRO_MESSAGEBOX_ERROR); 21 return false; 22 } 23 24 al_register_event_source(EventQueue, al_get_display_event_source(Screen)); 25 26 Buffer = al_create_bitmap(GAME_WIDTH, GAME_HEIGHT); ResourceManager.attach_resource(Buffer); 27 28 return true; 29}

Currently I allow non-square pixels by scaling width and height by independent integer amounts.

#define GAME_WIDTH 384 // undecided, 5x4 stretch ratio with rectangular pixels achieves 16:9 screen
#define GAME_HEIGHT 270

So, what can I do about this?


Can you describe what went wrong? The expected behavior is for Allegro to create windows with the specified pixel sizes, which means that Allegro will essentially ignore that scaling setting. Are you observing something else?


Yes, I think Allegro basically thought the device size was that much larger so when I tried to center the window it was offscreen and also the maximum size was bigger than the actual available screen size.

1#include "gamedata.h" 2#include "../defines/game_def.h" 3 4void gamedata :: resize_screen(unsigned short int wscale, unsigned short int hscale) 5{ 6 WidthScale = wscale; HeightScale = hscale; 7 8 if (!Fullscreen) 9 { 10 if (al_resize_display(Screen, GAME_WIDTH * wscale, GAME_HEIGHT * hscale)) 11 { 12 WindowWidth = al_get_display_width(Screen); 13 WindowHeight = al_get_display_height(Screen); 14 15 al_set_window_position(Screen, DeviceWidth/2 - WindowWidth/2, DeviceHeight/2 - WindowHeight/2); 16 } 17 } 18 else 19 { 20 WindowWidth = DeviceWidth; 21 WindowHeight = DeviceHeight; 22 } 23} 24 25void gamedata :: resize_screen(bool wh, unsigned short int newscale) 26{ 27 if (wh) resize_screen(WidthScale, newscale); // new height 28 else resize_screen(newscale, HeightScale); // new width 29} 30 31void gamedata :: resize_to_max() 32{ 33 menu_game_scaler* ws = WidthScaler; 34 menu_game_scaler* hs = HeightScaler; 35 36 if (Fullscreen) // set to max 37 { 38 ws->Index = (ws->Factors.size() - 1); 39 hs->Index = (hs->Factors.size() - 1); 40 } 41 else // less than max so window fits on desktop // TODO remove hard-coded windowed hack 42 { 43 ws->Index = (ws->Factors.size() - 2); 44 hs->Index = (hs->Factors.size() - 2); 45 } 46 47 resize_screen(ws->Factors[ws->Index], hs->Factors[hs->Index]); 48 ws->update(); hs->update(); 49}

I can't remember the syntax for highlighting a line in code on here.

Because my calculations are relative to monitor size maybe I need to do something different here?

    /* Monitor info */

    al_get_monitor_info(0, &info);
    DeviceWidth = info.x2 - info.x1;
    DeviceHeight = info.y2 - info.y1;


Do you have the source available to this game? I'm struggling to understand what you're trying to accomplish here. I checked, and al_get_monitor_info returns the correct values. I can believe al_set_window_position might be broken, I'll test that.


I authored it but been on hiatus since 2017. I have access to the code but no IDE at the moment

What I wanted (and had been working as expected until that Windows issue) was the ability to have automatic scaling options or manual scaling to change shape of pixels to preference (retro)


With automatic selected it detects the screen size and will make the game area as big as possible in integer scaled amounts without going offscreen. For 16:9 ratio this will not be square pixels so can manually adjust scale for square pixels, but less screen coverage.

When I resize a windowed screen I re-center it on the device

Admittedly I can't specifically remember if the game size itself was bigger due to the window scaling but I definitely know the centering was wrong (as though it thought the screen was bigger) but could've been due to an oversight in my code or something.

Thread #618454. Printed from