Browse Source

Add basic memory management, unloading surfaces that are not assigned

master
Krisjanis Rijnieks 11 years ago
parent
commit
af146d001f
  1. 35
      src/ofxSourcesEditor.cpp
  2. 3
      src/ofxSourcesEditor.h
  3. 68
      src/ofxSurfaceManager.cpp
  4. 6
      src/ofxSurfaceManager.h
  5. 16
      src/ofxSurfaceManagerGui.cpp
  6. 3
      src/ofxSurfaceManagerGui.h

35
src/ofxSourcesEditor.cpp

@ -45,7 +45,7 @@ void ofxSourcesEditor::setup(ofEventArgs& args)
}
gui->addLabel(defImgDir, OFX_UI_FONT_SMALL);
ofxUIRadio *radio = gui->addRadio("VR", vnames, OFX_UI_ORIENTATION_VERTICAL);
ofxUIRadio *radio = gui->addRadio("images", vnames, OFX_UI_ORIENTATION_VERTICAL);
radio->activateToggle("image0.png");
ofAddListener(gui->newGUIEvent,this,&ofxSourcesEditor::guiEvent);
@ -76,6 +76,11 @@ void ofxSourcesEditor::enable()
gui->enable();
}
void ofxSourcesEditor::setSurfaceManager(ofxSurfaceManager *newSurfaceManager)
{
surfaceManager = newSurfaceManager;
}
int ofxSourcesEditor::getLoadedTexCount()
{
return images.size();
@ -100,26 +105,18 @@ void ofxSourcesEditor::guiEvent(ofxUIEventArgs &e)
cout << name << "\t value: " << toggle->getValue() << endl;
}
// search for matching loaded image name
for ( int i=0; i<images.size(); i++ ) {
cout << "loaded: " << imageNames[i] << endl;
if ( imageNames[i] == name ) {
// image already loaded
ofImage* img = images[i];
images.erase(images.begin()+i);
images.push_back(img);
string imgName = imageNames[i];
imageNames.erase(imageNames.begin()+i);
imageNames.push_back(imgName);
cout << "image already loaded" << endl;
ofSendMessage("imageLoaded");
return;
}
if ( surfaceManager->getSelectedSurface() == NULL ) {
return;
}
if (name == "images") {
return;
}
stringstream ss;
ss << defImgDir << name;
loadImage(name, ss.str());
cout << "attempt to load image: " << ss.str() << endl;
ofTexture* texture = surfaceManager->loadImageSource(name, ss.str());
surfaceManager->getSelectedSurface()->setTexture(texture);
surfaceManager->manageMemory();
}

3
src/ofxSourcesEditor.h

@ -4,6 +4,7 @@
#include "ofGraphics.h"
#include "ofEvents.h"
#include "ofxUI.h"
#include "ofxSurfaceManager.h"
#define DEFAULT_IMAGES_DIR "sources/images/";
@ -21,11 +22,13 @@ public:
void loadImage( string name, string path );
void disable();
void enable();
void setSurfaceManager(ofxSurfaceManager* newSurfaceManager);
int getLoadedTexCount();
ofTexture* getTexture(int index);
private:
ofxSurfaceManager* surfaceManager;
string defImgDir;
ofxUICanvas *gui;
void guiEvent(ofxUIEventArgs &e);

68
src/ofxSurfaceManager.cpp

