include <directxsdk/d3d9.h>
include <directxsdk/d3dx9.h>
include <directxsdk/d3dx9tex.h>
include <windows.h>
include
int width = 1084;
int height = 628;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 注册窗口类
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "D3DWindowClass";
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0,
"D3DWindowClass",
"D3D Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, width, height,
NULL, NULL, hInstance, NULL
);
if (!hwnd) {
std::cerr << "Failed to create window." << std::endl;
return -1;
}
ShowWindow(hwnd, nCmdShow);
// 初始化Direct3D
IDirect3D9* d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (!d3d) {
std::cerr << "Failed to create D3D object." << std::endl;
return -1;
}
D3DPRESENT_PARAMETERS d3dpp = {};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
IDirect3DDevice9* device;
if (FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &device))) {
std::cerr << "Failed to create D3D device." << std::endl;
d3d->Release();
return -1;
}
// 加载图片
IDirect3DSurface9* surface;
if (FAILED(device->CreateOffscreenPlainSurface(width, height
, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL))) {
std::cerr << "Failed to create surface." << std::endl;
device->Release();
d3d->Release();
return -1;
}
// 从文件加载图像到表面
HRESULT hr = D3DXLoadSurfaceFromFile(
surface, // 目标表面
NULL, // 目标调色板
NULL, // 目标矩形
"D:\\image.jpg", // 源文件路径
NULL, // 源矩形
D3DX_FILTER_NONE, // 过滤器
0xFF000000, // 颜色键
NULL // 图像信息
);
if (FAILED(hr)) {
std::cerr << "Failed to load image from file." << std::endl;
surface->Release();
device->Release();
d3d->Release();
return -1;
}
// 渲染循环
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
// 清除屏幕
device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
// 开始场景
device->BeginScene();
// 将表面绘制到屏幕上
LPDIRECT3DSURFACE9 backBuffer;
device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
device->StretchRect(surface, NULL, backBuffer, NULL, D3DTEXF_NONE);
// 结束场景
device->EndScene();
// 交换缓冲区
device->Present(NULL, NULL, NULL, NULL);
}
// 释放资源
surface->Release();
device->Release();
d3d->Release();
return 0;
}
标签:include,return,实现,hwnd,Release,D3D9,device,NULL,图片 From: https://www.cnblogs.com/thinkinc999/p/18460748