Browse Source
Additionally refactor the code according to oF guidelines. To be done for the rest of the code.master
2 changed files with 143 additions and 125 deletions
@ -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
|
Loading…
Reference in new issue