Jump to content

  • Log In with Google      Sign In   
  • Create Account

MoBaT

Member Since 31 Jan 2013
Offline Last Active Feb 15 2013 06:35 PM

Posts I've Made

In Topic: Load MAP around player.

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);


		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);

					}
				}
			}
		}
}

In Topic: Load MAP around player.

09 February 2013 - 01:18 PM

Accidently posted twice.


In Topic: Load MAP around player.

09 February 2013 - 12:46 PM

Finally, I tried doing something similar but probably failed at it. I got it working with this code. Thanks for your hard work, I appreciate it. I have one more questions involving the camera. Since the code now works and draws the map around the character, how would I got by implementing the camera so the player stays on the middle of the screen at all times in my 800 x 600 windows. I thought of just going to my player render code and subtracting the offset the character was at it can stay in the middle, but that sounds like a dirty way.

 


                const int minX = 0;
		const int maxX = map->GetHeight();

		const int minY = 0;
		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 < minX) startX = minX;
		if((startX + showWidth) > maxX) startX = (maxX - showWidth);

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

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

 

 

 

I can keep the map on my screen by replacing the offset in my loop from:

    

    offset2.x = x * TileZoom;
    offset2.y =y * TileZoom;
    offset2.w = TileZoom;
    offset2.h = TileZoom;

to

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

In Topic: Load MAP around player.

08 February 2013 - 10:51 PM

I'm not sure I fully understand what method you're using. You're talking about loading maps, but it looks like you are showing code for drawing maps.
You said, "so basically you have map files from like 1-100". Well, that might work for a linear side-scrolling game, but my game is a 2D RPG, so my chunks have to have 2D numbers, like chunk (0,0), and chunk (0,1).
Let's imagine for a second that there are who knows how many areas in the game, and each 'Area' is identified by a string, like "Great Forest".
Now let's imagine that each area (for simplicity) is broken into 10 by 10 chunks, and each chunk is 20 by 20 (or 10x10 or whatever) tiles.
So if the player is at "Green Forest" in chunk (-4, 3), on tile (15, 12), then that means chunk (-4, 3) is loaded and each chunk around that:


 
 
If you could explain the method you're currently using in more detail, perhaps I could find the problem. All I currently see is two for() loops with nothing in them that could be doing almost anything. Your previous code post looked more like drawing the map and not loading the map. I'm not exactly sure what part of your code you are having difficulty with? Perhaps you have your drawing code too tightly integrated into your loading code and it's hard for you to separate them? The loading of the map should not be in the same functions as the drawing of the map.


Sorry I'm not being that clear. I mean I want to draw the map around my player similar to what you showed above in the images but using the code I have provided above. That's what my two for loops were above in my second post. I tried getting the characters position and then loading 10 tiles before him and 10 tiles after him. Same thing for the y axis.

In Topic: Load MAP around player.

07 February 2013 - 11:09 PM

So basically you have map files from like 1 - 100 let's say and each file is 20 x 20 tiles. Then you load the maps depending on the area they're at. 

What I tried doing was something like:
 

for (int y = playerYTILE - 10; y < playerYTILE + 20; ++y) 
{
  for (int x = playerYTILE - 10; x < playerYTILE + 10; ++x) 
  {
    //CODE
  }
}

 

But when I was walking, my character was moving faster than the map loading itself, which does not make sense to me since it uses the tile it's on. I could implement a chunk system, but spent some time trying to get this method working.


PARTNERS