Browse Source

Add `getWidth()` and `getHeight()`, additionally:

Additionally refactor the code according to oF guidelines. To be done for the rest of the code.
master
Krisjanis Rijnieks 10 years ago
parent
commit
b81cf62af4
  1. 47
      src/Sources/FboSource.cpp
  2. 41
      src/Sources/FboSource.h

47
src/Sources/FboSource.cpp

@ -2,12 +2,14 @@
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
FboSource::FboSource() : fbo(NULL) { FboSource::FboSource() : fbo(NULL) {
name = PIMAPPER_FBO_SOURCE_DEF_NAME; name = PIMAPPER_FBO_SOURCE_DEF_NAME;
loadable = false; loadable = false;
loaded = false; loaded = false;
type = SourceType::SOURCE_TYPE_FBO; type = SourceType::SOURCE_TYPE_FBO;
ofAddListener(ofEvents().setup, this, &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); ofAddListener(ofEvents().setup, this,
&FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP);
} }
FboSource::~FboSource() { FboSource::~FboSource() {
@ -17,27 +19,36 @@ namespace ofx {
void FboSource::addAppListeners() { void FboSource::addAppListeners() {
ofLogNotice("FboSource") << "Adding app listeners"; ofLogNotice("FboSource") << "Adding app listeners";
ofAddListener(ofEvents().update, this, &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); ofAddListener(ofEvents().update, this,
ofAddListener(ofEvents().draw, this, &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP);
ofAddListener(ofEvents().exit, this, &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_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() { void FboSource::removeAppListeners() {
ofLogNotice("FboSource") << "Removing app listeners"; ofLogNotice("FboSource") << "Removing app listeners";
ofRemoveListener(ofEvents().update, this, &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); ofRemoveListener(ofEvents().update, this,
ofRemoveListener(ofEvents().draw, this, &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP);
ofRemoveListener(ofEvents().exit, this, &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_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) { void FboSource::onAppSetup(ofEventArgs &args) {
ofRemoveListener(ofEvents().setup, this, &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); ofRemoveListener(ofEvents().setup, this,
&FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP);
setup(); setup();
// Check if FBO was allocated in user defined setup // Check if FBO was allocated in user defined setup
// If not, show warning and alocate to avoid panic // If not, show warning and alocate to avoid panic
if (!fbo->isAllocated()) { if (!fbo->isAllocated()) {
ofLogWarning("FboSource::onAppSetup") << "FBO not allocated, allocating with default values"; ofLogWarning("FboSource::onAppSetup") <<
allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH, PIMAPPER_FBO_SOURCE_DEF_HEIGHT); "FBO not allocated, allocating with default values";
allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH,
PIMAPPER_FBO_SOURCE_DEF_HEIGHT);
} }
} }
@ -84,5 +95,21 @@ namespace ofx {
} }
} }
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 piMapper
} // namespace ofx } // namespace ofx

41
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 #pragma once
#include "ofMain.h" #include "ofMain.h"
@ -16,39 +9,37 @@ class YourGenerativeSource : public FboSource {
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class FboSource : public BaseSource { class FboSource : public BaseSource {
public: public:
FboSource(); FboSource();
~FboSource(); ~FboSource();
// Add/remove calls to update and draw // Override these in your custom FBO source
// App listeners are added once the source is assigned to at least one surface virtual void setup() {};
// App listeners are removed once the source is not assigned anywhere virtual void update() {};
virtual void draw() {};
virtual void exit() {};
// The only method from BaseSource to be overriden
void clear();
// App listeners
void addAppListeners(); void addAppListeners();
void removeAppListeners(); void removeAppListeners();
// These are called on app events
void onAppSetup(ofEventArgs & args); void onAppSetup(ofEventArgs & args);
void onAppUpdate(ofEventArgs & args); void onAppUpdate(ofEventArgs & args);
void onAppDraw(ofEventArgs & args); void onAppDraw(ofEventArgs & args);
void onAppExit(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();
protected: protected:
ofFbo * fbo; ofFbo * fbo;
// 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); void allocate(int width, int height);
// Some handy getters
int getWidth();
int getHeight();
}; };
} // namespace piMapper } // namespace piMapper
} // namespace ofx } // namespace ofx
Loading…
Cancel
Save