How to get current screen resolution?

I wanted to launch desktop application with different window size depending on screen resolution. For example, if screen resolution less than 1920x1080 run application window in resolution 1024x768 else 2048x1536. Here config.lua:
[lua]
application =
{
   content =
   {
      width = 768,
      height = 1024,
   },
}
– see https://forums.coronalabs.com/topic/62568-blured-images-in-win32-app/
if not display then
    return
end
–print (display.pixelWidth, display.pixelHeight) – always 768, 1024 on desktop
if (display.pixelWidth >= 1080 or display.pixelHeight >= 1920) then
   application.content.width = 1536
   application.content.height = 2048
end
[/lua]
This works for mobile but not on desktops. How to get current screen resolution on desktop builds?

The problem is with desktops, we have to process config.lua before we know the environment. The display.* library isn’t ready to use at that point.

It’s best to have a set content area based on the standard 1:1.5 (320x480) and use:

_top = display.screenOriginY

_left = display.screenOriginX

_bottom = display.actualContentHeight

_right = display.actualContentWidth

_cx = display.contentCenterX

_cy = display.contentCenterY

Your content area can be 320x480 or 640x960 or 800x1200, whatever makes the most sense for your needs (though Apple still wants things done on a 320 point system. Android DPI’s work out to roughly 160 dpi and a typical phone is 2", so 320 works there too).

Make sure your background fills in more than the 320x480 (recommended 360x570) That way when you’re on a different shaped screen the image will fill the letterbox bars. Now some of it will bleed off the edges of the screen. If you’re on a 16:9 phone, you will get the center 320 points from the 360 point screen, but the full 570 will be visible. On an iPad, you will see the full 360 points, but the part of the screen greater than 480 points will be off screen.  The trick here is to not put an UI elements you have to have on screen as part of the background. Have them as their own display objects.

Rob

Is there any way an option to lock/restrict the aspect ratio of the window could be added?

At the moment there’s nothing to stop the user making it too long or too tall and you end up with either content off-screen or ugly black borders. Users will generally expect a game to stop them re-sizing the window outside of sensible parameters.

@Rob Miracle
I just need to know current display resolution when running desktop application (with mobile we have no any problem).
I wanted to start application in windowed mode with different window sizes depends on resolution (by setting application.content.*).
If I could not get resolution via display.*, maybe there is some parameter to native.getProperty? If no, could it be implemented?

This isn’t something we can easily do. As I explained above, when config.lua is first processed on the desktop we don’t have any information to define pixel widths and such. On mobile we have access to that information.

If you don’t want to start full screen, just start 4:3 (360x480) since it fits everywhere and let Corona handle the dynamic resolution. You can set limits in build.settings on how wide and tall you will permit it to go.

Rob

We do not currently have an API to fetch the resolution of the monitor(s).

Also, setting the content width and height in your “config.lua” does *not* set the pixel width and height of your window.  The content width and height only sets the scaled virtual coordinates to be used within the current window.

You can set the *default* width and height of the content area of the window on launch via the “build.settings” file as documented here…

   https://docs.coronalabs.com/daily/guide/distribution/win32Build/index.html#defaultviewwidth

   https://docs.coronalabs.com/daily/guide/distribution/osxBuild/index.html#defaultviewwidth

The above sets the launch width and height of the content area within the frame of the window.  Not what you are after, but this is how most desktop app developers do it.  I recommend that you set the window view width and height in your “build.settings” file to match your content width and height… or if you are using Corona’s dynamic image selection feature (ie: @2x, @3x, etc.), set it to a multiple of your image suffix scales within your “config.lua”.  The idea is that you want your window to launch to a pixel width and height that’ll load your images pixel perfect without scaling.

On Windows, Corona will also automatically scale up your window based on your “config.lua” file’s image suffix scales to best fit the monitor it’s launching in, which is the feature you’re after.  As in it’s a feature that Corona already supports.  So, if your window view width and height is 320x480 and you have an image suffix scale of @2x, then Corona will increase the view size of the window to 640x960 pixels if it’ll fit the workspace area of the monitor (ie: “workspace” area means excluding the windows taskbar).  Corona will attempt to use the large image suffix scale it can that’ll fit the monitor.

I also don’t recommend that you set the content width and height dynamically in your “config.lua” like that.  It really doesn’t make much sense to do so in a desktop app.  Especially if your configure your window to be resizable in the “build.settings” file.

Anyways, I hope this helps!

@Joshua Quick
Thank you for your answer, now it’s clear. I implemented what you recommend and it’s exactly what I needed.
 

On Windows, Corona will also automatically scale up your window based on your “config.lua” file’s image suffix scales to best fit the monitor it’s launching in, which is the feature you’re after.  As in it’s a feature that Corona already supports.

I think this should be added to desktop building guides (I didn’t find in current guides, but sorry if I miss it :))

>> I think this should be added to desktop building guides

Right.  It’s currently Windows only and is an experimental/undocumented feature that’s been there since we first went public with our Win32 app build feature.  Since PC monitor resolution and DPI can vary so wildly, the idea was that the window should launch at a reasonable size to make it feel more natural to the end user and at a scale that’s pixel perfect with your image suffix scales.  We’ll likely make it a “build.settings” option in the future since non-game desktop apps (aka: business apps) are expected to be sized based on Windows’ “DPI scale factor”, which the end-user sets under Windows Display Properties.

