Code archives/3D Graphics - Misc/Correct monitor aspect ratio

This code has been declared by its author to be Public Domain code.

Download source code

Correct monitor aspect ratio by chi2009
no more stretching on different monitors/resolutions
; Correct monitor aspect ratio on all resolutions
; by chi

Const monratio#	= 16/10.		; Enter your monitors aspect ratio [ (4/3.), (16/10.), (16/9.), ...]
;Global monratio#  = api_GetSystemMetrics(0) / Float(api_GetSystemMetrics(1))  ; Desktopwidth / Desktopheight 

Const w_width	= 800
Const w_height	= 600
Const w_mode	= 1

Graphics3D w_width, w_height, 0, w_mode : WireFrame 1

cube = CreateCube()

cam = CreateCamera()
multiX# = 1.
multiY# = 1.
If w_mode=1
	multiX# = monratio# / (4/3.)
	multiY# = (Float(w_width)/Float(w_height)) / (4/3.)
EndIf
ScaleEntity cam,multiX,multiY,multiX
PositionEntity cam, 0, 0, -5


While Not KeyHit(1)
	mxs# = MouseXSpeed()
    mys# = MouseYSpeed()
	If MouseDown(1)
		RotateEntity cam, EntityPitch(cam) + (mys#/5), EntityYaw(cam) - (mxs#/5), 0
		MoveMouse GraphicsWidth() / 2,GraphicsHeight() / 2
	EndIf
	If KeyDown(200) Then MoveEntity cam, 0, 0, 0.1
    If KeyDown(208) Then MoveEntity cam, 0, 0, -0.1
    If KeyDown(205) Then MoveEntity cam, 0.1, 0, 0
    If KeyDown(203) Then MoveEntity cam, -0.1, 0, 0
	UpdateWorld
	RenderWorld
	Text 10,10,"no stretching on Text(x,y,txt$) anymore ;)"
	Flip
	Delay 1
Wend
End

Comments

John Blackledge2009
Nice one, Chi.
I've managed to adapt this for 2 of my programs.


chi2009
packed into a function...
monratio# = api_GetSystemMetrics(0) / Float(api_GetSystemMetrics(1))

Graphics3D 800,600,0,1

cam=CreateCamera()
PositionEntity cam,0,0,-4

AspectRatio(cam,monratio#)

cube=CreateCube()

Repeat
	RenderWorld
	Delay 10
	Flip
Until KeyHit(1)
End


Function AspectRatio(Camera%, Mode# = 3)
	Select Mode#
		Case 1 						; force 4/3
			monratio# = 4 / 3.0
		Case 2						; force 16/9
			monratio# = 16 / 9.0
		Case 3						; force 16/10
			monratio# = 16 / 10.0
		Default
			monratio# = Mode#
	End Select
	multiX# = monratio#/1.33333
	multiY# = GraphicsWidth()/Float(GraphicsHeight())/1.33333
	ScaleEntity(Camera%, multiX#, multiY#, multiX#)
End Function


how is it going, john? ;)


John Blackledge2009
Hi Chi.

I have 1440x900 = 14.4/9 = 1.6

Surely your case statement will force this through Case 2 = 16/9, instead of 16/10, which it should be.

Or am I missing something?


_PJ_2010
This should work
Global monratio# =Float#( Float#(user32_GetSystemMetrics(0)) / Float(user32_GetSystemMetrics(1)))

Graphics3D 800,600,0,0

cam=CreateCamera()
PositionEntity cam,0,0,-4

AspectRatio(cam)

cube=CreateCube()

Repeat
	RenderWorld
	Delay 10
	Flip
Until KeyHit(1)
End


Function AspectRatio(Camera%)
	multiX# = monratio#/1.33333
	multiY# =Float#( GraphicsWidth())/Float(GraphicsHeight())*MultiX#
	ScaleEntity(Camera%, multiX#, multiY#, multiX#)
End Function




Code Archives Forum