News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

DirectX: CreateDevice failing

Started by Tripper, February 20, 2007, 08:51:55 PM

Previous topic - Next topic

Tripper

Hello All.

Let me start off by saying that I'm pretty new to MASM (and ASM in general). I used it on the Commodore 64 a bit, but other than that my experience is about zero. I found MASM32 and learned the very basics. Now it is time to get into DirectX also :D

I've read through some DirectX articles and looked at some source code, trying to get an idea of what makes it tick. But for the life of me I can't figure out why a basic window I put together ends in a MS error when I run it. I do know the problem is at the CreateDevice call, but I don't know where in the code it originates. I'd appreciate someone with a better understanding check the code and point me in the right direction :)

And please excuse the mess as this code has been through hell with me trying different things:
  .586
  .XMM
  .model flat,stdcall
  option casemap:none   ; Case sensitive

;============================================================================================
;                                                                    Include Section <=======
include ..\..\include\windows.inc
include ..\..\include\kernel32.inc
include ..\..\include\user32.inc
include ..\..\include\winmm.inc
include ..\..\include\gdi32.inc
include \masm32\include\comdlg32.inc

include ..\..\d3dx9includes\dx9macros.inc
include ..\..\d3dx9includes\d3dx9.inc

includelib ..\..\lib\kernel32.lib
includelib ..\..\lib\user32.lib
includelib ..\..\lib\winmm.lib
includelib ..\..\lib\gdi32.lib
includelib \masm32\lib\comdlg32.lib

includelib ..\..\lib\advapi32.lib
includelib ..\..\lib\uuid.lib
includelib ..\..\lib\libci.lib
includelib ..\..\lib\msvcrt.lib

includelib ..\..\d3dx9libs\d3dx9.lib
includelib ..\..\d3dx9libs\d3d9.lib
;                                                                        End Section <=======
;============================================================================================





WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
InitD3D proto :DWORD
CleanD3D proto
Render_Frame proto


.DATA                     ; initialized data
ClassName db "SimpleWinClass",0        ; the name of our window class
AppName db "Our First Window",0        ; the name of our window
Init1Err db "Failed at: Direct3DCreate9,D3D_SDK_VERSION",0
Init2Err db "GetAdapterIdentifier failed",0
Init3Err db "GetAdapterDisplayMode failed",0
Init4Err db "CreateDevice failed",0


g_pD3D LPDIRECT3D9 NULL
g_pD3DDevice LPDIRECT3DDEVICE9 NULL
d3dadapter D3DADAPTER_IDENTIFIER9<>
d3ddm D3DDISPLAYMODE<>
d3dpp D3DPRESENT_PARAMETERS<>
d3dcaps D3DCAPS9<>
starttime dd 0
hWnd dd 0



.DATA?                ; Uninitialized data
hInstance HINSTANCE ?
CommandLine LPSTR ?
LastError DWORD ?

.CODE


InitD3D proc hwnd:DWORD
 
invoke Direct3DCreate9,D3D_SDK_VERSION
.if eax == NULL
            invoke MessageBoxA, NULL, Addr Init1Err, NULL, MB_OK
return E_FAIL
.endif

mov g_pD3D,eax

coinvoke g_pD3D,IDirect3D9,GetAdapterIdentifier,D3DADAPTER_DEFAULT,0,addr d3dadapter
if_FAILED
            invoke MessageBoxA, NULL, Addr Init2Err, NULL, MB_OK
return E_FAIL
.endif

coinvoke g_pD3D,IDirect3D9,GetAdapterDisplayMode,D3DADAPTER_DEFAULT,ADDR d3ddm
if_FAILED
            invoke MessageBoxA, NULL, Addr Init3Err, NULL, MB_OK
return E_FAIL
.endif

      invoke  RtlZeroMemory,addr d3dpp,sizeof d3dpp

      mov  eax, hwnd
      mov  d3dpp.hDeviceWindow,eax
mov  d3dpp.Windowed,TRUE
mov  d3dpp.SwapEffect,D3DSWAPEFFECT_DISCARD
mov  eax,d3ddm.Format
mov  d3dpp.BackBufferFormat,eax
mov  d3dpp.EnableAutoDepthStencil,TRUE
mov  d3dpp.AutoDepthStencilFormat,D3DFMT_D16
mov  d3dpp.PresentationInterval,D3DPRESENT_INTERVAL_IMMEDIATE

SAFE_RELEASE g_pD3DDevice

coinvoke g_pD3D,IDirect3D9,CreateDevice,D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,\
      D3DCREATE_HARDWARE_VERTEXPROCESSING,addr d3dpp,addr g_pD3DDevice
if_FAILED
            invoke MessageBoxA, NULL, Init4Err, NULL, MB_OK
return E_FAIL
.endif

   return S_OK

InitD3D endp


Render_Frame proc

      ;// clear the window to a deep blue
coinvoke g_pD3DDevice,IDirect3DDevice9,Clear,0,NULL,D3DCLEAR_TARGET,\
D3DCOLOR_XRGB(0,40,100),FLOAT_(1.0f),0

;// Begin the scene
coinvoke g_pD3DDevice,IDirect3DDevice9,BeginScene

;// End the scene
coinvoke g_pD3DDevice,IDirect3DDevice9,EndScene

;// Present the backbuffer contents to the display
coinvoke g_pD3DDevice,IDirect3DDevice9,Present,NULL,NULL,NULL,NULL
     
   return S_OK
Render_Frame endp

CleanD3D proc
SAFE_RELEASE g_pD3DDevice
SAFE_RELEASE g_pD3D
ret
CleanD3D endp


WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
    LOCAL wc:WNDCLASSEX
    LOCAL msg:MSG
    LOCAL hwnd:HWND

    invoke RtlZeroMemory,addr wc,sizeof wc
   
    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_WINDOW+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,\
                300,\
                300,\
                640,\
                480,\
                NULL,\
                NULL,\
                hInst,\
                NULL
    mov   hwnd,eax

    invoke InitD3D, hwnd
    .IF eax != S_OK
jmp CloseD3d
      .ENDIF

    invoke ShowWindow, hwnd,CmdShow
    invoke UpdateWindow, hwnd

    invoke RtlZeroMemory,addr msg,sizeof msg



    .WHILE TRUE


           ;invoke GetTickCount
           ;mov starttime, eax
     
                invoke PeekMessage, ADDR msg,NULL,0,0, PM_REMOVE
                  .IF eax != 0
                    mov eax, msg.message
                      .IF eax == WM_QUIT
                      .BREAK
                      .ENDIF
                    invoke TranslateMessage, ADDR msg
                    invoke DispatchMessage, ADDR msg
                 .ENDIF

           invoke Render_Frame

    ;.repeat
        ;invoke GetTickCount
        ;sub eax,starttime
        ;  .until eax > 24
    invoke CleanD3D
       
               
   .ENDW 

CloseD3d:
invoke    CleanD3D
invoke    UnregisterClass,addr ClassName,wc.hInstance
return    msg.wParam

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



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



Thanks in advance for any help

TNick

Sorry I don't have time to have a proper look, but here's a link to what I've use to get into it.

Regards,
Nick