Android screen resolution problem [Help]

artem123

Member
Hi all.

So I was following Pixelated Pope's YT videos regarding scaling for devices. I am trying to apply scaling in Android devices. I followed the code in pixelated pope's video but did some changes since some functions are already obsolete in Game Maker Studio 2.3.

Here is the scaling code:

CREATE

GML:
ideal_width = 0
ideal_height = 2340 //maximum height supported

aspect_ratio = display_get_width()/display_get_height();

ideal_width = round(ideal_height*aspect_ratio)

if (ideal_width & 1)
   ideal_width++;

camera = camera_create_view(0, 0, ideal_width, ideal_height, 0, -1, -1, -1, 0, 0); 

for (var i=1; i <= room_last; i++)
{
   if (room_exists(i))
    {
      room_set_camera(i,0,camera); //originally room_set_view in pixelated pope's video, but this function is obsolete in GMS 2.3
      room_set_view_enabled(i, true);
    }
}

surface_resize(application_surface, ideal_width, ideal_height);
window_set_size(ideal_width, ideal_height);   

room_goto(room_next(room))
That scaling code is persistent and is only located in the room_init which is the first room. room_init has a dimension of 1080 x 1920 (portrait). Viewports are enabled (1080 x 1920). Under Android settings -> graphics -> scaling, Keep aspect ratio is checked. The other rooms have also a dimension of 1080 x 1920. The game is designed for a 1080 x 1920 phone, which is one of the most common phone resolutions, that is why I make the rooms 1080 x 1920.

Here lies the problem. I tested this in 3 devices (Samsung Galaxy J1 2016, some locally made low spec tablet, and Samsung Galaxy A30), as you can see in this picture, they still got the black bars.
Pic:
IMG_1034.jpeg

I would like to know some of your insights regarding how to remove the black bars and make it fit to their respective resolutions? Any links can sure help.

Thank you.
 

Attachments

gkri

Member
Here is a script that will handle the resolution for you:

