Advertisement

text dissapears ?!?

Started by March 29, 2002 11:33 AM
8 comments, last by da_cobra 22 years, 7 months ago
ok, this is going to be a long post I'm trying to make a leveleditor for a breakout clone I use simple win32 code to display the X & Y position of my mouse. The text appears correctly on my display but now the strange thing is after 10 a 15 sec the text just dissapears again?!? this is my function to draw text :

void DrawText(char *text, int size, int x, int y, COLORREF color)
{
	HDC hdc ;
	HFONT hfont = CreateFont(size,0,0,0,0,0,0,0,0,0,0,0,0, "Arial") ;

	lpDDSBack->GetDC(&hdc) ;

	SetTextColor(hdc,color);

	SetBkMode(hdc, TRANSPARENT);

	SelectObject(hdc, hfont);
	TextOut(hdc,x,y,text,strlen(text));

	lpDDSBack->ReleaseDC(hdc);

} // end DrawText
  
this is how I use the function :

void BlitMousePos()
{
	strstream stringA, stringB, stringC, stringD ;

	GetCursorPos(&lpArrowPos) ;
	stringA << "X-pos : " << lpArrowPos.x << ends ;
	stringB << "Y-pos : " << lpArrowPos.y << ends ; 
	// put these variables into a string
	
	DrawText(stringA.str(), 24, 674,  15, RGB(255, 255, 255)) ;
	DrawText(stringB.str(), 24, 674,  45, RGB(255, 255, 255)) ;

	stringC << "X = " << ((((lpArrowPos.x)+16)/32)-1) << ends ;
	stringD << "Y = " << ((((lpArrowPos.y))/16)-1) << ends ;

	DrawText(stringC.str(), 24, 674,  75, RGB(255, 255, 255)) ;
	DrawText(stringD.str(), 24, 674, 105, RGB(255, 255, 255)) ;
} // end of BlitMousePos()
  
and this is how my game loop looks like (although I think this isn't important to solve my prob :

oid Mapeditor()
{
	if (bFirstRunEditor) FirstRunEditor() ;
	BlitBackgroundEditor() ;
	ProcessMouseInEditor() ;
	ProcessKeyboardInEditor() ;
	BlitMousePos() ;
	BlitBlocks() ;
	lpDDSPrimary->Flip(NULL, DDFLIP_WAIT) ;	// flip the back-surface with the front
} // end of mapeditor()
  
can any1 pls help me here? I already tried it on a different PC and got the same bug thanx in advance for any help Edited by - da_cobra on March 29, 2002 12:34:18 PM
Look again at your DrawText function. You never delete the font you create. So after some time Windows hasn''t got any memory to give you to create yet another font.

You don''t need to call CreateFont every time you want to draw some text. Do it during init and save HFONT for later use.

Save the return value of the first SelectObject call. After calling TextOut, call SelectObject with the saved value. You must deselect a font from DC before you can free it.

Also, remember to actually free the font.
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
hey thanx for the answer

got a couple of questions though

first one : I want to choose which size I want when I call the function, but I create the font where I choose the size in the function, so how can I solve this

second : how do I free the font

thanx anyway for your help !!!
If you only use a few different sizes, you can have several variables e.g. hSmallFont, hLargeFont, hFancyFont etc.

To release memory occupied by the font, call DeleteObject. If you use windowsx.h, and you should, you can call DeleteFont macro as well.
---visit #directxdev on afternet <- not just for directx, despite the name
thanx!!!

I changed my DrawText() now to this :

void DrawText(char *text, int size, int x, int y, COLORREF color){	HDC hdc ;	HFONT hfont = CreateFont(size,0,0,0,0,0,0,0,0,0,0,0,0, "Arial") ;	//this function draws text in 16-bit DX mode	// with the selected font	// get the dc from surface	lpDDSBack->GetDC(&hdc) ;	// set the colors 	SetTextColor(hdc,color);	// set background mode to transparent	// so black isn't copied	SetBkMode(hdc, TRANSPARENT);	// draw the text using the font	SelectObject(hdc, hfont);	TextOut(hdc,x,y,text,strlen(text));	// release the dc	lpDDSBack->ReleaseDC(hdc);	//delete the font	DeleteObject(hfont) ; // <====== added this} // end DrawText 


is this the correct way, because now my text doesn't dissapears now

[edited by - da_cobra on March 30, 2002 2:44:39 AM]
You have to deselect the font from DC before deleting it.
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
msdn : To delete an object properly, first deselect it from any DC into which it was previously selected. To deselect an object, an application must select a different object of the same type into the DC. Common practice is to track the original object that was selected into the DC and select it back when all work is accomplished with the new object.

I have to select a different object?!? I don''t understand this

pls help me
quote: Original post by da_cobra
Common practice is to track the original object that was selected into the DC and select it back when all work is accomplished with the new object.

Like this:
HFONT hFont = CreateFont(...);HFONT hOldFont = SelectFont(hDC, hFont);// Use hFont hereSelectFont(hDC, hOldFont);DeleteFont(hFont);  
You need to include windowsx.h for this to work. Without it, this code will look like this:
HFONT hFont = CreateFont(...);HGDIOBJ hOldFont = SelectObject(hDC, hFont);// Use hFont hereSelectObject(hDC, hOldFont);DeleteObject(hFont);  
I suggest you just use windowsx.h, for your own safety.

[edited by - IndirectX on March 30, 2002 11:22:19 AM]
---visit #directxdev on afternet <- not just for directx, despite the name
thanx alot for your help !!!

edit => when I added your code I got this warning :

warning C4700: local variable 'hdc' used without having been initialized

although it's just a warning I don't see why he's giving it

this is my new DrawText()

void DrawText(char *text, int size, int x, int y, COLORREF color){	HDC hdc ;	HFONT hfont = CreateFont(size,0,0,0,0,0,0,0,0,0,0,0,0, "Arial") ;	HFONT hOldFont = SelectFont(hdc, hfont) ; // <== warning here	//this function draws text in 16-bit DX mode	// with the selected font	// get the dc from surface	lpDDSBack->GetDC(&hdc) ;	// set the colors 	SetTextColor(hdc,color) ;	// set background mode to transparent	// so black isn't copied	SetBkMode(hdc, TRANSPARENT) ;	// draw the text using the font	SelectObject(hdc, hfont);	TextOut(hdc,x,y,text,strlen(text)) ;	// release the dc	lpDDSBack->ReleaseDC(hdc) ;	//delete the font	SelectFont(hdc, hOldFont) ; 	DeleteFont(hfont) ;} // end DrawText  


[edited by - da_cobra on March 31, 2002 4:39:43 AM]

[edited by - da_cobra on March 31, 2002 4:40:13 AM]
Well the thing is you need to call "lpDDSBack->GetDC(&hdc);" first (ie before selectObjecting) That initializes hdc, so you can actually work withit.


- Sleepwalker
- Sleepwalker

This topic is closed to new replies.

Advertisement