Jump to content
  • Advertisement

Archived

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

KingsRevenge

Zen of D3D font engine

This topic is 6238 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Ok Here''s the function:

  
void PrintChar( int x, int y, char Character, BOOL bTransparent,
D3DCOLOR ColorKey, DWORD* pDestData, int DestPitch )
{
HRESULT r = 0;

div_t Result; // Holds the result of divisions


// The offset into the alphabet image

int OffsetX = 0, OffsetY = 0;

POINT LetterDestPoint = { 0, 0 }; // The destination point for the letter

RECT LetterRect = { 0, 0, 0, 0 }; // The source rectangle for the letter


// If the alphabet has not been loaded yet then exit

if( !g_bAlphabetLoaded )
return;

// The characters are specified in ASCII code, which begins at 32 so

// we want to decrement this value by 32 to make it zero based

Character -= 32;

// Divide the character code by the number of letters per row.

// The quotient will help get the vertical offset and the

// remainder will help get the horizontal offset

Result = div( Character, g_AlphabetLettersPerRow );

// Get the horizontal offset by multiplying the remainder

// by the width of the Letter

OffsetX = Result.rem * g_AlphabetLetterWidth;
// Get the vertical offset by multiplying the quotient

// by the height of the letter

OffsetY = Result.quot * g_AlphabetLetterHeight;

// Fill in the source rectangle with the computed offsets

SetRect( &LetterRect, OffsetX, OffsetY,
OffsetX + g_AlphabetLetterWidth, OffsetY + g_AlphabetLetterHeight );

// Fill in the destination point

LetterDestPoint.x = x;
LetterDestPoint.y = y;

D3DLOCKED_RECT LockedAlphabet; // Holds info about the alphabet surface


// Lock the source surface

r = g_pAlphabetSurface->LockRect( &LockedAlphabet, 0, D3DLOCK_READONLY );
if( FAILED( r ) )
{
SetError( "Couldnt lock alphabet surface for PrintChar()" );
return;
}

// Get a DWORD pointer to each surface

DWORD* pAlphaData = (DWORD*)LockedAlphabet.pBits;

// Convert the BYTE pitch pointer to a DWORD ptr

LockedAlphabet.Pitch /=4;
DestPitch /= 4;

// Compute the offset into the alphabet

int AlphaOffset = OffsetY * LockedAlphabet.Pitch + OffsetX;
// Compute the offset into the destination surface

int DestOffset = y * DestPitch + x;

// Loop for each row in the letter

for( int cy = 0 ; cy < g_AlphabetLetterHeight ; cy++ )
{
// Loop for each column in the letter

for( int cx = 0 ; cx < g_AlphabetLetterWidth ; cx++ )
{
if( bTransparent )
{
// If this alphabet pixel is not transparent

if( pAlphaData[ AlphaOffset ] != ColorKey )
{
// Then coy the pixel to the destination

pDestData[ DestOffset ] = pAlphaData[ AlphaOffset ];
}

// Increment the offsets to the next pixel

}
else
{
pDestData[ DestOffset ] = pAlphaData[ AlphaOffset ];
}

AlphaOffset++;
DestOffset++;
}

// Move the offsets to the start of the next row

DestOffset += DestPitch - g_AlphabetLetterWidth;
AlphaOffset += LockedAlphabet.Pitch - g_AlphabetLetterWidth;
}

// Unlock the surface

g_pAlphabetSurface->UnlockRect();

}


Eric Wright o0Programmer0o

AcidRain Productions

Share this post


Link to post
Share on other sites
It crashes for me too but...try printing a diffrent character and it works...I think his calculations are kind of wrong... I am experancing the same problems...I hope I could find what what he is doing wrong..

when you load the font I belive it''s something like
LoadAlphabet((file location), x, y)...
x is the width of one letter and y is the height...look at your bitmap of the fonts in one of your applications like adobe and see if you could find out the exact height and width....that''s what I did it doesn''t crash for me now but it still doesn''t produce the right results. . By the way for me I called the function like this LoadAphabet(file location, 8, 16)...

Frank

Share this post


Link to post
Share on other sites
I got Keebler to help me out with figuring out what was wrong, in the LoadALphabet function pass in 8x16, instead of the 16x32 he said what that code was trying to do was like writing to memory that wasn''t there or something.

Only thing jackedu p once I got it working was transparency wasn''t working so now I have to figure out how to get that to work and all. Once I do haveit working I wil progress until I reach chapter 4. THen make some kinda little 2d game or something.

Eric Wright o0Programmer0o

AcidRain Productions

Share this post


Link to post
Share on other sites
I got Keebler to help me out with figuring out what was wrong, in the LoadALphabet function pass in 8x16, instead of the 16x32 he said what that code was trying to do was like writing to memory that wasn''t there or something.

Only thing jackedu p once I got it working was transparency wasn''t working so now I have to figure out how to get that to work and all. Once I do haveit working I wil progress until I reach chapter 4. THen make some kinda little 2d game or something.

Eric Wright o0Programmer0o

AcidRain Productions

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!