# Text classes

- sge_TextEditor
- sge_text
- sge_TextSurface
- sge_TextSsprite
- sge_TextSprite
- sge_text_input

sge_TextEditor
Edits text from SDL_Events. This class just stores a text string in memory and lets you perfrom some basic editing on it. You can only change (or insert) the character left or right of the "cursor".
Constructor:

> sge_TextEditor(void)

Methods:

> bool insert(Uint16 c)
> Adds an (unicode) char before the cursor.
> bool remove_left(void)
> Removes the char left of cursor.
> bool remove_right(void)
> Removes the char right of cursor.
> inline bool move_left(void)
> Move the cursor to the left.
> inline bool move_right(void)
> Move the cursor to the right.
> bool move_start(void)
> Move the cursor to the start.
> bool move_end(void)
> Move the cursor to the end.
> std::string get_string(bool wCursor=true)
> Returns the text as latin1 with or without the cursor char.
> Uint16* get_ucstring(bool wCursor=true)
> Returns a unicode c-style string (allocated with new).
> virtual bool check(SDL_Event* event)
> Process an SDL_Event. The text can be edited with [Backspace], [Delete], [Left arrow] and [Right arrow]. Returns true if the text changed.
> void change_cursor(Uint16 c)
> Change the cursor char. Default is the '|' character.
> void clear_text(void)
> Clears the text string.
> void change_text(const std::string s)
> Changes the text.
> void change_uctext(Uint16 *text)
> Changes the text (c-style unicode).
> void change_textf(const char *text, ...)
> Changes the text (printf c-style).
> void max_chars(unsigned int c)
> Set max chars (default: limited only by memory).
> unsigned int get_chars(void)
> Returns the number of characters in the current string.

sge_text
Derived (public) from sge_TextEditor.
A class for rendering text. Can render both truetype and bitmap fonts. Change/Edit the text with the methods in sge_TextEditor.
Constructor:

> sge_text(void)

Methods:

> SDL_Surface* get_textSurface(bool copy=false)
> Get a pointer to the text surface or (if copy=true) returns a copy (don't forget to free it later). This will not work with SFonts with an alpha channel.
> bool update_textSurface(bool force=false)
> Updates the textsurface if the text has changed (or if force=true) and returns true if the surface was updated.
> void set_ttFont(sge_TTFont *font, Uint8 r, Uint8 g, Uint8 b, Uint8 br=0, Uint8 bg=0, Uint8 bb=0)
> Sets the truetype font and color. If you use antialiasing, the background color must be set to the background color of the area where you will render the text. If the background has more than one color it might be better to use sge_TTF_AA_Alpha().
> void set_bmFont(sge_bmpFont *bm_font)
> Sets the bitmap font.
> void show_cursor(bool mode)
> Should the cursor be drawn when rendering text?
> SDL_Rect render_text(SDL_Surface *surface, Sint16 x, Sint16 y)
> Renders the text directly to the given surface (instead of rendering to the text surface with update_textSurface()). Return the size and position of the rendered text.
> void use_TTrender(void)
> Render text using truetype fonts. The default is to use the last set font.
> void use_BMrender(void)
> Render text using bitmap fonts. The default is to use the last set font.
> void set_alpha(Uint8 alpha)
> Sets the alpha value for the text (doesn't work with alpha channel SFonts).
> bool get_color(SDL_Color *fg)
> Fills fg with the RGB color of the truetype font if set. Returns false if the color is not set.
> bool get_bg(SDL_Color *bg)
> Fills bg with the background RGB color of the truetype font if set. Returns false if the color is not set.

sge_TextSurface
Derived public from sge_text and sge_surface.
This class works exactly as sge_surface, but uses the current text surface. Note that get_img() will not work with SFonts with an alpha channel. See also the example.
Constructor:

> sge_TextSurface(SDL_Surface *screen, Sint16 x=0, Sint16 y=0)
> sge_TextSurface(SDL_Surface *screen, const std::string text, Sint16 x=0, Sint16 y=0)
> You can set a default text if you want.

Methods:

> See sge_text and sge_surface.

sge_TextSsprite
Derived public from sge_text and sge_ssprite.
This class works exactly as sge_ssprite, but uses the current text surface. The text surface is always the first frame (so you can display other frames). Will probably not work as normal with SFonts with an alpha channel.
Constructor:

> sge_TextSsprite(SDL_Surface *screen, Sint16 x=0, Sint16 y=0)
> sge_TextSsprite(SDL_Surface *screen, const std::string text, Sint16 x=0, Sint16 y=0)
> You can set a default text if you want.

Methods:

> See sge_text and sge_ssprite.

sge_TextSprite
Derived public from sge_text and sge_sprite.
This class works exactly as sge_sprite, but uses the current text surface. The text surface is always the first frame (so you can display other frames). Will probably not work as normal with SFonts with an alpha channel.
Constructor:

> sge_TextSprite(SDL_Surface *screen, Sint16 x=0, Sint16 y=0)
> sge_TextSprite(SDL_Surface *screen, const std::string text, Sint16 x=0, Sint16 y=0)
> You can set a default text if you want.

Methods:

> See sge_text and sge_sprite.

int sge_text_input(sge_TextSurface *tc, Uint8 flags)
A helper function for lazy users: blocking text input for sge_TextSurface objects. Does the same thing as the example below. The argument "flags" is the same as for BM and TTF input (which uses this function). Make sure "tc" is setup with the correct font etc. before calling this function.

## Example

This is a simple example of the sge_TextSurface class; draws TT text on the screen and lets the user edit it.
#include <stdio.h>
#include "SDL.h"
#include "sge.h"
...

/* Init font engine and exit on error */
if( sge_TTF_Init() != 0 ){
  fprintf(stderr,"TT error: %s\n", SDL_GetError());
  exit(1);
}

/* Open the TT font and exit on error */
sge_TTFont *font = sge_TTF_OpenFont("font.ttf", 25);
if( !font ){
  fprintf(stderr,"TT error: %s\n", SDL_GetError());
  exit(1);
}

/* The buffer for the background (only if you want to keep the background) */
SDL_Surface *buffer;
buffer = SDL_DisplayFormat(screen);
/* Enable keyrepeat */
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL+50);
SDL_EnableUNICODE(1); //This is VERY important!!
/* Init our text class */
sge_TextSurface text(screen,"Edit Me!",8,50);
text.set_ttFont(font,0,255,0); //Use the TT font
text.show_cursor(true); //Show a cursor
/* Draw the text for the first time */
text.draw();
text.UpdateRects();
sge_Update_OFF();

/* Main loop */
SDL_Event event;
do{
  SDL_Delay(10);
  /* Check events */
  if( SDL_PollEvent(&event) == 1 ){
    if( event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE ) break;
    if( event.type == SDL_QUIT ) break;
    /* Let the text class handle the event*/
    if( text.check(&event) ){
      /* The text has changed */
      text.clear(buffer, 8,50); //Remove the text
      text.draw(); //Draw the new text
      sge_Update_ON();
      text.UpdateRects(); //Update screen
      sge_Update_OFF();
    }
  }
}while(true);

/* Clean up */
SDL_FreeSurface(buffer);
sge_TTF_CloseFont(font);

Copyright © 1999-2003 Anders Lindström
Last updated 030808