Direct3D background scrolling 2D trouble [resolved]

Started by
0 comments, last by Sk1d_Row 18 years, 5 months ago
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]
Advertisement
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 :)

This topic is closed to new replies.

Advertisement