[LUA Script] dynamic window resolution table for your game settings

  • #1, z sebastianSaturday, 29. April 2017, 18:33 hodinky 8 years ago
    Problem: When you want to set your window resolution (window mode) in your settings menu you have to make a selection of different resolutions which should be available for your game. All hardcoded.

    Here its difficult to pick the right choices from the start because you normally don't know if a resolution you make available here is supported by the monitor of the player or if you just give too less options (max resolution is 1920x1080 but the player has a 8k monitor --> too small window). 
    The other way around this can end up in an e.g. 1920x1080 pixel window on a 1600x900 pixel monitor which is not that gooood.

    Of course you can make your window resizable, but i find this a bit too wanky and unprecisely.

            BUT BEHOOOOLD!

    Solution: You have to check the available screen resolutions from the players monitor and present the player only the resolutions which are less than or eqeal your monitors resolutions.

    Discovering the possibility to check which resolutions the players display supports (Thanks to SimonS) I made a script which checks them for 16:9 aspect ratio and add them to a table.
    I also made it sorted from low to high because the normal visionaire function has them unsorted (for me) . So mainly my script sorts them right and adds only wanted ratios inside a new table to work with.

    You can edit your settings/config menu/scripts to check against this table.

    Here is how:

    Inside a definition script i set a global table 
    displayModes = {}  

    Also i created a function with the same name which gets all display resolutions and adds only these to the displayModes table which are 16:9
    -- * get all Display Resolutions and put them in the displayModes Table * --
    function DisplayModes()
      local modes = system.getDisplayModes() --builtin Visionaire function returns all display resolutions
      displayModes[1] = "384x216" -- insert hardcoded supported resolution sizes
      displayModes[2] = "768x432"
      displayModes[3] = "1152x648" 
      displayModes[4] = "1536x864"
      for k,v in spairs(modes, function(t,a,b) return t[b] < t[a] end) do
        if is16x9(k) then
          table.insert(displayModes,k) -- add all 16:9 resolutions supported by the display
        end
      end
    end
    
    -- * determine if a given resolution string XxY is 16:9 aspect ratio* --
    function is16x9(res_string)
      local res = {}
      for i in string.gmatch(res_string, '([^x]+)') do table.insert(res,i) end -- res[1] = width , res[2] = height  
      if tonumber(res[2]) == (tonumber(res[1])/16)*9 then return true else return false end
    end

    spairs is a function which sorts them from low to high (from http://stackoverflow.com/questions/15706270/sort-a-table-in-lua):
    -- * sort functions for keys * --
    function spairs(t, order) -- collect the keys
        local keys = {}
        for k in pairs(t) do keys[#keys+1] = k end -- if order function given, sort by it by passing the table and keys a, b,
                                                       -- otherwise just sort the keys (order function is "function(t,a,b) return t[b] < t[a] end)")
        if order then
            table.sort(keys, function(a,b) return order(t, a, b) end)
        else
            table.sort(keys)
        end
                                                       -- return the iterator function
        local i = 0
        return function()
            i = i + 1
            if keys[i] then
                return keys[i], t[keys[i]]
            end
        end
    end

    now the lua table displayModes has all the 16:9 resolutions for the current players monitor + some hard coded inside to check against in your config file or make them available in your settings menu.

    In your game config loading script you can use this:
      DisplayModes()
      print("- - DISPLAY 16:9 RESOLUTIONS - -")
      for i=1, #displayModes do
        print(" "..string.format("%02d",i)..": "..displayModes[i])
      end

    which will print all the items of the table to your log.

    Maybe its useful for some of you.  =)

    ~Sebastian

    Kapitán

    2346 Posts


  • #2, z afrlmeSaturday, 29. April 2017, 19:59 hodinky 8 years ago
    Aye, & now you can display resolutions with new Lua draw function or by using a value & editing/checking the string part of it.

    When I saw your post title I was assuming it would be about updating game resolution during runtime, which is possible, but it's buggy - some kind of aspect ratio issue.

    P.S: if you include this line in your config.ini...

    RESIZEABLE = Yes

    ... then the person playing your game will be able to manually resize the window to fit their screen by dragging the edges/corners of the window. wink

    Imperátor

    7285 Posts

  • #3, z sebastianSaturday, 29. April 2017, 20:04 hodinky 8 years ago
    P.S: if you include this line in your config.ini...

    RESIZEABLE = Yes

    ... then the person playing your game will be able to manually resize the window to fit their screen by dragging the edges/corners of the window. wink

    i know ;D
    Of course you can make your window resizable, but i find this a bit too wanky and unprecisely.


    Kapitán

    2346 Posts

  • #4, z afrlmeSaturday, 29. April 2017, 20:07 hodinky 8 years ago
    P.S: if you include this line in your config.ini...

    RESIZEABLE = Yes

    ... then the person playing your game will be able to manually resize the window to fit their screen by dragging the edges/corners of the window. wink

    i know ;D
    Of course you can make your window resizable, but i find this a bit too wanky and unprecisely.


    Yeah, it would be nice if it could be dragged in proportion by holding down shift when dragging or something.

    Imperátor

    7285 Posts

  • #5, z sebastianSunday, 30. April 2017, 00:42 hodinky 8 years ago
    Added the resolution list to my game (idea) and did some kind of dropdown menu with it. I like it smile :

    If anyone is interested: https://cl.ly/0l1u0C1F2E3t (recorded in 1080p)

    Kapitán

    2346 Posts

  • #6, z afrlmeSunday, 30. April 2017, 00:48 hodinky 8 years ago
    Added the resolution list to my game (idea) and did some kind of dropdown menu with it. I like it smile :

    If anyone is interested: https://cl.ly/0l1u0C1F2E3t (recorded in 1080p)
    Nice & nice looking menu you got there. smile

    How did you sort out the popup scrolling resolution box? Animation? Display object texts & value based if queries?

    Imperátor

    7285 Posts

  • #7, z sebastianSunday, 30. April 2017, 01:02 hodinky 8 years ago
    thanks, mate.

    It's normal object text (for a button) which dispalys a value string which got put together out of the 3 resolutions (starting at index 1, offset 0) out of the global displayModes table.

    clicking on the arrows sets an offset value +/-1 and calling the "show object text" action to redraw all displayed texts. Because the offset is heigher/lower than before a function refills this ValueString with a different built string.

    clicking one of the resolutions sets the resolution of the displayed entry (1-3) + offset and sets the there found resolution from the global displayModes table to the current window resolution.

    The displayModes table could be as large as needed depending on the supported screen resolution of the players monitor. So the length of it is dynamic, text is also dynamic.

    Mostly Lua stuff and nearly no nested condition / value based if queries

    Kapitán

    2346 Posts

  • #8, z afrlmeSunday, 30. April 2017, 01:17 hodinky 8 years ago
    I used a display object text based resolution option for something recently too - might have been Paradigm. I forget. grin

    It's a much nicer solution than using values or conditions as you can query the string value of the value itself against a table or whatever you like.

    Do people play window mode at same resolution as their monitor resolution? I tend to play fullscreen. The only thing that makes sense to me at monitor resolution would be borderless window mode for people that want to record let's play video or stream on twitch or something.

    Imperátor

    7285 Posts

  • #9, z sebastianSunday, 30. April 2017, 01:31 hodinky 8 years ago
    yes. and thats why the selectable resolution should be less than the screen (because then fullscreen makes more sense). 
    I kept the max resolution in the table though. 
    Need to make the last field inside the table nil to have only lower than max display resolutions selectable ...

    Its also problematic if someone has a different (lower) than max resolution set for their monitor itself, because the max resolution which could be available is still listed.

    PS: i never saw the settings menu of Paradigm. Is it full of stuff ?

    Kapitán

    2346 Posts

  • #10, z afrlmeSunday, 30. April 2017, 02:43 hodinky 8 years ago
    no, surprisingly simple. option to enable hotspot system. option to enable hint system. subtitles. volume levels. fullscreen/window mode. I think that was it. Actually don't think it was Paradigm I did the display object text resolution for - was something more recent I think. Bloody memory.

    https://i.gyazo.com/3a684065bf4ce8ef3195de718e303d76.png

    The options menu for Paradigm was very simple, but worked quite nicely. Floppy in bottom right faded in-out to show that settings were being saved. The metal bit on the floppy slid across & back as well. I think there's quite a bit of retro stuff in the game, which I've still not gotten round to playing yet.

    * aye: it was another game - don't think Jacob even bothered adding in a resolution selection.

    Imperátor

    7285 Posts