0

I'm a beginner in programmation and I've been having trouble for a function called by SDL_TTF:

SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg);

This allows to create a surface with text in it, but my problem is that I need to send numbers to the second parameter. The second parameter only accepts char variables, while my variable is an int...

Is there any way to bypass that by stocking the int number into a char variable... or a way to type the second parameter so that it accepts int variables? huh..

  • 1
    Possible duplicate of [Convert int to string in standard C](http://stackoverflow.com/questions/36274902/convert-int-to-string-in-standard-c) – Retired Ninja Dec 14 '16 at 02:01

1 Answers1

0

In C there is no automatic conversion of integer (or other numeric formats) to text, you have to do it explicitly.

For example, you can write a wrapper function that does the conversion and calls the SDL library call:

#include <stdlib.h>

SDL_Surface* MyRenderInteger_Solid(TTF_Font* font, int value, SDL_Color fg)
{
   char numBuffer[20];
   itoa(value, numBuffer, 10); /* convert value to decimal string */
   return TTF_RenderText_Solid(font, numBuffer, fg);
}

Note that the "#include" should be at the top of the file, not just before you define the function (unless your function is also toward the top of the source file).

Daniel
  • 81
  • 7
  • "itoa()" is listed as a non-standard function, but few C runtime libraries are without it. You can shorten the code by using the return value of "itoa()" directly in the call to "TTF_RenderText_Solid()", since it returns the address of the buffer containing the numeric string. – Daniel Dec 14 '16 at 02:42
  • If you can't find "itoa()" in your library, you can replace it in the example with: `snprintf(numBuffer, sizeof numBuffer, "%d", value);` – Daniel Dec 14 '16 at 02:44