SFML View Problem

I want the Camera to stop when the camera hits the edges of the map. Here is the code i have to stop it.
1
2
3
4
5
6
7
8
9
void UpdateGame()
{
	objectManager.Update(mainWindow);
	CameraView.SetCenter(scroll_X, scroll_Y);
	if(CameraView.GetRect().Top <= 0)
		CameraView.SetCenter(scroll_X, SCREENHEIGHT/2);
	if(CameraView.GetRect().Left <= 0)
		CameraView.SetCenter(SCREENWIDTH/2, scroll_Y);
}


For some reason, the Left coordinate becomes 0, but the Top coordinate stays at -240. I tried changing the second paramter from SCREENHEIGHT/2 to just SCREENHEIGHT, and the Top coordinate still stays at -240. What am i doing wrong?
Try something like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
void UpdateGame()
{
    objectManager.Update(mainWindow) ;
    CameraView.SetCenter(scroll_X, scroll_Y) ;

    unsigned adjusted_x=scroll_X, adjusted_y=scroll_Y ;
    if ( CameraView.GetRect().Top <= 0 )
        adjusted_y = SCREENHEIGHT/2 ;
    if ( CameraView.GetRect().Left <=0 )
        adjusted_x = SCREENWIDTH /2 ;

    CameraView.SetCenter(adjusted_x, adjusted_y) ;
}


When both top and left were where you needed to adjust coordinates for each, you undid the y change in the second if.
Thank you! And if its not too much trouble could you tell me if you see anything wrong with this code? Its suppose to draw the part of the map that the cameraview can see. Instead the map is just not drawn.

1
2
3
4
5
6
7
8
9
10
11
12
	for( yPixel = window.GetView().GetRect().Top, mapTileY = yPixel/TILESIZE; yPixel < window.GetView().GetRect().Bottom; yPixel+=TILESIZE){
		for( xPixel = window.GetView().GetRect().Left, mapTileX = xPixel/TILESIZE; xPixel < window.GetView().GetRect().Right; xPixel+=TILESIZE){
			if( tiles[mapTileX][mapTileY].sprite.GetImage() != NULL)
			{
				tiles[mapTileX][mapTileY].sprite.SetPosition(xPixel,yPixel);
				window.Draw(tiles[mapTileX][mapTileY].sprite);
			}
                        mapTileX++;
		}
                mapTileY++;
	}
}
Last edited on
Topic archived. No new replies allowed.