From af146d001f0cc1cb72cda5c3bbd6e07fd3d8d939 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Tue, 13 May 2014 19:37:17 +0200 Subject: [PATCH] Add basic memory management, unloading surfaces that are not assigned --- src/ofxSourcesEditor.cpp | 35 +++++++++---------- src/ofxSourcesEditor.h | 3 ++ src/ofxSurfaceManager.cpp | 68 +++++++++++++++++++++++++++++++++--- src/ofxSurfaceManager.h | 6 ++++ src/ofxSurfaceManagerGui.cpp | 16 +-------- src/ofxSurfaceManagerGui.h | 3 +- 6 files changed, 91 insertions(+), 40 deletions(-) diff --git a/src/ofxSourcesEditor.cpp b/src/ofxSourcesEditor.cpp index 7ba604b..9c81f11 100644 --- a/src/ofxSourcesEditor.cpp +++ b/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; igetSelectedSurface() == 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(); } \ No newline at end of file diff --git a/src/ofxSourcesEditor.h b/src/ofxSourcesEditor.h index 8a385bd..efdabc3 100644 --- a/src/ofxSourcesEditor.h +++ b/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); diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp index 85dd28b..60326d1 100644 --- a/src/ofxSurfaceManager.cpp +++ b/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; igetTexture() == &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; igetTextureReference(); + } + } + + // 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(); } + + diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h index e5ebca3..5e136f6 100644 --- a/src/ofxSurfaceManager.h +++ b/src/ofxSurfaceManager.h @@ -19,15 +19,21 @@ public: void addSurface(int surfaceType, ofTexture* texturePtr); void addSurface(int surfaceType, vector vertices, vector texCoords); void addSurface(int surfaceType, ofTexture* texturePtr, vector vertices, vector 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 surfaces; ofxBaseSurface* selectedSurface; + vector loadedImageSourceNames; + vector loadedImageSources; }; #endif \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.cpp b/src/ofxSurfaceManagerGui.cpp index 766f55b..5e99b19 100644 --- a/src/ofxSurfaceManagerGui.cpp +++ b/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) ); - } } \ No newline at end of file diff --git a/src/ofxSurfaceManagerGui.h b/src/ofxSurfaceManagerGui.h index 483255b..605d8a1 100644 --- a/src/ofxSurfaceManagerGui.h +++ b/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 \ No newline at end of file