theoraTextureObject.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "gfx/video/theoraTextureObject.h"
  23. #include "console/engineAPI.h"
  24. #include "platform/platform.h"
  25. #include "sfx/sfxTypes.h"
  26. #ifdef TORQUE_OGGTHEORA
  27. IMPLEMENT_CONOBJECT( TheoraTextureObject );
  28. ConsoleDocClass( TheoraTextureObject,
  29. "@brief Definition of a named texture target playing a Theora video.\n\n"
  30. "TheoraTextureObject defines a named texture target that may play back a Theora video. This texture "
  31. "target can, for example, be used by materials to texture objects with videos.\n\n"
  32. "@tsexample\n"
  33. "// The object that provides the video texture and controls its playback.\n"
  34. "singleton TheoraTextureObject( TheVideo )\n"
  35. "{\n"
  36. " // Unique name for the texture target for referencing in materials.\n"
  37. " texTargetName = \"video\";\n"
  38. "\n"
  39. " // Path to the video file.\n"
  40. " theoraFile = \"./MyVideo.ogv\";\n"
  41. "};\n"
  42. "\n"
  43. "// Material that uses the video texture.\n"
  44. "singleton Material( TheVideoMaterial )\n"
  45. "{\n"
  46. " // This has to reference the named texture target defined by the\n"
  47. " // TheoraTextureObject's 'texTargetName' property. Prefix with '#' to\n"
  48. " // identify as texture target reference.\n"
  49. " diffuseMap[ 0 ] = \"#video\";\n"
  50. "};\n"
  51. "@endtsexample\n"
  52. "\n"
  53. "@ingroup Rendering"
  54. );
  55. //-----------------------------------------------------------------------------
  56. TheoraTextureObject::TheoraTextureObject()
  57. : mSFXDescription( NULL )
  58. {
  59. mIsPlaying = false;
  60. mLoop = false;
  61. mTexTarget.getTextureDelegate().bind( this, &TheoraTextureObject::_texDelegate );
  62. }
  63. //-----------------------------------------------------------------------------
  64. void TheoraTextureObject::initPersistFields()
  65. {
  66. addGroup( "Theora" );
  67. addField( "theoraFile", TypeStringFilename, Offset( mFilename, TheoraTextureObject ),
  68. "Theora video file to play." );
  69. addField( "texTargetName", TypeRealString, Offset( mTexTargetName, TheoraTextureObject ),
  70. "Name of the texture target by which the texture can be referenced in materials." );
  71. addField( "sfxDescription", TypeSFXDescriptionName, Offset( mSFXDescription, TheoraTextureObject ),
  72. "Sound description to use for the video's audio channel.\n\n"
  73. "If not set, will use a default one." );
  74. addField( "loop", TypeBool, Offset( mLoop, TheoraTextureObject ),
  75. "Should the video loop." );
  76. endGroup( "Theora" );
  77. Parent::initPersistFields();
  78. }
  79. //-----------------------------------------------------------------------------
  80. GFXTextureObject* TheoraTextureObject::_texDelegate( U32 index )
  81. {
  82. // Refresh the video texture state.
  83. mTheoraTexture.refresh();
  84. // No texture if not playing.
  85. if( !mTheoraTexture.isPlaying() )
  86. {
  87. // Was this video playing and should it loop?
  88. if(mIsPlaying && mLoop)
  89. {
  90. play();
  91. // It won't be ready this frame
  92. return NULL;
  93. }
  94. else
  95. {
  96. mIsPlaying = false;
  97. return NULL;
  98. }
  99. }
  100. // Return the Theora video texture for the current frame.
  101. return mTheoraTexture.getTexture();
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool TheoraTextureObject::onAdd()
  105. {
  106. if( !Parent::onAdd() )
  107. return false;
  108. if( mFilename == StringTable->EmptyString())
  109. {
  110. Con::errorf( "TheoraTextureObject::onAdd - 'filename' must be set" );
  111. return false;
  112. }
  113. if( mTexTargetName.isEmpty() )
  114. {
  115. Con::errorf( "TheoraTextureObject::onAdd - 'texTargetName' not set" );
  116. return false;
  117. }
  118. if( !mTexTarget.registerWithName( mTexTargetName ) )
  119. {
  120. Con::errorf( "TheoraTextureObject::onAdd - Could not register texture target '%s", mTexTargetName.c_str() );
  121. return false;
  122. }
  123. return true;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void TheoraTextureObject::onRemove()
  127. {
  128. // Stop playback if it's running.
  129. mTheoraTexture.stop();
  130. // Unregister the texture target.
  131. mTexTarget.unregister();
  132. Parent::onRemove();
  133. }
  134. //-----------------------------------------------------------------------------
  135. void TheoraTextureObject::play()
  136. {
  137. if( mTheoraTexture.getFilename().isEmpty() || mTheoraTexture.getFilename() != String(mFilename) )
  138. {
  139. if( !mTheoraTexture.setFile( mFilename, mSFXDescription ) )
  140. {
  141. Con::errorf( "TheoraTextureObject::play - Could not load video '%s'", mFilename );
  142. return;
  143. }
  144. }
  145. mIsPlaying = true;
  146. mTheoraTexture.play();
  147. }
  148. //=============================================================================
  149. // Console API.
  150. //=============================================================================
  151. // MARK: ---- Console API ----
  152. //-----------------------------------------------------------------------------
  153. DefineEngineMethod( TheoraTextureObject, play, void, (),,
  154. "Start playback of the video." )
  155. {
  156. object->play();
  157. }
  158. //-----------------------------------------------------------------------------
  159. DefineEngineMethod( TheoraTextureObject, stop, void, (),,
  160. "Stop playback of the video." )
  161. {
  162. object->stop();
  163. }
  164. //-----------------------------------------------------------------------------
  165. DefineEngineMethod( TheoraTextureObject, pause, void, (),,
  166. "Pause playback of the video." )
  167. {
  168. object->pause();
  169. }
  170. #endif // TORQUE_OGGTHEORA