asm 调用 d3d例子(四)

整理资源时发现的,大约3年多前,正好有段时间比较空,于是用asm调用d3d,实现了directx9.x的sdk附带的几个例子,使用masm32编译。

第四个例子,Meshes(对应d3d sdk的帮助Meshes)。资源文件用的是d3d9帮助里面的那个老虎和图。

下面是源代码:

.586
.model flat,stdcall
option casemap:none

  include windows.inc
  include d3d9.inc
   include d3dx9.inc
   include winuser.INC
   include d3d9types.INC
 
   includelib user32.lib
   includelib kernel32.lib
   includelib gdi32.lib
   includelib d3d9.lib
   includelib d3dx9d.lib
   includelib libc.lib
   includelib winmm.lib
  

WinMain proto :HINSTANCE,:HINSTANCE,:LPSTR,:DWORD
Init proto :HWND
Destroy proto
GameProc proto :HWND

Render proto
MemCopy proto Source:PTR BYTE,Dest:PTR BYTE,ln:DWORD
SetupMatrices proto

D3DXMatrixIdentity proto pOut: ptr D3DXMATRIX

Mymalloc proto :DWORD
Myfree proto :PTR DWORD
Error proto


PD3DMATERIAL9 typedef ptr D3DMATERIAL9
PLPDIRECT3DTEXTURE9 typedef ptr LPDIRECT3DTEXTURE9

.data
   ClassName db "MainWinClass",0
   AppName  db "Main Window",0
 ERRORTXT db "error", 0
 TExturefile db "tiger.bmp", 0
 XFilename db "tiger.x", 0
   errorflag db 0
   
   PI2 DD   6.2831854e-3
   F1000 dd 1000.0
   PIDIV4 FLOAT 0.785398163
   F1_0 REAL4 1.0
   F0_0 REAL4 0.0
   F100_0 REAL4 100.0
   F350_0 REAL4 350.0
   F1000_0 REAL4 1000.0
   FPI2  REAL4  6.2831855
   F1P_49 REAL4  2.0408163e-2
   F_1_0 REAL4 -1.0
   F500_0 REAL4 500.0
   F0_25 REAL4 0.25
   F_0_25 REAL4 -0.25
   F0_5 REAL4 0.5
.data?
   hInstance HINSTANCE ?
   pDevice LPDIRECT3DDEVICE9 ?
   g_pD3D LPDIRECT3D9 ?
   g_pTexture LPDIRECT3DTEXTURE9 ?
   g_pMesh LPD3DXMESH ?
   g_pMeshMaterials PD3DMATERIAL9 ?
   g_pMeshTextures PLPDIRECT3DTEXTURE9 ?
   g_dwNumMaterials DWORD ?
   g_pMeshMaterials_size DWORD ?
   g_pMeshTextures_size DWORD ?
.code

start:
	invoke GetModuleHandle, NULL
	mov    hInstance,eax	
	invoke WinMain, hInstance,NULL,NULL, SW_SHOWDEFAULT
	invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
	
	LOCAL wc:WNDCLASSEX
	LOCAL msg:MSG
	LOCAL hwnd:HWND
	
	mov   wc.cbSize,SIZEOF WNDCLASSEX
	mov   wc.style, CS_HREDRAW or CS_VREDRAW
	mov   wc.lpfnWndProc, OFFSET WndProc
	mov   wc.cbClsExtra,NULL
	mov   wc.cbWndExtra,NULL
	push  hInstance
	pop   wc.hInstance
	mov   wc.hbrBackground,COLOR_BTNFACE+1
	mov   wc.lpszMenuName,NULL
	mov   wc.lpszClassName,OFFSET ClassName
	invoke LoadIcon,NULL,IDI_APPLICATION
	mov   wc.hIcon,eax
	mov   wc.hIconSm,eax
	invoke LoadCursor,NULL,IDC_ARROW
	mov   wc.hCursor,eax
	invoke RegisterClassEx, addr wc
	INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW,100,\
           100,300,300,NULL,NULL,\
           hInst,NULL
	mov   hwnd,eax
	invoke Init, hwnd

	invoke ShowWindow, hwnd,SW_SHOWNORMAL
	invoke UpdateWindow, hwnd
	
	.WHILE TRUE
		invoke PeekMessage, ADDR msg,NULL,0,0,PM_REMOVE
		.BREAK .IF (msg.message == WM_QUIT )
		.if( eax )
		invoke TranslateMessage, ADDR msg
		invoke DispatchMessage, ADDR msg
		.else
		invoke Render
		.endif
	.ENDW

	invoke Destroy
	mov     eax,msg.wParam
	ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
	
	.IF uMsg==WM_DESTROY
		invoke PostQuitMessage,NULL
	.ELSE
		invoke DefWindowProc,hWnd,uMsg,wParam,lParam		
		ret
	.ENDIF
	
	xor eax,eax
	ret
