ScreenFadeState.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears 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. #pragma once
  16. /**
  17. * A colour whose components go from 0 (none) to 1 (full intensity).
  18. */
  19. struct FadeColour{
  20. /**
  21. * Red component.
  22. */
  23. float r = 0;
  24. /**
  25. * Green component.
  26. */
  27. float g = 0;
  28. /**
  29. * Blue component.
  30. */
  31. float b = 0;
  32. };
  33. /**
  34. * The rules of the screen fades of the FADE and NFADE opcodes.
  35. *
  36. * A fade is a linear tween of a colour that is drawn over the field, blended with it. Depending
  37. * on the fade type the colour is added to the field, subtracted from it, or mixed with it. Some
  38. * fades keep their last colour when the tween ends, and the others go back to showing the field.
  39. * A black colour in an additive or subtractive fade changes nothing.
  40. *
  41. * This class has no dependency on the rendering engine, so the rules can be tested.
  42. */
  43. class ScreenFadeState{
  44. public:
  45. /**
  46. * How the colour is combined with the field.
  47. */
  48. enum Blend{
  49. /**
  50. * Nothing is drawn.
  51. */
  52. BLEND_NONE,
  53. /**
  54. * The colour covers the field in proportion to the alpha.
  55. */
  56. BLEND_NORMAL,
  57. /**
  58. * The colour is added to the field.
  59. */
  60. BLEND_ADD,
  61. /**
  62. * The colour is subtracted from the field.
  63. */
  64. BLEND_SUBTRACT
  65. };
  66. /**
  67. * Starts a fade of the FADE opcode, replacing the one that is running.
  68. *
  69. * @param[in] type Fade type, from 1 to 10. 3 is not used.
  70. * @param[in] r Red component, from 0 to 255.
  71. * @param[in] g Green component, from 0 to 255.
  72. * @param[in] b Blue component, from 0 to 255.
  73. * @param[in] speed Speed of the fade. 8 takes a second, and double speed halves the time.
  74. * @return False if the type is unknown, and then nothing changes.
  75. */
  76. bool StartFade(const int type, const int r, const int g, const int b, const int speed);
  77. /**
  78. * Starts a fade of the NFADE opcode, replacing the one that is running.
  79. *
  80. * @param[in] type Fade type: 0 clears the fade, 11 adds and 12 subtracts.
  81. * @param[in] r Red component, from 0 to 255.
  82. * @param[in] g Green component, from 0 to 255.
  83. * @param[in] b Blue component, from 0 to 255.
  84. * @param[in] frames Duration of the fade, in frames of 30 per second.
  85. * @return False if the type is unknown, and then nothing changes.
  86. */
  87. bool StartNFade(const int type, const int r, const int g, const int b, const int frames);
  88. /**
  89. * Moves the fade forward in time.
  90. *
  91. * @param[in] seconds Time since the last call.
  92. * @return True if the tween of the fade ended with this call.
  93. */
  94. bool Advance(const float seconds);
  95. /**
  96. * Removes any fade, including the colour it holds.
  97. */
  98. void Reset();
  99. /**
  100. * Checks if a tween is running. A script waiting for the fade waits while it is.
  101. *
  102. * @return True if the fade is changing.
  103. */
  104. bool IsFading() const;
  105. /**
  106. * Checks if something has to be drawn: a tween, or the colour it holds.
  107. *
  108. * @return True if the fade covers the field.
  109. */
  110. bool IsVisible() const;
  111. /**
  112. * Retrieves how the colour has to be combined with the field.
  113. *
  114. * @return The blending.
  115. */
  116. Blend GetBlend() const;
  117. /**
  118. * Retrieves the colour to draw now, with all its components between 0 and 1.
  119. *
  120. * @return The colour.
  121. */
  122. FadeColour GetColour() const;
  123. /**
  124. * Retrieves how much the colour covers the field. Only the normal blending uses it.
  125. *
  126. * @return The alpha, between 0 and 1.
  127. */
  128. float GetAlpha() const;
  129. /**
  130. * Converts the speed of the FADE opcode to the seconds the fade takes.
  131. *
  132. * @param[in] speed The speed. 1 is 8 seconds, 2 is 4, 4 is 2 and 8 is 1.
  133. * @return The seconds. 0 for a speed that is not positive, meaning instant.
  134. */
  135. static float SpeedToSeconds(const int speed);
  136. private:
  137. /**
  138. * Starts a fade.
  139. *
  140. * @param[in] blend How the colour is combined with the field.
  141. * @param[in] from Colour at the start.
  142. * @param[in] to Colour at the end.
  143. * @param[in] alpha_from Alpha at the start.
  144. * @param[in] alpha_to Alpha at the end.
  145. * @param[in] seconds Duration. If it is 0 the fade is instant.
  146. * @param[in] hold Whether the final colour stays on the screen.
  147. */
  148. void Begin(
  149. const Blend blend, const FadeColour& from, const FadeColour& to, const float alpha_from,
  150. const float alpha_to, const float seconds, const bool hold
  151. );
  152. /**
  153. * How the colour is combined with the field.
  154. */
  155. Blend blend_ = BLEND_NONE;
  156. /**
  157. * Colour at the start of the tween.
  158. */
  159. FadeColour from_;
  160. /**
  161. * Colour at the end of the tween.
  162. */
  163. FadeColour to_;
  164. /**
  165. * Alpha at the start of the tween.
  166. */
  167. float alpha_from_ = 1;
  168. /**
  169. * Alpha at the end of the tween.
  170. */
  171. float alpha_to_ = 1;
  172. /**
  173. * Duration of the tween.
  174. */
  175. float duration_ = 0;
  176. /**
  177. * Time since the start of the tween.
  178. */
  179. float elapsed_ = 0;
  180. /**
  181. * Whether the tween is running.
  182. */
  183. bool fading_ = false;
  184. /**
  185. * Whether the final colour stays on the screen when the tween ends.
  186. */
  187. bool hold_ = false;
  188. /**
  189. * Whether the final colour is on the screen after the tween.
  190. */
  191. bool holding_ = false;
  192. };