W3DVideoBuffer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. //----------------------------------------------------------------------------
  24. //
  25. // Westwood Studios Pacific.
  26. //
  27. // Confidential Information
  28. // Copyright (C) 2001 - All Rights Reserved
  29. //
  30. //----------------------------------------------------------------------------
  31. //
  32. // Project: Generals
  33. //
  34. // Module: Video
  35. //
  36. // File name: W3DDevice/GameClient/W3DVideoBuffer.cpp
  37. //
  38. // Created: 10/23/01 TR
  39. //
  40. //----------------------------------------------------------------------------
  41. //----------------------------------------------------------------------------
  42. // Includes
  43. //----------------------------------------------------------------------------
  44. #include "Common/GameMemory.h"
  45. #include "WW3D2/texture.h"
  46. #include "WW3D2/textureloader.h"
  47. #include "W3DDevice/GameClient/W3DVideoBuffer.h"
  48. //----------------------------------------------------------------------------
  49. // Externals
  50. //----------------------------------------------------------------------------
  51. //----------------------------------------------------------------------------
  52. // Defines
  53. //----------------------------------------------------------------------------
  54. //----------------------------------------------------------------------------
  55. // Private Types
  56. //----------------------------------------------------------------------------
  57. //----------------------------------------------------------------------------
  58. // Private Data
  59. //----------------------------------------------------------------------------
  60. //----------------------------------------------------------------------------
  61. // Public Data
  62. //----------------------------------------------------------------------------
  63. //----------------------------------------------------------------------------
  64. // Private Prototypes
  65. //----------------------------------------------------------------------------
  66. //----------------------------------------------------------------------------
  67. // Private Functions
  68. //----------------------------------------------------------------------------
  69. //----------------------------------------------------------------------------
  70. // Public Functions
  71. //----------------------------------------------------------------------------
  72. //============================================================================
  73. // W3DVideoBuffer::W3DVideoBuffer
  74. //============================================================================
  75. W3DVideoBuffer::W3DVideoBuffer( VideoBuffer::Type format )
  76. : VideoBuffer(format),
  77. m_texture(NULL),
  78. m_surface(NULL)
  79. {
  80. }
  81. //============================================================================
  82. // W3DVideoBuffer::SetBuffer
  83. //============================================================================
  84. Bool W3DVideoBuffer::allocate( UnsignedInt width, UnsignedInt height )
  85. {
  86. free();
  87. m_width = width;
  88. m_height = height;
  89. m_textureWidth = width;;
  90. m_textureHeight = height;;
  91. unsigned int temp_depth=1;
  92. TextureLoader::Validate_Texture_Size( m_textureWidth, m_textureHeight, temp_depth);
  93. WW3DFormat w3dFormat = TypeToW3DFormat( m_format );
  94. if ( w3dFormat == WW3D_FORMAT_UNKNOWN )
  95. {
  96. return NULL;
  97. }
  98. m_texture = MSGNEW("TextureClass") TextureClass ( m_textureWidth, m_textureHeight, w3dFormat, MIP_LEVELS_1 );
  99. if ( m_texture == NULL )
  100. {
  101. return FALSE;
  102. }
  103. if ( lock() == NULL )
  104. {
  105. free();
  106. return FALSE;
  107. }
  108. unlock();
  109. return TRUE;
  110. }
  111. //============================================================================
  112. // W3DVideoBuffer::~W3DVideoBuffer
  113. //============================================================================
  114. W3DVideoBuffer::~W3DVideoBuffer()
  115. {
  116. free();
  117. }
  118. //============================================================================
  119. // W3DVideoBuffer::lock
  120. //============================================================================
  121. void* W3DVideoBuffer::lock( void )
  122. {
  123. void *mem = NULL;
  124. if ( m_surface != NULL )
  125. {
  126. unlock();
  127. }
  128. m_surface = m_texture->Get_Surface_Level();
  129. if ( m_surface )
  130. {
  131. mem = m_surface->Lock( (Int*) &m_pitch );
  132. }
  133. return mem;
  134. }
  135. //============================================================================
  136. // W3DVideoBuffer::unlock
  137. //============================================================================
  138. void W3DVideoBuffer::unlock( void )
  139. {
  140. if ( m_surface != NULL )
  141. {
  142. m_surface->Unlock();
  143. m_surface->Release_Ref();
  144. m_surface = NULL;
  145. }
  146. }
  147. //============================================================================
  148. // W3DVideoBuffer::valid
  149. //============================================================================
  150. Bool W3DVideoBuffer::valid( void )
  151. {
  152. return m_texture != NULL;
  153. }
  154. //============================================================================
  155. // W3DVideoBuffer::reset
  156. //============================================================================
  157. void W3DVideoBuffer::free( void )
  158. {
  159. unlock();
  160. if ( m_texture )
  161. {
  162. unlock();
  163. m_texture->Release_Ref();
  164. m_texture = NULL;
  165. }
  166. m_surface = NULL;
  167. VideoBuffer::free();
  168. }
  169. //============================================================================
  170. // W3DVideoBuffer::TypeToW3DFormat
  171. //============================================================================
  172. WW3DFormat W3DVideoBuffer::TypeToW3DFormat( VideoBuffer::Type format )
  173. {
  174. WW3DFormat w3dFormat = WW3D_FORMAT_UNKNOWN;
  175. switch ( format )
  176. {
  177. case TYPE_X8R8G8B8:
  178. w3dFormat = WW3D_FORMAT_X8R8G8B8;
  179. break;
  180. case TYPE_R8G8B8:
  181. w3dFormat = WW3D_FORMAT_R8G8B8;
  182. break;
  183. case TYPE_R5G6B5:
  184. w3dFormat = WW3D_FORMAT_R5G6B5;
  185. break;
  186. case TYPE_X1R5G5B5:
  187. w3dFormat = WW3D_FORMAT_X1R5G5B5;
  188. break;
  189. }
  190. return w3dFormat;
  191. }
  192. //============================================================================
  193. // W3DFormatToType
  194. //============================================================================
  195. VideoBuffer::Type W3DVideoBuffer::W3DFormatToType( WW3DFormat w3dFormat )
  196. {
  197. Type format = TYPE_UNKNOWN;
  198. switch ( w3dFormat )
  199. {
  200. case WW3D_FORMAT_X8R8G8B8:
  201. format = VideoBuffer::TYPE_X8R8G8B8;
  202. break;
  203. case WW3D_FORMAT_R8G8B8:
  204. format = VideoBuffer::TYPE_R8G8B8;
  205. break;
  206. case WW3D_FORMAT_R5G6B5:
  207. format = VideoBuffer::TYPE_R5G6B5;
  208. break;
  209. case WW3D_FORMAT_X1R5G5B5:
  210. format = VideoBuffer::TYPE_X1R5G5B5;
  211. break;
  212. }
  213. return format;
  214. }