AggrorJorn Posted August 15, 2013 Share Posted August 15, 2013 The current API has these nice String converters. //Leadwerks.h std::string IntToString( int i ); std::string String( double f ); std::string String( int i ); std::string String( unsigned int i ); std::string String( long l ); std::string String( const unsigned long l ); I use these Vector2, Vector3 and Vector4 converters myself. They might be useful for the API to be there: //Header std::string String(Leadwerks::Vec2 vector); std::string String(Leadwerks::Vec3 vector); std::string String(Leadwerks::Vec4 vector); //CPP std::string String(Vec2 v) { return "X: " + Leadwerks::String(v.x) + " Y: " + Leadwerks::String(v.y); } std::string String(Vec3 v) { return "X: " + Leadwerks::String(v.x) + " Y: " + Leadwerks::String(v.y) + " Z: " + Leadwerks::String(v.z); } std::string String(Vec4 v) { return "X: " + Leadwerks::String(v.x) + " Y: " + Leadwerks::String(v.y) + " Z: " + Leadwerks::String(v.z) + " W: " + Leadwerks::String(v.w); } Quote Link to comment Share on other sites More sharing options...
Josh Posted August 16, 2013 Share Posted August 16, 2013 There's also a ToString() method you can use on all objects. Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 16, 2013 Author Share Posted August 16, 2013 Sweet, didn't know that. Quote Link to comment Share on other sites More sharing options...
Mumbles Posted August 16, 2013 Share Posted August 16, 2013 Copying and pasting similar functions like this can lead to problems, particularly later when they are more complex. You'd do better to roll them all into a single function, particularly if you want to do more complicated things with your vectors later. This is a general programming thing //Header (Kept the same) std::string String(Leadwerks::Vec2 vector); std::string String(Leadwerks::Vec3 vector); std::string String(Leadwerks::Vec4 vector); //CPP (New function added at top. It's not declared in the header, so it's invisible outside this source file) std::string VectorToString(vec4 v, int elementCount) { std::string outputString = ""; char letters[] = {'X','Y','Z','W'}; for(unsigned int element = 0; element < elementCount; element += 1) { outputString += letters[element]; outputString += ": "; outputString += Leadwerks::String(v.a[element]); //I'm hoping the member 'a' was carried over from LE2's vectors, because in for-loops (like this) is where it really shines if(element != (elementCount - 1)) outputString += ", "; //If it's not the last element, put a comma after it. //Single line if statement, code below (if any) executes as part of the for-loop as normal } return outputString; } //Now, this is the only function that manipulates vectors into strings. If you notice a mistake, or want to change the formatting for example, you only rewrite this function, rather than 3 separate functions, Where you might forget one. Or you might typo in one, but not the other two... std::string String(Vec2 v) { vec4 tempVec; tempVec.x = v.x; tempVec.y = v.y; //We don't care about z or w... The VectorToString function will ignore them because we have passed the elementCount parameter as 2. return VectorToString(tempVec,2); } std::string String(Vec3 v) { vec4 tempVec; tempVec.x = v.x; tempVec.y = v.y; tempVec.z = v.z; return VectorToString(tempVec,3); } std::string String(Vec4 v) { return VectorToString(v,4); //We already have a Vec4, no need to create a new one just for purpose } As you see: Don't like the comma between the vector components? Take it out. Want to put brackets around the whole vector? Do that on either side of the for loop. Whatever change you want to make, you just make the change to that one function. You don't have to copy the change to the others as well. Copy and paste errors are one of the most frequent you'll come across. Try not to get into the habit of copy and pasting for small (but slightly different) functions, as projects get bigger, they begin to cause you no end of hell... 1 Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
AggrorJorn Posted August 16, 2013 Author Share Posted August 16, 2013 Thanks for the feedback Mumbles. You are absolutely right. This is far better programming. Quote Link to comment Share on other sites More sharing options...
Mumbles Posted August 16, 2013 Share Posted August 16, 2013 Thinking about it, it's probably a better idea to have the "big" function using a Vec16 instead. I assume Vec9 and Vec16 still exist? The only difference would be that if there are more than 4 elements you may wish to print an index number rather than a letter. Also, in case of a Vec9, you may want a new line every 3 elements, and for a Vec16, every 4 elements, just to make it more readable. Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.