theoraTextureObject.cpp
Engine/source/gfx/video/theoraTextureObject.cpp
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "gfx/video/theoraTextureObject.h" 25#include "console/engineAPI.h" 26#include "platform/platform.h" 27#include "sfx/sfxTypes.h" 28 29#ifdef TORQUE_OGGTHEORA 30 31IMPLEMENT_CONOBJECT( TheoraTextureObject ); 32 33ConsoleDocClass( TheoraTextureObject, 34 35 "@brief Definition of a named texture target playing a Theora video.\n\n" 36 37 "TheoraTextureObject defines a named texture target that may play back a Theora video. This texture " 38 "target can, for example, be used by materials to texture objects with videos.\n\n" 39 40 "@tsexample\n" 41 "// The object that provides the video texture and controls its playback.\n" 42 "singleton TheoraTextureObject( TheVideo )\n" 43 "{\n" 44 " // Unique name for the texture target for referencing in materials.\n" 45 " texTargetName = \"video\";\n" 46 "\n" 47 " // Path to the video file.\n" 48 " theoraFile = \"./MyVideo.ogv\";\n" 49 "};\n" 50 "\n" 51 "// Material that uses the video texture.\n" 52 "singleton Material( TheVideoMaterial )\n" 53 "{\n" 54 " // This has to reference the named texture target defined by the\n" 55 " // TheoraTextureObject's 'texTargetName' property. Prefix with '#' to\n" 56 " // identify as texture target reference.\n" 57 " diffuseMap[ 0 ] = \"#video\";\n" 58 "};\n" 59 "@endtsexample\n" 60 "\n" 61 62 "@ingroup Rendering" 63); 64 65 66//----------------------------------------------------------------------------- 67 68TheoraTextureObject::TheoraTextureObject() 69 : mSFXDescription( NULL ) 70{ 71 mIsPlaying = false; 72 mLoop = false; 73 74 mTexTarget.getTextureDelegate().bind( this, &TheoraTextureObject::_texDelegate ); 75} 76 77//----------------------------------------------------------------------------- 78 79void TheoraTextureObject::initPersistFields() 80{ 81 addGroup( "Theora" ); 82 83 addField( "theoraFile", TypeStringFilename, Offset( mFilename, TheoraTextureObject ), 84 "Theora video file to play." ); 85 86 addField( "texTargetName", TypeRealString, Offset( mTexTargetName, TheoraTextureObject ), 87 "Name of the texture target by which the texture can be referenced in materials." ); 88 89 addField( "sfxDescription", TypeSFXDescriptionName, Offset( mSFXDescription, TheoraTextureObject ), 90 "Sound description to use for the video's audio channel.\n\n" 91 "If not set, will use a default one." ); 92 93 addField( "loop", TypeBool, Offset( mLoop, TheoraTextureObject ), 94 "Should the video loop." ); 95 96 endGroup( "Theora" ); 97 98 Parent::initPersistFields(); 99} 100 101//----------------------------------------------------------------------------- 102 103GFXTextureObject* TheoraTextureObject::_texDelegate( U32 index ) 104{ 105 // Refresh the video texture state. 106 mTheoraTexture.refresh(); 107 108 // No texture if not playing. 109 if( !mTheoraTexture.isPlaying() ) 110 { 111 // Was this video playing and should it loop? 112 if(mIsPlaying && mLoop) 113 { 114 play(); 115 116 // It won't be ready this frame 117 return NULL; 118 } 119 else 120 { 121 mIsPlaying = false; 122 return NULL; 123 } 124 } 125 126 // Return the Theora video texture for the current frame. 127 return mTheoraTexture.getTexture(); 128} 129 130//----------------------------------------------------------------------------- 131 132bool TheoraTextureObject::onAdd() 133{ 134 if( !Parent::onAdd() ) 135 return false; 136 137 if( mFilename.isEmpty() ) 138 { 139 Con::errorf( "TheoraTextureObject::onAdd - 'filename' must be set" ); 140 return false; 141 } 142 143 if( mTexTargetName.isEmpty() ) 144 { 145 Con::errorf( "TheoraTextureObject::onAdd - 'texTargetName' not set" ); 146 return false; 147 } 148 149 if( !mTexTarget.registerWithName( mTexTargetName ) ) 150 { 151 Con::errorf( "TheoraTextureObject::onAdd - Could not register texture target '%s", mTexTargetName.c_str() ); 152 return false; 153 } 154 155 return true; 156} 157 158//----------------------------------------------------------------------------- 159 160void TheoraTextureObject::onRemove() 161{ 162 // Stop playback if it's running. 163 mTheoraTexture.stop(); 164 165 // Unregister the texture target. 166 mTexTarget.unregister(); 167 168 Parent::onRemove(); 169} 170 171//----------------------------------------------------------------------------- 172 173void TheoraTextureObject::play() 174{ 175 if( mTheoraTexture.getFilename().isEmpty() || mTheoraTexture.getFilename() != mFilename ) 176 { 177 if( !mTheoraTexture.setFile( mFilename, mSFXDescription ) ) 178 { 179 Con::errorf( "TheoraTextureObject::play - Could not load video '%s'", mFilename.c_str() ); 180 return; 181 } 182 } 183 184 mIsPlaying = true; 185 mTheoraTexture.play(); 186} 187 188//============================================================================= 189// Console API. 190//============================================================================= 191// MARK: ---- Console API ---- 192 193//----------------------------------------------------------------------------- 194 195DefineEngineMethod( TheoraTextureObject, play, void, (),, 196 "Start playback of the video." ) 197{ 198 object->play(); 199} 200 201//----------------------------------------------------------------------------- 202 203DefineEngineMethod( TheoraTextureObject, stop, void, (),, 204 "Stop playback of the video." ) 205{ 206 object->stop(); 207} 208 209//----------------------------------------------------------------------------- 210 211DefineEngineMethod( TheoraTextureObject, pause, void, (),, 212 "Pause playback of the video." ) 213{ 214 object->pause(); 215} 216 217#endif // TORQUE_OGGTHEORA 218