GML:
function set_resolution(_design_value, _is_design_value_vertical, _is_orientation_vertical, _resize_all_rooms, _scale, _overide_other_value) {
///@func set_resolution(design_value, is_design_vertical, is_orientation_vertical, resize_all_rooms, [scale], overide_other_value [optional])
///@arg _design_value                  real        width or height of ideal resolution
///@arg _is_design_value_vertical      boolean    When true, the design_value is condidered as height; when false, as width
///@arg _is_orientation_vertical      boolean    When true, the screen orientation is considered as portrait; when false, as landscape
///@arg _resize_all_rooms              boolean    When true: Rooms (except room index 0), will be resized to resulting value
///@arg _scale(optional)              real        (Optional) Scale the game window to better fit the monitor. Will ommited on any non windows and non macos builds
///@arg _overide_other_value(optional) real        (Optional) Overides the value other than the design_value arg for a custom aspect ratio. Will ommited like scale arg

    ///Written by George Kritikos (Harpwood studio) - Based on code written by Pixelpope
    ///@License MIT License https://opensource.org/licenses/MIT

    if _design_value == undefined or _is_design_value_vertical == undefined or _is_orientation_vertical == undefined or _resize_all_rooms == undefined
    then show_error("Error set_resolution script: Incorect number of arguments. Expected number of arguments are minimum of 4 to maximum of 6.", true);

    if 0 then return argument[0];
    if _scale == undefined then _scale = 1;
    //detect os_type only if is GMS2 IDE approprate
    _os_type = os_type == os_windows ? os_windows : os_macosx;

    // The design value is either the design width or height. Every calculation in build with Test -> VM get a temporary scaling
    var _desing_width    = os_type == _os_type ? _design_value * _scale : _design_value;
    var _desing_height    = os_type == _os_type ? _design_value * _scale : _design_value;

    var _real_width, _real_height, _aspect_ratio, _ideal_width, _ideal_height;

    if _is_orientation_vertical
    {
        //dirty way to get portait orientation for os_windows/os_macosx
        _real_width        = os_type == _os_type ? display_get_height()  * _scale : display_get_width();
        _real_height    = os_type == _os_type ? display_get_width()    * _scale : display_get_height();
        _aspect_ratio    = _real_width >= _real_height ? _real_height / _real_width : _real_width / _real_height;

        if _is_design_value_vertical    //The design value is reffering to vertical so we calculate the horizontal
        {
            _ideal_height = _desing_height;
            if os_type == _os_type then _ideal_width = _overide_other_value == undefined ? round(_ideal_height * _aspect_ratio) : _overide_other_value * _scale;
            else _ideal_width = round(_ideal_height * _aspect_ratio);
        }
        else                            //and vice versa
        {
            _ideal_width = _desing_width;
            if os_type == _os_type then _ideal_height = _overide_other_value == undefined ? round(_ideal_width / _aspect_ratio) : _overide_other_value * _scale;
            else _ideal_height = round(_ideal_width / _aspect_ratio);
        }

    }
    else
    {
        _real_width        = os_type == _os_type ? display_get_width()  * _scale : display_get_width();
        _real_height    = os_type == _os_type ? display_get_height() * _scale : display_get_height();
        _aspect_ratio    = _real_width >= _real_height ? _real_height / _real_width : _real_width / _real_height;


        if _is_design_value_vertical    //The design value is reffering to vertical so we calculate the horizontal
        {
            _ideal_height = _desing_height;
            if os_type == _os_type then _ideal_width = _overide_other_value == undefined ?  round(_ideal_height / _aspect_ratio) : _overide_other_value * _scale;
            else _ideal_width =  round(_ideal_height / _aspect_ratio);
        }
        else                            //and vice versa
        {
            _ideal_width = _desing_width;
            if os_type == _os_type then _ideal_height = _overide_other_value == undefined ? round(_ideal_width * _aspect_ratio) : _overide_other_value * _scale;
            else _ideal_height = round(_ideal_width * _aspect_ratio);
        }
    }

    //make the results more pixel perfect friendly
    if _ideal_width & 1 then _ideal_width++;
    if _ideal_height & 1 then _ideal_height++;

    if _resize_all_rooms //apply resolution results to all rooms?
    {
        for (var i = 1; i < room_last; i++) //all rooms except room with index 0, which usually is the init room
        {
            if room_exists(i)
            {
                room_set_width(i, _ideal_width);
                room_set_height(i, _ideal_height);
            }
        }
    }

    application_surface_enable(false);  // false as default behaviour
    window_set_size(_ideal_width, _ideal_height);
    surface_resize(application_surface, _real_width, _real_height);

    //remove the temporary scaling if building with Test -> VM and apply results in global vars for further use
    global.ideal_width = os_type == _os_type ? _ideal_width / _scale : _ideal_width;
    global.ideal_height = os_type == _os_type ? _ideal_height / _scale : _ideal_height;


}

And here you will find instructions of how to use it.

Simply use the advanced resolution manager on your very first (init) room and it will handle all the rooms of your game except the very first (init room).

Then at EVERY room call the next script once, passing x1 =0, y1 = 0 and the x2 = global.ideal_width and y2 = global.ideal_height values from previous script :

GML:
function set_camera(x1, y1, x2, y2) {
///@func set_camera
///@arg x1
///@arg y1
///@arg x2
///@arg y2
  
        view_enabled = true;
        view_visible[0] = true;
        view_wport[0] = x2;
        view_hport[0] = y2;

        view_camera[0] = camera_create_view(x1, y1, view_wport[0], view_hport[0], 0, -1, -1, -1, 0, 0);
}


Then for a smooth GUI developing experience, you can use this script ONCE at the very next room (after init room). It should be in effect for the rest of the game, unless you change the GUI size yourself.

GML:
function set_gui_size(_width, _height) {

    if 0 then return argument[0];

    if _height == undefined then _height = HEIGHT * _width / WIDTH;
    display_set_gui_size(_width, _height);
  
    global.width_gui = _width;
    global.height_gui = _height;
}

The 2nd argument is optional, so pass as width your ideal width, the script will figure out the ideal height...

If you need more help let me know!
 
Last edited:

gkri

Member
I am glad that is usefull to you (Χαίρομαι που σου είναι χρήσιμο)!
 
Top