Dx9 Max2d Updated

BlitzMax Forums/BlitzMax Programming/Dx9 Max2d Updated

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

@tonyg thanks for kind comments.

the offscreenplainsurface is used to allow copying from a rendertarget to a surface that cant be used as a texture or save to system memory. I use it to grab image.

it cant be a render target for that you need to use the following API.


HRESULT CreateRenderTarget(
UINT Width,
UINT Height,
D3DFORMAT Format,
D3DMULTISAMPLE_TYPE MultiSample,
DWORD MultisampleQuality,
BOOL Lockable,
IDirect3DSurface9** ppSurface,
HANDLE* pSharedHandle
);

Here is example that hooks the DirectX Driver Safely and creates an rendertarget.

Now the current MAX2d driver is not compatible but you can use raw DirectX to draw to it. Max2d could do it but you really would have to know all the little things to flush and ensure get maintained between switching render targets.


Here is simple sample

Import "Dx9Max2dDriver\d3d9.bmx"
Import "Dx9Max2dDriver\Dx9Max2dGraphicsDriver.bmx"
Strict 
SetGraphicsDriver D3D9Max2DDriver()

' this function 
Type TDx9DeviceObject
	Field _D3DDevice9 :IDirect3DDevice9
	Method SendMessage:Object( message:Object,sender:Object )
		Local Msg:TDx9GraphicsDeviceMessage= TDx9GraphicsDeviceMessage(message)
		If Msg
			Select Msg.Message 			
				Case TDx9GraphicsDeviceMessage.DX9GRAPHICSDEVICECREATED
					OnDeviceCreate(TDx9GraphicsDevice(Sender))	
				Case TDx9GraphicsDeviceMessage.DX9GRAPHICSDEVICERESET					
					OnDeviceReset(TDx9GraphicsDevice(Sender))			
				Case TDx9GraphicsDeviceMessage.DX9GRAPHICSDEVICELOST
					OnDeviceLost(TDx9GraphicsDevice(Sender))
				Case TDx9GraphicsDeviceMessage.DX9GRAPHICSDEVICEDESTROYED
					OnDeviceDestroy(TDx9GraphicsDevice(Sender))			
			End Select		
		End If
	End Method
				
	Method OnDeviceLost(Dx9GraphicsDevice:TDx9GraphicsDevice)
		Print "Device OnDeviceLost"
	End Method
	
	Method OnDeviceReset(Dx9GraphicsDevice:TDx9GraphicsDevice)
		Print "Device OnDeviceReset"
	End Method
	
	Method OnDeviceDestroy(Dx9GraphicsDevice:TDx9GraphicsDevice)
		Print "Device OnDeviceDestroy"
		_D3DDevice9 = Null
	End Method
	
	Method OnDeviceCreate(Dx9GraphicsDevice:TDx9GraphicsDevice)	
		Print "Device OnDeviceCreate"	
		' now we have our own referece to the D3D9 Device Interface to make raw dx9 calls
		_D3DDevice9 = Dx9GraphicsDevice._D3DDev9
	End Method	
End Type	

Type TDx9RenderTarget Extends TDx9DeviceObject
	Field _renderTarget: IDirect3DSurface9
	Field _saveTarget : IDirect3DSurface9
	
	' since this is not managegd surface we must create and destroy on reset/lost
	Method OnDeviceLost(Dx9GraphicsDevice:TDx9GraphicsDevice)
		super.OnDeviceLost(Dx9GraphicsDevice)
		If _renderTarget 
			_renderTarget.Release_()
			_renderTarget=Null
		End If
	End Method
	
	Method OnDeviceReset(Dx9GraphicsDevice:TDx9GraphicsDevice)
		super.OnDeviceReset(Dx9GraphicsDevice)
		If _D3DDevice9.CreateRenderTarget(128,128,D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE,0,False,_renderTarget,Null)<> 0
			Print "Failed To create"
		Else
			Print "Created Render Target"
		End If
	End Method
	
	Method Use()
		If _renderTarget
			If  _D3DDevice9.GetRenderTarget(0,_saveTarget)= 0 Then
				If _D3DDevice9.SetRenderTarget(0,_renderTarget)	=0 Print "Set Target"			
			End If
		End If
	End Method
	
	Method Restore()
		If _saveTarget 
			If _D3DDevice9.SetRenderTarget(0,_saveTarget)=0 Print "Restored Target"	
			_saveTarget.Release_
			_saveTarget=Null;
		End If
	End Method
	
	Method Cls()
		If  _D3DDevice9.Clear(0,Null,D3DCLEAR_TARGET,$FFFF0000,1.0,0)	<> 0 Throw "Clear Failed"
	End Method

End Type

' this function hooks into the DirectX Device Managment Messaging system
' Devices are created and destroyed you can create your own DX Code
Global rt:TDx9RenderTarget = New TDx9RenderTarget
D3D9Max2DDriver()._DXDriver.Dx9GraphicsDevice().AddDeviceObject(rt)



Graphics 320,240,0
SetClsColor 255,244,0
While Not KeyHit(KEY_ESCAPE)
  rt.Use()	
  rt.Cls()
  rt.Restore()	
  Cls
  DrawText "Regular Buffer",0,0	
  Flip 0
Wend


The device object pattern was designed just for these types of extensions. It will safely ensure whether you have a render surface or not regardless of screen mode changes.

Hope this helps. If you can give me idea what your trying to do or sample of what you want to accomplish I will provide you the extension.

Doug Stastny