win32 空项目 d3d9 装载个位图photo.png 为什么窗口一闪退出编译没错
#include<Windows.h>#include<d3d9.h>
#include<d3dx9.h>
#include<time.h>
#include<iostream>
using namespace std;
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
const string APPTITLE = "Draw Bitmap Program";
#define SCREENW 800
#define SCREENH 600
#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code)&0x8000)?1:0)
LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
LPDIRECT3DSURFACE9 backbuffer = NULL;
LPDIRECT3DSURFACE9 surface = NULL;
bool gameover = false;
bool Game_Init(HWND window)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
if (!d3d)
{
MessageBox(window, "Error initializing Direct3D", "Error", MB_OK);
return false;
}
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = SCREENW;
d3dpp.BackBufferHeight = SCREENH;
d3dpp.hDeviceWindow = window;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
if (!d3ddev)
{
MessageBox(window, "Error creating Dicect3D device", "Error", MB_OK);
return false;
}
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
HRESULT result = d3ddev->CreateOffscreenPlainSurface(SCREENW, SCREENH, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
if (result != D3D_OK) return false;
result = D3DXLoadSurfaceFromFile(surface, NULL, NULL, "phtot.png", NULL, D3DX_DEFAULT, 0, NULL);
if (result != D3D_OK) return false;
return true;
}
void Game_Run(HWND hwnd)
{
if (!d3ddev) return;
d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
if (d3ddev->BeginScene())
{
d3ddev->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
}
if (KEY_DOWN(VK_ESCAPE))
PostMessage(hwnd, WM_DESTROY, 0, 0);
}
void Game_End(HWND hwnd)
{
if (surface) surface->Release();
if (d3ddev) d3ddev->Release();
if (d3d) d3d->Release();
}
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
gameover = true;
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG message;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.lpszMenuName = NULL;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = "MainWindowClass";
if (!RegisterClassEx(&wc))
return FALSE;
hwnd = CreateWindow("MainWindowClass", APPTITLE.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, SCREENW, SCREENH, NULL, NULL, hInstance, NULL);
if (hwnd == 0)return 0;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
if (!Game_Init(hwnd)) return FALSE;
while (!gameover)
{
if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
Game_Run(hwnd);
}
return message.wParam;
}