WndProc endp
Error proc
mov errorflag, 1
	invoke MessageBox,NULL,addr ERRORTXT,addr ERRORTXT,MB_OK
		invoke Destroy
	
	ret

Error endp

Init proc hwnd:HWND
LOCAL i :DWORD
LOCAL d3dpp :D3DPRESENT_PARAMETERS 
LOCAL pD3DXMtrlBuffer :LPD3DXBUFFER
LOCAL d3dxMaterials :ptr D3DXMATERIAL
	invoke  Direct3DCreate9, D3D_SDK_VERSION
	.IF eax == 0
		jmp error
	.endif
	mov g_pD3D, eax
	invoke ZeroMemory, addr d3dpp, sizeof d3dpp
	mov d3dpp.BackBufferFormat, D3DFMT_UNKNOWN
	mov eax, hwnd
	mov d3dpp.hDeviceWindow, eax
	mov d3dpp.Windowed, TRUE
	mov d3dpp.SwapEffect, D3DSWAPEFFECT_DISCARD
	mov d3dpp.EnableAutoDepthStencil, TRUE
	mov d3dpp.AutoDepthStencilFormat, D3DFMT_D16
	invoke vf(g_pD3D, IDirect3D9, CreateDevice), D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, \
				D3DCREATE_SOFTWARE_VERTEXPROCESSING, addr d3dpp, addr pDevice
	.if FAILED(eax)
		jmp error
	.endif
	invoke vf( pDevice, IDirect3DDevice9, SetRenderState ), D3DRS_AMBIENT, 0ffffffffh
	invoke vf( pDevice, IDirect3DDevice9, SetRenderState ), D3DRS_LIGHTING, FALSE
	invoke vf( pDevice, IDirect3DDevice9, SetRenderState ), D3DRS_ZENABLE, TRUE
	invoke D3DXLoadMeshFromX, addr XFilename, D3DXMESH_SYSTEMMEM, pDevice, NULL, \
				  addr pD3DXMtrlBuffer, NULL, addr g_dwNumMaterials,\
				  addr g_pMesh
	.if FAILED( eax )
		jmp error
	.endif
	invoke vf( pD3DXMtrlBuffer, ID3DXBuffer, GetBufferPointer )
	mov d3dxMaterials, eax
	
	mov eax, g_dwNumMaterials
	mov ebx, sizeof D3DMATERIAL9
	imul ebx
	mov g_pMeshMaterials_size, ebx
	invoke Mymalloc, ebx
	.if eax == NULL
		jmp error
	.endif
	mov g_pMeshMaterials, eax
	
	mov eax, g_dwNumMaterials
	mov ebx, sizeof LPDIRECT3DTEXTURE9
	imul ebx
	mov g_pMeshTextures_size, ebx
	invoke Mymalloc, ebx
	.if eax == NULL
		jmp error
	.endif
	mov g_pMeshTextures, eax
	mov edi, 0
	mov ecx, 0
	mov i, 0
	.WHILE edi < g_dwNumMaterials
	mov ebx, d3dxMaterials
	mov ecx, g_pMeshMaterials
	mov eax, sizeof D3DXMATERIAL
	imul i
	add ebx, eax
	mov eax, sizeof D3DMATERIAL9
	imul i
	add eax, ecx
	assume ebx :ptr D3DXMATERIAL
	assume eax :ptr D3DMATERIAL9
	;g_pMeshMaterials[i].Diffuse = d3dxMaterials[i].MatD3D.Diffuse
	mov esi, [ebx].MatD3D.Diffuse.r
	mov [eax].Diffuse.r, esi
	mov esi, [ebx].MatD3D.Diffuse.g
	mov [eax].Diffuse.g, esi
	mov esi, [ebx].MatD3D.Diffuse.b
	mov [eax].Diffuse.b, esi
	mov esi, [ebx].MatD3D.Diffuse.a
	mov [eax].Diffuse.a, esi
	;g_pMeshMaterials[i].Ambient = d3dxMaterials[i].MatD3D.Ambient
	mov esi, [eax].Ambient.r
	mov [eax].Ambient.r, esi
	mov esi,[eax].Ambient.g
	mov [eax].Ambient.g, esi
	mov esi,[eax].Ambient.b
	mov [eax].Ambient.b, esi
	mov esi, [eax].Ambient.a
	mov [eax].Ambient.a, esi
	;g_pMeshMaterials[i].Specular = d3dxMaterials[i].MatD3D.Specular
	mov esi,[ebx].MatD3D.Specular.r
	mov [eax].Specular.r, esi
	mov esi,[ebx].MatD3D.Specular.g
	mov [eax].Specular.g, esi
	mov esi, [ebx].MatD3D.Specular.b
	mov [eax].Specular.b, esi
	mov esi, [ebx].MatD3D.Specular.a
	mov [eax].Specular.a, esi
	;g_pMeshMaterials[i].Emissive = d3dxMaterials[i].MatD3D.Emissive
	mov esi, [ebx].MatD3D.Emissive.r
	mov [eax].Emissive.r, esi
	mov esi, [ebx].MatD3D.Emissive.g
	mov [eax].Emissive.g, esi
	mov esi, [ebx].MatD3D.Emissive.b
	mov [eax].Emissive.b, esi
	mov esi, [ebx].MatD3D.Emissive.a
	mov [eax].Emissive.a, esi
	mov esi, [ebx].MatD3D.Power
	mov [eax].Power, esi
	assume ecx :ptr LPDIRECT3DTEXTURE9
		mov [ecx], 0	
	.if [ebx].pTextureFilename != NULL 
		invoke lstrlen,[ebx].pTextureFilename
		.if eax != 0
		invoke D3DXCreateTextureFromFile, pDevice, [ebx].pTextureFilename, addr g_pMeshTextures[edi]
		.if FAILED( eax )
			jmp error
		.endif
		.endif
	.endif
	add edi, 1
	add i, 1
	.ENDW
	invoke vf( pD3DXMtrlBuffer, ID3DXBuffer, Release )
	ret
	error:	
		invoke Error
		ret
