Zen of D3D font engine

Started by
4 comments, last by KingsRevenge 22 years, 7 months ago
has anyone here gotten the font engine in d3d to work, for some reason the PrintChar function is broke in my code and just crashes my app whenever the function is called. Eric Wright o0Programmer0o AcidRain Productions
Eric Wright o0Programmer0o
Advertisement
I don''t have the book, but try posting the code here, more people, who don''t have the book, will be able to help you out


- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
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
Eric Wright o0Programmer0o
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
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
Eric Wright o0Programmer0o
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
Eric Wright o0Programmer0o

This topic is closed to new replies.

Advertisement