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.

226 lines
5.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: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: DDWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of DDWnd.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. //#include "chicago.h"
  16. #include <DDraw.h>
  17. #include "DDWnd.h"
  18. #include "..\..\LgLib\Version.h"
  19. BEGIN_MESSAGE_MAP(CDDWnd, CWnd)
  20. //{{AFX_MSG_MAP(CDDWnd)
  21. ON_WM_PAINT()
  22. ON_WM_SIZE()
  23. ON_WM_DEVMODECHANGE()
  24. //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26. CDDWnd::CDDWnd()
  27. {
  28. ::CoInitializeEx( NULL, COINIT_MULTITHREADED );
  29. m_pDD = NULL;
  30. m_pFrontBuffer = NULL;
  31. m_pBackBuffer = NULL;
  32. }
  33. CDDWnd::~CDDWnd()
  34. {
  35. FreeDirectDraw();
  36. ::CoUninitialize();
  37. }
  38. BOOL CDDWnd::Create( CWnd *pParentWnd, int x, int y, int nWidth, int nHeight )
  39. {
  40. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_ARROW ), NULL, NULL );
  41. CRect rect( x, y, x + nWidth, y + nHeight );
  42. if( !CWnd::Create( strClassName, "", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS, rect, pParentWnd, 0, NULL ) )
  43. return FALSE;
  44. return InitDirectDraw();
  45. }
  46. BOOL CDDWnd::InitDirectDraw()
  47. {
  48. FreeDirectDraw();
  49. if( FAILED( ::DirectDrawCreateEx(NULL, (void**)&m_pDD, IID_IDirectDraw7, NULL ) ) )
  50. return FALSE;
  51. if( FAILED( m_pDD->SetCooperativeLevel( GetSafeHwnd(), DDSCL_NORMAL ) ) )
  52. return FALSE;
  53. // Create the primary surface
  54. DDSURFACEDESC2 ddsd;
  55. ZeroMemory( &ddsd, sizeof( ddsd ) );
  56. ddsd.dwSize = sizeof( ddsd );
  57. ddsd.dwFlags = DDSD_CAPS;
  58. ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  59. if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pFrontBuffer, NULL ) ) )
  60. return FALSE;
  61. RECT rect;
  62. if( !::GetClientRect( GetSafeHwnd(), &rect ) )
  63. return FALSE;
  64. // Create the backbuffer surface
  65. ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
  66. ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
  67. ddsd.dwWidth = rect.right - rect.left;
  68. ddsd.dwHeight = rect.bottom - rect.top;
  69. if( FAILED( m_pDD->CreateSurface( &ddsd, &m_pBackBuffer, NULL ) ) )
  70. return FALSE;
  71. LPDIRECTDRAWCLIPPER pClipper;
  72. if( FAILED( m_pDD->CreateClipper( 0, &pClipper, NULL ) ) )
  73. return FALSE;
  74. if( FAILED( pClipper->SetHWnd( 0, GetSafeHwnd() ) ) )
  75. {
  76. pClipper->Release();
  77. return FALSE;
  78. }
  79. if( FAILED( m_pFrontBuffer->SetClipper( pClipper ) ) )
  80. {
  81. pClipper->Release();
  82. return E_FAIL;
  83. }
  84. // Done with clipper
  85. pClipper->Release();
  86. return TRUE;
  87. }
  88. void CDDWnd::FreeDirectDraw()
  89. {
  90. if( m_pBackBuffer )
  91. {
  92. m_pBackBuffer->Release();
  93. m_pBackBuffer = NULL;
  94. }
  95. if( m_pFrontBuffer )
  96. {
  97. m_pFrontBuffer->Release();
  98. m_pFrontBuffer = NULL;
  99. }
  100. if( m_pDD )
  101. {
  102. m_pDD->Release();
  103. m_pDD = NULL;
  104. }
  105. }
  106. void CDDWnd::Draw()
  107. {
  108. if( !m_pDD || !m_pBackBuffer || !m_pFrontBuffer )
  109. return;
  110. HRESULT hr;
  111. // Check the cooperative level before rendering
  112. if( FAILED( hr = m_pDD->TestCooperativeLevel() ) )
  113. {
  114. switch( hr )
  115. {
  116. case DDERR_EXCLUSIVEMODEALREADYSET:
  117. // Do nothing because some other app has exclusive mode
  118. return;
  119. case DDERR_WRONGMODE:
  120. // The display mode changed on us. Update the
  121. // DirectDraw surfaces accordingly
  122. InitDirectDraw();
  123. return;
  124. }
  125. return;
  126. }
  127. OnDraw( m_pBackBuffer );
  128. RECT rect;
  129. if( !::GetClientRect( GetSafeHwnd(), &rect ) )
  130. return;
  131. ::MapWindowPoints( GetSafeHwnd(), HWND_DESKTOP, (LPPOINT)&rect, 2 );
  132. while( 1 )
  133. {
  134. hr = m_pFrontBuffer->Blt( &rect, m_pBackBuffer, NULL, DDBLT_WAIT, NULL );
  135. if( hr == DDERR_SURFACELOST )
  136. {
  137. m_pFrontBuffer->Restore();
  138. m_pBackBuffer->Restore();
  139. }
  140. if( hr != DDERR_WASSTILLDRAWING )
  141. break;
  142. }
  143. if( FAILED( hr ) )
  144. {
  145. if( hr != DDERR_SURFACELOST )
  146. return;
  147. // The surfaces were lost so restore them
  148. m_pDD->RestoreAllSurfaces();
  149. }
  150. }
  151. void CDDWnd::OnDraw( LPDIRECTDRAWSURFACE7 pBackBuffer )
  152. {
  153. // Erase the background
  154. DDBLTFX ddbltfx;
  155. ::ZeroMemory( &ddbltfx, sizeof(ddbltfx) );
  156. ddbltfx.dwSize = sizeof(ddbltfx);
  157. ddbltfx.dwFillColor = 0;
  158. pBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx );
  159. }
  160. BOOL CDDWnd::DestroyWindow()
  161. {
  162. FreeDirectDraw();
  163. return CWnd::DestroyWindow();
  164. }
  165. void CDDWnd::OnPaint()
  166. {
  167. CPaintDC dc(this);
  168. Draw();
  169. }
  170. void CDDWnd::OnSize(UINT nType, int cx, int cy)
  171. {
  172. CWnd::OnSize( nType, cx, cy );
  173. InitDirectDraw();
  174. }
  175. void CDDWnd::OnDevModeChange(LPTSTR lpDeviceName)
  176. {
  177. CWnd::OnDevModeChange(lpDeviceName);
  178. InitDirectDraw();
  179. }