render2dsentence.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/render2dsentence.h $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 8/29/01 10:58a $*
  29. * *
  30. * $Revision:: 6 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef RENDER2DSENTENCE_H
  39. #define RENDER2DSENTENCE_H
  40. #include "render2d.h"
  41. #include "refcount.h"
  42. #include "vector.h"
  43. #include "vector2i.h"
  44. #include "wwstring.h"
  45. #include "win.h"
  46. /*
  47. ** FontCharsClass
  48. */
  49. class SurfaceClass;
  50. //
  51. // Private data structures
  52. //
  53. class FontCharsClassCharDataStruct : public W3DMPO
  54. {
  55. W3DMPO_GLUE(FontCharsClassCharDataStruct)
  56. public:
  57. WCHAR Value;
  58. short Width;
  59. uint16 * Buffer;
  60. };
  61. enum { CHAR_BUFFER_LEN = 32768 };
  62. class FontCharsBuffer : public W3DMPO
  63. {
  64. W3DMPO_GLUE(FontCharsBuffer)
  65. public:
  66. uint16 Buffer[CHAR_BUFFER_LEN];
  67. };
  68. class FontCharsClass : public W3DMPO, public RefCountClass
  69. {
  70. W3DMPO_GLUE(FontCharsClass)
  71. public:
  72. FontCharsClass( void );
  73. ~FontCharsClass();
  74. // TR: Hack for unicode font support
  75. FontCharsClass *AlternateUnicodeFont;
  76. void Initialize_GDI_Font( const char *font_name, int point_size, bool is_bold );
  77. bool Is_Font( const char *font_name, int point_size, bool is_bold );
  78. const char * Get_Name( void ) { return Name; }
  79. int Get_Char_Height( void ) { return CharHeight; }
  80. int Get_Char_Width( WCHAR ch );
  81. int Get_Char_Spacing( WCHAR ch );
  82. int Get_Extra_Overlap(void) {return PixelOverlap;}
  83. void Blit_Char( WCHAR ch, uint16 *dest_ptr, int dest_stride, int x, int y );
  84. private:
  85. //
  86. // Private methods
  87. //
  88. void Create_GDI_Font( const char *font_name );
  89. void Free_GDI_Font( void );
  90. const FontCharsClassCharDataStruct * Store_GDI_Char( WCHAR ch );
  91. void Update_Current_Buffer( int char_width );
  92. const FontCharsClassCharDataStruct * Get_Char_Data( WCHAR ch );
  93. void Grow_Unicode_Array( WCHAR ch );
  94. void Free_Character_Arrays( void );
  95. //
  96. // Private member data
  97. //
  98. StringClass Name;
  99. DynamicVectorClass<FontCharsBuffer*> BufferList;
  100. int CurrPixelOffset;
  101. int CharHeight;
  102. int CharAscent;
  103. int CharOverhang;
  104. int PixelOverlap;
  105. int PointSize;
  106. StringClass GDIFontName;
  107. HFONT OldGDIFont;
  108. HBITMAP OldGDIBitmap;
  109. HBITMAP GDIBitmap;
  110. HFONT GDIFont;
  111. uint8 * GDIBitmapBits;
  112. HDC MemDC;
  113. FontCharsClassCharDataStruct * ASCIICharArray[256];
  114. FontCharsClassCharDataStruct ** UnicodeCharArray;
  115. uint16 FirstUnicodeChar;
  116. uint16 LastUnicodeChar;
  117. bool IsBold;
  118. };
  119. /*
  120. ** Render2DSentenceClass
  121. */
  122. class Render2DSentenceClass {
  123. public:
  124. //Render2DSentenceClass( FontCharsClass * font );
  125. Render2DSentenceClass( void );
  126. ~Render2DSentenceClass();
  127. void Render (void);
  128. virtual void Reset (void);
  129. void Reset_Polys (void);
  130. FontCharsClass * Peek_Font( void ) { return Font; }
  131. void Set_Font( FontCharsClass *font );
  132. void Set_Location( const Vector2 & loc );
  133. void Set_Base_Location( const Vector2 & loc );
  134. bool Set_Wrapping_Width (float width) { if(WrapWidth == width)
  135. return false;
  136. WrapWidth = width;
  137. return true; }
  138. bool Set_Word_Wrap_Centered( bool isCentered ) { if(Centered == isCentered)
  139. return false;
  140. Centered = isCentered;
  141. return true;}
  142. void Set_Hot_Key_Parse( bool parseHotKey ){ ParseHotKey = parseHotKey; }
  143. void Set_Use_Hard_Word_Wrap( bool useHardWrap){ useHardWordWrap = useHardWrap; }
  144. //
  145. // Clipping support
  146. //
  147. void Set_Clipping_Rect( const RectClass &rect ) { ClipRect = rect; IsClippedEnabled = true; }
  148. bool Is_Clipping_Enabled( void ) const { return IsClippedEnabled; }
  149. void Enable_Clipping( bool onoff ) { IsClippedEnabled = onoff; }
  150. //
  151. // Shader modification support
  152. //
  153. void Make_Additive (void);
  154. ShaderClass Get_Shader (void) const { return Shader; }
  155. void Set_Shader (ShaderClass shader);
  156. // void Draw_Block( const RectClass & screen, unsigned long color = 0xFFFFFFFF );
  157. const RectClass & Get_Draw_Extents( void ) { return DrawExtents; }
  158. // const RectClass & Get_Total_Extents( void ) { return TotalExtents; }
  159. // const Vector2 & Get_Cursor( void ) { return Cursor; }
  160. Vector2 Get_Text_Extents( const WCHAR * text );
  161. Vector2 Get_Formatted_Text_Extents( const WCHAR * text );
  162. //
  163. // Sentence control
  164. //
  165. void Build_Sentence (const WCHAR *text, int *hkX, int *hkY);
  166. void Draw_Sentence (uint32 color = 0xFFFFFFFF);
  167. //
  168. // Texture hint
  169. //
  170. void Set_Texture_Size_Hint( int hint ) { TextureSizeHint = hint; }
  171. int Get_Texture_Size_Hint( void ) const { return TextureSizeHint; }
  172. void Set_Mono_Spaced( bool onoff ) { MonoSpaced = onoff; }
  173. private:
  174. //
  175. // Private structures
  176. //
  177. struct SentenceDataStruct {
  178. SurfaceClass * Surface;
  179. RectClass ScreenRect;
  180. RectClass UVRect;
  181. bool operator== (const SentenceDataStruct &src) { return false; }
  182. bool operator!= (const SentenceDataStruct &src) { return true; }
  183. };
  184. struct PendingSurfaceStruct {
  185. SurfaceClass * Surface;
  186. DynamicVectorClass<Render2DClass *> Renderers;
  187. bool operator== (const PendingSurfaceStruct &src) { return false; }
  188. bool operator!= (const PendingSurfaceStruct &src) { return true; }
  189. };
  190. struct RendererDataStruct {
  191. Render2DClass * Renderer;
  192. SurfaceClass * Surface;
  193. bool operator== (const RendererDataStruct &src) { return false; }
  194. bool operator!= (const RendererDataStruct &src) { return true; }
  195. };
  196. //
  197. // Private methods
  198. //
  199. void Reset_Sentence_Data (void);
  200. void Build_Textures (void);
  201. void Record_Sentence_Chunk (void);
  202. void Allocate_New_Surface (const WCHAR *text, bool justCalcExtents = false);
  203. void Release_Pending_Surfaces (void);
  204. void Build_Sentence_Centered (const WCHAR *text, int *hkX, int *hkY);
  205. Vector2 Build_Sentence_Not_Centered (const WCHAR *text, int *hkX, int *hkY,bool justCalcExtents = false );
  206. //
  207. // Private member data
  208. //
  209. DynamicVectorClass<SentenceDataStruct> SentenceData;
  210. DynamicVectorClass<PendingSurfaceStruct> PendingSurfaces;
  211. DynamicVectorClass<RendererDataStruct> Renderers;
  212. FontCharsClass * Font;
  213. Vector2 BaseLocation;
  214. Vector2 Location;
  215. Vector2 Cursor;
  216. Vector2i TextureOffset;
  217. int TextureStartX;
  218. int CurrTextureSize;
  219. int TextureSizeHint;
  220. SurfaceClass * CurSurface;
  221. bool MonoSpaced;
  222. float WrapWidth;
  223. bool Centered; // Determines whether or not to center each line
  224. RectClass ClipRect;
  225. RectClass DrawExtents;
  226. bool IsClippedEnabled;
  227. bool ParseHotKey;
  228. bool useHardWordWrap;
  229. uint16 * LockedPtr;
  230. int LockedStride;
  231. TextureClass * CurTexture;
  232. ShaderClass Shader;
  233. };
  234. #endif // RENDER2DSENTENCE_H