News:

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

Directx9 framework

Started by Emil_halim, November 17, 2010, 08:02:15 AM

Previous topic - Next topic

Emil_halim

hi all

here is the directx9 calling style that i am used to use it when coding in C++.

i post here for any improvement and suggestion , of course i will divide it into  many units later.

program directxcreate;

#include("stdlib.hhf")
#include("w.hhf" )
#include("macros/macros.hhf")
#include("d3d9types.hhf");
#include("d3d9caps.hhf");


const
D3D_SDK_VERSION := 31; // For Direct3D9


procedure Direct3DCreate9(SDKVersion : dword);
@returns("eax");
@external("_Direct3DCreate9@4");


/****************************************************************************

****************************************************************************/

const D3DADAPTER_DEFAULT    := 0;


/****************************************************************************

****************************************************************************/

const
D3DCREATE_FPU_PRESERVE   :=               $00000002;
D3DCREATE_MULTITHREADED  :=               $00000004;

D3DCREATE_PUREDEVICE     :=               $00000010;
D3DCREATE_SOFTWARE_VERTEXPROCESSING :=    $00000020;
D3DCREATE_HARDWARE_VERTEXPROCESSING  :=   $00000040;
D3DCREATE_MIXED_VERTEXPROCESSING   :=     $00000080;

D3DCREATE_DISABLE_DRIVER_MANAGEMENT :=    $00000100;
D3DCREATE_ADAPTERGROUP_DEVICE    :=       $00000200;
/**************************************************************************************************

***************************************************************************************************/
namespace Dx9; @fast;

type
IDirect3D9 :
record
QueryInterface : procedure;
AddRef : procedure(p:dword);
Release : procedure(p:dword);

/*** IDirect3D9 methods ***/
RegisterSoftwareDevice : procedure(p:dword) ;@stdcall;
GetAdapterCount : procedure(p:dword) ;@stdcall;
GetAdapterIdentifier : procedure(p:dword) ; @stdcall;
GetAdapterModeCount :procedure(p:dword) ;@stdcall;
EnumAdapterModes :procedure(p:dword) ;@stdcall;
GetAdapterDisplayMode:procedure(p:dword) ;@stdcall;
CheckDeviceType :procedure (p:dword);@stdcall;
CheckDeviceFormat :procedure(p:dword) ;@stdcall;
CheckDeviceMultiSampleType :procedure(p:dword) ;@stdcall;
CheckDepthStencilMatch :procedure (p:dword);@stdcall;
CheckDeviceFormatConversion :procedure(p:dword) ;@stdcall;
GetDeviceCaps :procedure (p:dword);@stdcall;
GetAdapterMonitor :procedure (p:dword);@stdcall;
CreateDevice : procedure(p:dword;a:dword;b:dword;c:dword;d:dword;var e:dword;var f:dword) ;@stdcall;
endrecord;

type
LPDIRECT3D9 : pointer to IDirect3D9;

storage
    _pD3D : dword;

#macro Init(pD3D);
       mov(pD3D,Dx9._pD3D);
#endmacro;

#macro  CleanUp;
  if(Dx9._pD3D <> NULL) then
mov( Dx9._pD3D , eax);
mov([eax], eax);
(type Dx9.IDirect3D9 [eax]).Release(Dx9._pD3D);
  endif;
  mov(NULL,Dx9._pD3D);
#endmacro;

#macro CreateDevice(a,b,c,d,e,f);
mov( Dx9._pD3D , eax);
mov([eax], eax);
(type Dx9.IDirect3D9 [eax]).CreateDevice(Dx9._pD3D,a,b,c,d,e,f);
#endmacro;

end Dx9;
/**************************************************************************************************



***************************************************************************************************/
namespace Dx9Device; @fast;

