Code archives/Graphics/Projection Matrix

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

Download source code

Projection Matrix by _Skully2009
Projection Matrix - Scaling your game on the fly
"Yan to GfK to me to here :)
[code]
Type TVirtualGraphics
	Global virtualWidth, virtualHeight
	Global xRatio!, yRatio!
	
	Function Set(width=640, height=480, scale#=1)
		TVirtualGraphics.virtualWidth = width
		TVirtualGraphics.virtualHeight = height
		TVirtualGraphics.xRatio! = width / Double(GraphicsWidth())
		TVirtualGraphics.yRatio! = height / Double(GraphicsHeight())
		
	?Win32
		Local dxVer:Byte
		Local D3D7Driver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)
		Local D3D9Driver:TD3D9Max2DDriver = TD3D9Max2DDriver(_max2dDriver)

		If TD3D7Max2DDriver(_max2dDriver) <> Null
			dxVer = 7
		EndIf
 		If TD3D9Max2DDriver(_max2dDriver) <> Null
			dxVer = 9
		EndIf

		If dxVer <> 0 'dx driver was set, otherwise its GL
			Local matrix#[] = [2.0 / (width / scale#), 0.0, 0.0, 0.0,..
			 										0.0, -2.0 / (height / scale#), 0.0, 0.0,..
			 										0.0, 0.0, 1.0, 0.0,..
		 											-1 - (1.0 / width), 1 + (1.0 / height), 1.0, 1.0] ',scale#]
			
			Select dxVer
				Case 7
					D3D7Driver.device.SetTransform(D3DTS_PROJECTION, matrix)
				Case 9
					D3D9Driver._D3DDevice9.SetTransform(D3DTS_PROJECTION, matrix)
			End Select
		Else
	? 
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		glOrtho(0, width / scale:Float, height / scale:Float, 0, - 1, 1)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
	?Win32
		EndIf
	?
	End Function
	
	Function MouseX()
		Return (BRL.PolledInput.MouseX() * TVirtualGraphics.xRatio!)
	End Function
	
	Function MouseY()
		Return (BRL.PolledInput.MouseY() * TVirtualGraphics.yRatio!)
	End Function
End Type
[/code]

Comments

QuickSilva2009
Any chance of a usage example?

Does this method cause the same sort of performance hit that simply scaling the display up using the SetScale command does? For example, if I set my game to 2 times the scale my framerate drops by half using SetScale. Would this method do the same? [Edit - OK it does.]

Also, I`m getting a driver not found for the DX9 one. I assume that you have to find this yourself? Maybe this should be mentioned for new users?

Other than that what are the main reasons for using this method over a simple SetScale on all game objects?

Jason.


GW2009
http://code.google.com/p/max2ddx9/


_Skully2009
Usage is simple enough

T:TVirtualGraphics=new TVirtualGraphics
T.Set(800,600)


done...


Nigel Brown2009
Compiler Error Identifier 'TD3DpMax2DDriver' not found

Just downloaded and installed the above max2ddx9 link, yet get this error?


_Skully2009
Did you compile it?

You could always comment out the DX9 stuff to test it if thats what your looking to do.


GfK2009
Usage is simple enough

T:TVirtualGraphics=new TVirtualGraphics
T.Set(800,600)
Its not even THAT complex - you don't need to create a tVirtualGraphics object as it uses Global/Function rather than Field/Method:
Graphics 1024,768 'or any user-defined res 
tVirtualGraphics.Set(800,600)



therevills2009
Heres a quick example (without DX9):

Graphics 1024,768,0 'or any user-defined res 
tVirtualGraphics.Set(800,600)
AutoMidHandle 1
Global BMX01IMG:TImage = LoadImage("C:\BlitzMax\samples\birdie\misc\filmclip\media\B-Max.png",FILTEREDIMAGE|DYNAMICIMAGE)

While Not AppTerminate()
	Cls
	DrawLine 0,0,TVirtualGraphics.virtualWidth,TVirtualGraphics.virtualHeight
	DrawImage BMX01IMG,TVirtualGraphics.virtualWidth/2,TVirtualGraphics.virtualHeight/2
	DrawText "Virtual Width: "+TVirtualGraphics.virtualWidth,10,10
	DrawText "Virtual Width: "+TVirtualGraphics.virtualHeight	,10,20
	controls()
	Flip
Wend

Function controls()
	If KeyHit(KEY_1)
		tVirtualGraphics.Set(640,480)
	EndIf
	If KeyHit(KEY_2)
		tVirtualGraphics.Set(800,600)
	EndIf
	If KeyHit(KEY_3)
		tVirtualGraphics.Set(1024,768)
	EndIf
End Function


Type TVirtualGraphics
	Global virtualWidth, virtualHeight
	Global xRatio!, yRatio!
	
	Function Set(width=640, height=480, scale#=1)
		TVirtualGraphics.virtualWidth = width
		TVirtualGraphics.virtualHeight = height
		TVirtualGraphics.xRatio! = width / Double(GraphicsWidth())
		TVirtualGraphics.yRatio! = height / Double(GraphicsHeight())
		
	?Win32
		Local dxVer:Byte
		Local D3D7Driver:TD3D7Max2DDriver = TD3D7Max2DDriver(_max2dDriver)

		If TD3D7Max2DDriver(_max2dDriver) <> Null
			dxVer = 7
		EndIf


		If dxVer <> 0 'dx driver was set, otherwise its GL
			Local matrix#[] = [2.0 / (width / scale#), 0.0, 0.0, 0.0,..
			 										0.0, -2.0 / (height / scale#), 0.0, 0.0,..
			 										0.0, 0.0, 1.0, 0.0,..
		 											-1 - (1.0 / width), 1 + (1.0 / height), 1.0, 1.0] ',scale#]
			
			Select dxVer
				Case 7
					D3D7Driver.device.SetTransform(D3DTS_PROJECTION, matrix)
			End Select
		Else
	? 
		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()
		glOrtho(0, width / scale:Float, height / scale:Float, 0, - 1, 1)
		glMatrixMode(GL_MODELVIEW)
		glLoadIdentity()
	?Win32
		EndIf
	?
	End Function
	
	Function MouseX()
		Return (BRL.PolledInput.MouseX() * TVirtualGraphics.xRatio!)
	End Function
	
	Function MouseY()
		Return (BRL.PolledInput.MouseY() * TVirtualGraphics.yRatio!)
	End Function
End Type


Press 1 for (640,480)
Press 2 for (800,600)
Press 3 for (1024,768)


Chroma2010
No one has been able to get this to work in DX9 yet.


_Skully2010
Didn't Mark build this into BMax?


xlsior2010
Yup, 1.37's 'virtual resolution' commands are using the projection matrix.


Code Archives Forum