Init endp

Destroy proc
	.if ( g_pMeshMaterials )
		invoke Myfree, g_pMeshMaterials
	.endif
	.if( g_pMeshTextures )
		mov ecx, 0
		.while ecx < g_dwNumMaterials
			.if(  g_pMeshTextures[ecx] )
				invoke vf(  g_pMeshTextures[ecx], IDirect3DTexture9, Release )
			.endif
			add ecx, 1
		.endw
		invoke Myfree, g_pMeshTextures
	.endif
	.if( g_pTexture )
		invoke vf( g_pTexture, IDirect3DTexture9, Release )
	.endif
	.if( pDevice )
		invoke vf( pDevice, IUnknown, Release)
	.endif
	.if( g_pD3D )
		invoke vf( g_pD3D, IUnknown, Release)
	.endif
	ret
Destroy endp
GameProc proc hwnd:HWND
	ret
GameProc endp
Render proc
LOCAL i:DWORD
	.if errorflag != 0
		ret
	.endif
	invoke vf( pDevice, IDirect3DDevice9, Clear), 0, NULL, D3DCLEAR_TARGET+2, D3DCOLOR_XRGB(0,0,251), F1_0, 0
	invoke vf( pDevice, IDirect3DDevice9, BeginScene )
	.if FAILED( eax )
		invoke Error
		ret
	.endif
	invoke SetupMatrices

	mov edi, 0
	.WHILE edi < g_dwNumMaterials
		invoke vf( pDevice, IDirect3DDevice9, SetMaterial ), addr g_pMeshMaterials[edi]
		invoke vf( pDevice, IDirect3DDevice9,SetTexture ), 0, g_pMeshTextures[edi]
		invoke vf( g_pMesh, ID3DXMesh, DrawSubset), edi
		add edi,1
	.ENDW
	invoke vf( pDevice, IDirect3DDevice9, EndScene )
	invoke vf( pDevice, IDirect3DDevice9, Present ), NULL, NULL, NULL, NULL
	ret

