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. 195
      src/Sources/FboSource.cpp
  2. 77
      src/Sources/FboSource.h

195
src/Sources/FboSource.cpp

@ -1,88 +1,115 @@
#include "FboSource.h" #include "FboSource.h"
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
FboSource::FboSource() : fbo(NULL) {
name = PIMAPPER_FBO_SOURCE_DEF_NAME; FboSource::FboSource() : fbo(NULL) {
loadable = false; name = PIMAPPER_FBO_SOURCE_DEF_NAME;
loaded = false; loadable = false;
type = SourceType::SOURCE_TYPE_FBO; loaded = false;
ofAddListener(ofEvents().setup, this, &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); type = SourceType::SOURCE_TYPE_FBO;
} ofAddListener(ofEvents().setup, this,
&FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP);
FboSource::~FboSource() { }
removeAppListeners();
clear(); FboSource::~FboSource() {
} removeAppListeners();
clear();
void FboSource::addAppListeners() { }
ofLogNotice("FboSource") << "Adding app listeners";
ofAddListener(ofEvents().update, this, &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); void FboSource::addAppListeners() {
ofAddListener(ofEvents().draw, this, &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP); ofLogNotice("FboSource") << "Adding app listeners";
ofAddListener(ofEvents().exit, this, &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP); ofAddListener(ofEvents().update, this,
} &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP);
ofAddListener(ofEvents().draw, this,
void FboSource::removeAppListeners() { &FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP);
ofLogNotice("FboSource") << "Removing app listeners"; ofAddListener(ofEvents().exit, this,
ofRemoveListener(ofEvents().update, this, &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP); &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::removeAppListeners() {
ofLogNotice("FboSource") << "Removing app listeners";
void FboSource::onAppSetup(ofEventArgs &args) { ofRemoveListener(ofEvents().update, this,
ofRemoveListener(ofEvents().setup, this, &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP); &FboSource::onAppUpdate, OF_EVENT_ORDER_BEFORE_APP);
setup(); ofRemoveListener(ofEvents().draw, this,
&FboSource::onAppDraw, OF_EVENT_ORDER_BEFORE_APP);
// Check if FBO was allocated in user defined setup ofRemoveListener(ofEvents().exit, this,
// If not, show warning and alocate to avoid panic &FboSource::onAppExit, OF_EVENT_ORDER_AFTER_APP);
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::onAppSetup(ofEventArgs &args) {
} ofRemoveListener(ofEvents().setup, this,
} &FboSource::onAppSetup, OF_EVENT_ORDER_BEFORE_APP);
setup();
void FboSource::onAppUpdate(ofEventArgs &args) {
if (fbo == NULL || !fbo->isAllocated()) { // Check if FBO was allocated in user defined setup
ofLogWarning("FboSource") << "FBO not allocated"; // If not, show warning and alocate to avoid panic
return; if (!fbo->isAllocated()) {
} ofLogWarning("FboSource::onAppSetup") <<
update(); "FBO not allocated, allocating with default values";
} allocate(PIMAPPER_FBO_SOURCE_DEF_WIDTH,
PIMAPPER_FBO_SOURCE_DEF_HEIGHT);
void FboSource::onAppDraw(ofEventArgs &args) { }
if (fbo == NULL || !fbo->isAllocated()) { }
ofLogWarning("FboSource") << "FBO not allocated";
return; void FboSource::onAppUpdate(ofEventArgs &args) {
} if (fbo == NULL || !fbo->isAllocated()) {
fbo->begin(); ofLogWarning("FboSource") << "FBO not allocated";
draw(); return;
fbo->end(); }
} update();
}
void FboSource::onAppExit(ofEventArgs &args) {
exit(); void FboSource::onAppDraw(ofEventArgs &args) {
} if (fbo == NULL || !fbo->isAllocated()) {
ofLogWarning("FboSource") << "FBO not allocated";
void FboSource::allocate(int width, int height) { return;
clear(); }
fbo = new ofFbo(); fbo->begin();
fbo->allocate(width, height); draw();
fbo->end();
// Clear FBO }
fbo->begin();
ofClear(0); void FboSource::onAppExit(ofEventArgs &args) {
fbo->end(); exit();
}
texture = &(fbo->getTextureReference());
} void FboSource::allocate(int width, int height) {
clear();
void FboSource::clear() { fbo = new ofFbo();
texture = NULL; fbo->allocate(width, height);
if (fbo != NULL) {
delete fbo; // Clear FBO
fbo = NULL; fbo->begin();
} ofClear(0);
} fbo->end();
} // namespace piMapper 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 } // namespace ofx

77
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"
@ -15,40 +8,38 @@ class YourGenerativeSource : public FboSource {
#define PIMAPPER_FBO_SOURCE_DEF_HEIGHT 500 #define PIMAPPER_FBO_SOURCE_DEF_HEIGHT 500
namespace ofx { namespace ofx {
namespace piMapper { namespace piMapper {
class FboSource : public BaseSource {
public: class FboSource : public BaseSource {
FboSource(); public:
~FboSource(); FboSource();
~FboSource();
// Add/remove calls to update and draw
// App listeners are added once the source is assigned to at least one surface // Override these in your custom FBO source
// App listeners are removed once the source is not assigned anywhere virtual void setup() {};
void addAppListeners(); virtual void update() {};
void removeAppListeners(); virtual void draw() {};
virtual void exit() {};
// These are called on app events
void onAppSetup(ofEventArgs& args); // The only method from BaseSource to be overriden
void onAppUpdate(ofEventArgs& args); void clear();
void onAppDraw(ofEventArgs& args);
void onAppExit(ofEventArgs& args); // App listeners
void addAppListeners();
// Override these in your custom FBO source void removeAppListeners();
virtual void setup() {}; // Don't do any drawing here void onAppSetup(ofEventArgs & args);
virtual void update() {}; // Don't do any drawing here void onAppUpdate(ofEventArgs & args);
// You don't need to take care of fbo.begin() and fbo.end() here; void onAppDraw(ofEventArgs & args);
virtual void draw() {}; // But this is the only place where you shoud do drawing void onAppExit(ofEventArgs & args);
virtual void exit() {};
protected:
// The only method from BaseSource to be overriden ofFbo * fbo;
void clear(); void allocate(int width, int height);
protected: // Some handy getters
ofFbo* fbo; int getWidth();
int getHeight();
// 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
};
} // namespace piMapper
} // namespace ofx } // namespace ofx
Loading…
Cancel
Save