theoraTextureObject.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. docsURL;
  67. addGroup( "Theora" );
  68. addField( "theoraFile", TypeStringFilename, Offset( mFilename, TheoraTextureObject ),
  69. "Theora video file to play." );
  70. addField( "texTargetName", TypeRealString, Offset( mTexTargetName, TheoraTextureObject ),
  71. "Name of the texture target by which the texture can be referenced in materials." );
  72. addField( "sfxDescription", TypeSFXDescriptionName, Offset( mSFXDescription, TheoraTextureObject ),
  73. "Sound description to use for the video's audio channel.\n\n"
  74. "If not set, will use a default one." );
  75. addField( "loop", TypeBool, Offset( mLoop, TheoraTextureObject ),
  76. "Should the video loop." );
  77. endGroup( "Theora" );
  78. Parent::initPersistFields();
  79. }
  80. //-----------------------------------------------------------------------------
  81. GFXTextureObject* TheoraTextureObject::_texDelegate( U32 index )
  82. {
  83. // Refresh the video texture state.
  84. mTheoraTexture.refresh();
  85. // No texture if not playing.
  86. if( !mTheoraTexture.isPlaying() )
  87. {
  88. // Was this video playing and should it loop?
  89. if(mIsPlaying && mLoop)
  90. {
  91. play();
  92. // It won't be ready this frame
  93. return NULL;
  94. }
  95. else
  96. {
  97. mIsPlaying = false;
  98. return NULL;
  99. }
  100. }
  101. // Return the Theora video texture for the current frame.
  102. return mTheoraTexture.getTexture();
  103. }
  104. //-----------------------------------------------------------------------------
  105. bool TheoraTextureObject::onAdd()
  106. {
  107. if( !Parent::onAdd() )
  108. return false;
  109. if( mFilename == StringTable->EmptyString())
  110. {
  111. Con::errorf( "TheoraTextureObject::onAdd - 'filename' must be set" );
  112. return false;
  113. }
  114. if( mTexTargetName.isEmpty() )
  115. {
  116. Con::errorf( "TheoraTextureObject::onAdd - 'texTargetName' not set" );
  117. return false;
  118. }
  119. if( !mTexTarget.registerWithName( mTexTargetName ) )
  120. {
  121. Con::errorf( "TheoraTextureObject::onAdd - Could not register texture target '%s", mTexTargetName.c_str() );
  122. return false;
  123. }
  124. return true;
  125. }
  126. //-----------------------------------------------------------------------------
  127. void TheoraTextureObject::onRemove()
  128. {
  129. // Stop playback if it's running.
  130. mTheoraTexture.stop();
  131. // Unregister the texture target.
  132. mTexTarget.unregister();
  133. Parent::onRemove();
  134. }
  135. //-----------------------------------------------------------------------------
  136. void TheoraTextureObject::play()
  137. {
  138. if( mTheoraTexture.getFilename().isEmpty() || mTheoraTexture.getFilename() != String(mFilename) )
  139. {
  140. if( !mTheoraTexture.setFile( mFilename, mSFXDescription ) )
  141. {
  142. Con::errorf( "TheoraTextureObject::play - Could not load video '%s'", mFilename );
  143. return;
  144. }
  145. }
  146. mIsPlaying = true;
  147. mTheoraTexture.play();
  148. }
  149. //=============================================================================
  150. // Console API.
  151. //=============================================================================
  152. // MARK: ---- Console API ----
  153. //-----------------------------------------------------------------------------
  154. DefineEngineMethod( TheoraTextureObject, play, void, (),,
  155. "Start playback of the video." )
  156. {
  157. object->play();
  158. }
  159. //-----------------------------------------------------------------------------
  160. DefineEngineMethod( TheoraTextureObject, stop, void, (),,
  161. "Stop playback of the video." )
  162. {
  163. object->stop();
  164. }
  165. //-----------------------------------------------------------------------------
  166. DefineEngineMethod( TheoraTextureObject, pause, void, (),,
  167. "Pause playback of the video." )
  168. {
  169. object->pause();
  170. }
  171. #endif // TORQUE_OGGTHEORA