• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Text Engine

## Recommended Posts

Hey, I am working on a text engine and would like to know if you think my apporach is fast and whether there is a faster one. This is how it works. I have a struct like so:
  struct FONT { LPDIRECTDRAWSURFACE7 FontSurface; int BitmapHeigth; int BitmapWidth; int TextWidth; int TextHeigth; RECT TextRect[256]; }; 
At the beginning of the program you create a font with a function called, 'LoadFont(FONT *NewFont)'. It basically loads the font surface and fills TextRect[] with the position of each letter. Then, you call DrawText(char *TextString, FONT *Font) to draw text using your font. Now, basically this function goes through each character, and then converts it to ASCII using __toascii(). This is how the code looks...
  // Go through each character in the string while(*TextString) { // Convert Character to ASCII value ASCIIValue = __toascii(*TextString); // Fill up the character's rectangle. DestRect.left = x + (Width * index); DestRect.right = x + (Width * index) + Width; DestRect.top = y; DestRect.bottom = y + Heigth; // Draw the character with the correct ASCII value hr = PrimarySurface->Blt(&DestRect,Font->FontSurface, &Font->TextRect[ASCIIValue], DDBLT_WAIT | DDBLT_KEYSRC, NULL); if(DDFailedCheck(hr, TextString )) return false; // Move to next character in string TextString++; // Add to index, used to move characters width over. index++; } 
Now I am kind of a beginner so I would just like to know if that looks like it would be fast or really slow. If you have any suggestions to make it faster I would appreciate it. Thanks. :} BTW, that is the code for my DrawText(char *TextString, FONT *Font) and it is not the entire function, just the most important part. Edited by - JSCFaith on October 29, 2001 8:02:31 PM Edited by - JSCFaith on October 29, 2001 8:06:53 PM

##### Share on other sites
Well...

DestRect.top = y;
DestRect.bottom = y + Heigth;

that is unessacary (sp?). Those values will never change so put those outside of your loop.

ASCIIValue = __toascii(*TextString);

You won't even need that becuase when you stick the value *TextString into the TextRect[] array it will already equal your ascii value (ie: *TextString == 'A'. the 'A' character's numerical value is 65, which is also it's ascii value).

DestRect.left = x + (Width * index);
DestRect.right = x + (Width * index) + Width;

DestRect.left = x;

before the loop. Then inside the loop do

DestRect.right = DestRect.left + width;

before you blit the text. Then at the end of the loop do

DestRect.left = DestRect.right;

This would save you on those multiplications. Other than that, kick ass, man. Nice work

BTW, in case I wasn't entirely clear, here's the source with a few comments...

  // Setup Destination RectangleDestRect.left = x;// The vertical position of string shouldn't change, so declare onceDestRect.top = y;DestRect.bottom = y + Height;while(*TextString) { DestRect.right = DestRect.left + Width; // *TextString numerical value will equal it's ascii value in array hr = PrimarySurface->Blt(&DestRect,Font->FontSurface, &Font->TextRect[*TextString], DDBLT_WAIT | DDBLT_KEYSRC, NULL); if(DDFailedCheck(hr, TextString )) return false; // Reposition the rectangle next to it's former position DestRect.left = DestRect.right; // Move to the next letter TextString++;}

Have fun

EDIT - Forgot to close the "source" tag

Edited by - Big Sassy on October 29, 2001 8:29:12 PM

##### Share on other sites
Thanks, your code will definitely be faster. Thanks for the help man. Also, I just noticed that I spelled 'Height' wrong throughout my code. I spelled it 'Heigth.' I need to learn to spell, lol.

Thanks again man, you have been a big help.

Later,
James

Edited by - JSCFaith on October 29, 2001 8:33:24 PM

##### Share on other sites
No problem. Glad I could help

##### Share on other sites
I have a very helpful suggestion for you. Please do not spend a lot of time in the beginning optimizing your text renderer. I have done things like that and that just halts your progress. Unless you write it in a very contorted way, you should be able to optimize it later. Usually there is not that much text on your screen.