Anmeldungsdatum: Aug 2007
Beiträge: 8643
Benutzer-Bewertung:
|
Hallo,
wer schonmal einen D3D Hack geschrieben hat, kennt das Problem sicherlich. Wenn man vom Window in den Fullscreen Modus wechselt, gibt es einen DirectX Error und das Spiel stürzt ab. Damit man schnell testen kann, welche Ressourcen man freigeben muss und welche nicht, habe ich ein einfaches D3D9 Programm geschrieben, mit dem man das machen kann. Baut einfach das ein, was ihr braucht und schaut ob es beim Switch abstürzt. Der Code enthält bereits eine Font, ein Sprite und eine Line.
TEXT Code: #include <d3dx9.h> #include <stdio.h> #pragma comment(lib, "d3d9.lib") #pragma comment(lib, "d3dx9.lib") //--------------------------------------------------------------------------- LPDIRECT3D9 pD3D = NULL; LPDIRECT3DDEVICE9 pDevice = NULL; D3DPRESENT_PARAMETERS pp; LPD3DXFONT font; LPD3DXSPRITE sprite; LPD3DXLINE line; //--------------------------------------------------------------------------- bool windowed = true; int reset = 0; HMENU menu; //--------------------------------------------------------------------------- HRESULT D3DInit(HWND hwnd) { if(!(pD3D = Direct3DCreate9(D3D_SDK_VERSION))) return E_FAIL; D3DDISPLAYMODE dm; if(FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&dm))) return E_FAIL; ZeroMemory(&pp,sizeof(pp)); pp.MultiSampleType = D3DMULTISAMPLE_NONE; pp.MultiSampleQuality = 0; pp.BackBufferCount = 1; pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.BackBufferFormat = dm.Format; pp.EnableAutoDepthStencil = TRUE; pp.AutoDepthStencilFormat = D3DFMT_D16; pp.hDeviceWindow = hwnd; pp.Flags = 0; pp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; pp.Windowed = TRUE; pp.BackBufferWidth = 284; pp.BackBufferHeight = 262; if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&pp,&pDevice))) return E_FAIL; pDevice->SetRenderState(D3DRS_LIGHTING,FALSE); D3DXCreateFont(pDevice,13,0,0,0,FALSE,DEFAULT_CHARSET,OUT_TT_ONLY_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,TEXT("Tahoma"),&font); D3DXCreateSprite(pDevice,&sprite); D3DXCreateLine(pDevice,&line); line->SetWidth(1); line->SetAntialias(false); line->SetGLLines(true); return S_OK; } //--------------------------------------------------------------------------- void preReset() { font->OnLostDevice(); sprite->OnLostDevice(); line->OnLostDevice(); } //--------------------------------------------------------------------------- void postReset() { font->OnResetDevice(); sprite->OnResetDevice(); line->OnResetDevice(); } //--------------------------------------------------------------------------- void switchWindowMode(bool window) { bool dowindow = false; if(window) { pp.Windowed = TRUE; pp.BackBufferWidth = 284; pp.BackBufferHeight = 262; SetWindowPos(pp.hDeviceWindow,HWND_NOTOPMOST,100,100,300,300,SWP_SHOWWINDOW); dowindow = true; } else { SetWindowLong(pp.hDeviceWindow, GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE); if(!menu) { menu = GetMenu(pp.hDeviceWindow); SetMenu(pp.hDeviceWindow,NULL); } pp.Windowed = FALSE; pp.BackBufferWidth = GetSystemMetrics(0); pp.BackBufferHeight = GetSystemMetrics(1); } preReset(); HRESULT result = pDevice->Reset(&pp); if(result == D3D_OK) { reset++; postReset(); if(dowindow) { if(menu) SetMenu(pp.hDeviceWindow,menu); SetWindowLong(pp.hDeviceWindow,GWL_STYLE,WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU|WS_VISIBLE|WS_OVERLAPPED|WS_SIZEBOX); SetWindowPos(pp.hDeviceWindow,HWND_NOTOPMOST,100,100,300,300,SWP_FRAMECHANGED|SWP_SHOWWINDOW); } } } //--------------------------------------------------------------------------- void D3DRender() { if(!pDevice) return; pDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0); pDevice->BeginScene(); sprite->Begin(D3DXSPRITE_ALPHABLEND); //normal test rect D3DRECT rect = { 10, 10, 20, 20 }; pDevice->Clear(1,&rect,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xFFFFFFFF,0,0); //font + sprite RECT rct; rct.left = 10; rct.top = 30; rct.right = rct.left+100; rct.bottom = rct.top+30; char text[100]; sprintf(text,"window: %s\nresetcount: %i",(windowed)?"true":"false",reset); font->DrawText(sprite,text,-1,&rct,DT_NOCLIP,D3DCOLOR_ARGB(255,255,255,0)); //line D3DXVECTOR2 vLine[2]; vLine[0].x = 40; vLine[0].y = 5; vLine[1].x = 200; vLine[1].y = 35; line->Begin(); line->Draw(vLine,2,D3DCOLOR_ARGB(255,255,0,0)); line->End(); sprite->End(); pDevice->EndScene(); pDevice->Present(NULL,NULL,NULL,NULL); } //--------------------------------------------------------------------------- LRESULT WINAPI MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_KEYUP: if(wParam == VK_F1) { windowed = !windowed; switchWindowMode(windowed); } return 0; case WM_PAINT: D3DRender(); ValidateRect(hwnd,NULL); return 0; } return DefWindowProc(hwnd,msg,wParam,lParam); } //--------------------------------------------------------------------------- int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D", NULL }; RegisterClassEx(&wc); HWND hwnd = CreateWindow("D3D","D3D",WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL); if(SUCCEEDED(D3DInit(hwnd))) { ShowWindow(hwnd,SW_SHOWDEFAULT ); UpdateWindow(hwnd); MSG msg; ZeroMemory(&msg,sizeof(msg)); while(msg.message != WM_QUIT) { if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else D3DRender(); } } if(pDevice) pDevice->Release(); if(pD3D) pD3D->Release(); UnregisterClass("D3D",wc.hInstance); return 0; } //---------------------------------------------------------------------------
Mit F1 kann man den Vollbildmodus (de)aktivieren.
Viel Spaß beim Rumprobieren.
greetz KN4CK3R
|