Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualMoBaT

Posted 10 February 2013 - 04:25 PM

I understood the code you gave me and it worked fine with displaying the map around the character but what I meant was this:

Now the code you gave me displays the map around the character but the character dosent stay on my screen and can move out of the bounds becuase of the map loading around the character. 

mrPwU7j.png
 

When I add this code:

    offset2.x = (x - startX) * TileZoom;
    offset2.y =(y - startY) * TileZoom;
    offset2.w = TileZoom;
    offset2.h = TileZoom;

 

The map stays within my 800 x 600 screen but the character can venture off and it's not in the middle.

h3Ak7jV.png

 

Will I have to do some calculations for the player so it can stay in the middle of my screen? Let me just show my whole code instead of a partial bit.
 



void Map::OnRender(SDL_Surface* Surf_Display, int MapX, int MapY)
{
	//Rectangles
	SDL_Rect offset;
	SDL_Rect offset2;

	// Loop through laters
	for (int i = 0; i < map->GetNumLayers(); ++i) {
		const Tmx::Layer *layer = map->GetLayer(i);

		const int maxX = map->GetHeight();
		const int maxY = map->GetHeight();
		const int showWidth = 15;
		const int showHeight = 13;

		//Starting point of tiles.
		int startX = (MapX / (TILE_SIZE * ZOOMSCALE) - (showWidth / 2));
		if (startX <= 0) startX = 0;
		int startY = (MapY / (TILE_SIZE * ZOOMSCALE))- (showHeight / 2);
		if (startY <= 0) startY = 0;
	
		//Keep camera in bounds of the map.
		if(startX < 0) startX = 0;
		if((startX + showWidth) > maxX) startX = (maxX - showWidth);

		if(startY < 0) startX = 0;
		if((startY + showWidth) > maxY) startY = (maxY - showWidth);

		//Ending point of tiles.
		int endX = (startX + showWidth);
		int endY = (startY + showHeight);


		for (int y = startY; y < endY; ++y) 
		{
			for (int x = startX; x < endX; ++x) 
			{
				// Get tileID
				int TileID = layer->GetTileId(x, y);

				if (TileID != 0) {
		
					int tilesetID = map->GetLayer(i)->GetTileTilesetIndex(x, y);

					int margin = map->GetTileset(tilesetID)->GetMargin() * ZOOMSCALE;
					int spacing = map->GetTileset(tilesetID)->GetSpacing() * ZOOMSCALE;

					int TileZoom = (TILE_SIZE * ZOOMSCALE);
					int TilesWidth = tileSet[tilesetID]->w / TileZoom;

					int tileRow = TileID % TilesWidth;
					int  tileColumn = ((TileID - tileRow) / TilesWidth);

					// Set tileset tile X, Y
					offset.x = ((tileRow * TileZoom) + tileRow * margin) + spacing;
					offset.y = ((tileColumn * TileZoom) + tileColumn * margin) + spacing;
					offset.w = TileZoom;
					offset.h = TileZoom;
				
					//Where to draw on gamemap
					offset2.x = (x - startX) * TileZoom;
					offset2.y =(y - startY) * TileZoom;
					offset2.w = TileZoom;
					offset2.h = TileZoom;

					//Blitz.
					SDL_BlitSurface(tileSet[tilesetID], &offset, Surf_Display, &offset2);

					}
				}
			}
		}
}

#1MoBaT

Posted 10 February 2013 - 04:24 PM

I understood the code you gave me and it worked fine with displaying the map around the character but what I meant was this:

Now the code you gave me displays the map around the character but the character dosent stay on my screen and can move out of the bounds becuase of the map loading around the character. 

mrPwU7j.png
 

When I add this code:

    offset2.x = (x - startX) * TileZoom;
    offset2.y =(y - startY) * TileZoom;
    offset2.w = TileZoom;
    offset2.h = TileZoom;

 

The map stays within my 800 x 600 screen but the character can venture off and it's not in the middle.

h3Ak7jV.png

 

Will I have to do some calculations for the player so it can stay in the middle of my screen? Let me just show my whole code instead of a partial bit.
 



void Map::OnRender(SDL_Surface* Surf_Display, int MapX, int MapY)
{
	//Rectangles
	SDL_Rect offset;
	SDL_Rect offset2;

	// Loop through laters
	for (int i = 0; i < map->GetNumLayers(); ++i) {
		const Tmx::Layer *layer = map->GetLayer(i);

		const int maxX = map->GetHeight();
		const int maxY = map->GetHeight();
		const int showWidth = 15;
		const int showHeight = 13;

		//Starting point of tiles.
		int startX = (MapX / (TILE_SIZE * ZOOMSCALE) - (showWidth / 2));
		if (startX <= 0) startX = 0;
		int startY = (MapY / (TILE_SIZE * ZOOMSCALE))- (showHeight / 2);
		if (startY <= 0) startY = 0;
	
		//Keep camera in bounds of the map.
		if(startX < 0) startX = 0;
		if((startX + showWidth) > maxX) startX = (maxX - showWidth);

		if(startY < 0) startX = 0;
		if((startY + showWidth) > maxY) startY = (maxY - showWidth);

		//Ending point of tiles.
		int endX = (startX + showWidth);
		int endY = (startY + showHeight);

		// Loop through tiles from tile (0, 0) to (20, 20);
		for (int y = startY; y < endY; ++y) 
		{
			for (int x = startX; x < endX; ++x) 
			{
				// Get tileID
				int TileID = layer->GetTileId(x, y);

				if (TileID != 0) {
		
					int tilesetID = map->GetLayer(i)->GetTileTilesetIndex(x, y);

					int margin = map->GetTileset(tilesetID)->GetMargin() * ZOOMSCALE;
					int spacing = map->GetTileset(tilesetID)->GetSpacing() * ZOOMSCALE;

					int TileZoom = (TILE_SIZE * ZOOMSCALE);
					int TilesWidth = tileSet[tilesetID]->w / TileZoom;

					int tileRow = TileID % TilesWidth;
					int  tileColumn = ((TileID - tileRow) / TilesWidth);

					// Set tileset tile X, Y
					offset.x = ((tileRow * TileZoom) + tileRow * margin) + spacing;
					offset.y = ((tileColumn * TileZoom) + tileColumn * margin) + spacing;
					offset.w = TileZoom;
					offset.h = TileZoom;
				
					//Where to draw on gamemap
					offset2.x = (x - startX) * TileZoom;
					offset2.y =(y - startY) * TileZoom;
					offset2.w = TileZoom;
					offset2.h = TileZoom;

					//Blitz.
					SDL_BlitSurface(tileSet[tilesetID], &offset, Surf_Display, &offset2);

					}
				}
			}
		}
}

PARTNERS