Render endp
SetupMatrices proc
	LOCAL matWorld: D3DXMATRIX 
	LOCAL iTime:DWORD
	LOCAL fAngle:REAL4
	LOCAL matProj: D3DXMATRIX 
	LOCAL matView: D3DXMATRIX 
	LOCAL tvEyePt:D3DXVECTOR3
	LOCAL tvLookatPt:D3DXVECTOR3
	LOCAL tvUpVec:D3DXVECTOR3
	invoke D3DXMatrixIdentity, addr matWorld
	invoke timeGetTime	
	
	mov fAngle, eax
	fild fAngle
	fdiv F1000_0
	fstp fAngle
	fld vEyePt.x
	fstp tvEyePt.x
	fld vEyePt.y
	fstp tvEyePt.y
	fld vEyePt.z
	fstp tvEyePt.z
	fld vLookatPt.x
	fstp tvLookatPt.x
	fld vLookatPt.y
	fstp tvLookatPt.y
	fld vLookatPt.z
	fstp tvLookatPt.z
	fld vUpVec.x
	fstp tvUpVec.x
	fld vUpVec.y
	fstp tvUpVec.y
	fld vUpVec.z
	fstp tvUpVec.z
	invoke D3DXMatrixRotationY, addr matWorld, fAngle
	invoke vf( pDevice, IDirect3DDevice9, SetTransform ),D3DTS_WORLD-4, addr matWorld
	invoke D3DXMatrixLookAtLH, addr matView, addr tvEyePt, addr tvLookatPt, addr tvUpVec
	invoke vf( pDevice, IDirect3DDevice9, SetTransform ), D3DTS_VIEW, addr matView
	invoke D3DXMatrixPerspectiveFovLH, addr matProj, PIDIV4, F1_0,F1_0,F100_0
	invoke vf( pDevice, IDirect3DDevice9, SetTransform ), D3DTS_PROJECTION, addr matProj
	ret
	vEyePt  D3DVECTOR <0.0, 3.0,-5.0>
vLookatPt  D3DVECTOR <0.0, 0.0, 0.0>
vUpVec  D3DVECTOR <0.0, 1.0, 0.0>
SetupMatrices endp

D3DXMatrixIdentity proc pOut: ptr D3DXMATRIX
	invoke ZeroMemory, pOut, sizeof D3DXMATRIX
	fld1
	fld1
	fld1
	fld1
	fstp [pOut][0][0]
	fstp [pOut][1][1]
	fstp [pOut][2][2]
	fstp [pOut][3][3]
	ret
D3DXMatrixIdentity endp	

Mymalloc proc num: DWORD
	invoke VirtualAlloc, NULL, num,MEM_RESERVE,PAGE_NOACCESS
	mov ebx, num
	add ebx, 4
	invoke VirtualAlloc, eax, ebx, MEM_COMMIT, PAGE_EXECUTE_READWRITE
	push ebx
	pop [eax]
	add eax, 4
	ret

Mymalloc endp

Myfree proc paddr:PTR DWORD
	mov ebx, [paddr-4]
	add ebx, 4
	invoke VirtualFree,paddr-4, ebx, MEM_RELEASE
	ret
Myfree endp

end start

发布者

rix

如果连自己都不爱自己,哪还有谁来爱你