DX9GraphicsApi.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "GraphicsApi.h"
  2. #include "Bild.h"
  3. #include "Globals.h"
  4. #include "DLLRegister.h"
  5. #include "Fenster.h"
  6. #include <d3d9.h>
  7. using namespace Framework;
  8. DirectX9::DirectX9()
  9. : GraphicsApi( DIRECTX9 ),
  10. pDirect3D( 0 ),
  11. pDevice( 0 ),
  12. pBackBuffer( 0 ),
  13. backRect( new D3DLOCKED_RECT() )
  14. {
  15. uiBild = new Bild( 1 );
  16. }
  17. DirectX9::~DirectX9()
  18. {
  19. backRect->pBits = NULL;
  20. delete backRect;
  21. if( pBackBuffer )
  22. {
  23. pBackBuffer->Release();
  24. pBackBuffer = NULL;
  25. }
  26. if( pDevice )
  27. {
  28. pDevice->Release();
  29. pDevice = NULL;
  30. }
  31. if( pDirect3D )
  32. {
  33. pDirect3D->Release();
  34. pDirect3D = NULL;
  35. getDLLRegister()->releaseDLL( "d3d9.dll" );
  36. }
  37. uiBild->release();
  38. }
  39. typedef IDirect3D9 *( __stdcall *D3D9CreateFunction )( UINT );
  40. void DirectX9::initialize( WFenster *fenster, Vec2<int> backBufferSize, bool fullScreen )
  41. {
  42. if( pDirect3D )
  43. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  44. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  45. HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d9.dll", "d3d9.dll" );
  46. if( !dll )
  47. {
  48. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht gefunden werden." ), MB_ICONERROR );
  49. return;
  50. }
  51. D3D9CreateFunction direct3DCreate9 = (D3D9CreateFunction)GetProcAddress( dll, "Direct3DCreate9" );
  52. if( !direct3DCreate9 )
  53. {
  54. getDLLRegister()->releaseDLL( "d3d9.dll" );
  55. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "Der Einstiegspunkt Direct3DCreate9 fon DirectX 9 konnte nicht gefunden werden." ), MB_ICONERROR );
  56. return;
  57. }
  58. pDirect3D = direct3DCreate9( D3D_SDK_VERSION );
  59. D3DPRESENT_PARAMETERS d3dpp;
  60. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  61. d3dpp.Windowed = !fullScreen;
  62. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  63. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  64. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  65. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  66. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  67. d3dpp.BackBufferHeight = this->backBufferSize.y;
  68. d3dpp.BackBufferWidth = this->backBufferSize.x;
  69. uiBild->neuBild( this->backBufferSize.x, this->backBufferSize.y, 0xFF000000 );
  70. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  71. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  72. if( result != S_OK )
  73. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  74. if( pDevice )
  75. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  76. if( result != S_OK )
  77. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  78. }
  79. void DirectX9::update()
  80. {
  81. if( !pDirect3D )
  82. return;
  83. backRect->pBits = NULL;
  84. if( pBackBuffer )
  85. {
  86. pBackBuffer->Release();
  87. pBackBuffer = NULL;
  88. }
  89. if( pDevice )
  90. {
  91. pDevice->Release();
  92. pDevice = NULL;
  93. }
  94. D3DPRESENT_PARAMETERS d3dpp;
  95. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  96. d3dpp.Windowed = !fullScreen;
  97. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  98. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  99. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  100. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  101. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  102. if( !backBufferSize.x || !backBufferSize.y )
  103. backBufferSize = fenster->getKörperGröße();
  104. d3dpp.BackBufferHeight = backBufferSize.y;
  105. d3dpp.BackBufferWidth = backBufferSize.x;
  106. uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 );
  107. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  108. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  109. if( result != S_OK )
  110. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  111. if( pDevice )
  112. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  113. if( result != S_OK )
  114. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  115. }
  116. void DirectX9::beginFrame( bool fill2D, bool fill3D, int fillColor )
  117. {
  118. if( fill2D )
  119. uiBild->setFarbe( fillColor );
  120. }
  121. void DirectX9::presentFrame()
  122. {
  123. if( !uiBild->getBuffer() )
  124. return;
  125. HRESULT result;
  126. result = pBackBuffer->LockRect( backRect, 0, 0 );
  127. if( result != S_OK )
  128. {
  129. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  130. update();
  131. }
  132. // kopieren zum Bildschrirm
  133. int *bgBuff = uiBild->getBuffer();
  134. int tmpBr = sizeof( D3DCOLOR ) * uiBild->getBreite();
  135. for( int y = 0, pitch = 0, bry = 0; y < uiBild->getHeight(); ++y, pitch += backRect->Pitch, bry += uiBild->getBreite() )
  136. memcpy( &( (BYTE *)backRect->pBits )[ pitch ], ( void * ) & ( bgBuff[ bry ] ), tmpBr );
  137. // Beende Bild
  138. result = pBackBuffer->UnlockRect();
  139. if( result != S_OK )
  140. {
  141. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  142. update();
  143. }
  144. if( result != S_OK )
  145. {
  146. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  147. update();
  148. }
  149. result = pDevice->Present( 0, 0, 0, 0 );
  150. if( result != S_OK )
  151. {
  152. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  153. update();
  154. }
  155. }
  156. Bild *DirectX9::zUIRenderBild() const
  157. {
  158. return uiBild;
  159. }