List available screen modes filtered by aspect ratio

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

List available screen modes filtered by aspect ratio

Post by Joakim Christiansen »

I needed this, so I made it.
I am nice, so I shared it.
Have fun!

Code: Select all

EnableExplicit

Structure screenMode
  width.l
  height.l
  depth.l
  refreshRate.l
  aspectRatio$
EndStructure
Procedure gcd(a,b) ;finds the greatest common divisor
  If b=0
    ProcedureReturn a
  Else
    ProcedureReturn gcd(b,a%b)
  EndIf
EndProcedure
Procedure.s aspectRatio(width,height)
  Protected gcd, aspectRatio$
  gcd          = gcd(width,height)
  aspectRatio$ = Str(width/gcd)+":"+Str(height/gcd)
  Select aspectRatio$ ;change into more common values
    Case "8:5"    : aspectRatio$ = "16:10"
    Case "25:16"  : aspectRatio$ = "14:9"
    Case "85:48"  : aspectRatio$ = "16:9"
    Case "683:384": aspectRatio$ = "16:9"
  EndSelect
  ProcedureReturn aspectRatio$
EndProcedure
Procedure listScreenModes(List screenMode.screenMode(),depthFilter$,aspectFilter$)
  Protected aspectRatio$
  InitSprite()
  If ExamineScreenModes()
    While NextScreenMode()
      aspectRatio$ = aspectRatio(ScreenModeWidth(),ScreenModeHeight())
      If (depthFilter$="" Or Str(ScreenModeDepth())=depthFilter$) And (aspectFilter$="" Or aspectRatio$=aspectFilter$)
        AddElement(screenMode())
        With screenMode()
        \width       = ScreenModeWidth()
        \height      = ScreenModeHeight()
        \depth       = ScreenModeDepth()
        \refreshRate = ScreenModeRefreshRate()
        \aspectRatio$ = aspectRatio$
        EndWith
      EndIf
    Wend
  EndIf
EndProcedure

ExamineDesktops()
Define NewList screenMode.screenMode()
listScreenModes(screenMode(),Str(DesktopDepth(0)),aspectRatio(DesktopWidth(0),DesktopHeight(0)))
ForEach screenMode()
  With screenMode()
  Debug Str(\width)+"x"+Str(\height)+", "+Str(\depth)+"-bit, "+Str(\refreshRate)+"Hz, Ratio:"+\aspectRatio$
  EndWith
Next
I like logic, hence I dislike humans but love computers.
User avatar
idle
Always Here
Always Here
Posts: 5915
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: List available screen modes filtered by aspect ratio

Post by idle »

good idea, thanks!
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply