[SOLVED] Is tile_get_background broken?

Simon Gust

Member
What I am trying:

In my game I have a room filled with tiles on 3 different tile layers. Since I want my game to be prettier I would need even more tiles and tile layers. I didn’t want that, so I gave a new strategy a try:

In an object, find all the tiles on a layer with tile_get_ids_at_depth.
Iterate through the created array:

Code:
var bg = tile_get_background(tiles[i]);
var width = background_get_width(bg);
var wdt = tile_get_width(tiles[i]);
var hgt = tile_get_height(tiles[i]);
var lft = tile_get_left(tiles[i]);
var top = tile_get_top(tiles[i]);
var xx = tile_get_x(tiles[i]);
var yy = tile_get_y(tiles[i]);

//CONVERT DATA
var xps = xx / 16;
var yps = yy / 16;
var asset = bg;      
var index = ((top / hgt) * (width / wdt)) + (lft / wdt);

//SET DATA KEY
var key = scr_set_byte(asset,index); 

//OFFSET
var offset = 2 * (xps * (room_height / 16) + yps);

//WRITE DATA
buffer_poke(argument1,offset,buffer_u16,key);
I essentially save the background_index and the image_index in the same number and put it into a buffer.
The tiles are correctly put to their spaces in the buffer via poking.
Then on a chunk controller object I read that buffer and send it to an array:
Code:
for (var i = 0; i < wdt; i++)
{
   for (var j = 0; j < hgt; j++)
   {
      var offset = 2 * (i * hgt + j);
      var entry = buffer_peek(obj_global_control.buff_10000,offset,buffer_u16);
      global.cells[i,j] = entry;
   }
}
This works fine, and every spot is where it’s supposed to be =]

Then I create another array that holds areas of my room via chunks.
Then I look where the player is compared to that array of chunks and iterate around the player positions. Sort of a loading radius.
If a chunk that is close enough to be “seen”, I create a surface with my chunk array:
surf[a,b] = surface_create(SURF.SIZE,SURF.SIZE);
surface_set_target(surf[a,b]);
draw_clear_alpha(0,0);

Then I iterate through the global.cells array:
Code:
var key = global.cells[@ xx,yy];    
var tile = scr_get_byte(key,BIT.ASSET);
var index = scr_get_byte(key,BIT.INDEX);
var sprite = ds_map_find_value(obj_global_control.asset,tile);
if (!is_undefined(sprite))
{
   draw_sprite(sprite,index,xpos,ypos);
}
I use a ds_map to directly transfer the background_index to the sprite_index.

When I’m done iterating I reset the surface target and draw the surface to it’s correct position.
Yet some things are off.

What the problem is:
1. The tiles are not draw in the correct order to the surface no matter in which order I read the array from and draw them. Making some tiles originally in the front go in the back. I also tried to draw the other way around but I get the same result.

2. Tiles are breaking, they are refusing to be drawn either partially or even fully.
The red box is a sprite with an image index of 0, and it doesn’t like to be touched apparently.
One of the trees is just brutally cut and never seen again. Strange things.
ill1.png
ill2.png

My conclusions:
My guesses are there is something wrong with tile_get_background. Maybe it’s not functioning 100% of the time. It would be a shame if so...

Any help or info that I am missing would be nice :>

Update:
It seems that tiles aren't "breaking". Their order is just reversed and I have managed to draw them in correct order. So the red squares are solved. Now it's just the trees that get obliterated.
I have a new conclusion:
It may be that Tiles as big as the tree (64x128) have some of their areas get transfered to a different depth. When I only loaded the Midground (normal tiles) and the foreground (trees), the tree still got cut down. Of course I deleted my 3 tile layers. Strange...
 
Last edited:

Simon Gust

Member
Edit: SOLVED
Solution:
I'm dumb and got tricked again by how surfaces work.
Right on that tree there ends a surface so drawing onto that surface will not overlap to other surfaces.
So I just increased the area which is checked for keys in my array so that any surface can glance to their neighbour surface and
draw things essentially twice.

I splitted this into "normal tiles" iteration and "special tiles" iteration where special tiles are larger than 16x16 to keep performace up.
 
Top