Skip to content

Instantly share code, notes, and snippets.

@tinkerer-red
Created September 17, 2024 19:55
Show Gist options
  • Save tinkerer-red/105b38c0f8cfc80948fb267c6ba7674a to your computer and use it in GitHub Desktop.
Save tinkerer-red/105b38c0f8cfc80948fb267c6ba7674a to your computer and use it in GitHub Desktop.
buffer_array.gml
function buffer_array(_length, _type, _value=0) constructor {
width = _width;
height = _height;
size = width*height;
type = _type;
if (type == buffer_string)
|| (type == buffer_text) {
show_error("Type can not be a sting or text", true);
}
var _offset
switch (type) {
case buffer_u8:
case buffer_s8:
case buffer_bool:
_offset = 1
break;
case buffer_u16:
case buffer_s16:
case buffer_f16:
_offset = 2
break;
case buffer_u32:
case buffer_s32:
case buffer_f32:
_offset = 4
break;
case buffer_u64:
case buffer_f64:
_offset = 8
break;
}
offset = _offset
//memory
buff = buffer_create(size*offset, buffer_fixed, offset); // the main buffer for handling movement
static get = function (_i) {
var _byte_pos = _i * offset;
buffer_seek(buff, buffer_seek_start, _byte_pos);
buffer_read(buff, type);
}
static set = function (_i, _value) {
var _byte_pos = _i* offset;
buffer_seek(buff, buffer_seek_start, _byte_pos);
buffer_write(buff, type, _value);
}
static set_disk = function (_x, _y, _r, _val) {
var x1, y1, x2, y2, i, j, w, h;
w = width;
h = height;
x1 = (max(0, floor(_x - _r)));
x2 = (min(w - 1, ceil(_x + _r)));
y1 = (max(0, floor(_y - _r)));
y2 = (min(h - 1, ceil(_y + _r)));
_r = _r * _r;
for (i = x1; i <= x2; i++) {
var ix = (i - _x) * (i - _x);
for (j = y1; j <= y2; j++) {
var jy = j - _y;
//radius check
if (ix + (jy * jy) <= _r) {
if (i >= 0 && i < w && j >= 0 && j < h) {
set(i, j, _val)
}
}
}
}
}
}
function buffer_array_2d(_width, _height, _type, _value=0) constructor {
width = _width;
height = _height;
size = width*height;
type = _type;
if (type == buffer_string)
|| (type == buffer_text) {
show_error("Type can not be a sting or text", true)
}
var _offset
switch (type) {
case buffer_u8:
case buffer_s8:
case buffer_bool:
_offset = 1
break;
case buffer_u16:
case buffer_s16:
case buffer_f16:
_offset = 2
break;
case buffer_u32:
case buffer_s32:
case buffer_f32:
_offset = 4
break;
case buffer_u64:
case buffer_f64:
_offset = 8
break;
}
offset = _offset
//memory
buff = buffer_create(size*offset, buffer_fixed, offset); // the main buffer for handling movement
static get = function (_x, _y) {
var _byte_pos = (_x + (_y * width)) * offset;
buffer_seek(buff, buffer_seek_start, _byte_pos);
buffer_read(buff, type);
}
static set = function (_x, _y, _value) {
var _byte_pos = (_x + (_y * width)) * offset;
buffer_seek(buff, buffer_seek_start, _byte_pos);
buffer_write(buff, type, _value);
}
static set_disk = function (_x, _y, _r, _val) {
var x1, y1, x2, y2, i, j, w, h;
w = width;
h = height;
x1 = (max(0, floor(_x - _r)));
x2 = (min(w - 1, ceil(_x + _r)));
y1 = (max(0, floor(_y - _r)));
y2 = (min(h - 1, ceil(_y + _r)));
_r = _r * _r;
for (i = x1; i <= x2; i++) {
var ix = (i - _x) * (i - _x);
for (j = y1; j <= y2; j++) {
var jy = j - _y;
//radius check
if (ix + (jy * jy) <= _r) {
if (i >= 0 && i < w && j >= 0 && j < h) {
set(i, j, _val)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment