DX9GraphicsApi.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include <d3d9.h>
  2. #include "Bild.h"
  3. #include "DLLRegister.h"
  4. #include "Fenster.h"
  5. #include "Globals.h"
  6. #include "GraphicsApi.h"
  7. #include "Kam3D.h"
  8. #include "Textur.h"
  9. #include "Welt3D.h"
  10. #include "Zeit.h"
  11. using namespace Framework;
  12. DirectX9::DirectX9()
  13. : GraphicsApi(DIRECTX9),
  14. pDirect3D(0),
  15. pDevice(0),
  16. pBackBuffer(0),
  17. backRect(new D3DLOCKED_RECT())
  18. {
  19. uiBild = new Bild(1);
  20. }
  21. DirectX9::~DirectX9()
  22. {
  23. backRect->pBits = NULL;
  24. delete backRect;
  25. if (pBackBuffer)
  26. {
  27. pBackBuffer->Release();
  28. pBackBuffer = NULL;
  29. }
  30. if (pDevice)
  31. {
  32. pDevice->Release();
  33. pDevice = NULL;
  34. }
  35. if (pDirect3D)
  36. {
  37. pDirect3D->Release();
  38. pDirect3D = NULL;
  39. getDLLRegister()->releaseDLL("d3d9.dll");
  40. }
  41. uiBild->release();
  42. }
  43. typedef IDirect3D9*(__stdcall* D3D9CreateFunction)(UINT);
  44. void DirectX9::initialize(
  45. WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen)
  46. {
  47. if (pDirect3D)
  48. return GraphicsApi::initialize(fenster, backBufferSize, fullScreen);
  49. GraphicsApi::initialize(fenster, backBufferSize, fullScreen);
  50. HINSTANCE dll = getDLLRegister()->ladeDLL("d3d9.dll", "d3d9.dll");
  51. if (!dll)
  52. {
  53. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  54. new Text("Fehler"),
  55. new Text("DirectX 9 konnte nicht gefunden werden."),
  56. MB_ICONERROR);
  57. return;
  58. }
  59. D3D9CreateFunction direct3DCreate9
  60. = (D3D9CreateFunction)GetProcAddress(dll, "Direct3DCreate9");
  61. if (!direct3DCreate9)
  62. {
  63. getDLLRegister()->releaseDLL("d3d9.dll");
  64. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  65. new Text("Fehler"),
  66. new Text("Der Einstiegspunkt Direct3DCreate9 fon DirectX 9 konnte "
  67. "nicht gefunden werden."),
  68. MB_ICONERROR);
  69. return;
  70. }
  71. pDirect3D = direct3DCreate9(D3D_SDK_VERSION);
  72. D3DPRESENT_PARAMETERS d3dpp;
  73. ZeroMemory(&d3dpp, sizeof(d3dpp));
  74. d3dpp.Windowed = !fullScreen;
  75. d3dpp.hDeviceWindow = fenster ? fenster->getFensterHandle() : 0;
  76. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  77. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  78. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  79. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  80. d3dpp.BackBufferHeight = this->backBufferSize.y;
  81. d3dpp.BackBufferWidth = this->backBufferSize.x;
  82. uiBild->neuBild(this->backBufferSize.x, this->backBufferSize.y, 0xFF000000);
  83. HRESULT result = pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
  84. D3DDEVTYPE_HAL,
  85. fenster->getFensterHandle(),
  86. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
  87. &d3dpp,
  88. &pDevice);
  89. if (result != S_OK)
  90. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  91. new Text("Fehler"),
  92. new Text("DirectX 9 konnte nicht initialisiert werden."),
  93. MB_ICONERROR);
  94. if (pDevice)
  95. result = pDevice->GetBackBuffer(
  96. 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  97. if (result != S_OK)
  98. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  99. new Text("Fehler"),
  100. new Text("DirectX 9 konnte nicht initialisiert werden."),
  101. MB_ICONERROR);
  102. }
  103. void DirectX9::update()
  104. {
  105. if (!pDirect3D) return;
  106. backRect->pBits = NULL;
  107. if (pBackBuffer)
  108. {
  109. pBackBuffer->Release();
  110. pBackBuffer = NULL;
  111. }
  112. if (pDevice)
  113. {
  114. pDevice->Release();
  115. pDevice = NULL;
  116. }
  117. D3DPRESENT_PARAMETERS d3dpp;
  118. ZeroMemory(&d3dpp, sizeof(d3dpp));
  119. d3dpp.Windowed = !fullScreen;
  120. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  121. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  122. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  123. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  124. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  125. if (!backBufferSize.x || !backBufferSize.y)
  126. backBufferSize = fenster->getKörperGröße();
  127. d3dpp.BackBufferHeight = backBufferSize.y;
  128. d3dpp.BackBufferWidth = backBufferSize.x;
  129. uiBild->neuBild(backBufferSize.x, backBufferSize.y, 0xFF000000);
  130. HRESULT result = pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
  131. D3DDEVTYPE_HAL,
  132. fenster->getFensterHandle(),
  133. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
  134. &d3dpp,
  135. &pDevice);
  136. if (result != S_OK)
  137. WMessageBox(fenster->getFensterHandle(),
  138. new Text("Fehler"),
  139. new Text("DirectX 9 konnte nicht initialisiert werden."),
  140. MB_ICONERROR);
  141. if (pDevice)
  142. result = pDevice->GetBackBuffer(
  143. 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
  144. if (result != S_OK)
  145. WMessageBox(fenster->getFensterHandle(),
  146. new Text("Fehler"),
  147. new Text("DirectX 9 konnte nicht initialisiert werden."),
  148. MB_ICONERROR);
  149. }
  150. void DirectX9::beginFrame(bool fill2D, bool fill3D, int fillColor)
  151. {
  152. if (fill2D) uiBild->setFarbe(fillColor);
  153. }
  154. void DirectX9::renderKamera(Kam3D* zKamera)
  155. {
  156. Mat4<float> mat = zKamera->getProjectionMatrix();
  157. Mat4<float> inv = zKamera->getViewMatrix().getInverse();
  158. Welt3D* welt = zKamera->zWelt();
  159. ZeitMesser zm = ZeitMesser();
  160. zm.messungStart();
  161. for (int x = 0; x < zKamera->zViewPort()->width; x++)
  162. {
  163. for (int y = 0; y < zKamera->zViewPort()->height; y++)
  164. {
  165. Vec3<float> wPoint
  166. = Vec3<float>(x / (0.5f * zKamera->zViewPort()->width) - 1,
  167. y / (0.5f * zKamera->zViewPort()->height) - 1,
  168. 0.1f);
  169. Vec3<float> wPoint2 = Vec3<float>(wPoint.x, wPoint.y, 1.f);
  170. wPoint.x = (wPoint.x * 0.1f) / mat.elements[0][0];
  171. wPoint.y = (wPoint.y * 0.1f) / mat.elements[1][1];
  172. wPoint2.x = wPoint2.x / mat.elements[0][0];
  173. wPoint2.y = wPoint2.y / mat.elements[1][1];
  174. wPoint = inv * wPoint;
  175. wPoint2 = inv * wPoint2;
  176. Vec3<float> wDir = wPoint2 - wPoint;
  177. uiBild->setPixelDP(x + (int)zKamera->zViewPort()->x,
  178. y + (int)zKamera->zViewPort()->y,
  179. welt->traceRay(wPoint, wDir));
  180. }
  181. }
  182. zm.messungEnde();
  183. std::cout << zm.getSekunden() << "\n";
  184. }
  185. void DirectX9::presentFrame()
  186. {
  187. if (!uiBild->getBuffer()) return;
  188. HRESULT result;
  189. result = pBackBuffer->LockRect(backRect, 0, 0);
  190. if (result != S_OK)
  191. {
  192. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  193. new Text("Fehler"),
  194. new Text("Es ist ein Fehler beim rendern aufgetreten."),
  195. MB_ICONERROR);
  196. update();
  197. }
  198. // kopieren zum Bildschrirm
  199. int* bgBuff = uiBild->getBuffer();
  200. int tmpBr = sizeof(D3DCOLOR) * uiBild->getBreite();
  201. for (int y = 0, pitch = 0, bry = 0; y < uiBild->getHeight();
  202. ++y, pitch += backRect->Pitch, bry += uiBild->getBreite())
  203. memcpy(&((BYTE*)backRect->pBits)[pitch], (void*)&(bgBuff[bry]), tmpBr);
  204. // Beende Bild
  205. result = pBackBuffer->UnlockRect();
  206. if (result != S_OK)
  207. {
  208. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  209. new Text("Fehler"),
  210. new Text("Es ist ein Fehler beim rendern aufgetreten."),
  211. MB_ICONERROR);
  212. update();
  213. }
  214. if (result != S_OK)
  215. {
  216. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  217. new Text("Fehler"),
  218. new Text("Es ist ein Fehler beim rendern aufgetreten."),
  219. MB_ICONERROR);
  220. update();
  221. }
  222. result = pDevice->Present(0, 0, 0, 0);
  223. if (result != S_OK)
  224. {
  225. WMessageBox(fenster ? fenster->getFensterHandle() : 0,
  226. new Text("Fehler"),
  227. new Text("Es ist ein Fehler beim rendern aufgetreten."),
  228. MB_ICONERROR);
  229. update();
  230. }
  231. }
  232. Textur* DirectX9::createOrGetTextur(const char* name, Bild* b)
  233. {
  234. Textur* ret = new DX9Textur();
  235. ret->setBildZ(b);
  236. return ret;
  237. }
  238. Bild* DirectX9::zUIRenderBild() const
  239. {
  240. return uiBild;
  241. }
  242. DXBuffer* DirectX9::createIndexBuffer()
  243. {
  244. throw "Unsupported Operation for DirectX9 Graphics API. Use DirectX11 or "
  245. "12";
  246. }
  247. DXBuffer* DirectX9::createVertexBuffer()
  248. {
  249. throw "Unsupported Operation for DirectX9 Graphics API. Use DirectX11 or "
  250. "12";
  251. }
  252. DXBuffer* DirectX9::createStructuredBuffer(int eSize)
  253. {
  254. throw "Unsupported Operation for DirectX9 Graphics API. Use DirectX11 or "
  255. "12";
  256. }