OldSchoolHack

Register / Login English

[C++][D3D] Reset, Release ja oder nein?


icon [C++][D3D] Reset, Release ja oder nein? #1

Join Date: Aug 2007

Posts: 8643

User-Rating:

199 positive
33 negative
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:
  1. #include <d3dx9.h>
  2. #include <stdio.h>
  3. #pragma comment(lib, "d3d9.lib")
  4. #pragma comment(lib, "d3dx9.lib")
  5. //---------------------------------------------------------------------------
  6. LPDIRECT3D9 pD3D = NULL;
  7. LPDIRECT3DDEVICE9 pDevice = NULL;
  8. D3DPRESENT_PARAMETERS pp;
  9. LPD3DXFONT font;
  10. LPD3DXSPRITE sprite;
  11. LPD3DXLINE line;
  12. //---------------------------------------------------------------------------
  13. bool windowed = true;
  14. int reset = 0;
  15. HMENU menu;
  16. //---------------------------------------------------------------------------
  17. HRESULT D3DInit(HWND hwnd)
  18. {
  19. if(!(pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
  20. return E_FAIL;
  21.  
  22. D3DDISPLAYMODE dm;
  23. if(FAILED(pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&dm)))
  24. return E_FAIL;
  25.  
  26.  
  27. ZeroMemory(&pp,sizeof(pp));
  28. pp.MultiSampleType = D3DMULTISAMPLE_NONE;
  29. pp.MultiSampleQuality = 0;
  30. pp.BackBufferCount = 1;
  31. pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  32. pp.BackBufferFormat = dm.Format;
  33. pp.EnableAutoDepthStencil = TRUE;
  34. pp.AutoDepthStencilFormat = D3DFMT_D16;
  35. pp.hDeviceWindow = hwnd;
  36. pp.Flags = 0;
  37. pp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  38. pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
  39. pp.Windowed = TRUE;
  40. pp.BackBufferWidth = 284;
  41. pp.BackBufferHeight = 262;
  42.  
  43. if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&pp,&pDevice)))
  44. return E_FAIL;
  45.  
  46. pDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
  47.  
  48. D3DXCreateFont(pDevice,13,0,0,0,FALSE,DEFAULT_CHARSET,OUT_TT_ONLY_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,TEXT("Tahoma"),&font);
  49. D3DXCreateSprite(pDevice,&sprite);
  50. D3DXCreateLine(pDevice,&line);
  51. line->SetWidth(1);
  52. line->SetAntialias(false);
  53. line->SetGLLines(true);
  54.  
  55. return S_OK;
  56. }
  57. //---------------------------------------------------------------------------
  58. void preReset()
  59. {
  60. font->OnLostDevice();
  61. sprite->OnLostDevice();
  62. line->OnLostDevice();
  63. }
  64. //---------------------------------------------------------------------------
  65. void postReset()
  66. {
  67. font->OnResetDevice();
  68. sprite->OnResetDevice();
  69. line->OnResetDevice();
  70. }
  71. //---------------------------------------------------------------------------
  72. void switchWindowMode(bool window)
  73. {
  74. bool dowindow = false;
  75. if(window)
  76. {
  77. pp.Windowed = TRUE;
  78. pp.BackBufferWidth = 284;
  79. pp.BackBufferHeight = 262;
  80. SetWindowPos(pp.hDeviceWindow,HWND_NOTOPMOST,100,100,300,300,SWP_SHOWWINDOW);
  81. dowindow = true;
  82. }
  83. else
  84. {
  85. SetWindowLong(pp.hDeviceWindow, GWL_STYLE, WS_POPUP|WS_SYSMENU|WS_VISIBLE);
  86. if(!menu)
  87. {
  88. menu = GetMenu(pp.hDeviceWindow);
  89. SetMenu(pp.hDeviceWindow,NULL);
  90. }
  91. pp.Windowed = FALSE;
  92. pp.BackBufferWidth = GetSystemMetrics(0);
  93. pp.BackBufferHeight = GetSystemMetrics(1);
  94. }
  95. preReset();
  96. HRESULT result = pDevice->Reset(&pp);
  97. if(result == D3D_OK)
  98. {
  99. reset++;
  100. postReset();
  101. if(dowindow)
  102. {
  103. if(menu)
  104. SetMenu(pp.hDeviceWindow,menu);
  105. SetWindowLong(pp.hDeviceWindow,GWL_STYLE,WS_CAPTION|WS_MINIMIZEBOX|WS_SYSMENU|WS_VISIBLE|WS_OVERLAPPED|WS_SIZEBOX);
  106. SetWindowPos(pp.hDeviceWindow,HWND_NOTOPMOST,100,100,300,300,SWP_FRAMECHANGED|SWP_SHOWWINDOW);
  107. }
  108. }
  109. }
  110. //---------------------------------------------------------------------------
  111. void D3DRender()
  112. {
  113. if(!pDevice)
  114. return;
  115.  
  116. pDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,0),1.0f,0);
  117.  
  118. pDevice->BeginScene();
  119. sprite->Begin(D3DXSPRITE_ALPHABLEND);
  120.  
  121. //normal test rect
  122. D3DRECT rect = { 10, 10, 20, 20 };
  123. pDevice->Clear(1,&rect,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xFFFFFFFF,0,0);
  124.  
  125. //font + sprite
  126. RECT rct;
  127. rct.left = 10;
  128. rct.top = 30;
  129. rct.right = rct.left+100;
  130. rct.bottom = rct.top+30;
  131. char text[100];
  132. sprintf(text,"window: %s\nresetcount: %i",(windowed)?"true":"false",reset);
  133. font->DrawText(sprite,text,-1,&rct,DT_NOCLIP,D3DCOLOR_ARGB(255,255,255,0));
  134.  
  135. //line
  136. D3DXVECTOR2 vLine[2];
  137. vLine[0].x = 40;
  138. vLine[0].y = 5;
  139. vLine[1].x = 200;
  140. vLine[1].y = 35;
  141. line->Begin();
  142. line->Draw(vLine,2,D3DCOLOR_ARGB(255,255,0,0));
  143. line->End();
  144.  
  145. sprite->End();
  146. pDevice->EndScene();
  147. pDevice->Present(NULL,NULL,NULL,NULL);
  148. }
  149. //---------------------------------------------------------------------------
  150. LRESULT WINAPI MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  151. {
  152. switch(msg)
  153. {
  154. case WM_DESTROY:
  155. PostQuitMessage(0);
  156. return 0;
  157. case WM_KEYUP:
  158. if(wParam == VK_F1)
  159. {
  160. windowed = !windowed;
  161. switchWindowMode(windowed);
  162. }
  163. return 0;
  164. case WM_PAINT:
  165. D3DRender();
  166. ValidateRect(hwnd,NULL);
  167. return 0;
  168. }
  169.  
  170. return DefWindowProc(hwnd,msg,wParam,lParam);
  171. }
  172. //---------------------------------------------------------------------------
  173. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  174. {
  175. WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  176. GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
  177. "D3D", NULL };
  178. RegisterClassEx(&wc);
  179.  
  180. HWND hwnd = CreateWindow("D3D","D3D",WS_OVERLAPPEDWINDOW,100,100,300,300,GetDesktopWindow(),NULL,wc.hInstance,NULL);
  181.  
  182. if(SUCCEEDED(D3DInit(hwnd)))
  183. {
  184. ShowWindow(hwnd,SW_SHOWDEFAULT );
  185. UpdateWindow(hwnd);
  186.  
  187. MSG msg;
  188. ZeroMemory(&msg,sizeof(msg));
  189. while(msg.message != WM_QUIT)
  190. {
  191. if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
  192. {
  193. TranslateMessage(&msg);
  194. DispatchMessage(&msg);
  195. }
  196. else
  197. D3DRender();
  198. }
  199. }
  200.  
  201. if(pDevice)
  202. pDevice->Release();
  203. if(pD3D)
  204. pD3D->Release();
  205.  
  206. UnregisterClass("D3D",wc.hInstance);
  207. return 0;
  208. }
  209. //---------------------------------------------------------------------------

Mit F1 kann man den Vollbildmodus (de)aktivieren.

http://www.abload.de/img/d3dresettspx.jpg

Viel Spaß beim Rumprobieren.

greetz KN4CK3R

__________________

Hallo
icon #2

Join Date: Aug 2008

Posts: 295

Bleib dabei, bist ne coole sau!  8-)

__________________

http://www.abload.de/img/oshforg46x.jpg