From 197145bb33d53a72a70064528ed12b6dbdb57c5b Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Tue, 17 Jan 2017 13:32:12 +0100 Subject: [PATCH] Add piping down app events into fbo sources instead of registering --- src/Application/Application.cpp | 5 +++++ src/MediaServer/MediaServer.cpp | 34 +++++++++++++++++++++++++-------- src/MediaServer/MediaServer.h | 4 ++++ src/Sources/FboSource.cpp | 32 ++++--------------------------- src/Sources/FboSource.h | 15 ++++++++------- 5 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/Application/Application.cpp b/src/Application/Application.cpp index 09e3018..9e0f292 100644 --- a/src/Application/Application.cpp +++ b/src/Application/Application.cpp @@ -43,6 +43,9 @@ Application::Application(){ } void Application::setup(){ + // Setup components + _mediaServer.setup(); + if(!loadXmlSettings(PIMAPPER_SETTINGS_FILE)){ if(SettingsLoader::instance()->create(PIMAPPER_SETTINGS_FILE)){ bool success = loadXmlSettings(PIMAPPER_SETTINGS_FILE); @@ -69,6 +72,7 @@ void Application::setup(){ } void Application::update(){ + _mediaServer.update(); _state->update(this); // Autosave, do it only of the mode is not presentation mode @@ -86,6 +90,7 @@ ApplicationBaseMode * Application::getState(){ } void Application::draw(){ + _mediaServer.draw(); _state->draw(this); _info.draw(); } diff --git a/src/MediaServer/MediaServer.cpp b/src/MediaServer/MediaServer.cpp index 29c6fb9..f602031 100644 --- a/src/MediaServer/MediaServer.cpp +++ b/src/MediaServer/MediaServer.cpp @@ -32,6 +32,24 @@ MediaServer::~MediaServer(){ removeWatcherListeners(); } +void MediaServer::setup(){ + // We could setup sources here, but the sources are + // set up while adding them to media server. For now + // This method is here just to keep things consistent. +} + +void MediaServer::update(){ + for(int i = 0; i < fboSources.size(); ++i){ + fboSources[i]->updateFbo(); + } +} + +void MediaServer::draw(){ + for(int i = 0; i < fboSources.size(); ++i){ + fboSources[i]->drawFbo(); + } +} + int MediaServer::getNumImages(){ int numLocalImages = imageWatcher.getFilePaths().size(); int numPiImages = piImageWatcher.getFilePaths().size(); @@ -156,7 +174,7 @@ BaseSource * MediaServer::loadMedia(string & path, int mediaType){ stringstream ss; ss << "Can not load media of unknown type: " << mediaType; ofLogFatalError("MediaServer") << ss.str(); - exit(EXIT_FAILURE); + std::exit(EXIT_FAILURE); } return 0; } @@ -227,7 +245,7 @@ void MediaServer::unloadImage(string & path){ stringstream failss; failss << "Failed to remove image source: " << path; ofLogFatalError("MediaServer") << failss.str(); - exit(EXIT_FAILURE); + std::exit(EXIT_FAILURE); } BaseSource * MediaServer::loadVideo(string & path){ @@ -298,7 +316,7 @@ void MediaServer::unloadVideo(string & path){ stringstream failss; failss << "Failed to remove video source: " << path; ofLogFatalError("MediaServer") << failss.str(); - exit(EXIT_FAILURE); + std::exit(EXIT_FAILURE); } void MediaServer::unloadMedia(string & path){ @@ -313,7 +331,7 @@ void MediaServer::unloadMedia(string & path){ }else{ // Oh my god, what to do!? Relax and exit. ofLogFatalError("MediaServer") << "Attempt to unload media of unknown type"; - exit(EXIT_FAILURE); + std::exit(EXIT_FAILURE); } }else{ ofLogNotice("MediaServer") << "Nothing to unload"; @@ -341,7 +359,7 @@ BaseSource * MediaServer::getSourceByPath(string & mediaPath){ stringstream ss; ss << "Could not find source by path: " << mediaPath; ofLogFatalError("MediaServer") << ss.str(); - exit(EXIT_FAILURE); + std::exit(EXIT_FAILURE); } string MediaServer::getDefaultImageDir(){ @@ -361,7 +379,7 @@ string MediaServer::getDefaultMediaDir(int sourceType){ stringstream ss; ss << "Could not get default media dir. Unknown source type: " << sourceType; ofLogFatalError("MediaServer") << ss.str(); - exit(EXIT_FAILURE); + std::exit(EXIT_FAILURE); } } @@ -414,7 +432,7 @@ BaseSource * MediaServer::loadFboSource(string & fboSourceName){ // else // Not loaded, add to loaded sources and activate // source var should be set by now - source->addAppListeners(); + //source->addAppListeners(); source->referenceCount = 1; ofLogNotice("MediaServer") << "Current " << fboSourceName << " reference count: " << source->referenceCount; loadedSources[fboSourceName] = source; @@ -438,7 +456,7 @@ void MediaServer::unloadFboSource(string & fboSourceName){ if(source->referenceCount <= 0){ ofLogNotice("MediaServer") << fboSourceName << " reference count <= 0, removing from loaded sources"; source->referenceCount = 0; - source->removeAppListeners(); + //source->removeAppListeners(); map ::iterator it = loadedSources.find(fboSourceName); loadedSources.erase(it); ofLogNotice("MediaServer") << "Source count after FBO source removal: " << loadedSources.size() << endl; diff --git a/src/MediaServer/MediaServer.h b/src/MediaServer/MediaServer.h index 75507a6..c85e716 100644 --- a/src/MediaServer/MediaServer.h +++ b/src/MediaServer/MediaServer.h @@ -65,6 +65,10 @@ class MediaServer { public: MediaServer(); virtual ~MediaServer(); + + void setup(); + void update(); + void draw(); int getNumVideos(); int getNumImages(); diff --git a/src/Sources/FboSource.cpp b/src/Sources/FboSource.cpp index ebeb58a..6476744 100644 --- a/src/Sources/FboSource.cpp +++ b/src/Sources/FboSource.cpp @@ -3,7 +3,8 @@ namespace ofx { namespace piMapper { -FboSource::FboSource() : fbo(0){ +FboSource::FboSource(){ + fbo = 0; name = PIMAPPER_FBO_SOURCE_DEF_NAME; loadable = false; loaded = false; @@ -12,31 +13,10 @@ FboSource::FboSource() : fbo(0){ } FboSource::~FboSource(){ - removeAppListeners(); clear(); } -void FboSource::addAppListeners(){ - ofLogNotice("FboSource") << "Adding app listeners"; - ofAddListener(ofEvents().update, this, - &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); - ofAddListener(ofEvents().draw, this, - &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); - ofAddListener(ofEvents().exit, this, - &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP); -} - -void FboSource::removeAppListeners(){ - ofLogNotice("FboSource") << "Removing app listeners"; - ofRemoveListener(ofEvents().update, this, - &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); - ofRemoveListener(ofEvents().draw, this, - &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); - ofRemoveListener(ofEvents().exit, this, - &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP); -} - -void FboSource::onAppUpdate(ofEventArgs & args){ +void FboSource::updateFbo(){ if(fbo == 0 || !fbo->isAllocated()){ ofLogWarning("FboSource") << "FBO not allocated"; return; @@ -44,7 +24,7 @@ void FboSource::onAppUpdate(ofEventArgs & args){ update(); } -void FboSource::onAppDraw(ofEventArgs & args){ +void FboSource::drawFbo(){ if(fbo == 0 || !fbo->isAllocated()){ ofLogWarning("FboSource") << "FBO not allocated"; return; @@ -59,10 +39,6 @@ void FboSource::onAppDraw(ofEventArgs & args){ fbo->end(); } -void FboSource::onAppExit(ofEventArgs & args){ - exit(); -} - void FboSource::setDisableDraw(bool b){ _disableDraw = b; } diff --git a/src/Sources/FboSource.h b/src/Sources/FboSource.h index b11b6e3..1a82b2c 100644 --- a/src/Sources/FboSource.h +++ b/src/Sources/FboSource.h @@ -18,18 +18,19 @@ class FboSource : public BaseSource { virtual void setup(){} virtual void update(){} virtual void draw(){} - virtual void exit(){} + // We use this as replacement of draw internally in ofxPiMapper + // to populate the FBO texture that then can be drawn again by + // calling normal draw(); + // Or maybe this should be simplified? By leaving only one draw? + // And the user would have to allocate the fbo himself? + void updateFbo(); + void drawFbo(); + // The only method from BaseSource to be overriden void clear(); // App listeners - void addAppListeners(); - void removeAppListeners(); - void onAppSetup(ofEventArgs & args); - void onAppUpdate(ofEventArgs & args); - void onAppDraw(ofEventArgs & args); - void onAppExit(ofEventArgs & args); void setDisableDraw(bool b); // Use in cases with external ofFbo protected: