/** GDevelop - Video Object Extension Copyright (c) 2010-2014 Florian Rival (Florian.Rival@gmail.com) This project is released under the MIT License. */ #include "VideoWrapper.h" #include #include #include #include #include "OpenAL_AudioInterface.h" #include "TheoraMemoryLoader.h" #include #include VideoWrapper::VideoWrapper() : renderSprite(new sf::Sprite), currentFrameImage(new sf::Texture), clip(NULL), started(false), valid(false), volume(100) { if ( TheoraVideoManager::getSingletonPtr() == NULL ) new TheoraVideoManager(); if( TheoraVideoManager::getSingletonPtr()->getAudioInterfaceFactory() == NULL ) { TheoraVideoManager::getSingletonPtr()->setAudioInterfaceFactory(new OpenAL_AudioInterfaceFactory); } } VideoWrapper::~VideoWrapper() { if ( clip ) TheoraVideoManager::getSingletonPtr()->destroyVideoClip(clip); if ( currentFrameImage ) delete currentFrameImage; if ( renderSprite ) delete renderSprite; } void VideoWrapper::Init(const VideoWrapper & other) { if ( TheoraVideoManager::getSingletonPtr() == NULL ) new TheoraVideoManager; if( TheoraVideoManager::getSingletonPtr()->getAudioInterfaceFactory() == NULL ) { TheoraVideoManager::getSingletonPtr()->setAudioInterfaceFactory(new OpenAL_AudioInterfaceFactory); } if ( currentFrameImage ) delete currentFrameImage; currentFrameImage = new sf::Texture(*other.currentFrameImage); if ( renderSprite ) delete renderSprite; renderSprite = new sf::Sprite(*other.renderSprite); if ( clip ) TheoraVideoManager::getSingletonPtr()->destroyVideoClip(clip); clip = NULL; started = false; volume = other.volume; } bool VideoWrapper::Load(std::string filename) { //Destroy old clip if necessary if ( clip ) TheoraVideoManager::getSingletonPtr()->destroyVideoClip(clip); clip = NULL; valid = false; started = false; //Load new clip try { #if defined(GD_IDE_ONLY) clip = TheoraVideoManager::getSingletonPtr()->createVideoClip(filename, TH_RGBA); #else TheoraMemoryLoader *memLoad = new TheoraMemoryLoader(filename, (unsigned char*)gd::RessourcesLoader::Get()->LoadBinaryFile(filename), gd::RessourcesLoader::Get()->GetBinaryFileSize(filename)); clip = TheoraVideoManager::getSingletonPtr()->createVideoClip(memLoad, TH_RGBA); #endif } catch(...) { std::cout << "Error when opening video file " << filename << std::endl; } if ( clip ) { clip->setAutoRestart(1); clip->setAudioGain(static_cast(volume) / 100); currentFrameImage->create(clip->getWidth(), clip->getHeight()); valid = true; } return true; } void VideoWrapper::UpdateTime(float time_increase) { if ( clip == NULL ) return; if (started) { // let's wait until the system caches up a few frames on startup if (clip->getNumReadyFrames() < clip->getNumPrecachedFrames()*0.5f) return; started=false; } TheoraVideoManager::getSingletonPtr()->update(time_increase); } const sf::Texture & VideoWrapper::GetNextFrameImage() { if ( clip != NULL ) { TheoraVideoFrame* f=clip->getNextFrame(); if (f) { currentFrameImage->update((sf::Uint8*)f->getBuffer(), (unsigned int)f->getWidth(), (unsigned int)f->getHeight(),0,0); clip->popFrame(); } } return *currentFrameImage; } void VideoWrapper::SetLooping(bool loop) { if ( clip != NULL ) clip->setAutoRestart(loop); } void VideoWrapper::Seek(float time) { if ( clip != NULL ) clip->seek(time); if ( dynamic_cast(clip->getAudioInterface()) != NULL ) dynamic_cast(clip->getAudioInterface())->seek(time); } void VideoWrapper::SetPause(bool pause) { if ( clip != NULL ) { if ( pause ) { clip->pause(); dynamic_cast(clip->getAudioInterface())->pause(); } else { clip->play(); dynamic_cast(clip->getAudioInterface())->play(); } } } unsigned int VideoWrapper::GetVolume() { return volume; } void VideoWrapper::SetVolume(unsigned int vol) { volume = vol; if(clip != NULL) clip->setAudioGain(static_cast(volume) / 100); } void VideoWrapper::Restart() { if ( clip != NULL ) { clip->restart(); dynamic_cast(clip->getAudioInterface())->play(); } } float VideoWrapper::GetTimePosition() const { if ( clip != NULL ) return clip->getTimePosition(); return 0; } float VideoWrapper::GetDuration() const { if ( clip != NULL ) return clip->getDuration(); return 0; }