From b81cf62af4585877809425ef92a1b8cf90c711d4 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks <krisjanis.rijnieks@gmail.com> Date: Mon, 19 Oct 2015 19:20:01 +0200 Subject: [PATCH] Add `getWidth()` and `getHeight()`, additionally: Additionally refactor the code according to oF guidelines. To be done for the rest of the code. --- src/Sources/FboSource.cpp | 195 ++++++++++++++++++++++---------------- src/Sources/FboSource.h | 73 +++++++------- 2 files changed, 143 insertions(+), 125 deletions(-) diff --git a/src/Sources/FboSource.cpp b/src/Sources/FboSource.cpp index a866366..d61bad9 100644 --- a/src/Sources/FboSource.cpp +++ b/src/Sources/FboSource.cpp @@ -1,88 +1,115 @@ #include "FboSource.h" namespace ofx { - namespace piMapper { - FboSource::FboSource() : fbo(NULL) { - name = PIMAPPER_FBO_SOURCE_DEF_NAME; - loadable = false; - loaded = false; - type = SourceType::SOURCE_TYPE_FBO; - ofAddListener(ofEvents().setup, this, &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); - } - - 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::onAppSetup(ofEventArgs &args) { - ofRemoveListener(ofEvents().setup, this, &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); - setup(); - - // Check if FBO was allocated in user defined setup - // If not, show warning and alocate to avoid panic - if (!fbo->isAllocated()) { - ofLogWarning("FboSource::onAppSetup") << "FBO not allocated, allocating with default values"; - allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH, PIMAPPER_FBO_SOURCE_DEF_HEIGHT); - } - } - - void FboSource::onAppUpdate(ofEventArgs &args) { - if (fbo == NULL || !fbo->isAllocated()) { - ofLogWarning("FboSource") << "FBO not allocated"; - return; - } - update(); - } - - void FboSource::onAppDraw(ofEventArgs &args) { - if (fbo == NULL || !fbo->isAllocated()) { - ofLogWarning("FboSource") << "FBO not allocated"; - return; - } - fbo->begin(); - draw(); - fbo->end(); - } - - void FboSource::onAppExit(ofEventArgs &args) { - exit(); - } - - void FboSource::allocate(int width, int height) { - clear(); - fbo = new ofFbo(); - fbo->allocate(width, height); - - // Clear FBO - fbo->begin(); - ofClear(0); - fbo->end(); - - texture = &(fbo->getTextureReference()); - } - - void FboSource::clear() { - texture = NULL; - if (fbo != NULL) { - delete fbo; - fbo = NULL; - } - } - - } // namespace piMapper + namespace piMapper { + + FboSource::FboSource() : fbo(NULL) { + name = PIMAPPER_FBO_SOURCE_DEF_NAME; + loadable = false; + loaded = false; + type = SourceType::SOURCE_TYPE_FBO; + ofAddListener(ofEvents().setup, this, + &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); + } + + 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::onAppSetup(ofEventArgs &args) { + ofRemoveListener(ofEvents().setup, this, + &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); + setup(); + + // Check if FBO was allocated in user defined setup + // If not, show warning and alocate to avoid panic + if (!fbo->isAllocated()) { + ofLogWarning("FboSource::onAppSetup") << + "FBO not allocated, allocating with default values"; + allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH, + PIMAPPER_FBO_SOURCE_DEF_HEIGHT); + } + } + + void FboSource::onAppUpdate(ofEventArgs &args) { + if (fbo == NULL || !fbo->isAllocated()) { + ofLogWarning("FboSource") << "FBO not allocated"; + return; + } + update(); + } + + void FboSource::onAppDraw(ofEventArgs &args) { + if (fbo == NULL || !fbo->isAllocated()) { + ofLogWarning("FboSource") << "FBO not allocated"; + return; + } + fbo->begin(); + draw(); + fbo->end(); + } + + void FboSource::onAppExit(ofEventArgs &args) { + exit(); + } + + void FboSource::allocate(int width, int height) { + clear(); + fbo = new ofFbo(); + fbo->allocate(width, height); + + // Clear FBO + fbo->begin(); + ofClear(0); + fbo->end(); + + texture = &(fbo->getTextureReference()); + } + + void FboSource::clear() { + texture = NULL; + if (fbo != NULL) { + delete fbo; + fbo = NULL; + } + } + + int FboSource::getWidth() { + if (fbo->isAllocated()) { + return fbo->getWidth(); + } else { + return 0; + } + } + + int FboSource::getHeight() { + if (fbo->isAllocated()) { + return fbo->getHeight(); + } else { + return 0; + } + } + + } // namespace piMapper } // namespace ofx \ No newline at end of file diff --git a/src/Sources/FboSource.h b/src/Sources/FboSource.h index 3e3a3a5..ea54e92 100644 --- a/src/Sources/FboSource.h +++ b/src/Sources/FboSource.h @@ -1,10 +1,3 @@ -/* -Use this as base class for your generative sources: - -class YourGenerativeSource : public FboSource { - // Your code here -} -*/ #pragma once #include "ofMain.h" @@ -15,40 +8,38 @@ class YourGenerativeSource : public FboSource { #define PIMAPPER_FBO_SOURCE_DEF_HEIGHT 500 namespace ofx { - namespace piMapper { - class FboSource : public BaseSource { - public: - FboSource(); - ~FboSource(); - - // Add/remove calls to update and draw - // App listeners are added once the source is assigned to at least one surface - // App listeners are removed once the source is not assigned anywhere - void addAppListeners(); - void removeAppListeners(); - - // These are called on app events - void onAppSetup(ofEventArgs& args); - void onAppUpdate(ofEventArgs& args); - void onAppDraw(ofEventArgs& args); - void onAppExit(ofEventArgs& args); - - // Override these in your custom FBO source - virtual void setup() {}; // Don't do any drawing here - virtual void update() {}; // Don't do any drawing here - // You don't need to take care of fbo.begin() and fbo.end() here; - virtual void draw() {}; // But this is the only place where you shoud do drawing - virtual void exit() {}; - - // The only method from BaseSource to be overriden - void clear(); + namespace piMapper { + + class FboSource : public BaseSource { + public: + FboSource(); + ~FboSource(); - protected: - ofFbo* fbo; + // Override these in your custom FBO source + virtual void setup() {}; + virtual void update() {}; + virtual void draw() {}; + virtual void exit() {}; + + // 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); - // Use this instead fbo->allocate as it sets other source related settings - // It is protected to force the user to create derived FBO sources from this - void allocate(int width, int height); - }; - } // namespace piMapper + protected: + ofFbo * fbo; + void allocate(int width, int height); + + // Some handy getters + int getWidth(); + int getHeight(); + }; + + } // namespace piMapper } // namespace ofx \ No newline at end of file