此篇所言 只针对windows 32 bit 版本 Linux/Unix/Mac/Win64 无关... Win32 下 里面的那个makefile窝用了 无法编译成功 .. 如果你可以编译成功的话 可以无视这篇文章
1.
下载
首先 需要在官网下载d语言的编译器 ... http://dlang.org/download.html
然后 下载 d 语言的directx 库 - bindings 这个项目托管在 GitHub 了 ..https://github.com/CS-svnmirror/dsource-bindings
还有一个 omf 格式 的 lib 工具 这个工具在 下载包里面貌似是没有的 http://pan.baidu.com/s/1dD1ul6T(d语言使用 omf 格式的lib与vc的 coff 格式 lib冲突)
所有原料工具准备完成之后 开始配置环境 ..
2.
配置
dmd编译器设置 文本模式打开 dmd2\windows\bin 下的 sc.ini 在
; environment for both 32/64 bit
[Environment]
DFLAGS="-I%@P%\..\..\src\phobos" "-I%@P%\..\..\src\druntime\import" 后面加上 "-I%@P%\..\..\src" ( 示例 : DFLAGS="-I%@P%\..\..\src\phobos" "-I%@P%\..\..\src\druntime\import" "-I%@P%\..\..\src") 保存退出 ...
bindings 设置
把 bindings 下的 Win32 文件夹移动到 dmd 编译器 文件夹的 dmd2\src 下 ... 好了 剩下就是生成 lib 了 实际上这也是本篇比较麻烦的地方 窝只说怎么做 没说为什么 .. 有兴趣可以自己看看函数调用约定以及c/c++/d 风格的函数修饰名 以及 implib/.def/lib 的用法
lib 设置
打开 lib 包里面的 implib ... bindings 不提供 lib 不然自己要LoadLibaray 或者 GetProcAddress 了 ... 很麻烦 ...再把系统system32下的 d3d9.dll 拿出来 新建一个.def文件 写上 (其余函数/DLL一样)
LIBRARY "d3d9"
EXPORTS
_Direct3DCreate9@4 = Direct3DCreate9
保存退出 打开命令行 输入 implib /noi /system d3d9.lib d3d9.def 得到的lib 放在 默认的lib目录下 ... (dmd2\windows\lib)
编译一下如下代码试一下是否配置成功
module winsamp;
pragma (lib, "gdi32.lib");
pragma (lib, "d3d9.lib");
import core.runtime;
import core.sys.windows.windows;
//import win32.directx.d3d9caps;
import win32.directx.d3d9;
//import win32.directx.d3dx9;
import std.string;
IDirect3DDevice9 Device = null;
IDirect3DSurface9 _surface = null;
enum {
VIDEO_WIDTH = 256,
VIDEO_HEIGHT = 240,
WINDOW_BOUNDARY = 4,
WINDOW_TOP_BOUNDARY = 23
}
extern(C)
void FillBG (uint p32BitVram, uint pitch, uint color, uint width, uint height){
asm {
naked ; // use naked asm mode
pushad ; // - N save lod frame
// - N
mov ESI, 48[ESP] ; // ESI <- width
mov EAX, 44[ESP] ; // EAX <- color
mov EBP, 40[ESP] ; // EBP <- pitch
lea EBX, [ESI*4] ; // EBX <- get correct x width size (32 bit * 4)
mov EDI, 36[ESP] ; // EDI <- p32BitVram
sub EBP, EBX ; // EBP <- correct difference value
mov EBX, 52[ESP] ; // EBX <- y cnt
mov ECX, ESI ; // save frame
align 16 ; //
FBG_loop:
rep ; // pixel copy
stosd ;
add EDI, EBP ; // move pixel pointer
mov ECX, ESI ; // recover x cnt
dec EBX ; // dec y cnt
jnz FBG_loop ; // is end
popad ; // over restore REG
ret ; // return child proc end
}
}
void DrawSurface ( DWORD color )
{
static D3DSURFACE_DESC sur_desc;
static D3DLOCKED_RECT vramRECT;
Device.BeginScene(); // start drawing ...
Device.GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&_surface);
_surface.GetDesc(&sur_desc);//
_surface.LockRect(&vramRECT,null, 0);
FillBG(cast(uint)vramRECT.pBits, vramRECT.Pitch, color, VIDEO_WIDTH,VIDEO_HEIGHT);
_surface.UnlockRect();
Device.EndScene();
Device.Present(null, null, null, null);
}
extern(Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
int result;
void exceptionHandler(Throwable e) { throw e; }
try
{
Runtime.initialize(&exceptionHandler);
result = myWinMain(hInstance, hPrevInstance, lpCmdLine, iCmdShow);
Runtime.terminate(&exceptionHandler);
}
catch (Throwable e)
{
MessageBoxA(null, e.toString().toStringz, "Error", MB_OK | MB_ICONEXCLAMATION);
result = 0;
}
return result;
}
int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
string caption = "DirectX Sample";
string className = "DWndClass";
HWND hWnd, btnClick, btnDontClick;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = &WindowProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIconA(null, IDI_APPLICATION);
wndclass.hCursor = LoadCursorA(null, IDC_CROSS);
wndclass.hbrBackground = cast(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = null;
wndclass.lpszClassName = className.toStringz;
if (!RegisterClassA(&wndclass))
{
MessageBoxA(null, "Couldn't register Window Class!", caption.toStringz, MB_ICONERROR);
return 0;
}
hWnd = CreateWindowA(className.toStringz, // window class name
caption.toStringz, // window caption
WS_THICKFRAME |
WS_MAXIMIZEBOX |
WS_MINIMIZEBOX |
WS_SYSMENU |
WS_VISIBLE, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
VIDEO_WIDTH + WINDOW_BOUNDARY * 2, // initial x size
VIDEO_HEIGHT + WINDOW_BOUNDARY + WINDOW_TOP_BOUNDARY, // initial y size
HWND_DESKTOP, // parent window handle
null, // window menu handle
hInstance, // program instance handle
null); // creation parameters
if (hWnd is null)
{
MessageBoxA(null, "Couldn't create window.", caption.toStringz, MB_ICONERROR);
return 0;
}
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
IDirect3D9 pD3d9 = Direct3DCreate9(D3D_SDK_VERSION);
if( pD3d9 is null )
{
MessageBoxA(null, "Direct3DCreate9() - FAILED", "DirectX", MB_ICONERROR);
return false;
}
static D3DCAPS9 caps;
pD3d9.GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
int vp = 0;
if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = 0;
d3dpp.BackBufferHeight = 0;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.Windowed = true;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
pD3d9.CreateDevice(
D3DADAPTER_DEFAULT, // primary adapter
D3DDEVTYPE_HAL, // device type
hWnd, // window associated with device
vp, // vertex processing
&d3dpp, // present parameters
&Device); // return created device
pD3d9.Release(); // done with d3d9 object
while(msg.message != WM_QUIT)
{
if(PeekMessageA(&msg, null, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
else
{
DrawSurface(0xfe11c3);
}
}
Device.Release();
return cast(int) msg.wParam;
}
extern(Windows)
LRESULT WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProcA(hWnd, message, wParam, lParam);
}