unknowncheats uc-forum.com ucdownloads ucdownloads.com

Go Back   UC-Tutorials - Multiplayer Game Hacking and Cheat Tutorials > Programming > Direct3D

- Sponsored Advertisement -
http://www.myfpscheats.com/


Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2009, 04:21 AM
Alkatraz Alkatraz is offline
Administrator
 
Join Date: Jan 2007
Posts: 72
Default [Coding] - Centering your text

By Request ill explain first then show you how to impliment it:

When centering your text all you have to do is figure out how wide your string is then divide that by 2 and subtract that from the center of what ever your rendering it over. The width paramater can be defined by the string not just an object.

find your center:

center = x + (w/2)
stringlength = strlen(string) * 7 // 7 = the pixel width of the largest char when rendered(yours may be different)

so with those 2 you need to figure out where to place your text

textX = center - (stringlength / 2) // this way id subtract half the length of your string from the center of your object

my function i use: not 100 percent perfect but it gets the job done.


Code:
void CenterText( float x, float y, float w, char* Text, int r, int g, int b)
{
     float itextX = x + (w / 2) - strlen(Text)* 7.0f / 2;
     float itextY = y + 3.0f;
     MyDrawText(itextX, itextY, Text, r, g, b);
}
If anyone knows a better way please clue me in.
Reply With Quote
  #2  
Old 02-09-2009, 04:21 AM
Alkatraz Alkatraz is offline
Administrator
 
Join Date: Jan 2007
Posts: 72
Default

Credits zoomgod


Here's how I do it using the actual font size, I store my fonts in an array so FontNum is just an index into it.



Code:
D3DXVECTOR2 getTextSize(int FontNum, const char* text)
{

    RECT _recA;
    
    _recA.left=0;
    _recA.top=0;
    _recA.bottom=500;
    _recA.right=500;
    
    // this does not render to screen
    fonts[FontNum]->DrawTextA(NULL, text, -1, &_recA, DT_CALCRECT, rgbYellow );

    D3DXVECTOR2 size;
    size.x = (float)_recA.right;
    size.y = (float)_recA.bottom;

    return size;

}
Knowing actual text width and height you then adjust from rendering position and they are right on the money.
Reply With Quote
  #3  
Old 02-09-2009, 04:28 AM
Alkatraz Alkatraz is offline
Administrator
 
Join Date: Jan 2007
Posts: 72
Default

Credits R4z8r


Code:
VOID CDraw::Text_Center( char szString[], FLOAT PosX, FLOAT PosY, DWORD dwColor, ID3DXFont *MyFont)
{
	Text_Border( PosX - (Font.GetStringLengthInPixels( szString, MyFont ) / 2), PosY, dwColor, MyFont, szString ); 
}

INT CFont::GetStringLengthInPixels( char szString[], ID3DXFont* Font )
{
	TEXTMETRICA Met; 

	Font->GetTextMetrics( &Met );

	INT iStringSize = NULL;

	for(int i = 0; szString[i] != '\0'; i++)
		iStringSize++;

	return iStringSize * Met.tmAveCharWidth; 
}
Reply With Quote
Reply

  • Submit Thread to Digg
  • Submit Thread to del.icio.us
  • Submit Thread to StumbleUpon
  • Submit Thread to Google
  • Submit Thread to Facebook
  • Submit Thread to My Yahoo!
  • Submit Thread to MySpace
  • Submit Thread to Twitter
  • Submit Thread to Reddit

Tags
centering, coding, text

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 09:08 AM.