render2d.h 9.8 KB

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