The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
hi all,

i'm trying to figure out how I can render a given char* as centered text

i use this call currently :

SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(msg_font, msg, msg_color, screen_width);

in which msg_font is ptr to a TTF_Font object, msg is a char* ptr, msg_color is a ptr to an SDL_Color object and screen_width is calculated as the width of the current SDL window


msg is usually 3 to 6 lines of text, generated from internal code (so NOT read from an RTF file or something like that)

I'm trying to figure out how I can make the text-align set to centered`


any help would be greatly appreciated !
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Bob Rubbens
Guest

Just set screen_width to a portion of your screen width, and place the resulting surface more to the right? Or generate each line of the text seperately (using just TTF_RenderText_Blended()) and align it yourself? Op 27 mrt. 2015 13:33 schreef "jeroen clarysse":
Quote:
hi all,

i'm trying to figure out how I can render a given char* as centered text

i use this call currently :

        SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(msg_font, msg, msg_color, screen_width);

in which msg_font is ptr to a TTF_Font object, msg is a char* ptr, msg_color is a ptr to an SDL_Color object and screen_width is calculated as the width of the current SDL window


msg is usually 3 to 6 lines of text, generated from internal code (so NOT read from an RTF file or something like that)

I'm trying to figure out how I can make the text-align set to centered`


any help would be greatly appreciated !
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
the first suggestion will still keep all text left-aligned, just on a smaller width Sad

the 2nd suggestion would require the feature to calculate the width of each line, so I can position them properly


I was hoping there was a way to do this via SDL_RTF, but that library seems not to be SDL2 compatble (and contains no XCode nor VisualC project file... which I need both)


thanks for the reply anyway !




Quote:
Just set screen_width to a portion of your screen width, and place the resulting surface more to the right? Or generate each line of the text seperately (using just TTF_RenderText_Blended()) and align it yourself?

Op 27 mrt. 2015 13:33 schreef "jeroen clarysse":
hi all,

i'm trying to figure out how I can render a given char* as centered text

i use this call currently :

SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(msg_font, msg, msg_color, screen_width);

in which msg_font is ptr to a TTF_Font object, msg is a char* ptr, msg_color is a ptr to an SDL_Color object and screen_width is calculated as the width of the current SDL window


msg is usually 3 to 6 lines of text, generated from internal code (so NOT read from an RTF file or something like that)

I'm trying to figure out how I can make the text-align set to centered`


any help would be greatly appreciated !
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Bob Rubbens
Guest

With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.) Op 27 mrt. 2015 13:59 schreef "jeroen clarysse":
Quote:
the first suggestion will still keep all text left-aligned, just on a smaller width Sad

the 2nd suggestion would require the feature to calculate the width of each line, so I can position them properly


I was hoping there was a way to do this via SDL_RTF, but that library seems not to be SDL2 compatble (and contains no XCode nor VisualC project file... which I need both)


thanks for the reply anyway !




Quote:
Just set screen_width to a portion of your screen width, and place the resulting surface more to the right? Or generate each line of the text seperately (using just TTF_RenderText_Blended()) and align it yourself?

Op 27 mrt. 2015 13:33 schreef "jeroen clarysse":
hi all,

i'm trying to figure out how I can render a given char* as centered text

i use this call currently :

         SDL_Surface *msg1Sur = TTF_RenderUTF8_Blended_Wrapped(msg_font, msg, msg_color, screen_width);

in which msg_font is ptr to a TTF_Font object, msg is a char* ptr, msg_color is a ptr to an SDL_Color object and screen_width is calculated as the width of the current SDL window


msg is usually 3 to 6 lines of text, generated from internal code (so NOT read from an RTF file or something like that)

I'm trying to figure out how I can make the text-align set to centered`


any help would be greatly appreciated !
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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


Joined: 03 Jul 2014
Posts: 158
Do you want your text to appear like this: http://tinypic.com/view.php?pic=2j1n76c&s=8#.VRVXXuHn5Ms ?
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
sorry for the late reply !

indeed, that is what I want to achieve ! When each line is a "single" line of thext (= no wrapped text), this is simple. However if the text is a big block that gets wrapped over multiple lines, I don't know how to measure each broken-line individually and then shift it to the right