type
IDirect3DDevice9 :
record
/*** IUnknown methods ***/
QueryInterface : procedure(p:dword);
AddRef : procedure(p:dword);
Release : procedure(p:dword);
/*** IDirect3DDevice9 methods ***/
TestCooperativeLevel : procedure(p:dword);@stdcall;
GetAvailableTextureMem : procedure(p:dword);@stdcall;
EvictManagedResources : procedure(p:dword); @stdcall;
GetDirect3D : procedure(p:dword);@stdcall;
GetDeviceCaps : procedure(p:dword); @stdcall;
GetDisplayMode : procedure(p:dword); @stdcall;
GetCreationParameters : procedure(p:dword); @stdcall;
SetCursorProperties : procedure(p:dword);@stdcall;
SetCursorPosition : procedure(p:dword);@stdcall;
ShowCursor : procedure(p:dword);@stdcall;
CreateAdditionalSwapChain : procedure(p:dword); @stdcall;
GetSwapChain : procedure(p:dword); @stdcall;
GetNumberOfSwapChains : procedure(p:dword);@stdcall;
Reset : procedure; @stdcall;
Present  : procedure (p:dword;a:dword;b:dword;c:dword;d:dword); @stdcall;
GetBackBuffer : procedure(p:dword); @stdcall;
GetRasterStatus : procedure(p:dword);  @stdcall;
SetDialogBoxMode : procedure(p:dword); @stdcall;
SetGammaRamp : procedure(p:dword); @stdcall;
GetGammaRamp : procedure(p:dword); @stdcall;
CreateTexture : procedure(p:dword); @stdcall;
CreateVolumeTexture : procedure(p:dword); @stdcall;
CreateCubeTexture : procedure(p:dword); @stdcall;
CreateVertexBuffer : procedure(p:dword;a:dword;b:dword;c:dword;d:dword;e:dword;f:dword); @stdcall;
CreateIndexBuffer : procedure(p:dword); @stdcall;
CreateRenderTarget : procedure(p:dword); @stdcall;
CreateDepthStencilSurface : procedure(p:dword); @stdcall;
UpdateSurface : procedure(p:dword); @stdcall;
UpdateTexture : procedure(p:dword); @stdcall;
GetRenderTargetData : procedure(p:dword);@stdcall;
GetFrontBufferData : procedure(p:dword); @stdcall;
StretchRect : procedure(p:dword); @stdcall;
ColorFill : procedure(p:dword); @stdcall;
CreateOffscreenPlainSurface : procedure(p:dword); @stdcall;
SetRenderTarget : procedure(p:dword); @stdcall;
GetRenderTarget : procedure(p:dword); @stdcall;
SetDepthStencilSurface : procedure(p:dword); @stdcall;
GetDepthStencilSurface : procedure(p:dword); @stdcall;
BeginScene : procedure(p:dword); @stdcall;
EndScene : procedure(p:dword); @stdcall;
Clear : procedure(p:dword;a:dword;b:dword;c:dword;d:dword;e:real32;f:dword); @stdcall;
SetTransform : procedure(p:dword;State:dword;  pMatrix:dword); @stdcall;
GetTransform : procedure(p:dword); @stdcall;
MultiplyTransform : procedure(p:dword); @stdcall;
SetViewport : procedure(p:dword); @stdcall;
GetViewport : procedure(p:dword); @stdcall;
SetMaterial : procedure(p:dword); @stdcall;
GetMaterial : procedure(p:dword); @stdcall;
SetLight : procedure(p:dword); @stdcall;
GetLight : procedure(p:dword); @stdcall;
LightEnable : procedure(p:dword); @stdcall;
GetLightEnable : procedure(p:dword); @stdcall;
SetClipPlane : procedure(p:dword); @stdcall;
GetClipPlane : procedure(p:dword); @stdcall;
SetRenderState : procedure(p:dword;state:dword;value:dword ); @stdcall;
GetRenderState : procedure(p:dword;var pValue:dword ); @stdcall;
CreateStateBlock : procedure(p:dword); @stdcall;
BeginStateBlock : procedure(p:dword); @stdcall;
EndStateBlock : procedure(p:dword); @stdcall;
SetClipStatus : procedure(p:dword); @stdcall;
GetClipStatus : procedure(p:dword); @stdcall;
GetTexture : procedure(p:dword); @stdcall;
SetTexture : procedure(p:dword); @stdcall;
GetTextureStageState : procedure(p:dword); @stdcall;
SetTextureStageState : procedure(p:dword); @stdcall;
GetSamplerState : procedure(p:dword); @stdcall;
SetSamplerState : procedure(p:dword); @stdcall;
ValidateDevice : procedure(p:dword); @stdcall;
SetPaletteEntries : procedure(p:dword); @stdcall;
GetPaletteEntries : procedure(p:dword); @stdcall;
SetCurrentTexturePalette : procedure(p:dword);@stdcall;
GetCurrentTexturePalette : procedure(p:dword);@stdcall;
SetScissorRect : procedure(p:dword); @stdcall;
GetScissorRect : procedure(p:dword); @stdcall;
SetSoftwareVertexProcessing : procedure(p:dword); @stdcall;
GetSoftwareVertexProcessing : procedure(p:dword); @stdcall;
SetNPatchMode : procedure(p:dword); @stdcall;
GetNPatchMode : procedure(p:dword); @stdcall;
DrawPrimitive : procedure(p:dword;a:dword;b:dword;c:dword); @stdcall;

