Sign in to follow this  
scone

Trouble with print fonts to screen.

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
    DirectInput_Update();
	if(GetTickCount() - starttime > 1000)
	{
		move(0,1);
		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())
    {
		spriteobj->Begin(D3DXSPRITE_ALPHABLEND);

		draw_map();


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

        //stop rendering
        d3ddev->EndScene();
        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
Quote:
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

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
D3DXMATRIX mat;
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
Quote:
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this