RTT for 1.26

BlitzMax Forums/BlitzMax Programming/RTT for 1.26

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.

It works for me but I have changed the code to fix the scaling issue and removed OGL. The code above works as well but is messy. For OGL I would use Diablo's HighGfx code.
Here is the DX code I use now :
' Original author Tim Fisher
Graphics 800,600,0
tRender.Initialise()
Local logo:TImage = LoadImage("max.png",MIPMAPPEDIMAGE|FILTEREDIMAGE)
Local myImage:TImage = tRender.Create(ImageWidth(logo) , ImageHeight(logo) , MIPMAPPED | FILTEREDIMAGE)
tRender.TextureRender(myImage,True)
Cls	
DrawImage logo,0,0

SetBlend(ALPHABLEND)

While Not KeyHit(KEY_ESCAPE)
	tRender.BackBufferRender() 
	Cls
	SetAlpha 1.0
	DrawImage myimage , MouseX(),MouseY()
	DrawImage logo,300,200
	Flip
	tRender.TextureRender(myImage,True)
'	Cls	
'	DrawImage logo,0,0
	SetAlpha Rnd(0.3,1.0)
	SetColor Rand(255) , Rand(255) , Rand(255)
	Local cubx:Int = Rand(0 , 200)
	Local cuby:Int=Rand(0,200)
	DrawRect cubx,cuby,20,20
	SetColor 255,255,255
	DrawText "Hello World",10,10

	Delay 250
Wend

Const DDERR_INVALIDSURFACETYPE 	= $88760250
Const DDERR_INVALIDPARAMS		= $80070057
Const DDERR_INVALIDOBJECT 		= $88760082
Const DDERR_NOTFOUND 			= $887600ff
Const DDERR_SURFACELOST 		= $887601c2

Global tRenderERROR:String

Function tError:String(err:Int)

	Select err
		Case DDERR_INVALIDSURFACETYPE
			Return "DDERR_INVALIDSURFACETYPE"
			
		Case DDERR_INVALIDPARAMS
			Return "DDERR_INVALIDPARAMS"
			
		Case DDERR_INVALIDOBJECT 
			Return "DDERR_INVALIDOBJECT"
			
		Case DDERR_NOTFOUND
			Return "DDERR_NOTFOUND"
			
		Case DDERR_SURFACELOST
			Return "DDERR_SURFACELOST"
			
	End Select
End Function

Type tRender
	
	Global DXFrame:TD3D7ImageFrame
	Global backbuffer:IDirectDrawSurface7
	Global Image:TImage = CreateImage(1,1)
	Global Width:Int
	Global Height:Int
	Global o_r , o_g , o_b

Rem
bbdoc: Initialise the Module
about: 
The module must be initialised before use. This ensures usage of the correct render pipeline. If the render device is changed then you must 
re initialise the module.
End Rem		
	Function Initialise()
			D3D7GraphicsDriver().Direct3DDevice7().GetRenderTarget Varptr backbuffer
		DebugLog "tRender : Initialise OK"
		Return True
	End Function
	
'#################################################################################

Rem
bbdoc: Create Image with Render Characteristics
returns: TImage Handle to Object
about: 
 <table>
		<tr><td><b>Width:Int</td><td>Width in Pixels of Image, try to follow the normal rules of textures</td></tr>
		<tr><td><b>Height:Int</td><td>Height in Pixels of Image</td></tr>
		<tr><td><b>Flags:Int</td><td>Normal Image Flags</td></tr>
	</table>
End Rem	
	Function Create:TImage(Width:Int,Height:Int,Flags:Int=FILTEREDIMAGE)

		Local t:TImage=New TImage
		t.width=width
		t.height=height
		t.flags=flags
		t.mask_r= 0
		t.mask_g= 0
		t.mask_b= 0
		t.pixmaps=New TPixmap[1]
		t.frames=New TImageFrame[1]
		t.seqs=New Int[1]
		t.pixmaps[0]= t.Lock(0,True,False)
		t.seqs[0]=GraphicsSeq
		
			t.frames[0] = CreateFrame(TD3D7Max2DDriver(_max2dDriver),t.Width,t.Height,t.flags)
			
		DebugLog "tRender : Create OK"
		
		
		Return t

	End Function

'#################################################################################
		
	Function CreateFrame:TD3D7ImageFrame( driver:TD3D7Max2DDriver,width,height,flags )				
		Function Pow2Size( n )
			Local t=1
			While t<n
				t:*2
			Wend
			Return t
		End Function

		Local	swidth=Pow2Size(width)
		Local	sheight=Pow2Size(height)
		Local	desc:DDSURFACEDESC2=New DDSURFACEDESC2
		Local	res
						
		desc.dwSize=SizeOf(desc)
		desc.dwFlags=DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|DDSD_PIXELFORMAT
		desc.dwWidth=swidth
		desc.dwHeight=sheight	
		desc.ddsCaps=DDSCAPS_TEXTURE|DDSCAPS_3DDEVICE|DDSCAPS_VIDEOMEMORY|DDSCAPS_LOCALVIDMEM  ' **************************************************
		desc.ddsCaps2=DDSCAPS2_HINTDYNAMIC'|DDSCAPS2_TEXTUREMANAGE
		desc.ddpf_dwSize=SizeOf(DDPIXELFORMAT)
		desc.ddpf_dwFlags=DDPF_RGB|DDPF_ALPHAPIXELS
		desc.ddpf_BitCount=32
		desc.ddpf_BitMask_0=$ff0000
		desc.ddpf_BitMask_1=$00ff00
		desc.ddpf_BitMask_2=$0000ff
		desc.ddpf_BitMask_3=$ff000000

		
		Local surf:IDirectDrawSurface7
		If flags & MIPMAPPEDIMAGE desc.ddsCaps:|DDSCAPS_MIPMAP|DDSCAPS_COMPLEX
		res=D3D7GraphicsDriver().DirectDraw7().CreateSurface( desc,Varptr surf,Null )
		If res<>DD_OK 
			tRenderERROR = tError(res)
			DebugLog "tRender : CreateFrame ERROR : "+tRenderERROR
			Return Null
		EndIf
		'RuntimeError "Create DX7 surface Failed"	
							
		Local frame:TD3D7ImageFrame=New TD3D7ImageFrame
		frame.driver=driver
		frame.surface=surf
		frame.sinfo=New DDSURFACEDESC2
		frame.sinfo.dwSize=SizeOf(frame.sinfo)
		frame.xyzuv=New Float[24]
		frame.width=width
		frame.height=height
		frame.flags=flags
		frame.SetUV 0.0,0.0,Float(width)/swidth,Float(height)/sheight
	
			
		'frame.BuildMipMaps()
		DebugLog "tRender : CreateFrame OK"
		Return frame
	End Function

	
Rem
bbdoc: Begin the Texture Render Process
about: 
 <table>
	<tr><td><b>Image:TImage</td><td>The Image to Render to, as created with "Create" command.</td></tr>
	<tr><td><b>Viewport:Byte</td><td>True (default) to Automatically resize the viewport to the image size</td></tr>
 </table>
End Rem
	Function TextureRender(Image1:TImage,Viewport:Byte=True)
			D3D7GraphicsDriver().Direct3DDevice7().EndScene()

		If Viewport Then 
			Width = image1.width
			Height = image1.height
			
		Else
			Width = GraphicsWidth()
			Height = GraphicsHeight()
		EndIf
		SetViewport(0 , 0 , width , height)
			Local DXFrame:TD3D7ImageFrame = TD3D7ImageFrame (image1.frame(0))
			D3D7GraphicsDriver().Direct3DDevice7().SetRenderTarget( DXFrame.Surface,0)
			D3D7GraphicsDriver().Direct3DDevice7().BeginScene()
	'	debuglog "tRender : TextureRender_Begin OK"
		Return True
	End Function
	
'?win32	
	Function SetMipMap(Image:TImage,Level:Int)
			Local DXFrame:TD3D7ImageFrame = TD3D7ImageFrame (image.frame(0))
			
			'DXFrame.BuildMipMaps()
			
			Local	src:IDirectDrawSurface7
			Local	dest:IDirectDrawSurface7
			Local	caps2:DDSCAPS2
			caps2=New DDSCAPS2
			caps2.dwCaps=DDSCAPS_TEXTURE|DDSCAPS_MIPMAP'|DDSCAPS_3DDEVICE
			caps2.dwCaps2= DDSCAPS2_MIPMAPSUBLEVEL
		
			src = DXFrame.Surface
					
			Local res = src.GetAttachedSurface(caps2,Varptr dest)
			
			If res<>DD_OK 
				tRenderERROR = tError(res)
				Return False
			EndIf
			
			DXFrame.Surface =  dest
			dest.Release_
			
			DebugLog "MipMap Selected OK"
			Return True
	End Function
'#################################################################################
Rem
bbdoc: Begin the Normal BackBuffer Rendering Process
End Rem
	Function BackBufferRender()
			D3D7GraphicsDriver().Direct3DDevice7().EndScene()

			D3D7GraphicsDriver().Direct3DDevice7().SetRenderTarget(backbuffer,0)
		SetViewport(0,0,GraphicsWidth(),GraphicsHeight())
		DebugLog "tRender : BackBufferRender_Begin OK"
		Return True
	End Function
	
End Type


<edit> Just realise I have removed all the module info which listed Tim 'Indiepath' as the code originator so thanks to Tim.
In addition, the code now uses the setviewport command. There have been reports of this not working on some graphics cards.