TTF_RenderText_Solid using const char?

Im using SDL_TTF and it is working fine until I try to use a variable for the text parameter. A std:string / char gives convert to const char error. What’s up with it wanting to use a const. What should I pass to it?

Code:

char text = ‘m’;
message = TTF_RenderText_Solid(Font, text, color);

Second argument to ant TTF_Render functions has type const char *. If you want to use std::strings which are of course then you must do this:

std::string str(“some text”);

surface = TTF_RenderText_Solid(Font, str.c_str(), color);

I knew this would be next question :wink:

simplest solution that works most of the time:

template
std::string toString(const T& t)
{
std::ostringstream sstr;

sstr << t;

return sstr.str();
}

then later in code

std::string fps("fps: ");

fps.append(toString(fpsValue));

TTF_RenderText_Solid(Font, fps.c_str(), color);

and voila!

Also, I think you need bit more C/C++ learning before digging into other topics such as graphics / game programming. Passing correct arguments to functions and string conversions are pretty basic stuff.

I cant believe what a dinosaur c++ is. Basically none of the convert int to string methods I found work with this.

Lack of programming knowledge…On Wed, Feb 10, 2010 at 7:59 PM, DIProgan wrote:

I cant believe what a dinosaur c++ is. Basically none of the convert int
to string methods I found work with this.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

No kidding. It’s not even a string you’re trying to convert to here,
it’s a null terminated array of characters. Something like this:

char buffer[256];
sprintf(buffer, “sometext %d %f”, someInteger, someFloat);
message = TTF_RenderText_Solid(Font, buffer, color);

works fine. The “const” bit only refers to the function (in this case,
TTF_RenderText_Solid) not being allowed to change the value.On Thu, Feb 11, 2010 at 1:46 AM, Paulo Pinto wrote:

Lack of programming knowledge…

On Wed, Feb 10, 2010 at 7:59 PM, DIProgan wrote:

I cant believe what a dinosaur c++ is. Basically none of the convert int
to string methods I found work with this.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Obviously you can blame lack of knowledge at any time a programming attempt fails and there’s always a balance between user friendly and modifiability but to me at this specific juncture where other languages do the same in one line I feel like dropping dinosaur. No need to tell me im lacking why else would I be here. Now lets drop this.

Paulo Pinto wrote:> Lack of programming knowledge…

On Wed, Feb 10, 2010 at 7:59 PM, DIProgan <@DIProgan (@DIProgan)> wrote:

  I cant believe what a dinosaur c++ is. Basically none of the convert int to string methods I found work with this.

SDL mailing list
SDL at lists.libsdl.org (SDL at lists.libsdl.org)
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org (http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

He’s just pointing out that this is something that every C/C++ programmer
comes across fairly early. This is due to C’s strong type system. You
can do this in one line, just like in other languages, by writing a
function that does it. Many other languages do make it easier, so don’t
feel trapped with C++ (just wait until you’re really slinging pointers).
You can call functions from the SDL C library from just about any other
language and there are pre-existing bindings for many of them.

Jonny DOn Thu, Feb 11, 2010 at 10:41 AM, DIProgan wrote:

Obviously you can blame lack of knowledge at any time a programming
attempt fails and there’s always a balance between user friendly and
modifiability but to me at this specific juncture where other languages do
the same in one line I feel like dropping dinosaur. No need to tell me im
lacking why else would I be here. Now lets drop this.

Paulo Pinto wrote:

Lack of programming knowledge…

On Wed, Feb 10, 2010 at 7:59 PM, DIProgan <> wrote:

Quote:

I cant believe what a dinosaur c++ is. Basically none of the convert int
to string methods I found work with this.


SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

hardcoder wrote:

simplest solution that works most of the time:

Thanks worked.