Oh and check out sample project “Graphics\DynamicImageResolution” that comes with the Corona Simulator.  Build it for Win32 and then try launching it under different monitors having different resolutions.  You’ll be able to see the above mentioned feature in full effect with this app.  :slight_smile:

>> It’s currently Windows only and is an experimental/undocumented feature that’s been there since we first went public with our Win32 app build feature.

Please, make it available for Mac OS X too :slight_smile:

The problem is with desktops, we have to process config.lua before we know the environment. The display.* library isn’t ready to use at that point.

It’s best to have a set content area based on the standard 1:1.5 (320x480) and use:

_top = display.screenOriginY

_left = display.screenOriginX

_bottom = display.actualContentHeight

_right = display.actualContentWidth

_cx = display.contentCenterX

_cy = display.contentCenterY

Your content area can be 320x480 or 640x960 or 800x1200, whatever makes the most sense for your needs (though Apple still wants things done on a 320 point system. Android DPI’s work out to roughly 160 dpi and a typical phone is 2", so 320 works there too).

Make sure your background fills in more than the 320x480 (recommended 360x570) That way when you’re on a different shaped screen the image will fill the letterbox bars. Now some of it will bleed off the edges of the screen. If you’re on a 16:9 phone, you will get the center 320 points from the 360 point screen, but the full 570 will be visible. On an iPad, you will see the full 360 points, but the part of the screen greater than 480 points will be off screen.  The trick here is to not put an UI elements you have to have on screen as part of the background. Have them as their own display objects.

Rob

Is there any way an option to lock/restrict the aspect ratio of the window could be added?

At the moment there’s nothing to stop the user making it too long or too tall and you end up with either content off-screen or ugly black borders. Users will generally expect a game to stop them re-sizing the window outside of sensible parameters.

@Rob Miracle
I just need to know current display resolution when running desktop application (with mobile we have no any problem).
I wanted to start application in windowed mode with different window sizes depends on resolution (by setting application.content.*).
If I could not get resolution via display.*, maybe there is some parameter to native.getProperty? If no, could it be implemented?

This isn’t something we can easily do. As I explained above, when config.lua is first processed on the desktop we don’t have any information to define pixel widths and such. On mobile we have access to that information.

If you don’t want to start full screen, just start 4:3 (360x480) since it fits everywhere and let Corona handle the dynamic resolution. You can set limits in build.settings on how wide and tall you will permit it to go.

Rob

We do not currently have an API to fetch the resolution of the monitor(s).

Also, setting the content width and height in your “config.lua” does *not* set the pixel width and height of your window.  The content width and height only sets the scaled virtual coordinates to be used within the current window.

You can set the *default* width and height of the content area of the window on launch via the “build.settings” file as documented here…

   https://docs.coronalabs.com/daily/guide/distribution/win32Build/index.html#defaultviewwidth

   https://docs.coronalabs.com/daily/guide/distribution/osxBuild/index.html#defaultviewwidth

The above sets the launch width and height of the content area within the frame of the window.  Not what you are after, but this is how most desktop app developers do it.  I recommend that you set the window view width and height in your “build.settings” file to match your content width and height… or if you are using Corona’s dynamic image selection feature (ie: @2x, @3x, etc.), set it to a multiple of your image suffix scales within your “config.lua”.  The idea is that you want your window to launch to a pixel width and height that’ll load your images pixel perfect without scaling.

On Windows, Corona will also automatically scale up your window based on your “config.lua” file’s image suffix scales to best fit the monitor it’s launching in, which is the feature you’re after.  As in it’s a feature that Corona already supports.  So, if your window view width and height is 320x480 and you have an image suffix scale of @2x, then Corona will increase the view size of the window to 640x960 pixels if it’ll fit the workspace area of the monitor (ie: “workspace” area means excluding the windows taskbar).  Corona will attempt to use the large image suffix scale it can that’ll fit the monitor.

I also don’t recommend that you set the content width and height dynamically in your “config.lua” like that.  It really doesn’t make much sense to do so in a desktop app.  Especially if your configure your window to be resizable in the “build.settings” file.

Anyways, I hope this helps!

@Joshua Quick
Thank you for your answer, now it’s clear. I implemented what you recommend and it’s exactly what I needed.
 

On Windows, Corona will also automatically scale up your window based on your “config.lua” file’s image suffix scales to best fit the monitor it’s launching in, which is the feature you’re after.  As in it’s a feature that Corona already supports.

I think this should be added to desktop building guides (I didn’t find in current guides, but sorry if I miss it :))

>> I think this should be added to desktop building guides

Right.  It’s currently Windows only and is an experimental/undocumented feature that’s been there since we first went public with our Win32 app build feature.  Since PC monitor resolution and DPI can vary so wildly, the idea was that the window should launch at a reasonable size to make it feel more natural to the end user and at a scale that’s pixel perfect with your image suffix scales.  We’ll likely make it a “build.settings” option in the future since non-game desktop apps (aka: business apps) are expected to be sized based on Windows’ “DPI scale factor”, which the end-user sets under Windows Display Properties.

Oh and check out sample project “Graphics\DynamicImageResolution” that comes with the Corona Simulator.  Build it for Win32 and then try launching it under different monitors having different resolutions.  You’ll be able to see the above mentioned feature in full effect with this app.  :slight_smile:

>> It’s currently Windows only and is an experimental/undocumented feature that’s been there since we first went public with our Win32 app build feature.

Please, make it available for Mac OS X too :slight_smile: