W3DVideoBuffer.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. ** Command & Conquer Generals(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. TextureLoader::Validate_Texture_Size( m_textureWidth, m_textureHeight);
  92. WW3DFormat w3dFormat = TypeToW3DFormat( m_format );
  93. if ( w3dFormat == WW3D_FORMAT_UNKNOWN )
  94. {
  95. return NULL;
  96. }
  97. m_texture = MSGNEW("TextureClass") TextureClass ( m_textureWidth, m_textureHeight, w3dFormat, TextureClass::MIP_LEVELS_1 );
  98. if ( m_texture == NULL )
  99. {
  100. return FALSE;
  101. }
  102. if ( lock() == NULL )
  103. {
  104. free();
  105. return FALSE;
  106. }
  107. unlock();
  108. return TRUE;
  109. }
  110. //============================================================================
  111. // W3DVideoBuffer::~W3DVideoBuffer
  112. //============================================================================
  113. W3DVideoBuffer::~W3DVideoBuffer()
  114. {
  115. free();
  116. }
  117. //============================================================================
  118. // W3DVideoBuffer::lock
  119. //============================================================================
  120. void* W3DVideoBuffer::lock( void )
  121. {
  122. void *mem = NULL;
  123. if ( m_surface != NULL )
  124. {
  125. unlock();
  126. }
  127. m_surface = m_texture->Get_Surface_Level();
  128. if ( m_surface )
  129. {
  130. mem = m_surface->Lock( (Int*) &m_pitch );
  131. }
  132. return mem;
  133. }
  134. //============================================================================
  135. // W3DVideoBuffer::unlock
  136. //============================================================================
  137. void W3DVideoBuffer::unlock( void )
  138. {
  139. if ( m_surface != NULL )
  140. {
  141. m_surface->Unlock();
  142. m_surface->Release_Ref();
  143. m_surface = NULL;
  144. }
  145. }
  146. //============================================================================
  147. // W3DVideoBuffer::valid
  148. //============================================================================
  149. Bool W3DVideoBuffer::valid( void )
  150. {
  151. return m_texture != NULL;
  152. }
  153. //============================================================================
  154. // W3DVideoBuffer::reset
  155. //============================================================================
  156. void W3DVideoBuffer::free( void )
  157. {
  158. unlock();
  159. if ( m_texture )
  160. {
  161. unlock();
  162. m_texture->Release_Ref();
  163. m_texture = NULL;
  164. }
  165. m_surface = NULL;
  166. VideoBuffer::free();
  167. }
  168. //============================================================================
  169. // W3DVideoBuffer::TypeToW3DFormat
  170. //============================================================================
  171. WW3DFormat W3DVideoBuffer::TypeToW3DFormat( VideoBuffer::Type format )
  172. {
  173. WW3DFormat w3dFormat = WW3D_FORMAT_UNKNOWN;
  174. switch ( format )
  175. {
  176. case TYPE_X8R8G8B8:
  177. w3dFormat = WW3D_FORMAT_X8R8G8B8;
  178. break;
  179. case TYPE_R8G8B8:
  180. w3dFormat = WW3D_FORMAT_R8G8B8;
  181. break;
  182. case TYPE_R5G6B5:
  183. w3dFormat = WW3D_FORMAT_R5G6B5;
  184. break;
  185. case TYPE_X1R5G5B5:
  186. w3dFormat = WW3D_FORMAT_X1R5G5B5;
  187. break;
  188. }
  189. return w3dFormat;
  190. }
  191. //============================================================================
  192. // W3DFormatToType
  193. //============================================================================
  194. VideoBuffer::Type W3DVideoBuffer::W3DFormatToType( WW3DFormat w3dFormat )
  195. {
  196. Type format = TYPE_UNKNOWN;
  197. switch ( w3dFormat )
  198. {
  199. case WW3D_FORMAT_X8R8G8B8:
  200. format = VideoBuffer::TYPE_X8R8G8B8;
  201. break;
  202. case WW3D_FORMAT_R8G8B8:
  203. format = VideoBuffer::TYPE_R8G8B8;
  204. break;
  205. case WW3D_FORMAT_R5G6B5:
  206. format = VideoBuffer::TYPE_R5G6B5;
  207. break;
  208. case WW3D_FORMAT_X1R5G5B5:
  209. format = VideoBuffer::TYPE_X1R5G5B5;
  210. break;
  211. }
  212. return format;
  213. }