Detect and control desktop resolution?

BlitzMax Forums/BlitzMax Programming/Detect and control desktop resolution?

ErikT(Posted 2015) [#1]
Are there ways to get at the current desktop resolution and change it with blitzmax? I want to try and write a little resolution switcher for html5/node-webkit programs.


GreenVertical(Posted 2015) [#2]
yes. see below code which grabs the current resolution and then sets it to the same.
If you want to change it then change the values to different ones when you call 'Graphics'.



'get current desktop settings
w= DesktopWidth (); h= DesktopHeight (); d= DesktopDepth (); hz= DesktopHertz ()

Graphics w,h,d,hz 'set graphics window to current desktop settings


ErikT(Posted 2015) [#3]
Thanks! I got the initial post wrong tho' :] I meant to ask, is there a way to control the OS desktop resolution itself from a bmax program?


grable(Posted 2015) [#4]
If your on Windows, ChangeDisplaySettings is the api your looking for.
Just remember to allso broadcast a WM_DISPLAYCHANGE message afterwards, so that other programs can pick up the change.

The code i use:

Import "disp.c"
Extern "C"
	Function TestDisplayMode:Int( width:Int,height:Int, bpp:Int, hz:Int=60)
	Function ChangeDisplayMode:Int( width:Int=0,height:Int=0, bpp:Int=0, hz:Int=60, permanent:Int = False)
	Function CurrentDisplayMode:Int( width:Int Var,height:Int Var, bpp:Int Var, hz:Int Var)
EndExtern



ErikT(Posted 2015) [#5]
Lovely stuff!

Thanks a lot for sharing :D

Now to wrap my head around it... :P


ErikT(Posted 2015) [#6]
Okay I got a couple of small bmax programs working to change the resolution to and from desktop resolution that get called from the html5 game itself (thanks grable!!). All good. The only thing I'm stumped at is how to run the WM_DISPLAYCHANGE message from within blitzmax. I tried to read up on what information there is floating around but my brain is too small to grasp how to do it in blitz.

Here's the code that switches to game resolution:

Import "display.c"
Extern "C"
	Function TestDisplayMode:Int( width:Int,height:Int, bpp:Int, hz:Int=60)
	Function ChangeDisplayMode:Int( width:Int=0,height:Int=0, bpp:Int=0, hz:Int=60, permanent:Int = False)
	Function CurrentDisplayMode:Int( width:Int Var,height:Int Var, bpp:Int Var, hz:Int Var)
EndExtern

''''''''''''''load preferred resolutions from a text file'''''''''
Local GameExe:String
Local x:Int[4]
Local y:Int[4]


' read game resolution config file
in=ReadStream("launch.txt")
If Not In RuntimeError "Failed to open a ReadStream to file launch.txt"
GameExe=String(ReadLine(in))
x[1]=Int(ReadLine(in))
y[1]=Int(ReadLine(in))
x[2]=Int(ReadLine(in))
y[2]=Int(ReadLine(in))
x[3]=Int(ReadLine(in))
y[3]=Int(ReadLine(in))
x[4]=Int(ReadLine(in))
y[4]=Int(ReadLine(in))
CloseStream in

' write Desktop resolution to another config file
out=WriteStream("desktopres.txt")
If Not out RuntimeError "Failed to open a WriteStream to file desktopres.txt"
WriteLine out, DesktopWidth ()
WriteLine out, DesktopHeight ()
WriteLine out, DesktopDepth ()
WriteLine out, DesktopHertz ()

CloseStream out


' replace empty values with '1' to prevent fallback (necessary?)
For z = 1 To 4
	If x[z] = 0 Then x[z] = 1
	If y[z] = 0 Then y[z] = 1
Next


If GraphicsModeExists(x[1],y[1],0,0)
	ChangeDisplayMode(x[1],y[1],DesktopDepth(),DesktopHertz (),1)
'	Extern "C"
'	SendMessage(HWND_BROADCAST, WM_DISPLAYCHANGE, (WPARAM)(deviceMode.dmBitsPerPel), MAKELPARAM(newWidth, newHeight));
'	EndExtern
ElseIf GraphicsModeExists(x[2],y[2],0,0)
	ChangeDisplayMode(x[2],y[2],DesktopDepth(),DesktopHertz (),1)
ElseIf GraphicsModeExists(x[3],y[3],0,0)
	ChangeDisplayMode(x[3],y[3],DesktopDepth(),DesktopHertz (),1)
ElseIf GraphicsModeExists(x[4],y[4],0,0)
	ChangeDisplayMode(x[4],y[4],DesktopDepth(),DesktopHertz (),1)
EndIf

'system_ GameExe


End



... with the commented-out WM_DISPLAYCHANGE message command nicked from some example on the internet. It would be great if anyone could help me with this :)


grable(Posted 2015) [#7]
I assume you mean receiving the WM_DISPLAYCHANGE from within your own app?
You can do that by overriding the window proc... And since i like testable examples, heres one i adapted from another post:



If on the other hand you want to broadcast the message, its allready done by the code i posted previously. Its on line 30 of the c file if you want to check.


ErikT(Posted 2015) [#8]
Oh okay I see, I thought I needed to broadcast it with additional code. Thanks a lot for the help and the extra example :)