@ -7,11 +7,7 @@ ofxSurfaceManager::ofxSurfaceManager()
ofxSurfaceManager::~ofxSurfaceManager()
{
// delete all extra allocations from the heap
while ( surfaces.size() ) {
delete surfaces.back();
surfaces.pop_back();
}
clear();
}
void ofxSurfaceManager::draw()
@ -86,6 +82,48 @@ void ofxSurfaceManager::addSurface(int surfaceType, ofTexture* texturePtr, vecto
}
}
void ofxSurfaceManager::manageMemory()
{
// check if each of the sources is assigned to a surface or not
for ( int i=0; i<loadedImageSources.size(); i++ ) {
bool bAssigned = false;
for ( int j=0; j<surfaces.size(); j++ ) {
if ( surfaces[j]->getTexture() == &loadedImageSources[i]->getTextureReference() ) {
bAssigned = true;
break;
}
}
if ( !bAssigned ) {
// purge the image source from memory
delete loadedImageSources[i];
loadedImageSources.erase(loadedImageSources.begin()+i);
cout << "Deleting image source: " << loadedImageSourceNames[i] << endl;
loadedImageSourceNames.erase(loadedImageSourceNames.begin()+i);
i--;
}
}
}
void ofxSurfaceManager::clear()
{
// delete all extra allocations from the heap
while ( surfaces.size() ) {
delete surfaces.back();
surfaces.pop_back();
}
while ( loadedImageSources.size() ) {
delete loadedImageSources.back();
loadedImageSources.pop_back();
}
while ( loadedImageSourceNames.size() ) {
loadedImageSourceNames.pop_back();
}
}
ofxBaseSurface* ofxSurfaceManager::selectSurface(int index)
{
if ( index >= surfaces.size() ) {
@ -105,6 +143,24 @@ void ofxSurfaceManager::deselectSurface()
selectedSurface = NULL;
}
ofTexture* ofxSurfaceManager::loadImageSource(string name, string path)
{
// check if it is loaded
for ( int i=0; i<loadedImageSourceNames.size(); i++ ) {
if ( loadedImageSourceNames[i] == name ) {
// this image is already loaded
return &loadedImageSources[i]->getTextureReference();
}
}
// not loaded - load
ofImage* image = new ofImage();
image->loadImage(path);
loadedImageSources.push_back(image);
loadedImageSourceNames.push_back(name);
return &image->getTextureReference();
}
ofxBaseSurface* ofxSurfaceManager::getSurface(int index)
{
if ( index >= surfaces.size() ) {
@ -120,3 +176,5 @@ int ofxSurfaceManager::size()
return surfaces.size();
}

6
src/ofxSurfaceManager.h

@ -19,15 +19,21 @@ public:
void addSurface(int surfaceType, ofTexture* texturePtr);
void addSurface(int surfaceType, vector<ofVec2f> vertices, vector<ofVec2f> texCoords);
void addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords);
void manageMemory(); // deletes unasigned sources
void clear();
ofxBaseSurface* getSurface(int index);
int size();
ofxBaseSurface* selectSurface(int index);
ofxBaseSurface* getSelectedSurface();
void deselectSurface();
ofTexture* loadImageSource(string name, string path);
private:
vector<ofxBaseSurface*> surfaces;
ofxBaseSurface* selectedSurface;
vector<string> loadedImageSourceNames;
vector<ofImage*> loadedImageSources;
};
#endif

16
src/ofxSurfaceManagerGui.cpp

@ -6,13 +6,11 @@ ofxSurfaceManagerGui::ofxSurfaceManagerGui()
guiMode = ofxGuiMode::NONE;
bDrag = false;
registerMouseEvents();
ofRegisterGetMessages(this);
}
ofxSurfaceManagerGui::~ofxSurfaceManagerGui()
{
unregisterMouseEvents();
ofUnregisterGetMessages(this);
surfaceManager = NULL;
}
@ -168,6 +166,7 @@ void ofxSurfaceManagerGui::setSurfaceManager(ofxSurfaceManager* newSurfaceManage
{
surfaceManager = newSurfaceManager;
projectionEditor.setSurfaceManager( surfaceManager );
sourcesEditor.setSurfaceManager( surfaceManager );
}
void ofxSurfaceManagerGui::setMode(int newGuiMode)
@ -228,17 +227,4 @@ void ofxSurfaceManagerGui::startDrag()
void ofxSurfaceManagerGui::stopDrag()
{
bDrag = false;
}
void ofxSurfaceManagerGui::gotMessage(ofMessage& msg)
{
cout << msg.message << endl;
if ( msg.message == "imageLoaded" ) {
// assign texture to selected source
if (surfaceManager->getSelectedSurface() == NULL){
return;
}
surfaceManager->getSelectedSurface()->setTexture( sourcesEditor.getTexture(sourcesEditor.getLoadedTexCount()-1) );
}
}

3
src/ofxSurfaceManagerGui.h

@ -10,6 +10,7 @@
#include "ofxProjectionEditor.h"
#include "ofxSourcesEditor.h"
#include "ofxGuiMode.h"
#include "ofGraphics.h"
class ofxSurfaceManagerGui
{
@ -30,7 +31,6 @@ public:
void drawSelectedSurfaceTextureHighlight();
void startDrag();
void stopDrag();
void gotMessage(ofMessage& msg);
private:
ofxSurfaceManager* surfaceManager;
@ -40,6 +40,7 @@ private:
int guiMode;
bool bDrag;
ofVec2f clickPosition;
};
#endif
Loading…
Cancel
Save