From 920b3d2a0befb37c61fef398f1881b5d9f880b44 Mon Sep 17 00:00:00 2001 From: Krisjanis Rijnieks Date: Tue, 12 Jan 2016 19:57:34 +0100 Subject: [PATCH] Improve `SettingsLoader` Simplify the `load()` method by moving the quad and triangle surface creation to private methods --- src/Application/SettingsLoader.cpp | 219 +++++++++++++++-------------- src/Application/SettingsLoader.h | 3 + 2 files changed, 118 insertions(+), 104 deletions(-) diff --git a/src/Application/SettingsLoader.cpp b/src/Application/SettingsLoader.cpp index f4a00b4..7599682 100644 --- a/src/Application/SettingsLoader.cpp +++ b/src/Application/SettingsLoader.cpp @@ -54,123 +54,22 @@ bool SettingsLoader::load(SurfaceStack & surfaces, MediaServer & mediaServer, st source = mediaServer.loadMedia(sourcePath, typeEnum); } } + xmlSettings->popTag(); // source xmlSettings->pushTag("vertices"); - vector vertices; int vertexCount = xmlSettings->getNumTags("vertex"); if(vertexCount == 3){ - xmlSettings->pushTag("vertex", 0); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("vertex", 1); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("vertex", 2); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 100.0f))); - xmlSettings->popTag(); - - xmlSettings->popTag(); // vertices - - xmlSettings->pushTag("texCoords"); - - vector texCoords; - - xmlSettings->pushTag("texCoord", 0); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("texCoord", 1); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("texCoord", 2); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 1.0f))); - xmlSettings->popTag(); - - xmlSettings->popTag(); // texCoords - - // Create and add a triangle surface - BaseSurface * triangleSurface = - SurfaceFactory::instance()->createSurface( - SurfaceType::TRIANGLE_SURFACE); - triangleSurface->setVertices(vertices); - triangleSurface->setTexCoords(texCoords); - + BaseSurface * triangleSurface = getTriangleSurface(xmlSettings); if(sourceName != "none" && source != 0){ triangleSurface->setSource(source); } - surfaces.push_back(triangleSurface); - }else if(vertexCount == 4){ - xmlSettings->pushTag("vertex", 0); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("vertex", 1); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("vertex", 2); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), - xmlSettings->getValue("y", 100.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("vertex", 3); - vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 100.0f))); - xmlSettings->popTag(); - - xmlSettings->popTag(); // vertices - - xmlSettings->pushTag("texCoords"); - - vector texCoords; - - xmlSettings->pushTag("texCoord", 0); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("texCoord", 1); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), - xmlSettings->getValue("y", 0.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("texCoord", 2); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), - xmlSettings->getValue("y", 1.0f))); - xmlSettings->popTag(); - - xmlSettings->pushTag("texCoord", 3); - texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), - xmlSettings->getValue("y", 1.0f))); - xmlSettings->popTag(); - - xmlSettings->popTag(); // texCoords - - // Create and add quad surface - BaseSurface * quadSurface = - SurfaceFactory::instance()->createSurface( - SurfaceType::QUAD_SURFACE); - quadSurface->setVertices(vertices); - quadSurface->setTexCoords(texCoords); - + BaseSurface * quadSurface = getQuadSurface(xmlSettings); if(sourceName != "none" && source != 0){ quadSurface->setSource(source); } - surfaces.push_back(quadSurface); } @@ -236,5 +135,117 @@ bool SettingsLoader::save(SurfaceStack & surfaces, string fileName){ xmlSettings->save(fileName); } +BaseSurface * SettingsLoader::getTriangleSurface(ofxXmlSettings * xmlSettings){ + vector vertices; + + xmlSettings->pushTag("vertex", 0); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("vertex", 1); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("vertex", 2); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 100.0f))); + xmlSettings->popTag(); + + xmlSettings->popTag(); // vertices + + xmlSettings->pushTag("texCoords"); + + vector texCoords; + + xmlSettings->pushTag("texCoord", 0); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("texCoord", 1); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("texCoord", 2); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 1.0f))); + xmlSettings->popTag(); + + xmlSettings->popTag(); // texCoords + + // Create and add a triangle surface + BaseSurface * triangleSurface = + SurfaceFactory::instance()->createSurface( + SurfaceType::TRIANGLE_SURFACE); + triangleSurface->setVertices(vertices); + triangleSurface->setTexCoords(texCoords); + + return triangleSurface; +} + +BaseSurface * SettingsLoader::getQuadSurface(ofxXmlSettings * xmlSettings){ + vector vertices; + + xmlSettings->pushTag("vertex", 0); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("vertex", 1); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("vertex", 2); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 100.0f), + xmlSettings->getValue("y", 100.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("vertex", 3); + vertices.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 100.0f))); + xmlSettings->popTag(); + + xmlSettings->popTag(); // vertices + + xmlSettings->pushTag("texCoords"); + + vector texCoords; + + xmlSettings->pushTag("texCoord", 0); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("texCoord", 1); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), + xmlSettings->getValue("y", 0.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("texCoord", 2); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 1.0f), + xmlSettings->getValue("y", 1.0f))); + xmlSettings->popTag(); + + xmlSettings->pushTag("texCoord", 3); + texCoords.push_back(ofVec2f(xmlSettings->getValue("x", 0.0f), + xmlSettings->getValue("y", 1.0f))); + xmlSettings->popTag(); + + xmlSettings->popTag(); // texCoords + + // Create and add quad surface + BaseSurface * quadSurface = + SurfaceFactory::instance()->createSurface( + SurfaceType::QUAD_SURFACE); + quadSurface->setVertices(vertices); + quadSurface->setTexCoords(texCoords); + + return quadSurface; +} + } // namespace piMapper } // namespace ofx \ No newline at end of file diff --git a/src/Application/SettingsLoader.h b/src/Application/SettingsLoader.h index 65b8363..ed51461 100644 --- a/src/Application/SettingsLoader.h +++ b/src/Application/SettingsLoader.h @@ -19,6 +19,9 @@ class SettingsLoader { private: static SettingsLoader * _instance; + + BaseSurface * getTriangleSurface(ofxXmlSettings * xmlSettings); + BaseSurface * getQuadSurface(ofxXmlSettings * xmlSettings); }; }