Sign in to follow this  
adam23

Scrolling Game Question

Recommended Posts

I have been working on a 2D real time strategy game that has a world you can scroll around with the mouse. I have buildings, soldiers, and enemy's that all have relative world locations, but I find that when I update my current window position I update it in all of the sub classes as well. It works most of the time, but I find that some of objects seem to travel with the window a little bit. Does anyone have suggestions on a good way to position objects in a world, and then draw them relative to the current window position. This is part of my class that manages all the buildings
void BuildingManager::Render()
{
	D3DXMATRIX temp;
	D3DXVECTOR2 scale(0.5f,0.5f);
	D3DXMatrixTransformation2D(&temp, NULL,0.0f,&scale, NULL, 0.0f, NULL);
	g_engine->GetSprite()->SetTransform(&temp);
	while(m_list->Iterate())
	{
		m_list->GetCurrent()->Render(m_window);
	}
}
void BuildingManager::setWindow(RECT win)
{
	m_window = win;
}

This is my temple class that seems to travel a bit when the window scrolls
	virtual void Render(RECT &win)
	{
		//Make sure this function is called between sprite begin
		//and end
		D3DXVECTOR3 currPos;
		currPos.x = m_position.x - win.left;
		currPos.y = m_position.y - win.top;
		currPos.z = m_position.z;
		if(currPos.y > -300.0f && currPos.y < win.bottom && currPos.x > -200.0f && currPos.x < win.right)
			g_engine->GetSprite()->Draw(Data::Instance()->getTemple(), NULL, NULL, &currPos, 0xFFFFFFFF);
		
	}

As you can see I subtract the window position from the objects current top left position. In theory this should work. Here is how I am updating my window
		//Check for level scrolling
		if(g_engine->GetInput()->GetPosX()<5)
		{
			if(window.left >=2)
			{
				window.left-=1;
				window.right -=1;
			}
		}
		if(g_engine->GetInput()->GetPosX()>795)
		{
			if(window.right<2046)
			{
				window.left += 1;
				window.right += 1;
			}
		}
		if(g_engine->GetInput()->GetPosY()<5)
		{
			if(window.top >=2)
			{
				window.top-=1;
				window.bottom -=1;
			}
		}
		if(g_engine->GetInput()->GetPosY()>570)
		{
			if(window.bottom<2046)
			{
				window.top += 1;
				window.bottom += 1;
			}
		}

Then in my main game I call setWindow so all subclasses are set. The more I think about how I am doing this the more I hate it. I need to restructure my program to make this work does anyone have any suggestions. I basically some suggestions on methods to get the relative camera position. Thanks in advance

Share this post


Link to post
Share on other sites
Looks good to me...maybe try posting the code where you call setWindow and BuildingManager's Render function. Is it possible that you're rendering your background once without calling BuildingManager's setWindow, and then again this time with calling it?

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