Wednesday, June 2, 2010

SDL Text and Dialogs

For one of my recent projects, I wanted dialogs in an sdl program. This small library draws simple "dialogs" to ask the user for text or a number.

// where SDL_Surface* pSurface is the screen surface.
char * s; 
s= Dialog_GetText("Enter your name:", "Fred", pSurface);
Dialog_Messagef(pSurface, "Welcome, %s", s);

double val = 1.5;
Dialog_GetDouble("Enter a value", pSurface, &val);
int n = 1;
Dialog_GetInt("Choose a number.", pSurface, &n);
if (Dialog_GetBool("Double the number?", pSurface))
Dialog_Message("Goodbye", pSurface);

To draw text, I used the code at this page. (I added the ability to have newlines and tabs). It supports lower ascii chars, and code could be improved, but currently suits my purposes. There's also SFont and others.

Source (includes a bitmap font).

No comments: