Skip to content

Instantly share code, notes, and snippets.

@attic-stuff
Created December 31, 2023 01:35
Show Gist options
  • Select an option

  • Save attic-stuff/781f7d51708a1369277d77670789dbd9 to your computer and use it in GitHub Desktop.

Select an option

Save attic-stuff/781f7d51708a1369277d77670789dbd9 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