Trouble with print fonts to screen.

Started by
5 comments, last by Buckeye 14 years, 1 month ago
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.
http://Zombisoft.com coding for the apocalypse. http://sendmetoeurope.info Help me travel.
Advertisement
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,...).

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Are you transforming the sprite?
I'll try your two suggestions and see what I get.

And no I'm not doing any transformation to the sprites.
http://Zombisoft.com coding for the apocalypse. http://sendmetoeurope.info Help me travel.
What does your Sprite_Transform_Draw function do?
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.
http://Zombisoft.com coding for the apocalypse. http://sendmetoeurope.info Help me travel.
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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement