Browse Source

Add saving surface data to xml file

master
Krisjanis Rijnieks 11 years ago
parent
commit
5acb0e86aa
  1. 1
      example/src/ofApp.cpp
  2. 70
      src/ofxSurfaceManager.cpp
  3. 8
      src/ofxSurfaceManager.h

1
example/src/ofApp.cpp

@ -67,6 +67,7 @@ void ofApp::keyPressed(int key)
case 'i': bShowInfo = !bShowInfo; break;
case 'r': addRandomSurface(); break;
case 'f': ofToggleFullscreen(); break;
case 's': surfaceManager.saveXmlSettings("surfaces.xml"); break;
default: break;
}
}

70
src/ofxSurfaceManager.cpp

@ -124,6 +124,69 @@ void ofxSurfaceManager::clear()
}
}
void ofxSurfaceManager::saveXmlSettings(string fileName)
{
xmlSettings.clear();
// save surfaces
xmlSettings.addTag("surfaces");
xmlSettings.pushTag("surfaces");
for ( int i=0; i<surfaces.size(); i++ ) {
xmlSettings.addTag("surface");
xmlSettings.pushTag("surface", i);
ofxBaseSurface* surface = surfaces[i];
xmlSettings.addTag("vertices");
xmlSettings.pushTag("vertices");
vector<ofVec3f>* vertices = &surface->getVertices();
for ( int j=0; j<vertices->size(); j++ ) {
xmlSettings.addTag("vertex");
xmlSettings.pushTag("vertex", j);
ofVec3f* vertex = &(*vertices)[j];
xmlSettings.addValue("x", vertex->x);
xmlSettings.addValue("y", vertex->y);
// we don't need z as it will be 0 anyways
xmlSettings.popTag(); // vertex
}
xmlSettings.popTag(); // vertices
xmlSettings.addTag("texCoords");
xmlSettings.pushTag("texCoords");
vector<ofVec2f>* texCoords = &surface->getTexCoords();
for ( int j=0; j<texCoords->size(); j++ ) {
xmlSettings.addTag("texCoord");
xmlSettings.pushTag("texCoord", j);
ofVec2f* texCoord = &(*texCoords)[j];
xmlSettings.addValue("x", texCoord->x);
xmlSettings.addValue("y", texCoord->y);
xmlSettings.popTag(); // texCoord
}
xmlSettings.popTag(); // texCoords
xmlSettings.addTag("source");
xmlSettings.pushTag("source");
xmlSettings.addValue("source-type", "image");
xmlSettings.addValue("source-name", getSurfaceSourceName(surface));
//xmlSettings.addValue("source-path", "/root/etc/image.jpg");
xmlSettings.popTag(); // source
xmlSettings.popTag(); // surface
}
xmlSettings.popTag(); // surfaces
xmlSettings.save(fileName);
}
void ofxSurfaceManager::loadXmlSettings(string fileName)
{
}
ofxBaseSurface* ofxSurfaceManager::selectSurface(int index)
{
if ( index >= surfaces.size() ) {
@ -167,7 +230,12 @@ string ofxSurfaceManager::getSelectedSurfaceSourceName()
return "none";
}
ofTexture* tex = selectedSurface->getTexture();
return getSurfaceSourceName( selectedSurface );
}
string ofxSurfaceManager::getSurfaceSourceName(ofxBaseSurface *surface)
{
ofTexture* tex = surface->getTexture();
for ( int i=0; i<loadedImageSources.size(); i++ ) {
if (tex == &loadedImageSources[i]->getTextureReference()) {
return loadedImageSourceNames[i];

8
src/ofxSurfaceManager.h

@ -5,6 +5,7 @@
#include "ofxTriangleSurface.h"
#include "ofxSurfaceType.h"
#include "ofEvents.h"
#include "ofxXmlSettings.h"
using namespace std;
@ -21,6 +22,9 @@ public:
void addSurface(int surfaceType, ofTexture* texturePtr, vector<ofVec2f> vertices, vector<ofVec2f> texCoords);
void manageMemory(); // deletes unasigned sources
void clear();
void saveXmlSettings(string fileName);
void loadXmlSettings(string fileName);
ofxBaseSurface* getSurface(int index);
int size();
ofxBaseSurface* selectSurface(int index);
@ -28,13 +32,15 @@ public:
void deselectSurface();
ofTexture* loadImageSource(string name, string path);
string getSelectedSurfaceSourceName();
string getSurfaceSourceName( ofxBaseSurface* surface );
private:
vector<ofxBaseSurface*> surfaces;
ofxBaseSurface* selectedSurface;
vector<string> loadedImageSourceNames;
vector<ofImage*> loadedImageSources;
ofxXmlSettings xmlSettings;
};
#endif
Loading…
Cancel
Save