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.

255 lines
6.8 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: LgDirectDrawVideo.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgDirectDrawVideo.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgDirectDrawVideo.h"
  16. CLgDirectDrawVideo::CLgDirectDrawVideo()
  17. {
  18. m_pSurface = NULL;
  19. memset( m_pText, 0, sizeof(m_pText) );
  20. m_hFont = NULL;
  21. m_cColor = 0;
  22. }
  23. CLgDirectDrawVideo::~CLgDirectDrawVideo()
  24. {
  25. OnDestroy();
  26. }
  27. void CLgDirectDrawVideo::OnDestroy()
  28. {
  29. __try
  30. {
  31. // delete old surface
  32. if( m_pSurface )
  33. {
  34. delete m_pSurface;
  35. m_pSurface = NULL;
  36. }
  37. // destroy display objects
  38. m_Display.DestroyObjects();
  39. }
  40. __except( EXCEPTION_EXECUTE_HANDLER )
  41. {
  42. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnDestroy"), _T("Exception 0x%08X"), _exception_code() );
  43. }
  44. }
  45. BOOL CLgDirectDrawVideo::OnSize( HWND hWnd )
  46. {
  47. __try
  48. {
  49. // destroy old
  50. OnDestroy();
  51. // get window sizes
  52. RECT rect;
  53. GetClientRect( hWnd, &rect );
  54. int nWidth = rect.right - rect.left;
  55. int nHeight = rect.bottom - rect.top;
  56. // create direct draw
  57. HRESULT hr = m_Display.CreateWindowedDisplay( hWnd, nWidth, nHeight );
  58. if( FAILED( hr ) )
  59. {
  60. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDraw::OnSize") , _T("CreateWindowedDisplay failed with 0x%08X"), hr );
  61. return FALSE;
  62. }
  63. m_Display.Clear( 0 );
  64. // Create a video surface
  65. hr = m_Display.CreateSurface( &m_pSurface, nWidth, nHeight );
  66. if( FAILED( hr ) )
  67. {
  68. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnSize") , _T("CreateSurface failed with 0x%08X"), hr );
  69. return FALSE;
  70. }
  71. m_pSurface->Clear( 0 );
  72. }
  73. __except( EXCEPTION_EXECUTE_HANDLER )
  74. {
  75. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnSize"), _T("Exception 0x%08X"), _exception_code() );
  76. return FALSE;
  77. }
  78. return TRUE;
  79. }
  80. BOOL CLgDirectDrawVideo::OnPaint( BOOL bStretch )
  81. {
  82. __try
  83. {
  84. // Update the screen if we need to refresh. This case occurs
  85. // when in windowed mode and the window is behind others.
  86. // The app will not be active, but it will be visible.
  87. UpdateDirectDrawVideoDIB();
  88. // Display the new position of the sprite
  89. if( DisplayFrame( bStretch ) == DDERR_SURFACELOST )
  90. {
  91. // If the surfaces were lost, then restore and try again
  92. HRESULT hr = RestoreSurfaces();
  93. if( FAILED(hr) )
  94. return FALSE;
  95. DisplayFrame( bStretch );
  96. }
  97. }
  98. __except( EXCEPTION_EXECUTE_HANDLER )
  99. {
  100. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::OnPaint"), _T("Exception 0x%08X"), _exception_code() );
  101. return FALSE;
  102. }
  103. return TRUE;
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Name: DisplayFrame()
  107. // Desc: Blts a the sprites to the back buffer, then it blts or flips the
  108. // back buffer onto the primary buffer.
  109. //-----------------------------------------------------------------------------
  110. HRESULT CLgDirectDrawVideo::DisplayFrame( BOOL bStretch )
  111. {
  112. __try
  113. {
  114. HRESULT hr;
  115. // Fill the back buffer with black, ignoring errors until the flip
  116. // m_Display.Clear( 0 );
  117. m_Display.UpdateBounds();
  118. // Blt the help text on the backbuffer, ignoring errors until the flip
  119. if( bStretch )
  120. m_Display.Blt( m_pSurface );
  121. else
  122. {
  123. LPBITMAPINFOHEADER pBIH = GetDirectDrawVideoDIB();
  124. if( pBIH )
  125. {
  126. RECT wRect;
  127. ::GetClientRect( m_Display.GetHWnd(), &wRect );
  128. SIZE vSize = { pBIH->biWidth, pBIH->biHeight };
  129. SIZE wSize = { wRect.right - wRect.left, wRect.bottom - wRect.top };
  130. RECT vRect;
  131. vRect.left = wRect.left + ( wSize.cx - vSize.cx ) / 2;
  132. vRect.top = wRect.top + ( wSize.cy - vSize.cy ) / 2;
  133. vRect.right = vRect.left + vSize.cx;
  134. vRect.bottom = vRect.top + vSize.cy;
  135. RECT sRect;
  136. ::IntersectRect( &sRect, &vRect, &wRect );
  137. if( wRect.right - wRect.left > vSize.cx ||
  138. wRect.bottom - wRect.top > vSize.cy )
  139. m_Display.Blt( sRect.left, sRect.top, m_pSurface, NULL );
  140. else
  141. m_Display.Blt( sRect.left, sRect.top, m_pSurface, &sRect );
  142. }
  143. else
  144. {
  145. return S_OK;
  146. }
  147. }
  148. RECT rect;
  149. ::GetClientRect( m_Display.GetHWnd(), &rect );
  150. m_Display.BackBufferDrawText( m_hFont, m_pText, &rect, DT_SINGLELINE | DT_CENTER | DT_BOTTOM, m_cColor );
  151. // We are in windowed mode so perform a blt from the backbuffer
  152. // to the primary, returning any errors like DDERR_SURFACELOST
  153. if( FAILED( hr = m_Display.Present() ) )
  154. return hr;
  155. }
  156. __except( EXCEPTION_EXECUTE_HANDLER )
  157. {
  158. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::DisplayFrame"), _T("Exception 0x%08X"), _exception_code() );
  159. return 0x80000000;
  160. }
  161. return S_OK;
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Name: RestoreSurfaces()
  165. // Desc: Restore all the surfaces, and redraw the sprite surfaces.
  166. //-----------------------------------------------------------------------------
  167. HRESULT CLgDirectDrawVideo::RestoreSurfaces()
  168. {
  169. __try
  170. {
  171. HRESULT hr = m_Display.RestoreAllSurfaces();
  172. if( FAILED( hr ) )
  173. {
  174. OnSize( m_Display.GetHWnd() );
  175. }
  176. UpdateDirectDrawVideoDIB();
  177. }
  178. __except( EXCEPTION_EXECUTE_HANDLER )
  179. {
  180. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::RestoreSurfaces"), _T("Exception 0x%08X"), _exception_code() );
  181. }
  182. return S_OK;
  183. }
  184. void CLgDirectDrawVideo::UpdateDirectDrawVideoDIB()
  185. {
  186. __try
  187. {
  188. if( m_pSurface )
  189. {
  190. SIZE size = m_pSurface->GetSize();
  191. LPBITMAPINFOHEADER pBIH = GetDirectDrawVideoDIB();
  192. if( pBIH )
  193. {
  194. if( size.cx != pBIH->biWidth || size.cy != pBIH->biHeight )
  195. {
  196. delete m_pSurface;
  197. m_pSurface = NULL;
  198. // Create a video surface
  199. HRESULT hr = m_Display.CreateSurface( &m_pSurface, pBIH->biWidth, pBIH->biHeight );
  200. if( FAILED( hr ) || !m_pSurface )
  201. {
  202. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::UpdateDirectDrawVideoDIB") , _T("CreateSurface failed with 0x%08X"), hr );
  203. return;
  204. }
  205. }
  206. if( m_pSurface )
  207. m_pSurface->DrawBitmap( &GetDirectDrawVideoDIB() );
  208. }
  209. }
  210. }
  211. __except( EXCEPTION_EXECUTE_HANDLER )
  212. {
  213. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDrawVideo::UpdateDirectDrawVideoDIB"), _T("Exception 0x%08X"), _exception_code() );
  214. }
  215. }
  216. void CLgDirectDrawVideo::SetDirectDrawVideoText( LPCTSTR pText, HFONT hFont, COLORREF cColor )
  217. {
  218. _tcsncpy( m_pText, pText, MAX_PATH );
  219. m_hFont = hFont;
  220. m_cColor = cColor;
  221. }