DrawIndexedPrimitive : procedure(p:dword); @stdcall;
DrawPrimitiveUP : procedure(p:dword); @stdcall;
DrawIndexedPrimitiveUP : procedure(p:dword); @stdcall;
ProcessVertices : procedure(p:dword); @stdcall;
CreateVertexDeclaration : procedure(p:dword); @stdcall;
SetVertexDeclaration : procedure(p:dword); @stdcall;
GetVertexDeclaration : procedure(p:dword); @stdcall;
SetFVF : procedure(p:dword;a:dword); @stdcall;
GetFVF : procedure(p:dword; var a:dword); @stdcall;
CreateVertexShader : procedure(p:dword); @stdcall;
SetVertexShader : procedure(p:dword); @stdcall;
GetVertexShader : procedure(p:dword); @stdcall;
SetVertexShaderConstantF : procedure(p:dword);@stdcall;
GetVertexShaderConstantF : procedure(p:dword); @stdcall;
SetVertexShaderConstantI : procedure(p:dword);@stdcall;
GetVertexShaderConstantI : procedure(p:dword); @stdcall;
SetVertexShaderConstantB : procedure(p:dword); @stdcall;
GetVertexShaderConstantB : procedure(p:dword); @stdcall;
SetStreamSource : procedure(p:dword; a:dword;b:dword;c:dword;d:dword); @stdcall;
GetStreamSource : procedure(p:dword; a:dword;var b:dword;c:dword;d:dword); @stdcall;
SetStreamSourceFreq : procedure(p:dword); @stdcall;
GetStreamSourceFreq : procedure(p:dword); @stdcall;
SetIndices : procedure(p:dword); @stdcall;
GetIndices : procedure(p:dword); @stdcall;
CreatePixelShader : procedure(p:dword); @stdcall;
SetPixelShader : procedure(p:dword); @stdcall;
GetPixelShader : procedure(p:dword); @stdcall;
SetPixelShaderConstantF : procedure(p:dword); @stdcall;
GetPixelShaderConstantF : procedure(p:dword); @stdcall;
SetPixelShaderConstantI : procedure(p:dword); @stdcall;
GetPixelShaderConstantI : procedure(p:dword); @stdcall;
SetPixelShaderConstantB : procedure(p:dword); @stdcall;
GetPixelShaderConstantB : procedure(p:dword); @stdcall;
DrawRectPatch : procedure(p:dword); @stdcall;
DrawTriPatch : procedure(p:dword); @stdcall;
DeletePatch : procedure(p:dword); @stdcall;
CreateQuery : procedure(p:dword);@stdcall;
endrecord;
type
LPDIRECT3DDEVICE9 : pointer to IDirect3DDevice9;

storage
   _pd3dDevice : dword;

#macro Init(Device);
       mov(Device,Dx9Device._pd3dDevice);
#endmacro;

#macro  CleanUp;
  if(Dx9Device._pd3dDevice <> NULL) then
mov( Dx9Device._pd3dDevice , eax);
mov([eax], eax);
(type Dx9Device.IDirect3DDevice9 [eax]).Release(Dx9Device._pd3dDevice);
  endif;
  mov(NULL,Dx9Device._pd3dDevice);
#endmacro;

#macro Clear(a,b,c,d,e,f);
mov( Dx9Device._pd3dDevice , eax);
mov([eax], eax);
(type Dx9Device.IDirect3DDevice9 [eax]).Clear(Dx9Device._pd3dDevice,a,b,c,d,e,f);
#endmacro;   

#macro BeginScene;
mov( Dx9Device._pd3dDevice , eax);
mov([eax], eax);
(type Dx9Device.IDirect3DDevice9 [eax]).BeginScene(Dx9Device._pd3dDevice);
#endmacro;   

#macro EndScene;
mov( Dx9Device._pd3dDevice , eax);
mov([eax], eax);
(type Dx9Device.IDirect3DDevice9 [eax]).EndScene(Dx9Device._pd3dDevice);
#endmacro; 

