Reading a line of text

Hello

I’m trying to use SDL with C++ to make a GUI for a college project. I’m trying
to read in a line of text using SDL, and then outputting it afterwards. I
understand how to read individual keyboard events, and am able to store the
letters pressed as chars. However when using the SDL_TTF library to output the
text it requires a char* as an argument. I am unsure how to consecutively append
the chars such that at the end i am able to send a char* to be outputted. I cant
cast the char to the char* even when passing its address.

Example code shown:

void getText ()
{
SDL_Event event;
bool typing = true;

    char* output_line = ""
    char key = ' ';

while (typing)
{
        if (SDL_PollEvent(&event))
	{
		
		if (event.type == SDL_KEYDOWN)
		{
			SDLKey keyPressed = event2.key.keysym.sym;

			if (keyPressed == SDLK_ESCAPE)
		   		typing = false;
			else
			{
				key = char(keyPressed);
				//want to do something like:
                                    //output_line = output_line + key;
                                    //to add consecutive keyboard inputs   
                                
			}
			
		}
	}
}

    drawText(output_line);

}

Some of it may depend on what you’re using to hold the input string. If
you’re using a char buffer like

char input [256];

you probably need to access it on a character by character basis using
an index, say ‘i’, into the array

int i;

input [i] = input_char;
i++; // increment the index

or alternatively

input[i++] = input_char;

or by using an incrementing pointer.

char *p = input;

on each input character you woudl do

*(p++) = input_char;

But you have to be careful about what’s input and how much. You don’t
want to exceed the size of your buffer and you’ll need to account for
backspaces and dels and then be sure not to underrun your buffer.

You might also use std::string, which doesn’t present the need for size
checking. But off hand I don’t know if it handles backspaces and such.

Lilith

Hello

I’m trying to use SDL with C++ to make a GUI for a college project.
I’m
trying
to read in a line of text using SDL, and then outputting it
afterwards. I
understand how to read individual keyboard events, and am able to
store the
letters pressed as chars. However when using the SDL_TTF library to
output
the
text it requires a char* as an argument. I am unsure how to
consecutively
append
the chars such that at the end i am able to send a char* to be
outputted. I
cant
cast the char to the char* even when passing its address.

Example code shown:

void getText ()
{
SDL_Event event;
bool typing = true;

    char* output_line = ""
    char key = ' ';

while (typing)
{
if (SDL_PollEvent(&event))
{

  	if (event.type == SDL_KEYDOWN)
  	{
  		SDLKey keyPressed =

event2.key.keysym.sym;

  		if (keyPressed == SDLK_ESCAPE)
  	   		typing = false;
  		else
  		{
  			key = char(keyPressed);
  			//want to do something like:
                                    //output_line = output_line +

key;

                                    //to add consecutive keyboard

inputs>>> On 9/22/2006 at 7:29 AM, in message <loom.20060922T141951-997 at post.gmane.org>, Dino wrote:

  		}
  		
  	}
  }

}

    drawText(output_line);

}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Hello

I’m trying to use SDL with C++ to make a GUI for a college project. I’m
trying
to read in a line of text using SDL, and then outputting it afterwards. I
understand how to read individual keyboard events, and am able to store
the
letters pressed as chars. However when using the SDL_TTF library to output
the
text it requires a char* as an argument. I am unsure how to consecutively
append
the chars such that at the end i am able to send a char* to be outputted.
I cant
cast the char to the char* even when passing its address.

Example code shown:

void getText ()
{
SDL_Event event;
bool typing = true;

    char* output_line = ""
    char key = ' ';

while (typing)
{
if (SDL_PollEvent(&event))
{

  	if (event.type == SDL_KEYDOWN)
  	{
  		SDLKey keyPressed = event2.key.keysym.sym;

  		if (keyPressed == SDLK_ESCAPE)
  	   		typing = false;
  		else
  		{
  			key = char(keyPressed);
  			//want to do something like:
                                    //output_line = output_line + key;
                                    //to add consecutive keyboard

inputs

  		}

  	}
  }

}

    drawText(output_line);

}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

The easiest way: use std::string like so:

#include

void getText ()
{
SDL_Event event;
bool typing = true;

     std::string output_line = "";
     char key = ' ';

while (typing)
{
        if (SDL_PollEvent(&event))
	{

		if (event.type == SDL_KEYDOWN)
		{
			SDLKey keyPressed = event2.key.keysym.sym;

			if (keyPressed == SDLK_ESCAPE)
		   		typing = false;
			else
			{
				key = char(keyPressed);
				//want to do something like:
                                     //output_line = output_line + key;
                                     //to add consecutive keyboard inputs
                                    output_line += key;

			}

		}
	}
}

    drawText(output_line.c_str());

}