Embedded Code in Text for DrawFont Wrapper
This doesn't quite slot in anywhere specificly in terms of graphics engine code but I am trying to put together a new wrapper for drawing text. In creating a wholesome wrapper for this I am trying to bypass having alot of lines for the rendering function to display the process of rendering one thing by creating more elaborate and intelligent wrappers.
StreamForCompilingText.str("");
StreamForCompilingText << "FPS: " << TimeDisplayObject[0].fps << ".";
std::string s3(StreamForCompilingText.str());
length = s3.length();
DrawFont(s3.c_str(), length, DT_RIGHT | DT_TOP, 255, 255, 255, 255);
There is my current programs code. 5 Lines for one Draw, thats not good additionally for every display of the font I am creating a unique variable of length and a unique string number because they dont get broken down till render ends. Assuming I throw the code that it would write out and the objects needed are passed to my DrawFont function in the future is there a way to get the below, "bulk" pass of text method to work?
DrawFont("<< \"FPS: \" << TimeDisplayObject[0].fps << \".\"", 0, DT_LEFT | DT_BOTTOM, 200, 255, 0, 0);
The '\' should ensure that the " is treated as a quote it works great. In my remake of my wrapper however it isn't turning out to be easy to undo this process. I cant simply remove the \'s from the string using a loop because they dont appear visibly.
Draw font takes the string as a LPCSTR.
This in turn is what I see when I hover over the text string.
<< "FPS: " << TimeDisplayObject[0].fps << "."
which is exactly what I want! only I want the program to take this command as a literal piece of embedded code in text passed to my function and throw it into my string as the value returned from the TimeDisplayObject, so that the FPS counter displays the FPS to the screen as it does in the 5 line method. Is there any possible way to shatter my string into being processed as if it were just code? I can imagine there being some sort of compiler feasability problem with this concept so feel free to tell me if im aiming for the impossible thats what I want to find out!
std::ostringstream StreamForCompilingText;
StreamForCompilingText << myLPCTSTR;
std::string s1(StreamForCompilingText.str());
int lengths = s1.length();
If I can make the above work correctly anything I pass The DrawFont function object wise should be displayable and my code can go back to being neatened up as a one line can let me render anything with sprites and text dealy which is what I want oh so much the more easy for me I make the wrappers for fonts and sprites the less difficult its going to be to manage them when I start dealing with building wrappers for 3d textured models and the better I am going to know the limiations of such wrappers and in theory the less confusing its all going to be when I start loading in animated models with my own wrapper :p
oh yeah ignore the other passed variables to drawfont they wont appear sensical and the wrapper is incomplete even if I get this part working I still need to get a sprite of my own for the text to draw to efficiently along with clever coordinates for said displaying of sprite so I guess its going to be a two line process anyway to draw a sprite then font to the sprite, still this bits important no matter how I put together the rest!
[Edited by - EnlightenedOne on July 27, 2009 11:12:26 AM]
[Edited by - EnlightenedOne on July 27, 2009 11:12:26 AM]
In a word, no. You can't pass a string literal containing << operators to a stringstream at runtime and have it parse it automatically.
If you really want to go mad, you can do something like this:
That's not tested but it is a fairly common C++ trick of creating a temporary object whose destructor flushes the stream when the temporary instance dies at the end of the expression.
If you really want to go mad, you can do something like this:
class FontStream{private: std::ostringstream Os; DWORD Flags; int A,R,G,B;public: FontStream(DWORD F,int A,int R,int G,int B) : Flags(F),A(A),R(R),G(G),B(B) { } ~FontStream(){ DrawFont(Os.str().c_str(),Os.str().length(),Flags,A,R,G,B); } template<class T> FontStream &operator<<(const T &t){ Os << t; return *this; }};void f(){ FontStream(DT_LEFT|DT_BOTTOM,255,255,255,255) << "FPS:" << TimeDisplayObject[0].fps << ".";}
That's not tested but it is a fairly common C++ trick of creating a temporary object whose destructor flushes the stream when the temporary instance dies at the end of the expression.
I didn't think there would be a way to resolve that string issue im sure its come up before with me too :p. Thats a very intelligent solution it had not even crossed my mind to use a class as a means of calling a function and having the classes temporary values do all the string business neatly for me, I wouldnt have guessed you could apply the "<<" to an objects constuctor but I guess if you have a template of what to do it can't really go too wrong. I will definetely have a go at implementing this into my code. Thank you!
Im attempting to implement the class desconstructor function call trick however I have my Direct3D functions kept seperate from my Rendering process. I have a Global.h which enables functions from different .cpp files to interact provided I place a prototype into Global.
When it comes to this however in my rendering file.
FontStream(DT_LEFT|DT_BOTTOM,200,255,0,0) << "FPS:" << TimeDisplayObject[0].fps << ".";
I am at a loss as how to prototype that function call. In global.h I have the line.
void FontStream(DWORD F,int A,int R,int G,int B);
And in my Direct3D file I have the actual function.
I am guessing that I need to add something to the end of the prototype but I am at a loss as of what to put there. I have never seen this trick before what do you call a class constructor function you call with '<<' after the functions parameters? I am alittle out of my depth when it comes to this can you show me the correct method of creating a prototype when throwing parameters at a constructor?
[Edited by - EnlightenedOne on July 27, 2009 11:40:51 AM]
When it comes to this however in my rendering file.
FontStream(DT_LEFT|DT_BOTTOM,200,255,0,0) << "FPS:" << TimeDisplayObject[0].fps << ".";
I am at a loss as how to prototype that function call. In global.h I have the line.
void FontStream(DWORD F,int A,int R,int G,int B);
And in my Direct3D file I have the actual function.
I am guessing that I need to add something to the end of the prototype but I am at a loss as of what to put there. I have never seen this trick before what do you call a class constructor function you call with '<<' after the functions parameters? I am alittle out of my depth when it comes to this can you show me the correct method of creating a prototype when throwing parameters at a constructor?
[Edited by - EnlightenedOne on July 27, 2009 11:40:51 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement