Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.1 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 04/03/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: DirectDrawWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DirectDrawWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. #include <DDraw.h>
  17. #include "DirectDrawWnd.h"
  18. CDirectDrawWnd::CDirectDrawWnd()
  19. {
  20. ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
  21. m_pDD = NULL;
  22. m_pFrontBuffer = NULL;
  23. m_pBackBuffer = NULL;
  24. }
  25. CDirectDrawWnd::~CDirectDrawWnd()
  26. {
  27. FreeDirectDraw();
  28. ::CoUninitialize();
  29. }
  30. BOOL CDirectDrawWnd::Create( HWND hWndParent, int x, int y, int nWidth, int nHeight )
  31. {
  32. if( !CWnd::Create( _T("DirectDrawWndClass"), "", WS_CHILD, x, y, nWidth, nHeight, hWndParent, NULL, NULL ) )
  33. return FALSE;
  34. return InitDirectDraw();
  35. }
  36. LRESULT CDirectDrawWnd::OnDestroy()
  37. {
  38. FreeDirectDraw();
  39. return 0;
  40. }
  41. BOOL CDirectDrawWnd::InitDirectDraw()
  42. {
  43. FreeDirectDraw();
  44. if( FAILED( ::DirectDrawCreateEx(NULL, (void**)&m_pDD, IID_IDirectDraw7, NULL ) ) )
  45. return FALSE;
  46. if( FAILED( m_pDD->SetCooperativeLevel( GetSafeHwnd(), DDSCL_NORMAL ) ) )
  47. return FALSE;
  48. // Create the primary surface
  49. DDSURFACEDESC2 ddsd;
  50. ZeroMemory( &ddsd, sizeof( ddsd ) );
  51. ddsd.dwSize = sizeof( ddsd );
  52. ddsd.dwFlags = DDSD_CAPS;
  53. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  54. if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pFrontBuffer, NULL ) ) )
  55. return FALSE;
  56. RECT rect;
  57. if( !::GetClientRect( GetSafeHwnd(), &rect ) )
  58. return FALSE;
  59. // Create the backbuffer surface
  60. ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  61. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
  62. ddsd.dwWidth = rect.right - rect.left;
  63. ddsd.dwHeight = rect.bottom - rect.top;
  64. if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pBackBuffer, NULL ) ) )
  65. return FALSE;
  66. LPDIRECTDRAWCLIPPER pClipper;
  67. if( FAILED( m_pDD->CreateClipper( 0, &pClipper, NULL ) ) )
  68. return FALSE;
  69. if( FAILED( pClipper->SetHWnd( 0, GetSafeHwnd() ) ) )
  70. {
  71. pClipper->Release();
  72. return FALSE;
  73. }
  74. if( FAILED( m_pFrontBuffer->SetClipper( pClipper ) ) )
  75. {
  76. pClipper->Release();
  77. return E_FAIL;
  78. }
  79. // Done with clipper
  80. pClipper->Release();
  81. return TRUE;
  82. }
  83. void CDirectDrawWnd::FreeDirectDraw()
  84. {
  85. if( m_pBackBuffer )
  86. {
  87. m_pBackBuffer->Release();
  88. m_pBackBuffer = NULL;
  89. }
  90. if( m_pFrontBuffer )
  91. {
  92. m_pFrontBuffer->Release();
  93. m_pFrontBuffer = NULL;
  94. }
  95. if( m_pDD )
  96. {
  97. m_pDD->Release();
  98. m_pDD = NULL;
  99. }
  100. }
  101. void CDirectDrawWnd::Draw()
  102. {
  103. HRESULT hr;
  104. // Check the cooperative level before rendering
  105. if( FAILED( hr = m_pDD->TestCooperativeLevel() ) )
  106. {
  107. switch( hr )
  108. {
  109. case DDERR_EXCLUSIVEMODEALREADYSET:
  110. // Do nothing because some other app has exclusive mode
  111. return;
  112. case DDERR_WRONGMODE:
  113. // The display mode changed on us. Update the
  114. // DirectDraw surfaces accordingly
  115. InitDirectDraw();
  116. return;
  117. }
  118. return;
  119. }
  120. OnDraw( m_pBackBuffer );
  121. RECT rect;
  122. if( !::GetClientRect( GetSafeHwnd(), &rect ) )
  123. return;
  124. while( 1 )
  125. {
  126. hr = m_pFrontBuffer->Blt( &rect, m_pBackBuffer, NULL, DDBLT_WAIT, NULL );
  127. if( hr == DDERR_SURFACELOST )
  128. {
  129. m_pFrontBuffer->Restore();
  130. m_pBackBuffer->Restore();
  131. }
  132. if( hr != DDERR_WASSTILLDRAWING )
  133. break;
  134. }
  135. if( FAILED( hr ) )
  136. {
  137. if( hr != DDERR_SURFACELOST )
  138. return;
  139. // The surfaces were lost so restore them
  140. m_pDD->RestoreAllSurfaces();
  141. }
  142. }
  143. void CDirectDrawWnd::OnDraw( LPDIRECTDRAWSURFACE7 pBackBuffer )
  144. {
  145. }