ScreenFadeState.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <algorithm>
  16. #include "core/ScreenFadeState.h"
  17. namespace{
  18. // Frames per second the NFADE durations are given in.
  19. const float FRAMES_PER_SECOND = 30.0f;
  20. // The colour of the opcode as it is given.
  21. FadeColour Standard(const int r, const int g, const int b){
  22. FadeColour colour;
  23. colour.r = r / 255.0f;
  24. colour.g = g / 255.0f;
  25. colour.b = b / 255.0f;
  26. return colour;
  27. }
  28. // 255 minus each component of the colour.
  29. FadeColour Inverse1(const int r, const int g, const int b){
  30. FadeColour colour;
  31. colour.r = (255 - r) / 255.0f;
  32. colour.g = (255 - g) / 255.0f;
  33. colour.b = (255 - b) / 255.0f;
  34. return colour;
  35. }
  36. // Four times 255 minus each component of the colour. It can go over full intensity.
  37. FadeColour Inverse4(const int r, const int g, const int b){
  38. FadeColour colour = Inverse1(r, g, b);
  39. colour.r *= 4;
  40. colour.g *= 4;
  41. colour.b *= 4;
  42. return colour;
  43. }
  44. // Black adds or subtracts nothing.
  45. FadeColour Black(){return FadeColour();}
  46. float Clamp(const float value){return std::min(1.0f, std::max(0.0f, value));}
  47. }
  48. float ScreenFadeState::SpeedToSeconds(const int speed){
  49. if (speed <= 0) return 0;
  50. return 8.0f / speed;
  51. }
  52. void ScreenFadeState::Begin(
  53. const Blend blend, const FadeColour& from, const FadeColour& to, const float alpha_from,
  54. const float alpha_to, const float seconds, const bool hold
  55. ){
  56. blend_ = blend;
  57. from_ = from;
  58. to_ = to;
  59. alpha_from_ = alpha_from;
  60. alpha_to_ = alpha_to;
  61. duration_ = seconds;
  62. elapsed_ = 0;
  63. hold_ = hold;
  64. fading_ = seconds > 0;
  65. holding_ = !fading_ && hold;
  66. if (!fading_ && !hold) Reset();
  67. }
  68. bool ScreenFadeState::StartFade(
  69. const int type, const int r, const int g, const int b, const int speed
  70. ){
  71. const float seconds = SpeedToSeconds(speed);
  72. switch (type){
  73. case 1:
  74. Begin(BLEND_SUBTRACT, Inverse4(r, g, b), Black(), 1, 1, seconds, false);
  75. return true;
  76. case 2:
  77. Begin(BLEND_SUBTRACT, Black(), Inverse4(r, g, b), 1, 1, seconds, true);
  78. return true;
  79. case 4:
  80. // The screen goes black at once and stays that way.
  81. Begin(BLEND_NORMAL, Black(), Black(), 1, 1, 0, true);
  82. return true;
  83. case 5:
  84. Begin(BLEND_ADD, Standard(r, g, b), Black(), 1, 1, seconds, false);
  85. return true;
  86. case 6:
  87. // The pair of type 5: the colour is added to the field little by little and kept. The
  88. // scripts use the pair for flashes (5 after 6 with the same colour) and for the tint
  89. // of the scenes that are told (a dark blue added to the field, not a screen of it).
  90. Begin(BLEND_ADD, Black(), Standard(r, g, b), 1, 1, seconds, true);
  91. return true;
  92. case 7:
  93. Begin(BLEND_SUBTRACT, Inverse1(r, g, b), Inverse1(r, g, b), 1, 1, seconds, false);
  94. return true;
  95. case 8:
  96. Begin(BLEND_SUBTRACT, Inverse1(r, g, b), Inverse1(r, g, b), 1, 1, seconds, true);
  97. return true;
  98. case 9:
  99. Begin(BLEND_ADD, Standard(r, g, b), Standard(r, g, b), 1, 1, seconds, false);
  100. return true;
  101. case 10:
  102. Begin(BLEND_ADD, Standard(r, g, b), Standard(r, g, b), 1, 1, seconds, true);
  103. return true;
  104. default: return false;
  105. }
  106. }
  107. bool ScreenFadeState::StartNFade(
  108. const int type, const int r, const int g, const int b, const int frames
  109. ){
  110. const float seconds = frames > 0 ? frames / FRAMES_PER_SECOND : 0;
  111. switch (type){
  112. case 0:
  113. Reset();
  114. return true;
  115. case 11:
  116. Begin(BLEND_ADD, Black(), Standard(r, g, b), 1, 1, seconds, true);
  117. return true;
  118. case 12:
  119. Begin(BLEND_SUBTRACT, Black(), Standard(r, g, b), 1, 1, seconds, true);
  120. return true;
  121. default: return false;
  122. }
  123. }
  124. bool ScreenFadeState::Advance(const float seconds){
  125. if (!fading_) return false;
  126. elapsed_ += seconds;
  127. if (elapsed_ < duration_) return false;
  128. fading_ = false;
  129. holding_ = hold_;
  130. if (!holding_) Reset();
  131. return true;
  132. }
  133. void ScreenFadeState::Reset(){*this = ScreenFadeState();}
  134. bool ScreenFadeState::IsFading() const{return fading_;}
  135. bool ScreenFadeState::IsVisible() const{return fading_ || holding_;}
  136. ScreenFadeState::Blend ScreenFadeState::GetBlend() const{return IsVisible() ? blend_ : BLEND_NONE;}
  137. FadeColour ScreenFadeState::GetColour() const{
  138. FadeColour colour = to_;
  139. if (fading_){
  140. const float progress = duration_ > 0 ? Clamp(elapsed_ / duration_) : 1.0f;
  141. colour.r = from_.r + (to_.r - from_.r) * progress;
  142. colour.g = from_.g + (to_.g - from_.g) * progress;
  143. colour.b = from_.b + (to_.b - from_.b) * progress;
  144. }
  145. colour.r = Clamp(colour.r);
  146. colour.g = Clamp(colour.g);
  147. colour.b = Clamp(colour.b);
  148. return colour;
  149. }
  150. float ScreenFadeState::GetAlpha() const{
  151. if (!fading_) return Clamp(alpha_to_);
  152. const float progress = duration_ > 0 ? Clamp(elapsed_ / duration_) : 1.0f;
  153. return Clamp(alpha_from_ + (alpha_to_ - alpha_from_) * progress);
  154. }