Does this code look right for centering an image on the screen (in this case a chess board):

int Chess::Board::Center(int width,int height)
{
	if(m_texture == NULL)
		return 0;

	D3DXVECTOR2 center;
	center.x = (float)width/2.0f;
	center.y = (float)height/2.0f;

	m_position.x = center.x - (m_texture->GetWidth())/2;
	m_position.y = center.y - (m_texture->GetHeight())/2;

	return 1;
}

Chess is a namesapce, Board is a class, and Center is a method of Board.

width and height are the width and height of the screen, and m_position is a 2-coordinate vector (D3DXVECTOR2, for any DirectX people) containing the coordinates of where the upper-left hand corner of the image is.

m_texture is a pointer to a Texture, a wrapper class for the IDirectTexture9 interface.

Any ideas? The board isn't centered when I run the program.

Recommended Answers

All 5 Replies

Your algorithm seems to be correct. How are you calling the method?

Your algorithm seems to be correct. How are you calling the method?

Hi, sorry I haven't responded for a while.

The method is called here:

if(!chess_board->Center(SCREEN_WIDTH,SCREEN_HEIGHT))
		return 0;

SCREEN_WIDTH and SCREEN_HEIGHT are defined here:

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

they are used to call the CreateWindow function here:

if((hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Chess",myStyle,CW_USEDEFAULT,CW_USEDEFAULT,SCREEN_WIDTH,SCREEN_HEIGHT,NULL,NULL,hInstance,NULL)) == NULL)
	{
		MessageBox(NULL,"Error creating the window","Error",MB_OK | MB_ICONERROR);
		return 0;
	}

and are passed to the function GraphicsInit() here:

if(!GraphicsInit(hwnd,SCREEN_WIDTH,SCREEN_HEIGHT,!FULLSCREEN))
		return 0;

which uses them in the presentation parameters here:

d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;

width and height are the parameters that SCREEN_WIDTH and SCREEN_HEIGHT were passed to in the code two snippets above.


I've tried this with two images, one I got off the internet, and the other I drew myself. Both didn't work. I also used the debugger, and the values that are returned by the GetWidth() and GetHeight() for m_texture match the values that the images are (according to Windows and Paint).

I've tried this procedure and technique for several times. It doesn't work. If possible, please repost it. Maybe there is typing error

I've tried this procedure and technique for several times. It doesn't work. If possible, please repost it. Maybe there is typing error

Err, what procedure exactly? The centering function?

If you've tried it multiple times and it doesn't work - then why do you think there's a typo? It doesn't work for me either, so that would indicate there's an error in the logic I'm using (which I can't find).

What exactly do you mean by: it's not working? Do you mean it won't compile? If so, it's because I only gave you a fraction of the code. The actual code in its entirety is much larger.

Just fyi, I did some tests to see if the size of the window was actually SCREEN_WIDTH x SCREEN_HEIGHT. I made a 5x5 image and drew it on the screen at SCREEN_WIDTH-5 and SCREEN_HEIGHT-5 (I didn't actually write that - I wrote 635 and 475). At first it didn't work, but then I tried bringing the values back, and slowly approached 640 and 480. When I finally got to 635x475, I could see the square, but it was partially covered, as values a bit less that 640 and 480 showed the box close up the space with the edge of the window at 632 x. But I think the difference is due to the way DirectX draws images or makes the the device(or how windows makes the window). The amount of pixels that the board is off-center is much more than a few pixels.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.