Quote:

Do you want your text to appear like this: http://tinypic.com/view.php?pic=2j1n76c&s=8#.VRVXXuHn5Ms ?
_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Jonny D


Joined: 12 Sep 2009
Posts: 932
You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):https://github.com/grimfang4/nfont



It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.


Jonny D



On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
> With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list
[url=javascript:;][/url]
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Re: rendering centered text
Naith


Joined: 03 Jul 2014
Posts: 158
jeroen.clarysse wrote:
indeed, that is what I want to achieve ! When each line is a "single" line of text (= no wrapped text), this is simple. However if the text is a big block that gets wrapped over multiple lines, I don't know how to measure each broken-line individually and then shift it to the right

I made a little function that you can use to render multiple lines of text with some options. I know it's not the most efficient code (checking each character in the string and doing this: TextLine += rText[i]) but until I, you, or someone else comes up with a better solution, you can use it if you want. I wrote a function description above it so you know how to use it. The code for the function is below:

Code:

/**
* Render a multi-line text
* \param pFont                  The font to render the text with
* \param rText                  The text to render
* \param rTextColor             The text color
* \param YPosition              The vertical position of the first line of text
* \param DistanceBetweenLines   The distance between each line of text
* \param CenteredInWindow       A bool specifying if the text lines should be centered (horizontal) in the window or not (defaults to true)
* \param XPosition              The horizontal position of the text lines (defaults to 0. Only used if CenteredInWindow is set to false)
* \note                         Create a new line by inserting the \n command in the string. The string needs to end with a \n command for the last line of text to be rendered
*/
void RenderMultiLineText(const TTF_Font* pFont, const std::string& rText, const SDL_Color& rTextColor, const int YPosition, const int DistanceBetweenLines, const bool CenteredInWindow = true, const int XPosition = 0)
{
   // Make sure that the string contains at least 1 character
   if(!rText.empty())
   {
      SDL_Surface* pSurface = NULL;
      SDL_Texture* pTexture = NULL;

      const int   Length      = rText.length();
      int         CurrentLine = 0;

      // This string will contain one line of text
      std::string TextLine = "";

      for(int i = 0; i < Length; ++i)
      {
         // Create the text line as long as the current character is not a \n command
         if(rText[i] != '\n')
            TextLine += rText[i];

         // Since the current character is now a \n command, it's time to create the texture containing the current text line, render it, clear the created text string and then start on a new line
         else
         {
            if(pFont)
               pSurface = TTF_RenderText_Solid((TTF_Font*)pFont, TextLine.c_str(), rTextColor);

            if(pSurface)
            {
               pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pSurface);

               if(pTexture)
               {
                  const int TextWidth    = pSurface->w;
                  const int TextHeight   = pSurface->h;

                  SDL_Rect PositionQuad = {0, 0, 0, 0};

                  // Position the quad centered (horizontal) in the window
                  if(CenteredInWindow)
                     PositionQuad.x = (WINDOW_WIDTH / 2) - (TextWidth / 2);

                  // Position the quad according to the XPosition parameter
                  else
                     PositionQuad.x = XPosition;
                  
                  PositionQuad.y = YPosition + (DistanceBetweenLines * CurrentLine);
                  PositionQuad.w = TextWidth;
                  PositionQuad.h = TextHeight;

                  SDL_RenderCopy(g_pRenderer, pTexture, NULL, &PositionQuad);
                  
                  // Avoid memory leak
                  SDL_DestroyTexture(pTexture);
                  pTexture = NULL;
               }

               // Avoid memory leak
               SDL_FreeSurface(pSurface);
               pSurface = NULL;
            }
            
            // The current line of text has now been rendered (if the texture was successfully created and so on) and the text line string now needs to be cleared
            TextLine = "";

            // Time for a new line
            ++CurrentLine;
         }
      }
   }
}


The function should be called in your render loop and is called in this way:

Code:

   // Render some black multi-line text, centered (horizontal) in the window
   RenderMultiLineText(g_pFont, "This is a text line\nThis is the next text line\nThis is another text line\n", g_Black, 200, 20);
   
   // Render some red multi-line text, not centered in the window but instead positioned at 100 pixels in, from the left
   RenderMultiLineText(g_pFont, "Some random text\nMore random text\nEven more random text\n", g_Red, 400, 25, false, 100);

rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
it is a nice routine indeed, but it suffers from the drawback I described : you can not use wrapping...


Quote:
On 30 Mar 2015, at 00:07, Naith wrote:








jeroen.clarysse wrote:
indeed, that is what I want to achieve ! When each line is a "single" line of text (= no wrapped text), this is simple. However if the text is a big block that gets wrapped over multiple lines, I don't know how to measure each broken-line individually and then shift it to the right

I made a little function that you can use to render multiple lines of text with some options. I know it's not the most efficient code (checking each character in the string and doing this: TextLine += rText[i]) but until I, you, or someone else comes up with a better solution, you can use it if you want. I wrote a function description above it so you know how to use it. The code for the function is below:








Code:

/**
* Render a multi-line text
* \param pFont The font to render the text with
* \param rText The text to render
* \param rTextColor The text color
* \param YPosition The vertical position of the first line of text
* \param DistanceBetweenLines The distance between each line of text
* \param CenteredInWindow A bool specifying if the text lines should be centered (horizontal) in the window or not (defaults to true)
* \param XPosition The horizontal position of the text lines (defaults to 0. Only used if CenteredInWindow is set to false)
* \note Create a new line by inserting the \n command in the string. The string needs to end with a \n command for the last line of text to be rendered
*/
void RenderMultiLineText(const TTF_Font* pFont, const std::string& rText, const SDL_Color& rTextColor, const int YPosition, const int DistanceBetweenLines, const bool CenteredInWindow = true, const int XPosition = 0)
{
// Make sure that the string contains at least 1 character
if(!rText.empty())
{
SDL_Surface* pSurface = NULL;
SDL_Texture* pTexture = NULL;

const int Length = rText.length();
int CurrentLine = 0;

// This string will contain one line of text
std::string TextLine = "";

for(int i = 0; i < Length; ++i)
{
// Create the text line as long as the current character is not a \n command
if(rText[i] != '\n')
TextLine += rText[i];

// Since the current character is now a \n command, it's time to create the texture containing the current text line, render it, clear the created text string and then start on a new line
else
{
if(pFont)
pSurface = TTF_RenderText_Solid((TTF_Font*)pFont, TextLine.c_str(), rTextColor);

if(pSurface)
{
pTexture = SDL_CreateTextureFromSurface(g_pRenderer, pSurface);

if(pTexture)
{
const int TextWidth = pSurface->w;
const int TextHeight = pSurface->h;

SDL_Rect PositionQuad = {0, 0, 0, 0};

// Position the quad centered (horizontal) in the window
if(CenteredInWindow)
PositionQuad.x = (WINDOW_WIDTH / 2) - (TextWidth / 2);

// Position the quad according to the XPosition parameter
else
PositionQuad.x = XPosition;

PositionQuad.y = YPosition + (DistanceBetweenLines * CurrentLine);
PositionQuad.w = TextWidth;
PositionQuad.h = TextHeight;

SDL_RenderCopy(g_pRenderer, pTexture, NULL, &PositionQuad);

// Avoid memory leak
SDL_DestroyTexture(pTexture);
pTexture = NULL;
}

// Avoid memory leak
SDL_FreeSurface(pSurface);
pSurface = NULL;
}

// The current line of text has now been rendered (if the texture was successfully created and so on) and the text line string now needs to be cleared
TextLine = "";

// Time for a new line
++CurrentLine;
}
}
}
}


The function should be called in your render loop and is called in this way:








Code:

// Render some black multi-line text, centered (horizontal) in the window
RenderMultiLineText(g_pFont, "This is a text line\nThis is the next text line\nThis is another text line\n", g_Black, 200, 20);

// Render some red multi-line text, not centered in the window but instead positioned at 100 pixels in, from the left
RenderMultiLineText(g_pFont, "Some random text\nMore random text\nEven more random text\n", g_Red, 400, 25, false, 100);

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
ah, now I see alas that the NFont library is ascii only... no unicode support Sad


does anyone know if SDL_RTF is operational on both windows and mac ? I find no sln or xcode file in the download folder ...


Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Pallav Nawani


Joined: 19 May 2011
Posts: 122
Location: Dehradun, India
What you can do is:


(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.


If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.


int aX = (total_width - max_text_width)/2;







Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768



On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
Quote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Pallav Nawani


Joined: 19 May 2011
Posts: 122
Location: Dehradun, India
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.



Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768



On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
Quote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Bob Rubbens
Guest

Afaik SDL_ttf supports unicode just fine: http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC53


On 30 March 2015 at 09:59, jeroen clarysse wrote:
Quote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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


rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
SDL_TTF does, but the code in NFont doesn't : it generates individual glyphs for each character, and then blits char-per-char to make centered txt. Only the list of chars below is "prepared" as glyps. As a result, other chars won't work



Quote:
Afaik SDL_ttf supports unicode just fine: http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html#SEC53

On 30 March 2015 at 09:59, jeroen clarysse wrote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
well, it is a bit more complicated : preprocessing text into individual lines is not trivial since the RenderText routines in SDL_TTF do not provide a way to see WHERE you have to split... Splitting by "\n" is simple, but for the automatic wrapping, it is not.

right now I see two options :

a) get SDL_RTF working
b) look at the SLD_RenderText() routines and see if I can patch them to support an alignment parameter



Quote:
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.


Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Bob Rubbens
Guest

Just split a long std::string into an array of std::string, render each element of the array with SDL_ttf, store resulting surfaces in another array, and finally render each surface independently. You can center every surface independently by subtracting half the width of each surface from the x position.
Splitting a std::string into an array of std::string can be done by just splitting the string each 50 characters. This can be improved by splitting on the first space before or after the 50-char breakpoint, but that's the basic idea. If your string is in unicode it is probably a bit harder but still doable without any library. It's not perfect but it probably works just fine if you keep in mind your own requirements. Op 30 mrt. 2015 13:16 schreef "jeroen clarysse":
Quote:
well, it is a bit more complicated : preprocessing text into individual lines is not trivial since the RenderText routines in SDL_TTF do not provide a way to see WHERE you have to split... Splitting by "\n" is simple, but for the automatic wrapping, it is not.

right now I see two options :

a) get SDL_RTF working
b) look at the SLD_RenderText() routines and see if I can patch them to support an alignment parameter



Quote:
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.


Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


     ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
jeroen.clarysse


Joined: 22 Feb 2010
Posts: 69
hm.. if eel like reinventing the weel a bit with that routine : SDL_redertext() does more or less the same... only without alignment




Quote:
On 30 Mar 2015, at 13:34, Bob Rubbens wrote:

Just split a long std::string into an array of std::string, render each element of the array with SDL_ttf, store resulting surfaces in another array, and finally render each surface independently. You can center every surface independently by subtracting half the width of each surface from the x position.

Splitting a std::string into an array of std::string can be done by just splitting the string each 50 characters. This can be improved by splitting on the first space before or after the 50-char breakpoint, but that's the basic idea. If your string is in unicode it is probably a bit harder but still doable without any library. It's not perfect but it probably works just fine if you keep in mind your own requirements.

Op 30 mrt. 2015 13:16 schreef "jeroen clarysse":
well, it is a bit more complicated : preprocessing text into individual lines is not trivial since the RenderText routines in SDL_TTF do not provide a way to see WHERE you have to split... Splitting by "\n" is simple, but for the automatic wrapping, it is not.

right now I see two options :

a) get SDL_RTF working
b) look at the SLD_RenderText() routines and see if I can patch them to support an alignment parameter



Quote:
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.


Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter: http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf. If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
rendering centered text
Jonny D


Joined: 12 Sep 2009
Posts: 932
NFont does support Unicode.  It caches the glyphs as you said, but if you are using SDL_ttf to load a Unicode font, then NFont will dynamically load the glyphs it needs as they are used.

Jonny D






On Mon, Mar 30, 2015 at 8:26 AM, jeroen clarysse wrote:
Quote:
hm.. if eel like reinventing the weel a bit with that routine : SDL_redertext() does more or less the same... only without alignment




Quote:
On 30 Mar 2015, at 13:34, Bob Rubbens wrote:

