Formatting a string on a variadic parameter function without using GC
David G. Maziero via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Mar 1 21:04:37 PST 2016
I figured out what I wanted. Thanks for your answer Mike, sorry
to bother you though.
Here's the result:
void RenderText( FontBMP font, int x, int y, const char* text,
... )
{
SDL_Rect rect1, rect2;
rect2.x = x;
rect2.y = y;
rect2.w = font.width;
rect2.h = font.height;
rect1.w = font.width;
rect1.h = font.height;
char[256] buff;
va_list ap;
va_start( ap, text );
sprintf( buff.ptr, text, ap );
va_end( ap );
for( int r=0; buff[r]!='\0'; ++r )
{
char letter = buff[r];
rect1.x = font.width*(letter%font.horizontal_count);
rect1.y = font.height*(letter/font.horizontal_count);
if( letter>=33 ) SDL_RenderCopy( g_renderer, font.texture,
&rect1, &rect2 );
rect2.x += font.spacing_x;
if( letter==10 )
{
rect2.x = x;
rect2.y += font.spacing_y;
}
}
}
Since the "string" is built by sprintf, it'll be null-terminated.
Sorry for posting without doing more research. I didn't realise I
could use va_start/va_end just like in C.
More information about the Digitalmars-d-learn
mailing list