DX9GraphicsApi.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "GraphicsApi.h"
  2. #include "Bild.h"
  3. #include "Globals.h"
  4. #include "DLLRegister.h"
  5. #include "Fenster.h"
  6. #include "Kam3D.h"
  7. #include "Welt3D.h"
  8. #include "Textur.h"
  9. #include "Zeit.h"
  10. #include <d3d9.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( WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen )
  45. {
  46. if( pDirect3D )
  47. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  48. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  49. HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d9.dll", "d3d9.dll" );
  50. if( !dll )
  51. {
  52. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht gefunden werden." ), MB_ICONERROR );
  53. return;
  54. }
  55. D3D9CreateFunction direct3DCreate9 = (D3D9CreateFunction)GetProcAddress( dll, "Direct3DCreate9" );
  56. if( !direct3DCreate9 )
  57. {
  58. getDLLRegister()->releaseDLL( "d3d9.dll" );
  59. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "Der Einstiegspunkt Direct3DCreate9 fon DirectX 9 konnte nicht gefunden werden." ), MB_ICONERROR );
  60. return;
  61. }
  62. pDirect3D = direct3DCreate9( D3D_SDK_VERSION );
  63. D3DPRESENT_PARAMETERS d3dpp;
  64. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  65. d3dpp.Windowed = !fullScreen;
  66. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  67. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  68. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  69. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  70. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  71. d3dpp.BackBufferHeight = this->backBufferSize.y;
  72. d3dpp.BackBufferWidth = this->backBufferSize.x;
  73. uiBild->neuBild( this->backBufferSize.x, this->backBufferSize.y, 0xFF000000 );
  74. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  75. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  76. if( result != S_OK )
  77. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  78. if( pDevice )
  79. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  80. if( result != S_OK )
  81. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  82. }
  83. void DirectX9::update()
  84. {
  85. if( !pDirect3D )
  86. return;
  87. backRect->pBits = NULL;
  88. if( pBackBuffer )
  89. {
  90. pBackBuffer->Release();
  91. pBackBuffer = NULL;
  92. }
  93. if( pDevice )
  94. {
  95. pDevice->Release();
  96. pDevice = NULL;
  97. }
  98. D3DPRESENT_PARAMETERS d3dpp;
  99. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  100. d3dpp.Windowed = !fullScreen;
  101. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  102. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  103. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  104. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  105. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  106. if( !backBufferSize.x || !backBufferSize.y )
  107. backBufferSize = fenster->getKörperGröße();
  108. d3dpp.BackBufferHeight = backBufferSize.y;
  109. d3dpp.BackBufferWidth = backBufferSize.x;
  110. uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 );
  111. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  112. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  113. if( result != S_OK )
  114. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  115. if( pDevice )
  116. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  117. if( result != S_OK )
  118. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  119. }
  120. void DirectX9::beginFrame( bool fill2D, bool fill3D, int fillColor )
  121. {
  122. if( fill2D )
  123. uiBild->setFarbe( fillColor );
  124. }
  125. void DirectX9::renderKamera( Kam3D* zKamera )
  126. {
  127. Mat4< float > mat = zKamera->getProjectionMatrix();
  128. Mat4< float > inv = zKamera->getViewMatrix().getInverse();
  129. Welt3D* welt = zKamera->zWelt();
  130. ZeitMesser zm = ZeitMesser();
  131. zm.messungStart();
  132. for( int x = 0; x < zKamera->zViewPort()->width; x++ )
  133. {
  134. for( int y = 0; y < zKamera->zViewPort()->height; y++ )
  135. {
  136. Vec3< float > wPoint = Vec3< float >( x / (0.5f * zKamera->zViewPort()->width) - 1, y / (0.5f * zKamera->zViewPort()->height) - 1, 0.1f );
  137. Vec3< float > wPoint2 = Vec3< float >( wPoint.x, wPoint.y, 1.f );
  138. wPoint.x = (wPoint.x * 0.1f) / mat.elements[ 0 ][ 0 ];
  139. wPoint.y = (wPoint.y * 0.1f) / mat.elements[ 1 ][ 1 ];
  140. wPoint2.x = wPoint2.x / mat.elements[ 0 ][ 0 ];
  141. wPoint2.y = wPoint2.y / mat.elements[ 1 ][ 1 ];
  142. wPoint = inv * wPoint;
  143. wPoint2 = inv * wPoint2;
  144. Vec3< float > wDir = wPoint2 - wPoint;
  145. uiBild->setPixelDP( x + (int)zKamera->zViewPort()->x, y + (int)zKamera->zViewPort()->y, welt->traceRay( wPoint, wDir ) );
  146. }
  147. }
  148. zm.messungEnde();
  149. std::cout << zm.getSekunden() << "\n";
  150. }
  151. void DirectX9::presentFrame()
  152. {
  153. if( !uiBild->getBuffer() )
  154. return;
  155. HRESULT result;
  156. result = pBackBuffer->LockRect( backRect, 0, 0 );
  157. if( result != S_OK )
  158. {
  159. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  160. update();
  161. }
  162. // kopieren zum Bildschrirm
  163. int* bgBuff = uiBild->getBuffer();
  164. int tmpBr = sizeof( D3DCOLOR ) * uiBild->getBreite();
  165. for( int y = 0, pitch = 0, bry = 0; y < uiBild->getHeight(); ++y, pitch += backRect->Pitch, bry += uiBild->getBreite() )
  166. memcpy( &((BYTE*)backRect->pBits)[ pitch ], (void*)&(bgBuff[ bry ]), tmpBr );
  167. // Beende Bild
  168. result = pBackBuffer->UnlockRect();
  169. if( result != S_OK )
  170. {
  171. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  172. update();
  173. }
  174. if( result != S_OK )
  175. {
  176. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  177. update();
  178. }
  179. result = pDevice->Present( 0, 0, 0, 0 );
  180. if( result != S_OK )
  181. {
  182. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  183. update();
  184. }
  185. }
  186. Textur* DirectX9::createOrGetTextur( const char* name, Bild* b )
  187. {
  188. Textur* ret = new DX9Textur();
  189. ret->setBildZ( b );
  190. return ret;
  191. }
  192. Bild* DirectX9::zUIRenderBild() const
  193. {
  194. return uiBild;
  195. }
  196. DXBuffer* DirectX9::createIndexBuffer()
  197. {
  198. throw "Unsupported Operation for DirectX9 Graphics API. Use DirectX11 or 12";
  199. }
  200. DXBuffer* DirectX9::createVertexBuffer()
  201. {
  202. throw "Unsupported Operation for DirectX9 Graphics API. Use DirectX11 or 12";
  203. }