Jump to content
  • Advertisement
Sign in to follow this  

Trouble with print fonts to screen.

This topic is 3162 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

I am making a tetris clone and I'm doing fine with drawing sprites for the blocks, but printing text to the screen is doing something weird. If I print the font, and don't draw any sprites the font draws right where it should, but if I draw the sprites and draw the fonts it draws positioned relative to the moving block sprite. It also moves along with the block when I control it with the keys. All relevant code is posted below.

void FontPrint(LPD3DXFONT font, int x, int y, string text, D3DCOLOR color)
    //figure out the text boundary
    RECT rect = { x, y, 0, 0 };
    font->DrawText(NULL, text.c_str(), text.length(), &rect, DT_CALCRECT, color); 

    //print the text
    font->DrawText(spriteobj, text.c_str(), text.length(), &rect, DT_LEFT, color); 

void draw_map()
	//draw preview piece
	for(int px=0; px < 4; px++)
		for(int py=0; py < 4; py++)
			if(sPrePiece.size[px][py] != TILENODRAW)
				draw_block(sPrePiece.x + px, sPrePiece.y + py, sPrePiece.size[px][py]);
	//draw map
	for(int mx=0; mx < MAPWIDTH; mx++)
		for(int my=0; my < MAPHEIGHT; my++)
			if(map[mx][my] != TILENODRAW)
				draw_block(mx, my, map[mx][my]);

	//draw moving piece
	for(int x=0; x < 4; x++)
		for(int y=0; y<4; y++)
			if(sPiece.size[x][y] != TILENODRAW)
				draw_block(sPiece.x + x, sPiece.y + y, sPiece.size[x][y]);


void draw_block(int x, int y, int block)
	Sprite_Transform_Draw(imgBlocks, x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE, block, 9);

void Game_Run(HWND window)
    //make sure the Direct3D device is valid
    if (!d3ddev) return;

    //update input devices
	if(GetTickCount() - starttime > 1000)
		starttime = GetTickCount();

    //clear the scene
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

    //start rendering
    if (d3ddev->BeginScene())


		FontPrint(fontArial15, 25, 5, "Score: ");
		FontPrint(fontArial15, SCREENW - 200, 50, "Next Piece:");

        //stop rendering
        d3ddev->Present(NULL, NULL, NULL, NULL);

	if (KEY_DOWN(VK_ESCAPE)) gameover = true;
    //controller Back button also ends
    if (controllers[0].wButtons & XINPUT_GAMEPAD_BACK)
        gameover = true;


I can't figure out why the font is drawn this way.

Share this post

Link to post
Share on other sites
All relevant code is posted

That's probably not true.

If draw_map() somehow uses spriteobj, try putting a spriteobj->Flush() call after the draw_map() function.

Alternately, don't use the sprite to draw the text. Use font->DrawText(NULL,...).

Share this post

Link to post
Share on other sites
I'll try your two suggestions and see what I get.

And no I'm not doing any transformation to the sprites.

Share this post

Link to post
Share on other sites

void Sprite_Transform_Draw(LPDIRECT3DTEXTURE9 image, int x, int y, int width, int height,
int frame, int columns, float rotation, float scaling, D3DCOLOR color)
//create a scale vector
D3DXVECTOR2 scale( scaling, scaling );

//create a translate vector
D3DXVECTOR2 trans( x, y );

//set center by dividing width and height by two
D3DXVECTOR2 center( (float)( width * scaling )/2, (float)( height * scaling )/2);

//create 2D transformation matrix
D3DXMatrixTransformation2D( &mat, NULL, 0, &scale, &center, rotation, &trans );

//tell sprite object to use the transform
spriteobj->SetTransform( &mat );

//calculate frame location in source image
int fx = (frame % columns) * width;
int fy = (frame / columns) * height;
RECT srcRect = {fx, fy, fx + width, fy + height};

//draw the sprite frame
spriteobj->Draw( image, &srcRect, NULL, NULL, color );

Using font->DrawText(NULL,...) worked, however using spriteobj->Flush() after drawMap() did not change anything.

Share this post

Link to post
Share on other sites
spriteobj->SetTransform( &mat );

This transform was being applied to the font call. That's why font->Draw(NULL...) fixed the problem.

When you set a sprite transform, it's applied to anything drawn by the sprite thereafter.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • 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!