From 5acb0e86aa641f16b2477069067d24a4b7d361d0 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Thu, 15 May 2014 15:37:09 +0200 Subject: [PATCH] Add saving surface data to xml file --- example/src/ofApp.cpp | 1 + src/ofxSurfaceManager.cpp | 70 ++++++++++++++++++++++++++++++++++++++- src/ofxSurfaceManager.h | 8 ++++- 3 files changed, 77 insertions(+), 2 deletions(-) diff --git a/example/src/ofApp.cpp b/example/src/ofApp.cpp index 8704343..83228f7 100644 --- a/example/src/ofApp.cpp +++ b/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; } } diff --git a/src/ofxSurfaceManager.cpp b/src/ofxSurfaceManager.cpp index 7d7a167..6293387 100644 --- a/src/ofxSurfaceManager.cpp +++ b/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* vertices = &surface->getVertices(); + for ( int j=0; jsize(); 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* texCoords = &surface->getTexCoords(); + for ( int j=0; jsize(); 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; igetTextureReference()) { return loadedImageSourceNames[i]; diff --git a/src/ofxSurfaceManager.h b/src/ofxSurfaceManager.h index 709d29e..b6516f9 100644 --- a/src/ofxSurfaceManager.h +++ b/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 vertices, vector 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 surfaces; ofxBaseSurface* selectedSurface; vector loadedImageSourceNames; vector loadedImageSources; + ofxXmlSettings xmlSettings; + }; #endif \ No newline at end of file