Direct3D9:
using System;using System.Drawing;using SlimDX;using SlimDX.Direct3D9;using SlimDX.Windows;namespace Sample{ static class Program { [STAThread] static void Main() { var form = new RenderForm("SlimDX Comparison"); var device = new Device(new Direct3D(), 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, new PresentParameters() { BackBufferWidth = form.ClientSize.Width, BackBufferHeight = form.ClientSize.Height }); MessagePump.Run(form, () => { device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); device.BeginScene(); device.EndScene(); device.Present(); }); foreach (var item in ObjectTable.Objects) item.Dispose(); } }}
The C++ equivalent is, as you might imagine, quite disgusting by comparison:
#include #include #pragma comment(lib, "d3d9.lib")LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){ switch (message) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_PAINT: ValidateRect(hWnd, 0); return 0; } return DefWindowProc(hWnd, message, wParam, lParam);}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursor(hInstance, IDC_ARROW); wcex.hbrBackground = reinterpret_cast(GetStockObject(WHITE_BRUSH)); wcex.lpszMenuName = NULL; wcex.lpszClassName = L"TestWindowClass"; wcex.hIconSm = wcex.hIcon; RegisterClassEx(&wcex); HWND hWnd = CreateWindow(L"TestWindowClass", L"SlimDX Comparison", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, 0, 0, hInstance, 0); RECT rect = {0, 0, 800, 600}; AdjustWindowRect(&rect, GetWindowLong(hWnd, GWL_STYLE), FALSE); SetWindowPos(hWnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE); ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); LPDIRECT3D9 d3d = Direct3DCreate9(D3D_SDK_VERSION); if (!d3d) { MessageBox(NULL, L"Direct3DCreate9 - Failed", 0, 0); return 0; } D3DPRESENT_PARAMETERS pp; pp.BackBufferCount = 1; pp.BackBufferFormat = D3DFMT_X8R8G8B8; pp.BackBufferWidth = 800; pp.BackBufferHeight = 600; pp.MultiSampleType = D3DMULTISAMPLE_NONE; pp.MultiSampleQuality = 0; pp.Windowed = TRUE; pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.hDeviceWindow = hWnd; pp.EnableAutoDepthStencil = TRUE; pp.AutoDepthStencilFormat = D3DFMT_D24X8; pp.Flags = 0; pp.FullScreen_RefreshRateInHz = 0; pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; LPDIRECT3DDEVICE9 device; HRESULT hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &device); if (FAILED(hr)) { d3d->Release(); MessageBox(NULL, L"CreateDevice - Failed", 0, 0); return 0; } MSG msg; while (1) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage (&msg); } else { device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0); device->BeginScene(); device->EndScene(); device->Present(0, 0, 0, 0); } } device->Release(); d3d->Release(); return msg.wParam;}
This is one using our latest addition of Direct3D11. The D3D10 version looks quite similar to this.
using System;using SlimDX;using SlimDX.Direct3D11;using SlimDX.DXGI;using SlimDX.Windows;namespace Demo{ static class Program { [STAThread] static void Main() { var form = new RenderForm(); var desc = new SwapChainDescription() { BufferCount = 1, ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.B8G8R8A8_UNorm), IsWindowed = true, OutputHandle = form.Handle, SampleDescription = new SampleDescription(1, 0), SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput }; SwapChain swapChain; SlimDX.Direct3D11.Device device; SlimDX.Direct3D11.Device.CreateWithSwapChain(null, DriverType.Reference, DeviceCreationFlags.Debug, desc, out device, out swapChain); var context = new DeviceContext(device); var renderView = new RenderTargetView(device, swapChain.GetBuffer2D
context.OutputMerger.SetTargets(renderView);
context.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height));
MessagePump.Run(form, () =>
{
context.ClearRenderTargetView(renderView, new Color4(0.0f, 0.5f, 1.0f));
swapChain.Present(0, PresentFlags.None);
});
foreach (var item in ObjectTable.Objects)
item.Dispose();
}
}
}
I've been tossing around the idea of making these into project templates for Visual Studio. Thoughts?