#macro Present(a,b,c,d);
mov( Dx9Device._pd3dDevice , eax);
mov([eax], eax);
(type Dx9Device.IDirect3DDevice9 [eax]).Present(Dx9Device._pd3dDevice,a,b,c,d);
#endmacro;   

end Dx9Device;
/**************************************************************************************************

***************************************************************************************************/
#macro ZeroMemory(_dest_, _bytes_);
mov (_dest_, edi);
xor(eax,eax);
mov(_bytes_, ecx);
rep.stosb();
#endmacro
/**************************************************************************************************

***************************************************************************************************/

static
align(4);
hwnd : dword := NULL;
ClassName : string := "DirectXHLA";
AppCaption : string := "Directx9 frame work for HLA";
   
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
procedure initD3D;
static
d3dpp :  D3DPRESENT_PARAMETERS ;
pd3dDevice : dword ;
begin initD3D;

    mov(Direct3DCreate9(D3D_SDK_VERSION),eax );
    Dx9.Init(eax);
       
  ZeroMemory(&d3dpp, @size( D3DPRESENT_PARAMETERS));
 
  mov(true,d3dpp.Windowed);
  mov(D3DSWAPEFFECT_DISCARD,d3dpp.SwapEffect);
  mov(D3DFMT_UNKNOWN,d3dpp.BackBufferFormat );
   
   
    Dx9.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hwnd,
                  D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp, &pd3dDevice ) ;
                 
    Dx9Device.Init(pd3dDevice);
   
end initD3D;

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
procedure CleanUp;
begin CleanUp;
Dx9.CleanUp;
Dx9Device.CleanUp;
end CleanUp;


//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
procedure Render;
begin Render;

Dx9Device.Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0, 0);

Dx9Device.BeginScene;

   

Dx9Device.EndScene;

Dx9Device.Present(NULL, NULL, NULL, NULL);

end Render;

//-----------------------------------------------------------------------------
// Name: WndProc()
// Desc: The window's message handler
procedure WndProc( hwnd : dword;
uMsg : uns32;
wParam : dword;
lParam : dword ); @stdcall; @NoDisplay;
begin WndProc;
if ( uMsg == w.WM_DESTROY ) then
CleanUp();
w.PostQuitMessage( 0 );
elseif ( uMsg ==  w.WM_KEYDOWN ) then
if (wParam == w.VK_ESCAPE) then
w.PostMessage(hwnd, w.WM_CLOSE, NULL, NULL);
endif;
endif;
w.DefWindowProc( hwnd, uMsg, wParam, lParam );
end WndProc;

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
procedure WinMain ( hInstance:DWORD;
    prevInstance:DWORD;
    cmdlinePtr:DWORD;
    cmdShow:DWORD );
static
wc:w.WNDCLASSEX;
msg:w.MSG;
begin WinMain;

ZeroMemory( &wc, @size( w.WNDCLASSEX ) );
assign( wc.cbSize = @size( w.WNDCLASSEX ),
wc.style = w.CS_HREDRAW | w.CS_VREDRAW,
wc.lpfnWndProc = &WndProc,
wc.hbrBackground = w.COLOR_BTNFACE+1,
wc.lpszClassName = ClassName,
wc.hInstance = hInstance,
wc.hIcon  = wc.hIconSm = w.LoadIcon( hInstance, "Icon_App" ) returns eax,
wc.hCursor = w.LoadCursor( NULL, val w.IDC_ARROW ) returns eax );

w.RegisterClassEx( wc );

// Create the application's window
assign( hwnd = w.CreateWindowEx(NULL,
ClassName, AppCaption,
w.WS_OVERLAPPEDWINDOW,
100, 100, 640, 480,
w.GetDesktopWindow(), NULL, hInstance, NULL ) returns eax );

// Initialize Direct3D
initD3D();

// Show the window
w.ShowWindow( hwnd, w.SW_SHOWDEFAULT );
    w.UpdateWindow( hwnd );
    // Enter the message loop
while(msg.message <> w.WM_QUIT) do
if( w.PeekMessage( &msg, NULL, 0, 0, w.PM_REMOVE )) then
w.TranslateMessage( msg );
w.DispatchMessage( msg );
else
Render();
endif;
endwhile;

  w.UnregisterClass(ClassName, hInstance);
mov( 0 , eax);

end WinMain;   
begin directxcreate;
WinMain(w.GetModuleHandle( NULL ), NULL, NULL, NULL);
end directxcreate;