render2d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. *** 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 : public W3DMPO
  86. {
  87. W3DMPO_GLUE(Render2DClass)
  88. public:
  89. Render2DClass( TextureClass* tex = NULL );
  90. virtual ~Render2DClass(void);
  91. virtual void Reset(void);
  92. void Render(void);
  93. void Set_Coordinate_Range( const RectClass & range );
  94. void Set_Texture(TextureClass* tex);
  95. TextureClass * Peek_Texture( void ) { return Texture; }
  96. void Set_Texture( const char * filename );
  97. void Enable_Additive(bool b);
  98. void Enable_Alpha(bool b);
  99. void Enable_Grayscale(bool b);///<added for generals to draw disabled button states - MW
  100. void Enable_Texturing(bool b);
  101. ShaderClass * Get_Shader( void ) { return &Shader; }
  102. static ShaderClass Get_Default_Shader( void );
  103. // Add Quad
  104. void Add_Quad( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  105. void Add_Quad_Backfaced( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  106. void Add_Quad( const RectClass & screen, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  107. void Add_Quad( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, unsigned long color = 0xFFFFFFFF );
  108. void Add_Quad( const RectClass & screen, unsigned long color = 0xFFFFFFFF );
  109. void Add_Quad_VGradient( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, const RectClass & uv, unsigned long top_color, unsigned long bottom_color );
  110. void Add_Quad_VGradient( const RectClass & screen, unsigned long top_color, unsigned long bottom_color );
  111. void Add_Quad_HGradient( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3, const RectClass & uv, unsigned long left_color, unsigned long right_color );
  112. void Add_Quad_HGradient( const RectClass & screen, unsigned long left_color, unsigned long right_color );
  113. // Add Tri
  114. 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 );
  115. // Primitive support
  116. void Add_Line( const Vector2 & a, const Vector2 & b, float width, unsigned long color = 0xFFFFFFFF );
  117. void Add_Line( const Vector2 & a, const Vector2 & b, float width, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  118. void Add_Line( const Vector2 & a, const Vector2 & b, float width, unsigned long color , unsigned long color2);
  119. void Add_Line( const Vector2 & a, const Vector2 & b, float width, const RectClass & uv, unsigned long color, unsigned long color2);
  120. void Add_Outline( const RectClass & rect, float width = 1.0F, unsigned long color = 0xFFFFFFFF );
  121. void Add_Outline( const RectClass & rect, float width, const RectClass & uv, unsigned long color = 0xFFFFFFFF );
  122. void Add_Rect( const RectClass & rect, float border_width = 1.0F, uint32 border_color = 0xFF000000, uint32 fill_color = 0xFFFFFFFF);
  123. void Set_Hidden( bool hide ) { IsHidden = hide; }
  124. // Z-value support (this is usefull for playing tricks with the z-buffer)
  125. void Set_Z_Value (float z_value) { ZValue = z_value; }
  126. // Move all verts
  127. void Move( const Vector2 & a );
  128. // Force all alphas
  129. void Force_Alpha( float alpha );
  130. void Force_Color( int color );
  131. // Color access
  132. DynamicVectorClass<unsigned long> & Get_Color_Array (void) { return Colors; }
  133. // statics to access the Screen Resolution in Pixels
  134. static void Set_Screen_Resolution( const RectClass & screen );
  135. static const RectClass & Get_Screen_Resolution( void ) { return ScreenResolution; }
  136. protected:
  137. Vector2 CoordinateScale;
  138. Vector2 CoordinateOffset;
  139. Vector2 BiasedCoordinateOffset;
  140. TextureClass * Texture;
  141. ShaderClass Shader;
  142. DynamicVectorClass<unsigned short> Indices;
  143. unsigned short PreAllocatedIndices[60];
  144. DynamicVectorClass<Vector2> Vertices;
  145. Vector2 PreAllocatedVertices[60];
  146. DynamicVectorClass<Vector2> UVCoordinates;
  147. Vector2 PreAllocatedUVCoordinates[60];
  148. DynamicVectorClass<unsigned long> Colors;
  149. unsigned long PreAllocatedColors[60];
  150. bool IsHidden;
  151. bool IsGrayScale;
  152. float ZValue;
  153. static RectClass ScreenResolution;
  154. Vector2 Convert_Vert( const Vector2 & v );
  155. void Convert_Vert( Vector2 & vert_out, const Vector2 & vert_in );
  156. void Convert_Vert( Vector2 & vert_out, float x_in, float y_in );
  157. void Update_Bias( void );
  158. void Internal_Add_Quad_Vertices( const Vector2 & v0, const Vector2 & v1, const Vector2 & v2, const Vector2 & v3 );
  159. void Internal_Add_Quad_Vertices( const RectClass & screen );
  160. void Internal_Add_Quad_UVs( const RectClass & uv );
  161. void Internal_Add_Quad_Colors( unsigned long color );
  162. void Internal_Add_Quad_VColors( unsigned long color1, unsigned long color2 );
  163. void Internal_Add_Quad_HColors( unsigned long color1, unsigned long color2 );
  164. void Internal_Add_Quad_Indicies( int start_vert_index, bool backfaced = false );
  165. };
  166. /*
  167. ** Render2DTextClass
  168. */
  169. class Render2DTextClass : public Render2DClass {
  170. public:
  171. Render2DTextClass(Font3DInstanceClass *font=NULL);
  172. ~Render2DTextClass();
  173. virtual void Reset(void);
  174. Font3DInstanceClass * Peek_Font( void ) { return Font; }
  175. void Set_Font( Font3DInstanceClass *font );
  176. void Set_Location( const Vector2 & loc ) { Location = loc; Cursor = loc; }
  177. void Set_Wrapping_Width (float width) { WrapWidth = width; }
  178. // Clipping support
  179. void Set_Clipping_Rect( const RectClass &rect ) { ClipRect = rect; IsClippedEnabled = true; }
  180. bool Is_Clipping_Enabled( void ) const { return IsClippedEnabled; }
  181. void Enable_Clipping( bool onoff ) { IsClippedEnabled = onoff; }
  182. void Draw_Text( const char * text, unsigned long color = 0xFFFFFFFF );
  183. void Draw_Text( const WCHAR * text, unsigned long color = 0xFFFFFFFF );
  184. void Draw_Block( const RectClass & screen, unsigned long color = 0xFFFFFFFF );
  185. const RectClass & Get_Draw_Extents( void ) { return DrawExtents; }
  186. const RectClass & Get_Total_Extents( void ) { return TotalExtents; }
  187. const Vector2 & Get_Cursor( void ) { return Cursor; }
  188. Vector2 Get_Text_Extents( const WCHAR * text );
  189. private:
  190. Font3DInstanceClass* Font;
  191. Vector2 Location;
  192. Vector2 Cursor;
  193. float WrapWidth;
  194. RectClass DrawExtents;
  195. RectClass TotalExtents;
  196. RectClass BlockUV;
  197. RectClass ClipRect;
  198. bool IsClippedEnabled;
  199. void Draw_Char( WCHAR ch, unsigned long color );
  200. };
  201. #endif // RENDER2D_H