render2d.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 : WW3D *
  23. * *
  24. * $Archive:: /Commando/Code/ww3d2/render2d.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 12/17/01 11:47a $*
  29. * *
  30. * $Revision:: 26 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef RENDER2D_H
  39. #define RENDER2D_H
  40. #include "always.h"
  41. //#include "simplevec.h"
  42. #include "vector.h"
  43. #include "vector2.h"
  44. #include "shader.h"
  45. #include "widestring.h"
  46. #include "rect.h"
  47. #include "bittype.h"
  48. class Font3DInstanceClass;
  49. class TextureClass;
  50. class Vector3;
  51. class Vector4;
  52. /*
  53. ** Macros
  54. */
  55. //
  56. // Vector RGB to INT32 methods (normalized components)
  57. //
  58. #define VRGB_TO_INT32(rgb) (unsigned(rgb[0]*255.0f)<<16)|(unsigned(rgb[1]*255.0f)<<8)|(unsigned(rgb[2]*255.0f))|0xFF000000
  59. #define VRGBA_TO_INT32(rgb) (unsigned(rgb[3]*255.0f)<<24)|(unsigned(rgb[0]*255.0f)<<16)|(unsigned(rgb[1]*255.0f)<<8)|(unsigned(rgb[2]*255.0f))
  60. //
  61. // RGB to INT32 methods (each component is a value from 0 - 255)
  62. //
  63. #define RGB_TO_INT32(r,g,b) (unsigned(r)<<16)|(unsigned(g)<<8)|(unsigned(b))|0xFF000000
  64. #define RGBA_TO_INT32(r,g,b,a) (unsigned(a)<<24)|(unsigned(r)<<16)|(unsigned(g)<<8)|(unsigned(b))
  65. //
  66. // Float RGB to INT32 methods (each component is a float between 0.0 and 1.0)
  67. //
  68. #define FRGB_TO_INT32(r,g,b) (unsigned(r*255.0f)<<16)|(unsigned(g*255.0f)<<8)|(unsigned(b*255.0f))|0xFF000000
  69. #define FRGBA_TO_INT32(r,g,b,a) (unsigned(a*255.0f)<<24)|(unsigned(r*255.0f)<<16)|(unsigned(g*255.0f)<<8)|(unsigned(b*255.0f))
  70. //
  71. // INT32 to Vector RGB methods
  72. //
  73. #define INT32_TO_VRGB(color, vrgb) \
  74. vrgb[0] = ((color & 0x00FF0000) >> 16) / 256.0F; \
  75. vrgb[1] = ((color & 0x0000FF00) >> 8) / 256.0F; \
  76. vrgb[2] = ((color & 0x000000FF)) / 256.0F;
  77. #define INT32_TO_VRGBA(color, vrgba) \
  78. vrgba[0] = ((color & 0x00FF0000) >> 16) / 256.0F; \
  79. vrgba[1] = ((color & 0x0000FF00) >> 8) / 256.0F; \
  80. vrgba[2] = ((color & 0x000000FF)) / 256.0F; \
  81. vrgba[3] = ((color & 0xFF000000) >> 24) / 256.0F;
  82. /*
  83. ** Render2DClass
  84. */
  85. class Render2DClass {
  86. public:
  87. Render2DClass( TextureClass* tex = NULL );
  88. virtual ~Render2DClass(void);
  89. virtual void Reset(void);
  90. void Render(void);
  91. void Set_Coordinate_Range( const RectClass & range );
  92. void Set_Texture(TextureClass* tex);
  93. TextureClass * Peek_Texture( void ) { return Texture; }
  94. void Set_Texture( const char * filename );
  95. void Enable_Additive(bool b);
  96. void Enable_Alpha(bool b);
  97. void Enable_Texturing(bool b);
  98. ShaderClass * Get_Shader( void ) { return &Shader; }
  99. static ShaderClass Get_Default_Shader( void );
  100. // Add Quad
  101. void Add_Quad( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  102. void Add_Quad_Backfaced( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  103. void Add_Quad( const RectClass & screen, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  104. void Add_Quad( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, unsigned long color = 0xFFFFFFFF );
  105. void Add_Quad( const RectClass & screen, unsigned long color = 0xFFFFFFFF );
  106. void Add_Quad_VGradient( const RectClass & screen, unsigned long top_color, unsigned long bottom_color );
  107. void Add_Quad_HGradient( const RectClass & screen, unsigned long left_color, unsigned long right_color );
  108. // Add Tri
  109. void Add_Tri( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & uv0, const Vector2 & uv1, const Vector2 & uv2, unsigned long color = 0xFFFFFFFF );
  110. // Primitive support
  111. void Add_Line( const Vector2 & a, const Vector2 & b, float width, unsigned long color = 0xFFFFFFFF );
  112. void Add_Line( const Vector2 & a, const Vector2 & b, float width, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  113. void Add_Outline( const RectClass & rect, float width = 1.0F, unsigned long color = 0xFFFFFFFF );
  114. void Add_Outline( const RectClass & rect, float width, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  115. void Add_Rect( const RectClass & rect, float border_width = 1.0F, uint32 border_color = 0xFF000000, uint32 fill_color = 0xFFFFFFFF);
  116. void Set_Hidden( bool hide ) { IsHidden = hide; }
  117. // Z-value support (this is usefull for playing tricks with the z-buffer)
  118. void Set_Z_Value (float z_value) { ZValue = z_value; }
  119. // Move all verts
  120. void Move( const Vector2 & a );
  121. // Force all alphas
  122. void Force_Alpha( float alpha );
  123. void Force_Color( int color );
  124. // Color access
  125. DynamicVectorClass<unsigned long> & Get_Color_Array (void) { return Colors; }
  126. // statics to access the Screen Resolution in Pixels
  127. static void Set_Screen_Resolution( const RectClass & screen );
  128. static const RectClass & Get_Screen_Resolution( void ) { return ScreenResolution; }
  129. protected:
  130. Vector2 CoordinateScale;
  131. Vector2 CoordinateOffset;
  132. Vector2 BiasedCoordinateOffset;
  133. TextureClass * Texture;
  134. ShaderClass Shader;
  135. DynamicVectorClass<unsigned short> Indices;
  136. unsigned short PreAllocatedIndices[60];
  137. DynamicVectorClass<Vector2> Vertices;
  138. Vector2 PreAllocatedVertices[60];
  139. DynamicVectorClass<Vector2> UVCoordinates;
  140. Vector2 PreAllocatedUVCoordinates[60];
  141. DynamicVectorClass<unsigned long> Colors;
  142. unsigned long PreAllocatedColors[60];
  143. bool IsHidden;
  144. float ZValue;
  145. static RectClass ScreenResolution;
  146. Vector2 Convert_Vert( const Vector2 & v );
  147. void Convert_Vert( Vector2 & vert_out, const Vector2 & vert_in );
  148. void Convert_Vert( Vector2 & vert_out, float x_in, float y_in );
  149. void Update_Bias( void );
  150. void Internal_Add_Quad_Vertices( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3 );
  151. void Internal_Add_Quad_Vertices( const RectClass & screen );
  152. void Internal_Add_Quad_UVs( const RectClass & uv );
  153. void Internal_Add_Quad_Colors( unsigned long color );
  154. void Internal_Add_Quad_VColors( unsigned long color1, unsigned long color2 );
  155. void Internal_Add_Quad_HColors( unsigned long color1, unsigned long color2 );
  156. void Internal_Add_Quad_Indicies( int start_vert_index, bool backfaced = false );
  157. };
  158. /*
  159. ** Render2DTextClass
  160. */
  161. class Render2DTextClass : public Render2DClass {
  162. public:
  163. Render2DTextClass(Font3DInstanceClass *font=NULL);
  164. ~Render2DTextClass();
  165. virtual void Reset(void);
  166. Font3DInstanceClass * Peek_Font( void ) { return Font; }
  167. void Set_Font( Font3DInstanceClass *font );
  168. void Set_Location( const Vector2 & loc ) { Location = loc; Cursor = loc; }
  169. void Set_Wrapping_Width (float width) { WrapWidth = width; }
  170. // Clipping support
  171. void Set_Clipping_Rect( const RectClass &rect ) { ClipRect = rect; IsClippedEnabled = true; }
  172. bool Is_Clipping_Enabled( void ) const { return IsClippedEnabled; }
  173. void Enable_Clipping( bool onoff ) { IsClippedEnabled = onoff; }
  174. void Draw_Text( const char * text, unsigned long color = 0xFFFFFFFF );
  175. void Draw_Text( const WCHAR * text, unsigned long color = 0xFFFFFFFF );
  176. void Draw_Block( const RectClass & screen, unsigned long color = 0xFFFFFFFF );
  177. const RectClass & Get_Draw_Extents( void ) { return DrawExtents; }
  178. const RectClass & Get_Total_Extents( void ) { return TotalExtents; }
  179. const Vector2 & Get_Cursor( void ) { return Cursor; }
  180. Vector2 Get_Text_Extents( const WCHAR * text );
  181. private:
  182. Font3DInstanceClass* Font;
  183. Vector2 Location;
  184. Vector2 Cursor;
  185. float WrapWidth;
  186. RectClass DrawExtents;
  187. RectClass TotalExtents;
  188. RectClass BlockUV;
  189. RectClass ClipRect;
  190. bool IsClippedEnabled;
  191. void Draw_Char( WCHAR ch, unsigned long color );
  192. };
  193. #endif // RENDER2D_H