Sign in to follow this  
Sk1d_Row

Direct3D background scrolling 2D trouble [resolved]

Recommended Posts

I have been trying to write a 2D graphics engine that will let me keep a car in the center of the screen while the viewport scrolls as the player moves. I got this to work when I would display the entire world (2560x2560), but I know how that is extremely inefficient, so now I'm trying to implement just drawing what is in my viewport. The problem that results from my code is that it only gives me 1 viewport size. It will scroll for that little bit that is displayed. Its just not quite the effect I need...In other words, as it scrolls I want ones off the screen to not be drawn but when the viewport gets really close it should draw them. EDIT: I am using ID3DXSPRITE...
void render()
{
	D3DXMATRIX mat;
	D3DXVECTOR2 MapVector(-map_xPos, -map_yPos); // these change to arrow key input
	D3DXVECTOR2 SpritePos(xPosition, yPosition); // center of the screen for car position

	D3DXVECTOR2 x_offset(TILESIZE, 0.0f); // offsets the position of tiles
	D3DXVECTOR2 y_offset(-(TILESIZE * VIEWWIDTH), TILESIZE);

	int xPos = (int)map_xPos / TILESIZE; // just so I know what tile to access
	int yPos = (int)map_yPos / TILESIZE; // since the map is stored in a 2D array	

	g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0 );

	g_pd3dDevice->BeginScene();

	g_pMapSprite->Begin(D3DXSPRITE_ALPHABLEND); // draws map in tiles

		for(int y = 0; y < VIEWHEIGHT; ++y) // only so many tiles for height of view
		{
			for(int x = 0; x < VIEWWIDTH; ++x) // only so many tiles for width of view
			{
				D3DXMatrixTransformation2D(&mat, NULL, NULL, NULL, NULL, NULL, &MapVector);
				g_pMapSprite->SetTransform(&mat);

				g_pMapSprite->Draw(	g_pBackground[ pp_map[yPos + y][xPos + x] ], // 2D array that stores the map tiles
									NULL, 
									NULL,
									NULL, 
									D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, 1.0f) );	

				MapVector += x_offset; // offsets over tile width to draw the next
			}

			MapVector += y_offset; // puts it back to first column then goes down 1 row
		}

	g_pMapSprite->End();


	D3DXMatrixTransformation2D(&mat, NULL, NULL, NULL, NULL, NULL, &SpritePos);
	g_pSprite->SetTransform(&mat);

	g_pSprite->Begin(D3DXSPRITE_ALPHABLEND); // draws my car

		g_pSprite->Draw(	g_pSTexture, 
							NULL, 
							NULL,
							NULL, 
							D3DCOLOR_COLORVALUE(1.0f, 1.0f, 1.0f, 1.0f) );

	g_pSprite->End();

	g_pd3dDevice->EndScene();

	g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
}








Thanks for your time and consideration! [Edited by - Sk1d_Row on November 5, 2005 6:01:58 PM]

Share this post


Link to post
Share on other sites
My problem was I wasnt keeping track of the textures coordinates to offset it by...I accomplished this by using a 2D array of structs...I think I can actually do it without that...but either way I solved my own problem :)

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