Font Engine - Need assistance pls!

Started by
2 comments, last by En3my 23 years, 6 months ago
I''ve been using GDI for displaying in-game information and statistics for some time, but as it''s painfully slow I think it''s about time to make a font engine myself... Haven''t been doing any coding as of yet, only some very basic planning... What I have in mind is using an offscreen surface holding a-z and 0-9 characters, and having a function looping through the string blitting the characters one after another using DirectX... I don''t have a problem with normal text strings, but when it comes to displaying score and such I have no idea how to make it happen... Yet! In a text string i can just use "Switch (Current_Character), Case ´a´: Source_Cords_To_Blit_From = xy_Values_For_A_In_Bitmap and so on... Is it possible to loop through int stored values the same way as char[] strings? Any other better approach to determine which digit in turn? I think I''ve seen some Windows function calls somewhere combining text and numerals, can''t remember where though... Thanks in advance!
Advertisement
Just create one string containing the complete output character list and draw that.

Either use a CStream in C++, or write a printf()-like function in C, this is fairly easy:
     void MyPrintf(LPDIRECTDRAWSURFACE7 p_pDDSurface,               int p_nX, int p_nY, char *p_pszText, ...) {   char    l_pszText[1024];   va_list l_CList;   int     l_nPos;   char    l_nChar;    // Expand the format string   va_start(l_CList, p_pszText);   vsprintf(l_pszText, p_pszText, l_CList);   va_end(l_CList);    l_nPos = 0;   while(l_nChar = l_pszText[l_nPos++]) {     // Blit l_nChar at p_nX, p_nY     // increase p_nX by char''s width   }    return; }    


If you, say, call the function like this
MyPrintf(lpDD, 10, 10, "Player: %s | Score: %i", pszPlayerName, nScore);
Inside the function, l_pszText will read "Player: Me | Score 123" - you''ve got a single string containing all the text and numbers in order.

There''s also sprintf() which is like printf(), it just prints into a string instead to the screen (stdout). And there are conversion functions like itoa() atoi() (int to asciiz, asciiz to int) ftoa() ultoa(), etc.

-Markus-
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
vsprintf?! Argh..I wish I hadn''t come across this thread! Some time ago I wrote my own version of that very function, not knowing there already was one! *sigh* It even supported most of the format specifiers that vsprintf does and some new too..like %b for binary output hehe. Anyways, seeing that there''s a function to do the same job as mine I think I''ll have to go change it..even though mine was a bit faster with medium and long "formatings" .

Until later..I''m off to bang my head against the wall and erase two hundred lines of code .

"Paranoia is the belief in a hidden order behind the visible." - Anonymous
Thanks for the help Markus, now I''m back on track again!!

This topic is closed to new replies.

Advertisement