[SDL] Reading a line of text

benang at cs.its.ac.id benang at cs.its.ac.id
Sat Sep 23 20:47:41 PDT 2006


> 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 <string>

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());
}






More information about the SDL mailing list