GLBasic forum

Main forum => GLBasic - en => Topic started by: Charlie on 2011-May-28

Title: Tile Map Scrolling Issue
Post by: Charlie on 2011-May-28
I'm drawing tile maps stored in a 3 dimensional array. Third dimension is for added layers.
It is drawing fine and scrolling fine, as long as every tile in the map is assigned an image tile.
But if I leave a map position blank "0", then when the tile map scrolls, the blank portions jerk and never scroll off the screen while the rest of the map does.
Below is my drawing and scrolling code. Any ideas?

Code (glbasic) Select
//MAIN GAME LOOP

WHILE KEY(01) = FALSE

//Draw Tile Map
FOR y = 0  TO tilesDown +1
FOR x = 0  TO tilesAcross +1
IF map[x][y][1].tile <> 0
DRAWSPRITE map[x + tileAddX][y + tileAddY][1].tile, (x*tileSize) + ScrollAmountX, (y*tileSize) + ScrollAmountY
ENDIF
NEXT
NEXT


//Go Right
IF KEY(32) OR KEY(205)
ScrollAmountX = ScrollAmountX - 2
IF ScrollAmountX < tileSize - (tileSize * 2)
ScrollAmountX = 0
tileAddX = tileAddX + 1
ENDIF
ENDIF

//Go Left
IF KEY(30) OR KEY(203)
IF tileAddX > 0
ScrollAmountX = ScrollAmountX + 2
ENDIF
ENDIF

IF ScrollAmountX > 0
ScrollAmountX = -48
IF tileAddX > 0
tileAddX = tileAddX - 1
ENDIF
ENDIF

//Go Up
IF KEY(31) OR KEY(208)
ScrollAmountY = ScrollAmountY - 2
IF ScrollAmountY < tileSize - (tileSize * 2)
ScrollAmountY = 0
tileAddY = tileAddY + 1
ENDIF
ENDIF

//Go Down
IF KEY(17) OR KEY(200)
IF tileAddY > 0
ScrollAmountY = ScrollAmountY + 2
ENDIF
ENDIF

IF ScrollAmountY > 0
ScrollAmountY = -48
IF tileAddY > 0
tileAddY = tileAddY - 1
ENDIF
ENDIF


SHOWSCREEN


WEND
Title: Re: Tile Map Scrolling Issue
Post by: Charlie on 2011-May-28
my "simple" solution is to draw a white tile on the blank positions (white being transparent in my game) ...they will not show through but this is not very efficient lol
Title: Re: Tile Map Scrolling Issue
Post by: bigsofty on 2011-May-29
I don't think it has anything to do with blank tiles, the vars ScrollAmountX and           ScrollAmountY are being continuously reset to -48. This may be the cause of your jerking.
Title: Re: Tile Map Scrolling Issue
Post by: Slydog on 2011-May-30
As long as the tile size is 48, this should be good:
Code (glbasic) Select
ScrollAmountX = -48but why not use the variable:
Code (glbasic) Select
ScrollAmountX = -tileSize
[Edit] However, I would put that range checking inside the left key check, just like you did for the right movement, or else take it out of the right movement check, to make them the same. (plus do it for the up/down) (unless there is a good reason not to, I don't know your code)

Also, for simplicity, this:
Code (glbasic) Select
IF ScrollAmountY < tileSize - (tileSize * 2)  //eg: = 32 - (32 * 2) = 32 - (64) = -32 = -tileSizeis the same as:
Code (glbasic) Select
IF ScrollAmountY < -tileSize

But I think your problem has something to do with your main loop:
Code (glbasic) Select
FOR y = 0  TO tilesDown +1
  FOR x = 0  TO tilesAcross +1
    IF map[x][y][1].tile <> 0
      DRAWSPRITE map[x + tileAddX][y + tileAddY][1].tile, (x*tileSize) + ScrollAmountX, (y*tileSize) + ScrollAmountY
    ENDIF
  NEXT
NEXT

The 'IF' statement checks a certain array position, but then your DRAWSPRITE command references another array location.
What is the purpose of 'tileAddX' and 'tileAddY'?
Is this to keep track of how many tiles you have scrolled in each direction?
If so, then why wouldn't your 'IF' statement be:
Code (glbasic) Select
    IF map[x + tileAddX][y + tileAddY][1].tile <> 0

But, a good start for a fairly complicated game concept, tile map scrolling!
Title: Re: Tile Map Scrolling Issue
Post by: Slydog on 2011-May-30
Also, you may want to add a range check in your loop to not draw any sprite off screen.
Something like:
Code (glbasic) Select
min_x =  ...
max_x =  ...
min_y =  ...
max_y =  ...

FOR y = 0  TO tilesDown +1
  FOR x = 0  TO tilesAcross +1
    IF x<min_x or x>max_x THEN CONTINUE
    IF y<min_y or y>max_y THEN CONTINUE
    IF map[x][y][1].tile = 0 THEN CONTINUE
    DRAWSPRITE map[x + tileAddX][y + tileAddY][1].tile, (x*tileSize) + ScrollAmountX, (y*tileSize) + ScrollAmountY
  NEXT
NEXT


Or only loop through the valid ranges to begin with:
Code (glbasic) Select
FOR y = min_y TO max_y
  FOR x = min_x TO max_x
    IF map[x][y][1].tile = 0 THEN CONTINUE
    DRAWSPRITE map[x + tileAddX][y + tileAddY][1].tile, (x*tileSize) + ScrollAmountX, (y*tileSize) + ScrollAmountY
  NEXT
NEXT


The hard part of course if figuring out the min/max for x and y.
You'll generally want to draw one extra tile width/height than the screen holds, for partial tiles.
Title: Re: Tile Map Scrolling Issue
Post by: Charlie on 2011-Jun-03
Haha thanks Slydog. I was wrecking my brain over an obvious error. You were correct in that:

Code (glbasic) Select
FOR y = 0  TO tilesDown +1
  FOR x = 0  TO tilesAcross +1
    IF map[x][y][1].tile <> 0
      DRAWSPRITE map[x + tileAddX][y + tileAddY][1].tile, (x*tileSize) + ScrollAmountX, (y*tileSize) + ScrollAmountY
    ENDIF
  NEXT
NEXT


the if statement need the tileAddX and tileAddY added to the x and y as well.
tileAddX and tileAddY do in fact keep up with the number of tiles that have been scrolled. I started with a static map and later forgot about adding the offsets in there. Just couldn't catch it lol.

Oh, and it already only draws the tiles that are onscreen. tilesDown and tilesAcross are the number of tiles on screen (based on screen resolution).

Quote from: bigsofty on 2011-May-29
I don't think it has anything to do with blank tiles, the vars ScrollAmountX and           ScrollAmountY are being continuously reset to -48. This may be the cause of your jerking.

This was fine. (I understand I didn't explain what any of my variables do lol)...This enables smooth per pixel scrolling as opposed to tile by tile scrolling. Once it reaches a certain point it adds or subtracts a tile offset value to tileAddX and tileAddY and then it resets.

This isn't my first time doing a tile map...I just didn't catch my error and needed another eye. Thanks again.