Author Topic: Using Direct3D + tinyptc_ext to load images  (Read 2610 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
This is freebasic version of Jim's d3dx9 image loader, tested on fb 015 and the new 018b

And this is a good excuse to test the new tinyptc_ext command "ptc_getwindow()"

The only downside is that freebasic d3dx9 library uses "d3dx9d.dll" debug version, dunno why...
If you don't have this dll on your system, you can just rename one "d3dx9_XX.dll" to "d3dx9d.dll", and copy to your system32 directory or keep it on same folder of your executable, bear in mind that directx will always convert your image into power of two.

It can be easily changed to load from exe file, with some help of bin2bas ofcourse ;)

Code: [Select]
'
' Using Direct3D + tinyptc_ext  to load images
'
'

#include "win/d3d9.bi"
#include "win/d3dx9.bi"
#include "windows.bi"
#include "crt.bi"
#include "tinyptc_ext.bi"

declare function LoadD3DX9FileTextures(byval hWnd as HWND, byval SrcFile as string) as integer

Const XRES = 800
Const YRES = 600
dim shared buffer(XRES*YRES) as uinteger
dim shared hwnd as HWND
redim shared pix(1) as uinteger
dim shared Tex_Width as integer
dim shared Tex_Height as integer
dim x as integer
dim y as integer

'No dialog
ptc_setdialog(0,"",1,0)

'Open tinyptc window
If( ptc_open( "D3DX9 Texture Test", XRES, YRES ) = 0 ) Then
    End -1
End if

'get ptc window handle
hwnd = ptc_getwindow()

'Load texture
if( LoadD3DX9FileTextures(hwnd, "Kappa Space.jpg") ) = 0 then
    print "Load image error!!!"
    ptc_close()
    Sleep(3000)
    End -1
end if

'Display tex size info
print "Tex_Width  = "; Tex_Width
print "Tex_Height = "; Tex_Height

'main loop
while(GetAsyncKeyState(VK_ESCAPE)<>-32767)
   
    for y=0 to Tex_Height-1
        for x = 0 to Tex_Width-1
            buffer((x mod XRES) + ((y mod YRES)*XRES)) = pix(x + (y*Tex_Width))
        next
    next
    ptc_update(@buffer(0))
wend
 
 
function LoadD3DX9FileTextures(byval hWnd as HWND, byval SrcFile as string) as integer

dim hres as HRESULT
dim d3d9 as LPDIRECT3D9
dim d3ddev as LPDIRECT3DDEVICE9
dim d3dtex as LPDIRECT3DTEXTURE9
dim d3dpp as D3DPRESENT_PARAMETERS
clear(d3dpp, 0, len(d3dpp) )
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP
d3dpp.Windowed = TRUE
d3d9 = Direct3DCreate9(D3D_SDK_VERSION)

hres = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, @d3dpp, @d3ddev)
if(hres <> D3D_OK) then return false

hres = D3DXCreateTextureFromFile(d3ddev, SrcFile, @d3dtex)
if(hres <> D3D_OK) then return false

dim dlock as D3DLOCKED_RECT
dim d3ddesc as D3DSURFACE_DESC
IDirect3DTexture9_GetLevelDesc(d3dtex, 0, @d3ddesc)
redim pix(d3ddesc.Width * d3ddesc.Height) as uinteger
   
Tex_Width = d3ddesc.Width
Tex_Height = d3ddesc.Height
   
IDirect3DTexture9_LockRect(d3dtex, 0, @dlock, NULL, D3DLOCK_READONLY)
dim dst as uinteger ptr
dim src as uinteger ptr
src = cast(uinteger ptr, dlock.pBits)
dst = @pix(0)
     
dim dl as uinteger
dim y as uinteger
for y = 0 to d3ddesc.Height-1
memcpy(dst, src, d3ddesc.Width * sizeof(uinteger))
dst += d3ddesc.Width
dl = dlock.Pitch / sizeof(uinteger)
src += dl
next

IDirect3DTexture9_UnlockRect(d3dtex,0)
IDirect3DTexture9_Release(d3dtex)
IDirect3DDevice9_Release(d3ddev)
IDirect3D9_Release(d3d9) 

return true

end function
« Last Edit: March 20, 2008 by rbz »
Challenge Trophies Won: