how do i scroll tile map?

Started by
4 comments, last by computer_guy 21 years, 2 months ago
ive set up my map like so, int map[12][12] and to draw it i do as such, for(int downx =0; downx<12; downx++) { for(int downy=0; downy<12; downy++) { curr_tile = map[downx][downy]; if(curr_tile == 1) { tile = tile1; } if(curr_tile == 2) { tile = tile2; } tile.x=downx*32; tile.y=downy*32; // Drawing Bitmap function // Draw_Bitmap16(&tile,back_buffer,back_lpitch,1); } } HOW DO I SCROLL IT ??? sorry about the long post..
I'll Be Back
Advertisement
Heres a few pointers. The way I do my scrolling I have a POINT variable for which tile is the upperleft. I use that in my for loops where you are assigning downx,y to 0. Then I have the a variable that is the number of tiles that will fit on the screen so I loop until it gets to the UpperLeftTile+MaxTiles. The other POINT variable (I call this ScrollPix)I used keeps track of how many pixels to the right of the start of the tile it is scrolled. If say the upperleft tile is 3,3(and is 32x32) and the ScrollPix.x is at 30 and then i scroll to the right 5 pixels that makes the upperleft tile 4,3 and scrollpix.x 3. I also have another POINT variable that is simply the number of pixels scrolled from the origin. When I call the scroll function I pass it the number of pixels(positive or negative) that I want to scroll and it adds those on to AbsScrollPix and uses that to figure out what is the upperlefttile and how much to offset that tile(ScrollPix)

Heres my scrolling code
void DxMap::Scroll(int x, int y){	Mapinfo->AbsScrollPix.x += x;	Mapinfo->AbsScrollPix.y += y;	//bounds checking	//left	if(Mapinfo->AbsScrollPix.x < 0)		Mapinfo->AbsScrollPix.x = 0;	//right	if(Mapinfo->AbsScrollPix.x > (Map->size()*Mapinfo->TileSize.x - (Mapinfo->ViewPort.right - Mapinfo->ViewPort.left)))		Mapinfo->AbsScrollPix.x = (Map->size()*Mapinfo->TileSize.x - (Mapinfo->ViewPort.right - Mapinfo->ViewPort.left));	//top	if(Mapinfo->AbsScrollPix.y < 0)		Mapinfo->AbsScrollPix.y = 0;	//bottom	if(Mapinfo->AbsScrollPix.y > (*Map)[0].size()*Mapinfo->TileSize.y - (Mapinfo->ViewPort.bottom-Mapinfo->ViewPort.top))		Mapinfo->AbsScrollPix.y = (*Map)[0].size()*Mapinfo->TileSize.y - (Mapinfo->ViewPort.bottom-Mapinfo->ViewPort.top);	Mapinfo->UpperLeft.x = Mapinfo->AbsScrollPix.x / Mapinfo->TileSize.x;	Mapinfo->ScrollPix.x = Mapinfo->AbsScrollPix.x % Mapinfo->TileSize.x;	Mapinfo->UpperLeft.y = Mapinfo->AbsScrollPix.y / Mapinfo->TileSize.y;	Mapinfo->ScrollPix.y = Mapinfo->AbsScrollPix.y % Mapinfo->TileSize.y;	ObjList->UpdateScreenPos();}  


here is how i set my dest rect

	SetRect(&dest,x*Layerinfo.Mapinfo->TileSize.x+Layerinfo.Mapinfo->ViewPort.left-Layerinfo.Mapinfo->ScrollPix.x,		y*Layerinfo.Mapinfo->TileSize.y+Layerinfo.Mapinfo->ViewPort.top-Layerinfo.Mapinfo->ScrollPix.y,		x*Layerinfo.Mapinfo->TileSize.x+Layerinfo.Mapinfo->TileSize.x+Layerinfo.Mapinfo->ViewPort.left-Layerinfo.Mapinfo->ScrollPix.x,		y*Layerinfo.Mapinfo->TileSize.y+Layerinfo.Mapinfo->TileSize.y+Layerinfo.Mapinfo->ViewPort.top-Layerinfo.Mapinfo->ScrollPix.y);  




[edited by - Brad8383 on February 9, 2003 12:03:02 AM]
I hope this doesn''t sound too beginnerish, (sp), I''m still trying to get the scrolling theory down. Do you draw the ENTIRE map to the buffer and then just "move" it when scrolling is needed, or do you draw new tiles as they become "visible" to the player?

I''m pretty sure your code explains this itself, i''m just not sure, and haven''t had enough time to examine it.

Kris
ForceFedDrummer (I need to register!)
ok, now iv''e changed the code so it looks like what most people do but don''t know how to scroll the map. Please help!

for(downx; downx<12; downx++)
{
for(int downy=0; downy<12; downy++)
{

curr_tile = map[downx][downy];

// select source position to grab bitmap from
if(curr_tile == 1)
{
source_x = 0;
source_y = 0;
}
if(curr_tile == 2)
{
source_x = 32;
source_y = 0;
}


// select destination position to put to
dest_x = downx*32;
dest_y = downy*32;


// select width and height of rectangle
int width = 32;
int height = 32;

// set up rectangles
RECT source_rect, dest_rect;

source_rect.left = source_x;
source_rect.top = source_y;
source_rect.right = source_x + width;
source_rect.bottom = source_y + height;

dest_rect.left = dest_x;
dest_rect.top = dest_y;
dest_rect.right = dest_x + width;
dest_rect.bottom = dest_y + height;

// perform the blit from back to primary
lpddsprimary->Blt(&dest_rect, lpddsback, &source_rect, DDBLT_KEYSRC | DDBLT_WAIT, NULL);

}
}
I'll Be Back
I have the following method to implement scrolling - and it allows partial tiles on the screen.

1. Store the distance from the top corner of the screen to the top corner of the map (MapLeft, MapTop)

2. When Drawing the map tile by tile - calculate the normal top left of the tile, then subtract the MapLeft, MapTop from the coordinates. This gives the coordinates of the tile relative to the screen.

My Code (Delphi) - Tile size 128x64

For J := 0 to 80 do //Columns
For I := 0 to 30 do //Rows
Begin
X := I * 128;
Y := (J * 32);
If J mod 2 = 1 then
X := X + 32
Else
X := X - 32;

Cell[I,J].X := X;
Cell[I,J].Y := Y;
X := X - MapLeft;
Y := Y - MapTop;
If (X > -128) and (X < Width) and
(Y > -256) and (Y < Height-128) then
Begin
Cell[I,J].Draw;
End;
End;

By setting the MapTop, MapLeft the map can be scrolled neatly accross the screen.

Note: MapTop and MapLeft are pixel values not tile values.
My suggestion is to experiment with tile at a time scrolling. To do this you need a variable to keep track of what tile is in the upper left. Say you call this value UpperLeft_x and _y. You would also need a value for the max tiles on a screen. one value for max in the x direction and one for the y direction. You could hardcode that I guess. But once you have that your for loops look like this

for(int downx = UpperLeft_x;downx < UpperLeft_x + TilesPerScreen_x;x++)

and in your scroll function you just add or subtract to UpperLeft depeneding on which way you want to go but be sure to do bounds checking. UpperLeft cant be less that zero and cant be greater than the max tiles on the map - the tiles that can fit on a screen.

This topic is closed to new replies.

Advertisement