Is there any plans to incorporate text output? Something like
SDL_Text(font,x,y,text) would be cool =) I know this is not high up on
the agenda but I think it would be really usefull for alot of us newbies
You could implement this yourself. I’ve done it a lot, especially with
number digits. I create either one large image with ten digits in it,
or ten images with one digit in each. Let’s assume I do the former.
To make it easy to code, and since it’s actually nicer for things like
score displays to have a consistent size, we’ll just assume non-proportional
spacing. So, our file will be X * 10 x Y in size. (Where X x Y is the size
of a particular digit.)
In ASCII, something like:
Admittedly, this is a VERY small font. (Each digit is 4x5)
Now, let’s say we wanted to draw a number using it. There’s a number of
ways (no pun intended) to do this. I go for the lame way, using a string.
This lets me use the wonderful capabilities of the printf() family of
Draw a number onto a surface.
screen - the surface to draw onto
num - the number to draw
x, y - the starting point to draw from (will be top-left of 1st digit)
font - a surface containing a fixed-width set of 10 number images
void draw_number(SDL_Surface * screen,
Uint32 num, int x, int y, SDL_Surface * font)
char temp; /* The maximum Uint32 could hold 10 digits: 4294967296 */
int i, w, h;
SDL_Rect src, dest;
/* Create the string: */
sprintf(temp, “%lu”, num); /* You could reformat it with “.” or “+”, too /
/ That could allow for “001234” or " 1234" */
/* Determine the size of one particular digit (for future reference): */
w = (font -> w) / 10;
h = (font -> h);
/* Draw the string! */
for (i = 0; i < strlen(temp); i++)
if (temp[i] >= ‘0’ && temp[i] <= ‘9’)
/* If this part of the string is a digit (ie, not a space, if
there are any), the let’s draw it! */
src.x = (temp[i] - '0') * w; /* Start at the X'th character */
src.y = 0;
src.w = w;
src.h = h;
dest.x = x + (i * w);
dest.y = y;
dest.w = w;
dest.h = h;
SDL_BlitSurface(font, &src, screen, &dest);
/* Update the surface (you may or may not wish to do this here!) */
SDL_UpdateRect(screen, x, y, strlen(temp) * w, h);
Note: I typed this code directly into this e-mail, so it’s untested.
But, it should work.
FYI, another, probably better way, to figure out how to draw the number,
would be to do “/” and “%” on the number.
For example, if “i” were the number 42, the last four digits we would get
(i / 1000) % 10 --> 0
(i / 100) % 10 --> 0
(i / 10) % 10 --> 4
(i / 1) % 10 --> 2
If you wish to pad the number with leading spaces instead of leading zeros,
you could have a flag which says “have we seen a non-zero digit yet?”.
If it’s not set, don’t draw zeros, since they’re leading. (UNLESS, OF COURSE,
you’re at the very LAST digit… You don’t want your function to refuse
to display anything when you send it the number “0”!)
You can also get rid of leading anythings (therefore left-justifying the
digits, like the printf()-using example does above, the way it’s currently
formatted). Use that same flag, and have a variable (say, “xx”) which starts
out with “x” and only gets incremented when you actually draw something.
Then change the “dest.x =” line to:
dest.x = xx;
xx = xx + w;
Finally, if you want PROPORTIONAL characters, it’s probably easier to have
an array of character images (each one cropped to different widths, depending
on the letter or number), and then use an ‘xx’ variable, like above.
Then just increment it by:
xx = xx + (numbers[temp[i] - ‘0’] -> w);
(Where “numbers” is an array of SDL_Surfaces. The 0th one containing an
image for the number 0. Or, you could do pretty much the entire ASCII
FINALLY… if this is too much for you, look at the “ttflib/” directory
in the SDL demos archive. It contains a library you can use (which,
admittedly, I haven’t looked at yet, except to run the “ttfdemo” program)
to draw TTF (TrueType Fonts) onto an SDL surface.
Good luck! May the source be with you!
bill at newbreedsoftware.com