healthbarctrl.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 : wwui *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/healthbarctrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 10/25/01 4:33p $*
  29. * *
  30. * $Revision:: 2 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "healthbarctrl.h"
  36. #include "texture.h"
  37. #include "assetmgr.h"
  38. #include "stylemgr.h"
  39. //////////////////////////////////////////////////////////////////////
  40. // Local constants
  41. //////////////////////////////////////////////////////////////////////
  42. static const char * TEXTURE_NAME = "HUD_C&C_HEALTHBAR.TGA";
  43. static const Vector2 TEXTURE_SIZE = Vector2 (16, 16);
  44. //////////////////////////////////////////////////////////////////////
  45. //
  46. // HealthBarCtrlClass
  47. //
  48. //////////////////////////////////////////////////////////////////////
  49. HealthBarCtrlClass::HealthBarCtrlClass (void) :
  50. Percent (1.0F)
  51. {
  52. //
  53. // Configure the renderer
  54. //
  55. StyleMgrClass::Configure_Renderer (&ControlRenderer);
  56. ControlRenderer.Enable_Texturing (true);
  57. ControlRenderer.Enable_Alpha (true);
  58. //
  59. // Load the texture we'll use
  60. //
  61. TextureClass *texture = WW3DAssetManager::Get_Instance ()->Get_Texture (TEXTURE_NAME, TextureClass::MIP_LEVELS_1);
  62. ControlRenderer.Set_Texture (texture);
  63. REF_PTR_RELEASE (texture);
  64. return ;
  65. }
  66. //////////////////////////////////////////////////////////////////////
  67. //
  68. // ~HealthBarCtrlClass
  69. //
  70. //////////////////////////////////////////////////////////////////////
  71. HealthBarCtrlClass::~HealthBarCtrlClass (void)
  72. {
  73. return ;
  74. }
  75. ////////////////////////////////////////////////////////////////
  76. //
  77. // Blit_Section
  78. //
  79. ////////////////////////////////////////////////////////////////
  80. static void
  81. Blit_Section
  82. (
  83. Render2DClass & renderer,
  84. const Vector2 & screen_pos,
  85. const Vector2 & texture_pos,
  86. const Vector2 & pixels,
  87. const Vector2 & texture_dimensions,
  88. int color
  89. )
  90. {
  91. RectClass screen_rect;
  92. screen_rect.Left = screen_pos.X;
  93. screen_rect.Top = screen_pos.Y;
  94. screen_rect.Right = screen_rect.Left + pixels.X;
  95. screen_rect.Bottom = screen_rect.Top + pixels.Y;
  96. //
  97. // Determine the UV coordinates
  98. //
  99. RectClass uv_rect (texture_pos.X, texture_pos.Y, texture_pos.X + pixels.X, texture_pos.Y + pixels.Y);
  100. uv_rect.Inverse_Scale (Vector2 (texture_dimensions.X, texture_dimensions.Y));
  101. //
  102. // Draw the chunk
  103. //
  104. renderer.Add_Quad (screen_rect, uv_rect, color);
  105. return ;
  106. }
  107. //////////////////////////////////////////////////////////////////////
  108. //
  109. // Create_Control_Renderer
  110. //
  111. //////////////////////////////////////////////////////////////////////
  112. void
  113. HealthBarCtrlClass::Create_Control_Renderer (void)
  114. {
  115. Render2DClass &renderer = ControlRenderer;
  116. renderer.Reset ();
  117. //
  118. // Determine what color to draw the bar in
  119. //
  120. int color = RGB_TO_INT32 (0, 255, 0);
  121. if (Percent <= 0.25F) {
  122. color = RGB_TO_INT32 (255, 0, 0);
  123. } else if (Percent <= 0.5F) {
  124. color = RGB_TO_INT32 (255, 255, 0);
  125. }
  126. //
  127. // Calculate how long to draw the texture
  128. //
  129. float full_width = Rect.Width ();
  130. int width = (full_width * Percent);
  131. Vector2 size1 (8.0F, 12.0F);
  132. Vector2 size2 (7.0F, 12.0F);
  133. Vector2 texture_pos1 (0.0F, 2.0F);
  134. Vector2 texture_pos2 (9.0F, 2.0F);
  135. Vector2 *size = &size1;
  136. Vector2 *texture_pos = &texture_pos1;
  137. //
  138. // Tile the sections horizontally until we've reached our edge
  139. //
  140. int index = 0;
  141. float remaining_width = width;
  142. float x_pos = Rect.Left;
  143. while (remaining_width > 0) {
  144. size->X = min (remaining_width, size->X);
  145. //
  146. // Draw this section
  147. //
  148. ::Blit_Section (ControlRenderer, Vector2 (x_pos, Rect.Top), *texture_pos,
  149. *size, TEXTURE_SIZE, color);
  150. //
  151. // Advance to the next section
  152. //
  153. x_pos += size->X;
  154. remaining_width -= size->X;
  155. //
  156. // Advance to the next texture chunk
  157. //
  158. index ++;
  159. if (index > 0) {
  160. size = &size2;
  161. texture_pos = &texture_pos2;
  162. }
  163. }
  164. return ;
  165. }
  166. //////////////////////////////////////////////////////////////////////
  167. //
  168. // Set_Life
  169. //
  170. //////////////////////////////////////////////////////////////////////
  171. void
  172. HealthBarCtrlClass::Set_Life (float value)
  173. {
  174. Percent = value;
  175. //
  176. // Force an update
  177. //
  178. Set_Dirty ();
  179. return ;
  180. }
  181. ////////////////////////////////////////////////////////////////
  182. //
  183. // Render
  184. //
  185. ////////////////////////////////////////////////////////////////
  186. void
  187. HealthBarCtrlClass::Render (void)
  188. {
  189. //
  190. // Recreate the renderers (if necessary)
  191. //
  192. if (IsDirty) {
  193. Create_Control_Renderer ();
  194. }
  195. //
  196. // Render the bar...
  197. //
  198. ControlRenderer.Render ();
  199. DialogControlClass::Render ();
  200. return ;
  201. }