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.



Usage:
// where SDL_Surface* pSurface is the screen surface.
char * s; 
s= Dialog_GetText("Enter your name:", "Fred", pSurface);
Dialog_Messagef(pSurface, "Welcome, %s", s);
free(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))
  n*=2;
Dialog_Message("Goodbye", pSurface);
freeFonts();


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: