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.

250 lines
7.3 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: LgDirectDraw.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgDirectDraw.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgDirectDraw.h"
  16. //#include "Resource.h"
  17. CLgDirectDraw::CLgDirectDraw()
  18. {
  19. m_pTextSurface = NULL;
  20. m_bActive = FALSE;
  21. }
  22. CLgDirectDraw::~CLgDirectDraw()
  23. {
  24. FreeDirectDraw();
  25. }
  26. //-----------------------------------------------------------------------------
  27. // Name: InitDirectDraw()
  28. // Desc: Create the DirectDraw object, and init the surfaces
  29. //-----------------------------------------------------------------------------
  30. HRESULT CLgDirectDraw::InitDirectDraw( HWND hWnd )
  31. {
  32. LPDIRECTDRAWPALETTE pDDPal = NULL;
  33. HRESULT hr;
  34. RECT rect;
  35. GetClientRect( hWnd, &rect );
  36. int WINDOW_WIDTH = rect.right-rect.left;
  37. int WINDOW_HEIGHT = rect.bottom-rect.top;
  38. if( FAILED( hr = m_Display.CreateWindowedDisplay( hWnd, WINDOW_WIDTH, WINDOW_HEIGHT ) ) )
  39. {
  40. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgDirectDraw::InitDirectDraw") , _T("Failed initializing DirectDraw") );
  41. return hr;
  42. }
  43. if( FAILED( hr = m_Display.CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( 0 ) ) ) )
  44. return hr;
  45. m_Display.SetPalette( pDDPal );
  46. SAFE_RELEASE( pDDPal );
  47. // Create a surface, and draw text to it.
  48. if( FAILED( hr = m_Display.CreateSurfaceFromText( &m_pTextSurface, NULL, HELPTEXT, RGB(0,0,0), RGB(255, 255, 0) ) ) )
  49. return hr;
  50. return S_OK;
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Name: FreeDirectDraw()
  54. // Desc: Release all the DirectDraw objects
  55. //-----------------------------------------------------------------------------
  56. VOID CLgDirectDraw::FreeDirectDraw()
  57. {
  58. SAFE_DELETE( m_pTextSurface );
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Name: ProcessNextFrame()
  62. // Desc: Move the sprites, blt them to the back buffer, then
  63. // flip or blt the back buffer to the primary buffer
  64. //-----------------------------------------------------------------------------
  65. HRESULT CLgDirectDraw::ProcessNextFrame( HWND hWnd )
  66. {
  67. HRESULT hr;
  68. // Check the cooperative level before rendering
  69. if( FAILED( hr = m_Display.GetDirectDraw()->TestCooperativeLevel() ) )
  70. {
  71. switch( hr )
  72. {
  73. case DDERR_EXCLUSIVEMODEALREADYSET:
  74. // Do nothing because some other app has exclusive mode
  75. Sleep(10);
  76. return S_OK;
  77. case DDERR_WRONGMODE:
  78. // The display mode changed on us. Update the
  79. // DirectDraw surfaces accordingly
  80. FreeDirectDraw();
  81. return InitDirectDraw( hWnd );
  82. }
  83. return hr;
  84. }
  85. // Display the sprites on the screen
  86. if( FAILED( hr = DisplayFrame() ) )
  87. {
  88. if( hr != DDERR_SURFACELOST )
  89. return hr;
  90. // The surfaces were lost so restore them
  91. RestoreSurfaces();
  92. }
  93. return S_OK;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Name: DisplayFrame()
  97. // Desc: Blts a the sprites to the back buffer, then it blts or flips the
  98. // back buffer onto the primary buffer.
  99. //-----------------------------------------------------------------------------
  100. HRESULT CLgDirectDraw::DisplayFrame()
  101. {
  102. HRESULT hr;
  103. // Fill the back buffer with black, ignoring errors until the flip
  104. // m_Display.Clear( 0 );
  105. // Blt the help text on the backbuffer, ignoring errors until the flip
  106. m_Display.Blt( 10, 10, m_pTextSurface, NULL );
  107. // We are in windowed mode so perform a blt from the backbuffer
  108. // to the primary, returning any errors like DDERR_SURFACELOST
  109. if( FAILED( hr = m_Display.Present() ) )
  110. return hr;
  111. return S_OK;
  112. }
  113. //-----------------------------------------------------------------------------
  114. // Name: RestoreSurfaces()
  115. // Desc: Restore all the surfaces, and redraw the sprite surfaces.
  116. //-----------------------------------------------------------------------------
  117. HRESULT CLgDirectDraw::RestoreSurfaces()
  118. {
  119. LPDIRECTDRAWPALETTE pDDPal = NULL;
  120. HRESULT hr;
  121. if( FAILED( hr = m_Display.GetDirectDraw()->RestoreAllSurfaces() ) )
  122. return hr;
  123. // No need to re-create the surface, just re-draw it.
  124. if( FAILED( hr = m_pTextSurface->DrawText( NULL, HELPTEXT,
  125. 0, 0, RGB(0,0,0), RGB(255, 255, 0), FALSE ) ) )
  126. return hr;
  127. // We need to release and re-load, and set the palette again to
  128. // redraw the bitmap on the surface. Otherwise, GDI will not
  129. // draw the bitmap on the surface with the right palette
  130. if( FAILED( hr = m_Display.CreatePaletteFromBitmap( &pDDPal, MAKEINTRESOURCE( 0 ) ) ) )
  131. return hr;
  132. m_Display.SetPalette( pDDPal );
  133. SAFE_RELEASE( pDDPal );
  134. return S_OK;
  135. }
  136. void CLgDirectDraw::OnWmMove()
  137. {
  138. m_Display.UpdateBounds();
  139. }
  140. void CLgDirectDraw::OnWmPaint()
  141. {
  142. // Update the screen if we need to refresh. This case occurs
  143. // when in windowed mode and the window is behind others.
  144. // The app will not be active, but it will be visible.
  145. // Display the new position of the sprite
  146. if( DisplayFrame() == DDERR_SURFACELOST )
  147. {
  148. // If the surfaces were lost, then restore and try again
  149. RestoreSurfaces();
  150. DisplayFrame();
  151. }
  152. }
  153. void CLgDirectDraw::OnWmQueryNewPalette()
  154. {
  155. // If we are in windowed mode with a desktop resolution in 8 bit
  156. // color, then the palette we created during init has changed
  157. // since then. So get the palette back from the primary
  158. // DirectDraw surface, and set it again so that DirectDraw
  159. // realises the palette, then release it again.
  160. LPDIRECTDRAWPALETTE pDDPal = NULL;
  161. m_Display.GetFrontBuffer()->GetPalette( &pDDPal );
  162. m_Display.GetFrontBuffer()->SetPalette( pDDPal );
  163. SAFE_RELEASE( pDDPal );
  164. }
  165. void CLgDirectDraw::OnWmDestroy()
  166. {
  167. // Cleanup and close the app
  168. FreeDirectDraw();
  169. }
  170. void CLgDirectDraw::OnWmSize( HWND hWnd, WPARAM wParam )
  171. {
  172. // Check to see if we are losing our window...
  173. if( SIZE_MAXHIDE==wParam || SIZE_MINIMIZED==wParam )
  174. m_bActive = FALSE;
  175. else
  176. m_bActive = TRUE;
  177. FreeDirectDraw();
  178. InitDirectDraw( hWnd );
  179. m_Display.UpdateBounds();
  180. }
  181. BOOL CLgDirectDraw::OnIdle( HWND hWnd )
  182. {
  183. static DWORD dwTime = 0;
  184. if( m_bActive )
  185. {
  186. // Move the sprites, blt them to the back buffer, then
  187. // flip or blt the back buffer to the primary buffer
  188. DWORD dwCurTime = GetTickCount();
  189. if( dwCurTime - dwTime >= 10 )
  190. {
  191. dwTime = dwCurTime;
  192. if( FAILED( ProcessNextFrame( hWnd ) ) )
  193. {
  194. return FALSE;
  195. }
  196. }
  197. else
  198. Sleep(1);
  199. }
  200. else
  201. {
  202. // Make sure we go to sleep if we have nothing else to do
  203. WaitMessage();
  204. }
  205. return TRUE;
  206. }