Just split a long std::string into an array of std::string, render each element of the array with SDL_ttf, store resulting surfaces in another array, and finally render each surface independently. You can center every surface independently by subtracting half the width of each surface from the x position.

Splitting a std::string into an array of std::string can be done by just splitting the string each 50 characters. This can be improved by splitting on the first space before or after the 50-char breakpoint, but that's the basic idea. If your string is in unicode it is probably a bit harder but still doable without any library. It's not perfect but it probably works just fine if you keep in mind your own requirements.

Op 30 mrt. 2015 13:16 schreef "jeroen clarysse":
well, it is a bit more complicated : preprocessing text into individual lines is not trivial since the RenderText routines in SDL_TTF do not provide a way to see WHERE you have to split... Splitting by "\n" is simple, but for the automatic wrapping, it is not.

right now I see two options :

a) get SDL_RTF working
b) look at the SLD_RenderText() routines and see if I can patch them to support an alignment parameter



Quote:
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.


Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


     ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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


rendering centered text
Pallav Nawani


Joined: 19 May 2011
Posts: 122
Location: Dehradun, India
Wouldn't
TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h)

or
TTF_SizeUNICODE(TTF_Font *font, const Unit16 *text, int *w, int *h)

Solve the problem of determining where to split?





Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768



On Mon, Mar 30, 2015 at 4:46 PM, jeroen clarysse wrote:
Quote:
well, it is a bit more complicated : preprocessing text into individual lines is not trivial since the RenderText routines in SDL_TTF do not provide a way to see WHERE you have to split... Splitting by "\n" is simple, but for the automatic wrapping, it is not.

right now I see two options :

a) get SDL_RTF working
b) look at the SLD_RenderText() routines and see if I can patch them to support an alignment parameter



Quote:
ok, though I gave you a suggestion of how to solve it in your own code, by just using sdl_ttf.


Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:29 PM, jeroen clarysse wrote:
I walked through the code for a moment, but found one disappointing thing : the code uses a set of pre-rendered glyphs. Basically, these characters are rendered on a surface :


     ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~


and then the final text is split up by blitting each of these characters separately from that surface.

this means unfortunately that the code is not unicode compatible at all... if for instance french text with accented characters ( é è à ê œ , ...) or scandinavian characters as ö and
Ø will be impossible... and then we're not even talking about greek, arabic, hebrew or asian languages





Quote:
On 30 Mar 2015, at 09:52, Pallav Nawani wrote:

What you can do is:

(a) Pre-Process the text to separate it into lines
(b) Center & render each line individually.

If you are wordwrapping, then obviously you know max_text_width, exceeding which the text will be wrapped. So use that and the newline character to separate the text into several lines.
Now you can center each individual line. If you want to center a WHOLE BLOCK of wordwrapped text, and not each line individually, then it is still easier: just use max_text_width to calculate centered rendering position.

int aX = (total_width - max_text_width)/2;




Pallav Nawani
IronCode Gaming Private Limited
Website: http://www.ironcode.com
Twitter:  http://twitter.com/Ironcode_Gaming
Facebook: http://www.facebook.com/Ironcode.Gaming
Mobile: 9997478768

On Mon, Mar 30, 2015 at 1:08 PM, jeroen clarysse wrote:
this looks very promising !!!

thanks for the link

Quote:
On 29 Mar 2015, at 15:56, Jonathan Dearborn wrote:

You could try NFont (warning: the C version is old now, so I only recommend the C++ version until they're merged):
https://github.com/grimfang4/nfont

It does a lot of useful stuff (including text alignment and multiline text) and also loads fonts using SDL_ttf.  If you are using SDL_Renderer, then use the NFont.h header from the NFontR directory.

Jonny D


On Sunday, March 29, 2015, jeroen clarysse wrote:
Quote:
With TTF_sizetext you can get the width of the text using a certain font, without generating a surface. You can also just generate the surfaces if you already know where the newlines are, and then just ask the width of the generated surface (surface->width or something iirc.)


like I said in my previous reply : this only works if the text isn't wrapped automatically

I have been thinking about using SDL_RTF : wrap a block of text in a few RTF commands to make it centered, but the SDL_RTF project seems to be incompatible with SDL2.0 ?
_______________________________________________
SDL mailing list

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

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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

_______________________________________________
SDL mailing list

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