Skip to content

Instantly share code, notes, and snippets.

@ousttrue
Last active April 8, 2019 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ousttrue/2267a2cd578e11d3e31f617e6b1210a1 to your computer and use it in GitHub Desktop.
Save ousttrue/2267a2cd578e11d3e31f617e6b1210a1 to your computer and use it in GitHub Desktop.
minimum win32api sample
//
// add d3d11 to libs in dub.json
//
import core.runtime;
import core.sys.windows.windows;
extern (Windows)
{
enum D3D_DRIVER_TYPE
{
D3D_DRIVER_TYPE_UNKNOWN = 0,
D3D_DRIVER_TYPE_HARDWARE = (D3D_DRIVER_TYPE_UNKNOWN + 1),
D3D_DRIVER_TYPE_REFERENCE = (D3D_DRIVER_TYPE_HARDWARE + 1),
D3D_DRIVER_TYPE_NULL = (D3D_DRIVER_TYPE_REFERENCE + 1),
D3D_DRIVER_TYPE_SOFTWARE = (
D3D_DRIVER_TYPE_NULL + 1),
D3D_DRIVER_TYPE_WARP = (D3D_DRIVER_TYPE_SOFTWARE + 1)
}
enum D3D_FEATURE_LEVEL
{
D3D_FEATURE_LEVEL_9_1 = 0x9100,
D3D_FEATURE_LEVEL_9_2 = 0x9200,
D3D_FEATURE_LEVEL_9_3 = 0x9300,
D3D_FEATURE_LEVEL_10_0 = 0xa000,
D3D_FEATURE_LEVEL_10_1 = 0xa100,
D3D_FEATURE_LEVEL_11_0 = 0xb000,
D3D_FEATURE_LEVEL_11_1 = 0xb100,
D3D_FEATURE_LEVEL_12_0 = 0xc000,
D3D_FEATURE_LEVEL_12_1 = 0xc100
}
interface IDXGIAdapter
{
}
interface ID3D11Device
{
}
interface ID3D11DeviceContext
{
}
HRESULT D3D11CreateDevice(IDXGIAdapter pAdapter, D3D_DRIVER_TYPE DriverType,
HMODULE Software, UINT Flags, const(D3D_FEATURE_LEVEL)* pFeatureLevels,
UINT FeatureLevels, UINT SDKVersion, ID3D11Device* ppDevice,
D3D_FEATURE_LEVEL* pFeatureLevel, ID3D11DeviceContext* ppImmediateContext);
}
extern (Windows) LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProcW(hWnd, message, wParam, lParam);
}
}
extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
Runtime.initialize();
scope (exit)
Runtime.terminate();
wstring appClass = "ClassName";
wstring appName = "WindowName";
WNDCLASSW wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = &WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIconW(cast(void*) null, cast(wchar*) IDI_APPLICATION);
wndclass.hCursor = LoadCursorW(cast(void*) null, cast(wchar*) IDC_ARROW);
wndclass.hbrBackground = null; //cast(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = null;
wndclass.lpszClassName = appClass.ptr;
if (!RegisterClassW(&wndclass))
{
return 1;
}
auto hWnd = CreateWindowW(appClass.ptr, // window class name
appName.ptr, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
null, // parent window handle
null, // window menu handle
hInstance, // program instance handle
null); // creation parameters
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hWnd == HWND.init)
{
return 2;
}
D3D_FEATURE_LEVEL[] levels = [
D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL.D3D_FEATURE_LEVEL_11_0,
];
ID3D11Device pDevice;
D3D_FEATURE_LEVEL level;
ID3D11DeviceContext pDeviceContext;
auto result = D3D11CreateDevice(null, D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE,
null, 0, levels.ptr, cast(uint) levels.length, 7, &pDevice,
&level, &pDeviceContext);
if (result != 0)
{
return 3;
}
// main loop
MSG msg;
while (true)
{
if (GetMessageW(&msg, null, 0, 0) == 0)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return cast(int) msg.wParam;
}
{
"libs": ["user32"] // <- required
}
import core.runtime;
import core.sys.windows.windows;
extern (Windows) LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) nothrow
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProcW(hWnd, message, wParam, lParam);
}
}
extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
Runtime.initialize();
scope (exit)
Runtime.terminate();
wstring appClass = "ClassName";
wstring appName = "WindowName";
WNDCLASSW wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = &WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIconW(cast(void*) null, cast(wchar*) IDI_APPLICATION);
wndclass.hCursor = LoadCursorW(cast(void*) null, cast(wchar*) IDC_ARROW);
wndclass.hbrBackground = null; //cast(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = null;
wndclass.lpszClassName = appClass.ptr;
if (!RegisterClassW(&wndclass))
{
return 1;
}
auto hWnd = CreateWindowW(appClass.ptr, // window class name
appName.ptr, // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
null, // parent window handle
null, // window menu handle
hInstance, // program instance handle
null); // creation parameters
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
if (hWnd == HWND.init)
{
return 2;
}
// main loop
MSG msg;
while (true)
{
if (GetMessageW(&msg, null, 0, 0) == 0)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return cast(int) msg.wParam;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment