Skip to content

Instantly share code, notes, and snippets.

@Kruger0
Forked from attic-stuff/tilemap_get_buffer.gml
Created November 27, 2025 15:58
Show Gist options
  • Select an option

  • Save Kruger0/6283f8bf4c41de9d5e9490d2b7ae483b to your computer and use it in GitHub Desktop.

Select an option

Save Kruger0/6283f8bf4c41de9d5e9490d2b7ae483b to your computer and use it in GitHub Desktop.
functions for reading and writing tilemaps from and to buffers
/**
* writes a tilemap to a buffer
* @param {id.TileMapElement} map the tilemap source
* @return {id.Buffer}
*/
function buffer_get_tilemap(map) {
var width = tilemap_get_width(map);
var height = tilemap_get_height(map);
var buffer = buffer_create(width * height * buffer_sizeof(buffer_u32), buffer_fixed, 1);
for (var i = 0; i < width; i++) {
for (var j = 0; j < height; j++) {
buffer_write(buffer, buffer_u32, tilemap_get(map, i, j));
}
}
return buffer;
}
/**
* writes a tilemap to a buffer
* @param {id.TileMapElement} map the tilemap destination
* @param {id.Buffer} buffer the buffer source
*/
function buffer_set_tilemap(map, buffer) {
var width = tilemap_get_width(map);
var height = tilemap_get_height(map);
for (var i = 0; i < width; i++) {
for (var j = 0; j < height; j++) {
tilemap_set(map, buffer_read(buffer, buffer_u32), i, j);
}
}
}
/**
* writes a tilemap to a buffer within a specific position and size
* @param {id.TileMapElement} map the tilemap source
* @return {id.Buffer}
*/
function buffer_get_tilemap_ext(map, x, y, width, height) {
var buffer = buffer_create(width * height * buffer_sizeof(buffer_u32), buffer_fixed, 1);
for (var i = x, il = x + width; i < il; i++) {
for (var j = y, jl = y + height; j < jl; j++) {
buffer_write(buffer, buffer_u32, tilemap_get(map, i, j));
}
}
return buffer;
}
/**
* writes a tilemap to a buffer in a specific position and size
* @param {id.TileMapElement} map the tilemap destination
* @param {id.Buffer} buffer the buffer source
*/
function buffer_set_tilemap_ext(map, buffer, x, y, width, height) {
for (var i = x, il = x + width; i < il; i++) {
for (var j = y, jl = y + height; j < jl; j++) {
tilemap_set(map, buffer_read(buffer, buffer_u32), i, j);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment