Crash problem

BlitzMax Forums/BlitzMax Programming/Crash problem

Well here is the code, Sorry to give you the trouble. The teture file used ->dxt1.dds is created with DirectTexture tool of direct draw sdk. If you require I can mail you the texture file also. Thanks very much for your help.

SuperStrict

DDSTest( "dxt1.dds" )


Function DDSTest( fileName:String )
	Local fs:TStream = ReadFile( fileName )
	
	For Local i:Int = 1 To 128
		ReadByte(fs)
	Next

	SetGraphicsDriver( D3D7Max2DDriver() )
	Graphics( 800, 600 )
	
	Local width:Int = 512
	Local height:Int = 512
	
	Local texture:IDirectDrawSurface7 = CreateTexture( width, height, 0 )
	Local textureData:Byte Ptr = LockTexture( texture )
	
	For Local i:Int = 0 To 131072 - 1
		textureData[i] = ReadByte( fs )
	Next
	
	UnlockTexture( texture )
	
	SetClsColor( 255, 0, 255 )

	While Not KeyDown( KEY_ESCAPE )
		Cls()
		BindTexture( texture )
		DrawTexture( texture )
		Flip( 1 )
	Wend
End Function


Function CreateTexture:IDirectDrawSurface7( width:Int, height:Int, flags:Int )
	Local desc:DDSURFACEDESC2 = New DDSURFACEDESC2
	
	desc.dwSize = SizeOf(desc)
	desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS | DDSD_PIXELFORMAT
	desc.dwWidth = width
	desc.dwHeight = height
	desc.ddsCaps = DDSCAPS_TEXTURE
	desc.ddsCaps2 = DDSCAPS2_TEXTUREMANAGE
	desc.ddpf_dwSize = SizeOf(DDPIXELFORMAT)
	
	desc.ddpf_dwFlags = DDPF_FOURCC
	desc.ddpf_dwFourCC = $31545844 'dxt1
'	desc.ddpf_dwFourCC = $32545844 'dxt2
'	desc.ddpf_dwFourCC = $33545844 'dxt3
'	desc.ddpf_dwFourCC = $34545844 'dxt4

	'for argb8888 format 
	Rem
	desc.ddpf_dwFlags = DDPF_RGB 
	desc.ddpf_BitCount = 32
	desc.ddpf_BitMask_0 = $00ff0000
	desc.ddpf_BitMask_1 = $0000ff00
	desc.ddpf_BitMask_2 = $000000ff
	desc.ddpf_BitMask_3 = $ff000000
	End Rem
	
	If flags & MIPMAPPEDIMAGE Then 
		DebugLog "mipmapped image"
		desc.ddsCaps :| DDSCAPS_MIPMAP | DDSCAPS_COMPLEX
	End If	
	
	Local surf:IDirectDrawSurface7 = D3D7GraphicsDriver().CreateSurface( desc )
	If Not surf Then Throw "Create DX7 surface Failed"
	
	Return surf
End Function


Function DrawTexture( texture:IDirectDrawSurface7 )
	Local device:IDirect3DDevice7 = D3D7GraphicsDriver().Direct3DDevice7()
	
	Local numVerts:Int = 4
	Local vertData:Byte Ptr = New Byte[numVerts * 6]
	Local vertFloat:Float Ptr = Float Ptr( vertData )
	Local vertInt:Int Ptr = Int Ptr( vertData )
	
	vertFloat[0] = 0
	vertFloat[1] = 0
	vertFloat[2] = 0
	vertInt[3] = $ffffffff
	vertFloat[4] = 0
	vertFloat[5] = 0
	
	vertFloat[6] = 512
	vertFloat[7] = 0
	vertFloat[8] = 0
	vertInt[9] = $ffffffff
	vertFloat[10] = 1
	vertFloat[11] = 0
	
	vertFloat[12] = 512
	vertFloat[13] = 512
	vertFloat[14] = 0
	vertInt[15] = $ffffffff
	vertFloat[16] = 1
	vertFloat[17] = 1
	
	vertFloat[18] = 0
	vertFloat[19] = 512
	vertFloat[20] = 0
	vertInt[21] = $ffffffff
	vertFloat[22] = 0
	vertFloat[23] = 1
	
	device.DrawPrimitive( D3DPT_TRIANGLEFAN, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1, vertData, numVerts, 0 )
End Function


Function BindTexture( texture:IDirectDrawSurface7 )
	Local device:IDirect3DDevice7 = D3D7GraphicsDriver().Direct3DDevice7()
	device.SetTexture( 0, texture )
	device.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE )
	device.SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE )
	device.SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTFG_LINEAR )
	device.SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTFG_LINEAR )
	device.SetTextureStageState( 0, D3DTSS_MIPFILTER, D3DTFG_LINEAR )
End Function


Function LockTexture:Byte Ptr( texture:IDirectDrawSurface7 )
	Local desc:DDSURFACEDESC2 = New DDSURFACEDESC2
	desc.dwSize = SizeOf( desc )
	Local flags:Int = DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR | DDLOCK_WRITEONLY
	Local hresult:Int = texture.Lock( Null, desc, flags, Null )
	If ( hresult <> DD_OK ) Then RuntimeError "DD3D7ImageFrame Lock failed"
	Return desc.lpSurface
End Function


Function UnlockTexture( texture:IDirectDrawSurface7 )
	texture.Unlock( Null )
End Function