Sprite.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _SPRITE_H_
  23. #include "Sprite.h"
  24. #endif
  25. #ifndef _DGL_H_
  26. #include "graphics/dgl.h"
  27. #endif
  28. #ifndef _STRINGBUFFER_H_
  29. #include "string/stringBuffer.h"
  30. #endif
  31. // Script bindings.
  32. #include "Sprite_ScriptBinding.h"
  33. //------------------------------------------------------------------------------
  34. IMPLEMENT_CONOBJECT(Sprite);
  35. //------------------------------------------------------------------------------
  36. Sprite::Sprite() :
  37. mFlipX(false),
  38. mFlipY(false)
  39. {
  40. mUseComplexColor = false;
  41. mComplexColor0 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  42. mComplexColor1 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  43. mComplexColor2 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  44. mComplexColor3 = ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  45. }
  46. //------------------------------------------------------------------------------
  47. Sprite::~Sprite()
  48. {
  49. }
  50. //------------------------------------------------------------------------------
  51. void Sprite::copyTo(SimObject* object)
  52. {
  53. // Call to parent.
  54. Parent::copyTo(object);
  55. // Cast to sprite.
  56. Sprite* pSprite = static_cast<Sprite*>(object);
  57. // Sanity!
  58. AssertFatal(pSprite != NULL, "Sprite::copyTo() - Object is not the correct type.");
  59. /// Render flipping.
  60. pSprite->setFlip( getFlipX(), getFlipY() );
  61. }
  62. //------------------------------------------------------------------------------
  63. void Sprite::initPersistFields()
  64. {
  65. // Call parent.
  66. Parent::initPersistFields();
  67. /// Render flipping.
  68. addField("FlipX", TypeBool, Offset(mFlipX, Sprite), &writeFlipX, "");
  69. addField("FlipY", TypeBool, Offset(mFlipY, Sprite), &writeFlipY, "");
  70. addField("useComplexColor", TypeBool, Offset(mUseComplexColor, Sprite), &writeUseComplexColor, "");
  71. }
  72. //------------------------------------------------------------------------------
  73. void Sprite::sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer )
  74. {
  75. if (mUseComplexColor)
  76. {
  77. ImageFrameProviderCore::renderComplex(
  78. getFlipX(), getFlipY(),
  79. mRenderOOBB[0],
  80. mRenderOOBB[1],
  81. mRenderOOBB[2],
  82. mRenderOOBB[3],
  83. pBatchRenderer,
  84. mComplexColor0,
  85. mComplexColor1,
  86. mComplexColor2,
  87. mComplexColor3);
  88. }
  89. else
  90. {
  91. ImageFrameProvider::render(
  92. getFlipX(), getFlipY(),
  93. mRenderOOBB[0],
  94. mRenderOOBB[1],
  95. mRenderOOBB[2],
  96. mRenderOOBB[3],
  97. pBatchRenderer );
  98. }
  99. }
  100. //------------------------------------------------------------------------------
  101. const ColorF& Sprite::getComplexColor(const S8 cornerID)
  102. {
  103. if (cornerID == 1)
  104. {
  105. return mComplexColor0;
  106. }
  107. else if (cornerID == 2)
  108. {
  109. return mComplexColor1;
  110. }
  111. else if (cornerID == 3)
  112. {
  113. return mComplexColor2;
  114. }
  115. else if (cornerID == 4)
  116. {
  117. return mComplexColor3;
  118. }
  119. else
  120. {
  121. return ColorF(1.0f, 1.0f, 1.0f, 1.0f);
  122. }
  123. }
  124. //-----------------------------------------------------------------------------
  125. void Sprite::updateBlendColor(const F32 elapsedTime)
  126. {
  127. if (!mUseComplexColor)
  128. {
  129. SceneObject::updateBlendColor(elapsedTime);
  130. }
  131. else
  132. {
  133. updateFadeColor(mComplexColor0, mFadeToColorTL, elapsedTime);
  134. updateFadeColor(mComplexColor1, mFadeToColorTR, elapsedTime);
  135. updateFadeColor(mComplexColor2, mFadeToColorBR, elapsedTime);
  136. updateFadeColor(mComplexColor3, mFadeToColorBL, elapsedTime);
  137. }
  138. }
  139. //-----------------------------------------------------------------------------
  140. void Sprite::checkFadeComplete()
  141. {
  142. if (mFadeActive &&
  143. ((mUseComplexColor &&
  144. mComplexColor0 == mFadeToColorTL.TargetColor &&
  145. mComplexColor1 == mFadeToColorTR.TargetColor &&
  146. mComplexColor2 == mFadeToColorBR.TargetColor &&
  147. mComplexColor3 == mFadeToColorBL.TargetColor) ||
  148. (!mUseComplexColor &&
  149. mBlendColor == mFadeToColor.TargetColor)))
  150. {
  151. mFadeActive = false;
  152. PROFILE_SCOPE(Sprite_onFadeToComplete);
  153. Con::executef(this, 1, "onFadeToComplete");
  154. }
  155. }
  156. bool Sprite::fadeToComplex(const S8 cornerID, const ColorF& targetColor, const F32 deltaRed, const F32 deltaGreen, const F32 deltaBlue, const F32 deltaAlpha)
  157. {
  158. // Check in a scene.
  159. if (!getScene())
  160. {
  161. Con::warnf("Sprite::fadeToComplex() - Cannot fade object (%d) to a color as it is not in a scene.", getId());
  162. return false;
  163. }
  164. // Check targetColor.
  165. if (!targetColor.isValidColor())
  166. {
  167. Con::warnf("Sprite::fadeToComplex() - Cannot fade object (%d) because the color is invalid.", getId());
  168. return false;
  169. }
  170. // Check that the sprite is using complex colors
  171. if (!mUseComplexColor)
  172. {
  173. Con::warnf("Sprite::fadeToComplex() - Cannot fade object (%d) because the sprite is not using complex colors.", getId());
  174. return false;
  175. }
  176. // Only set fading active if the target color is not the blending color.
  177. if (targetColor != getComplexColor(cornerID))
  178. {
  179. mFadeActive = true;
  180. if (cornerID == 1) { mFadeToColorTL.set(targetColor, deltaRed, deltaGreen, deltaBlue, deltaAlpha); }
  181. else if (cornerID == 2) { mFadeToColorTR.set(targetColor, deltaRed, deltaGreen, deltaBlue, deltaAlpha); }
  182. else if (cornerID == 3) { mFadeToColorBL.set(targetColor, deltaRed, deltaGreen, deltaBlue, deltaAlpha); }
  183. else if (cornerID == 4) { mFadeToColorBR.set(targetColor, deltaRed, deltaGreen, deltaBlue, deltaAlpha); }
  184. }
  185. return true;
  186. }