textdraw.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. ** Command & Conquer Renegade(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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/textdraw.cpp $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 3/22/01 8:03p $*
  29. * *
  30. * $Revision:: 7 $*
  31. * *
  32. *-------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "textdraw.h"
  36. #include "font3d.h"
  37. #include "simplevec.h"
  38. /***********************************************************************************************
  39. * *
  40. * TextDrawClass::TextDrawClass( int ) -- Constructor *
  41. * *
  42. * Creates a TextDrawClass object by creating and initializing a Dynamic Mesh, inserting it *
  43. * into the given scene, and allocating space for the given number of maximum chars. *
  44. * *
  45. ***********************************************************************************************/
  46. TextDrawClass::TextDrawClass( int max_chars ) :
  47. DynamicMeshClass( max_chars * 2, max_chars * 4 ),
  48. TextColor( 1.0f, 1.0f, 1.0f )
  49. {
  50. // Build the default Vertex Material
  51. DefaultVertexMaterial = NEW_REF( VertexMaterialClass, () );
  52. DefaultVertexMaterial->Set_Diffuse( 0, 0, 0 );
  53. DefaultVertexMaterial->Set_Opacity(1);
  54. DefaultVertexMaterial->Set_Emissive( 1, 1, 1 );
  55. Set_Vertex_Material( DefaultVertexMaterial );
  56. // Build the default Shader
  57. DefaultShader.Set_Depth_Mask( ShaderClass::DEPTH_WRITE_DISABLE );
  58. DefaultShader.Set_Depth_Compare( ShaderClass::PASS_ALWAYS );
  59. DefaultShader.Set_Dst_Blend_Func( ShaderClass::DSTBLEND_ONE_MINUS_SRC_ALPHA );
  60. DefaultShader.Set_Src_Blend_Func( ShaderClass::SRCBLEND_SRC_ALPHA );
  61. DefaultShader.Set_Texturing( ShaderClass::TEXTURING_ENABLE );
  62. DefaultShader.Set_Cull_Mode( ShaderClass::CULL_MODE_DISABLE );
  63. Set_Shader( DefaultShader );
  64. Enable_Sort();
  65. Set_Coordinate_Ranges( Vector2( 0,0 ), Vector2( 1,1 ), Vector2( -1,0.75f ), Vector2( 1,-0.75 ) );
  66. // Set_Coordinate_Ranges( Vector2( -320,240 ), Vector2( 320,-240 ), Vector2( -320,240 ), Vector2( 320,-240 ) );
  67. }
  68. /***********************************************************************************************
  69. * *
  70. * TextDrawClass::~TextDrawClass( void ) -- Destructor *
  71. * *
  72. ***********************************************************************************************/
  73. TextDrawClass::~TextDrawClass( void )
  74. {
  75. DefaultVertexMaterial->Release_Ref();
  76. }
  77. /***********************************************************************************************
  78. * *
  79. * TextDrawClass::Reset( void ) -- Flush the mesh
  80. * *
  81. ***********************************************************************************************/
  82. void TextDrawClass::Reset( void )
  83. {
  84. Reset_Flags();
  85. Reset_Mesh_Counters();
  86. // Reinstall the default vertex material and shader
  87. Enable_Sort();
  88. Set_Vertex_Material( DefaultVertexMaterial );
  89. Set_Shader( DefaultShader );
  90. }
  91. /*
  92. **
  93. */
  94. void TextDrawClass::Set_Coordinate_Ranges(
  95. const Vector2 & src_ul, const Vector2 & src_lr,
  96. const Vector2 & dest_ul, const Vector2 & dest_lr )
  97. {
  98. TranslateScale.X = (dest_lr.X - dest_ul.X) / (src_lr.X - src_ul.X);
  99. TranslateScale.Y = (dest_lr.Y - dest_ul.Y) / (src_lr.Y - src_ul.Y);
  100. TranslateOffset.X = dest_ul.X - TranslateScale.X * src_ul.X;
  101. TranslateOffset.Y = dest_ul.Y - TranslateScale.Y * src_ul.Y;
  102. PixelSize.X = fabs((src_lr.X - src_ul.X) / 640.0f);
  103. PixelSize.Y = fabs((src_lr.Y - src_ul.Y) / 480.0f);
  104. }
  105. /*
  106. **
  107. */
  108. void TextDrawClass::Quad( float x0, float y0, float x1, float y1, float u0, float v0, float u1, float v1 )
  109. {
  110. // Translate coordinates
  111. x0 = x0 * TranslateScale.X + TranslateOffset.X;
  112. x1 = x1 * TranslateScale.X + TranslateOffset.X;
  113. y0 = y0 * TranslateScale.Y + TranslateOffset.Y;
  114. y1 = y1 * TranslateScale.Y + TranslateOffset.Y;
  115. bool flip_faces = ((x1-x0) * (y1-y0)) > 0;
  116. Begin_Tri_Strip();
  117. Vertex( x0, y0, 0, u0, v0);
  118. if ( flip_faces ) {
  119. Vertex( x1, y0, 0, u1, v0);
  120. Vertex( x0, y1, 0, u0, v1);
  121. } else {
  122. Vertex( x0, y1, 0, u0, v1);
  123. Vertex( x1, y0, 0, u1, v0);
  124. }
  125. Vertex( x1, y1, 0, u1, v1);
  126. End_Tri_Strip();
  127. }
  128. void TextDrawClass::Quad( const RectClass & rect, const RectClass & uv )
  129. {
  130. TextDrawClass::Quad( rect.Left, rect.Top, rect.Right, rect.Bottom, uv.Left, uv.Top, uv.Right, uv.Bottom );
  131. }
  132. void TextDrawClass::Line( const Vector2 & _a, const Vector2 & _b, float width )
  133. {
  134. // Translate coordinates
  135. Vector2 a;
  136. Vector2 b;
  137. a.X = _a.X * TranslateScale.X + TranslateOffset.X;
  138. a.Y = _a.Y * TranslateScale.Y + TranslateOffset.Y;
  139. b.X = _b.X * TranslateScale.X + TranslateOffset.X;
  140. b.Y = _b.Y * TranslateScale.Y + TranslateOffset.Y;
  141. width *= WWMath::Fabs(TranslateScale.X);
  142. Vector2 corner_offset = a - b; // get line relative to b
  143. float temp = corner_offset.X; // Rotate 90
  144. corner_offset.X = corner_offset.Y;
  145. corner_offset.Y = -temp;
  146. corner_offset.Normalize(); // scale to length width/2
  147. corner_offset *= width / 2;
  148. Begin_Tri_Strip();
  149. Vertex( a + corner_offset );
  150. Vertex( a - corner_offset );
  151. Vertex( b + corner_offset );
  152. Vertex( b - corner_offset );
  153. End_Tri_Strip();
  154. }
  155. void TextDrawClass::Line_Ends( const Vector2 & a, const Vector2 & b, float width, float end_percent )
  156. {
  157. Vector2 a_ = b - a;
  158. a_ *= end_percent;
  159. a_ += a;
  160. Line( a, a_, width );
  161. Vector2 b_ = a - b;
  162. b_ *= end_percent;
  163. b_ += b;
  164. Line( b, b_, width );
  165. }
  166. /***********************************************************************************************
  167. * *
  168. * float TextDrawClass::Get_Width( Font3DInstanceClass *, char * ) *
  169. * *
  170. * WARNING: Should not be used to draw characters which need to wrap or have embedded line *
  171. * feeds. *
  172. * *
  173. * Returns the scaled string width in normalized screen unit *
  174. * *
  175. ***********************************************************************************************/
  176. float TextDrawClass::Get_Width( Font3DInstanceClass *font, const char *message )
  177. {
  178. float total_width = 0.0f;
  179. /*
  180. ** for each character, get_width it
  181. */
  182. while (*message != 0) {
  183. total_width += font->Char_Spacing( *message++ );
  184. }
  185. return total_width;
  186. }
  187. float TextDrawClass::Get_Inter_Char_Width( Font3DInstanceClass *font )
  188. {
  189. // Get rid of this...
  190. // return font->Get_Inter_Char_Spacing();
  191. return 1;
  192. }
  193. float TextDrawClass::Get_Height( Font3DInstanceClass *font, const char *message )
  194. {
  195. return font->Char_Height();
  196. }
  197. /***********************************************************************************************
  198. * *
  199. * float TextDrawClass::Print( Font3DInstanceClass *, char, float, float, float ) *
  200. * *
  201. * Draws (actually creates two trianlges to display) a character on the screen at the given *
  202. * normalized screen unit coordinates at the current font scale. *
  203. * *
  204. * Returns the scaled character width in normalized screen unit *
  205. * *
  206. ***********************************************************************************************/
  207. float TextDrawClass::Print( Font3DInstanceClass *font, char ch, float screen_x, float screen_y )
  208. {
  209. /*
  210. ** Get the char width in scaled normalized screen units
  211. */
  212. float width = font->Char_Width( ch ); // in scaled normalized screen units
  213. float spacing = font->Char_Spacing( ch ); // in scaled normalized screen units
  214. /*
  215. ** If asked to draw the space char (' '), don't add any triangles, just return the scaled spacing
  216. */
  217. if (ch == ' ' )
  218. return spacing;
  219. /*
  220. ** Calculate the lower right edge of the displayed rectangle
  221. */
  222. // center each char in its spacing (in case mono spaced )
  223. // also, round to the nearest 640x480 pixels (needs to change for other reses)
  224. float screen_x0 = screen_x + spacing/2 - width/2;
  225. screen_x0 = floor(screen_x0 / PixelSize.X + 0.5f) * PixelSize.X;
  226. float screen_x1 = screen_x0 + width;
  227. screen_x1 = floor(screen_x1 / PixelSize.X + 0.5f) * PixelSize.X;
  228. float screen_y0 = screen_y;
  229. screen_y0 = floor(screen_y0 / PixelSize.Y + 0.5f) * PixelSize.Y;
  230. float screen_y1 = screen_y0 + (font->Char_Height() * WWMath::Sign( -TranslateScale.Y ));
  231. screen_y1 = floor(screen_y1 / PixelSize.Y + 0.5f) * PixelSize.Y;
  232. if ( WW3D::Is_Screen_UV_Biased() ) { // Global bais setting
  233. screen_x0 += PixelSize.X / 2;
  234. screen_x1 += PixelSize.X / 2;
  235. screen_y0 += PixelSize.Y / 2;
  236. screen_y1 += PixelSize.Y / 2;
  237. }
  238. /*
  239. ** Get the font texture uv coordinate for teh upper right and lower left corners of the rect
  240. */
  241. RectClass font_uv = font->Char_UV( ch );
  242. /*
  243. ** Set the triangles' texture
  244. */
  245. Set_Texture( font->Peek_Texture( ch ) );
  246. /*
  247. ** Draw the quad
  248. */
  249. Quad( screen_x0, screen_y0, screen_x1, screen_y1, font_uv.Left, font_uv.Top, font_uv.Right, font_uv.Bottom );
  250. /*
  251. ** return the scaled spacing
  252. */
  253. return spacing;
  254. }
  255. /***********************************************************************************************
  256. * *
  257. * float TextDrawClass::Print( Font3DInstanceClass *, char *, float, float, float ) *
  258. * *
  259. * Draws the given string at the given pixel coordinates. Uses the given font and its current *
  260. * scale. Passes each character to the above routine and moves the x-coordinate forward after *
  261. * each char. * *
  262. * *
  263. * WARNING: Should not be used to draw characters which need to wrap or have embedded line *
  264. * feeds. * *
  265. * *
  266. * Returns the string pixel width *
  267. * *
  268. ***********************************************************************************************/
  269. float TextDrawClass::Print( Font3DInstanceClass *font, const char *message, float screen_x, float screen_y )
  270. {
  271. /*
  272. ** Keep track of the total drawn width
  273. */
  274. float total_width = 0.0f;
  275. /*
  276. ** for each character, print it and moved screen_x forward
  277. */
  278. while (*message != 0) {
  279. float width = Print( font, *message++, screen_x, screen_y );
  280. screen_x += width;
  281. total_width += width;
  282. }
  283. /*
  284. ** return the total drawn width
  285. */
  286. return total_width;
  287. }
  288. /***********************************************************************************************
  289. * *
  290. * void TextDrawClass::Show_Font( Font3DInstanceClass *, float, float, float ) *
  291. * *
  292. * Dumps the font texture to the screen as two triangles. For debugging only. *
  293. * *
  294. ***********************************************************************************************/
  295. void TextDrawClass::Show_Font( Font3DInstanceClass *font, float screen_x, float screen_y )
  296. {
  297. // normalize coordinates
  298. float size_x = PixelSize.X * 256;
  299. float size_y = PixelSize.Y * 256;
  300. Set_Texture( font->Peek_Texture('A') );
  301. Quad( screen_x, screen_y, screen_x + size_x, screen_y + size_y * WWMath::Sign( -TranslateScale.Y ) );
  302. }