接前三节的头文件,写一个简单的测试:
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
;*--==--* fasm directdraw test.
;*--==--* By G-Spider
;*--==--* fasm ddraw.asm ddraw.exe
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
format PE GUI 4.0
entry start
include 'win32.inc'
include 'DDraw.inc'
macro retjnz labal
{
test eax,eax
jnz labal
}
macro retjz labal
{
test eax,eax
jz labal
}
MY_DDRAWERR = (WM_USER + 1)
;------------------------------------------------------------
section '.text' code readable writeable executable
start:
invoke GetModuleHandle,0
mov [wc.hInstance],eax
invoke LoadCursor,0,IDC_ARROW
mov [wc.hCursor],eax
invoke RegisterClass,wc
retjz end_loop
invoke GetSystemMetrics,SM_CXSCREEN
mov [point.x],eax
shr eax,1
mov [txtpoint.x],eax
invoke GetSystemMetrics,SM_CYSCREEN
mov [point.y],eax
shr eax,1
mov [txtpoint.y],eax
invoke CreateWindowEx,0,szClass,szTitle,WS_POPUP,0,0,[point.x],[point.y],NULL,NULL,[wc.hInstance],NULL
retjz end_loop
mov [hwnd],eax
;=========================================
; 创建DirectCraw对象
invoke DirectDrawCreate,NULL,DDraw,NULL
retjnz end_loop
; 取得独占和全屏模式
cominvk DDraw,SetCooperativeLevel,[hwnd],DDSCL_EXCLUSIVE+DDSCL_FULLSCREEN
retjnz ddraw_err
; 初始化DDSURFACEDESC结构
invoke RtlZeroMemory,ddsd,sizeof.DDSURFACEDESC
mov [ddsd.dwSize],sizeof.DDSURFACEDESC
; 取得当前的显示模式
cominvk DDraw,GetDisplayMode,ddsd
retjnz ddraw_err
; 设置显示模式
cominvk DDraw,SetDisplayMode,[ddsd.dwWidth],[ddsd.dwHeight],[ddsd.ddpfPixelFormat.dwRGBBitCount]
retjnz ddraw_err
; 填充主页面信息
mov [ddsd.dwFlags],DDSD_CAPS
mov [ddsd.ddsCaps.dwCaps],DDSCAPS_PRIMARYSURFACE
cominvk DDraw,CreateSurface,ddsd,DDSPrimary,NULL
retjnz ddraw_err
; 创建该页面的设备环境句柄,该设备环境与 GDI 是兼容的
cominvk DDSPrimary,GetDC,hdc
retjnz ddraw_err
; 输出文字
invoke SetBkColor,[hdc],0x000000ff
invoke SetTextColor,[hdc],0x00ffff00
invoke TextOut,[hdc],[txtpoint.x],[txtpoint.y],stMsg,dwLenMsg
invoke ShowWindow,[hwnd],SW_SHOWNORMAL
invoke UpdateWindow,[hwnd]
;=========================================
msg_loop:
invoke GetMessage,msg,NULL,0,0
retjz end_loop
invoke TranslateMessage,msg
invoke DispatchMessage,msg
jmp msg_loop
end_loop:
invoke ExitProcess,[msg.wParam]
ddraw_err:
invoke wsprintf,buff,szErrMsg,eax
invoke SendMessage,[hwnd],MY_DDRAWERR,buff,szErrTitle
jmp msg_loop
;=================================================================
proc WindowProc uses ebx esi edi, hwnd,umsg,wparam,lparam
cmp [umsg],WM_DESTROY
je .wmdestroy
cmp [umsg],MY_DDRAWERR
je .myddrawerr
cmp [umsg],WM_KEYDOWN
je .wmkeydown
cmp [umsg],WM_PAINT
je .wnpaint
.defwndproc:
invoke DefWindowProc,[hwnd],[umsg],[wparam],[lparam]
jmp .finish
.myddrawerr:
invoke MessageBox,[hwnd],[wparam],[lparam],MB_OK+MB_ICONERROR
invoke DestroyWindow,[hwnd]
invoke PostQuitMessage,1
jmp .finish
.wmkeydown:
mov eax,[wparam]
cmp eax,VK_ESCAPE
jne .defwndproc
invoke PostQuitMessage,1
jmp .finish
.wnpaint:
invoke TextOut,[hdc],[txtpoint.x],[txtpoint.y],stMsg,dwLenMsg
jmp .finish
.wmdestroy:
;cominvk DDraw,RestoreDisplayMode
cominvk DDSPrimary,ReleaseDC,[hdc]
cominvk DDraw,Release
invoke PostQuitMessage,0
xor eax,eax
.finish:
ret
endp
;------------------------------------------------------------
section '.bss' readable writeable
hinstance HANDLE
hwnd HANDLE
hdc HANDLE
msg MSG
point POINT
txtpoint POINT
ddsd DDSURFACEDESC
DDraw DirectDraw
DDSPrimary DirectDrawSurface
buff TCHAR 256 dup (?)
;------------------------------------------------------------
section '.data' data readable writeable
szTitle TCHAR 'flat assembler DirectDraw application',0
szClass TCHAR 'FDDRAW32',0
; DEFINE_GUID <IID_IDirectDraw7,0x15e65ec0,0x3b9c,0x11d2,0xb9,0x2f,0x00,0x60,0x97,0x97,0xea,0x5b >
wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_WINDOW + 1,NULL,szClass
szErrTitle TCHAR 'Error',0
szPause TCHAR 'pause',0
szFmt0 TCHAR 'msgindex=%x',0ah,'%d*%d*%d %dHz',0
szErrMsg TCHAR 'ddraw_err_code=0x%x',0ah,0
stMsg TCHAR 'G-Spider @2013'
dwLenMsg =($-stMsg)/sizeof.TCHAR
;------------------------------------------------------------
section '.idata' import data readable writeable
library kernel32,'KERNEL32.DLL',\
user32,'USER32.DLL',\
ddraw,'DDRAW.DLL',\
gdi32,'gdi32.dll'
include 'api\kernel32.inc'
include 'api\user32.inc'
include 'api\gdi32.inc'
import ddraw,\
DirectDrawCreate,'DirectDrawCreate'