Pixel-Composer/scripts/surface_functions/surface_functions.gml

242 lines
6.3 KiB
Text
Raw Normal View History

2023-02-19 02:13:19 +01:00
//draw
2022-01-13 05:24:03 +01:00
function draw_surface_safe(surface, _x, _y) {
2022-11-22 14:25:39 +01:00
if(!is_surface(surface)) return;
draw_surface(surface, _x, _y);
2022-01-13 05:24:03 +01:00
}
2023-01-25 06:49:00 +01:00
function draw_surface_stretched_safe(surface, _x, _y, _w, _h) {
if(!is_surface(surface)) return;
draw_surface_stretched(surface, _x, _y, _w, _h);
}
2022-11-22 14:25:39 +01:00
function draw_surface_ext_safe(surface, _x, _y, _xs = 1, _ys = 1, _rot = 0, _col = c_white, _alpha = 1) {
if(!is_surface(surface)) return;
draw_surface_ext(surface, _x, _y, _xs, _ys, _rot, _col, _alpha);
2022-01-13 05:24:03 +01:00
}
2022-11-22 14:25:39 +01:00
function draw_surface_tiled_ext_safe(surface, _x, _y, _xs = 1, _ys = 1, _col = c_white, _alpha = 1) {
if(!is_surface(surface)) return;
draw_surface_tiled_ext(surface, _x, _y, _xs, _ys, _col, _alpha);
2022-01-13 05:24:03 +01:00
}
2022-11-22 14:25:39 +01:00
function draw_surface_part_ext_safe(surface, _l, _t, _w, _h, _x, _y, _xs = 1, _ys = 1, _rot = 0, _col = c_white, _alpha = 1) {
if(!is_surface(surface)) return;
draw_surface_part_ext(surface, _l, _t, _w, _h, _x, _y, _xs, _ys, _col, _alpha);
}
2022-01-13 05:24:03 +01:00
2023-02-19 02:13:19 +01:00
//check
function is_surface(s) {
2023-02-28 09:43:01 +01:00
if(is_undefined(s)) return false;
2023-02-19 02:13:19 +01:00
if(is_array(s)) return false;
if(!is_real(s)) return false;
if(!s) return false;
if(!surface_exists(s)) return false;
2022-01-13 05:24:03 +01:00
2023-02-19 02:13:19 +01:00
if(surface_get_width(s) <= 0) return false;
if(surface_get_height(s) <= 0) return false;
2022-01-13 05:24:03 +01:00
2022-11-22 14:25:39 +01:00
return true;
2022-01-13 05:24:03 +01:00
}
2023-02-19 02:13:19 +01:00
function surface_verify(surf, w, h) {
if(!is_surface(surf))
return surface_create_valid(w, h);
surface_size_to(surf, w, h);
return surf;
2022-12-10 05:06:01 +01:00
}
2023-02-19 02:13:19 +01:00
//create
function surface_create_size(surface) {
2022-11-03 11:44:49 +01:00
var s = surface_create_valid(surface_get_width(surface), surface_get_height(surface));
2022-01-13 05:24:03 +01:00
surface_set_target(s);
draw_clear_alpha(0, 0);
surface_reset_target();
return s;
}
2023-02-19 02:13:19 +01:00
function surface_create_valid(w, h) {
var s = surface_create(surface_valid_size(w), surface_valid_size(h));
surface_set_target(s);
2022-01-13 05:24:03 +01:00
draw_clear_alpha(0, 0);
surface_reset_target();
2023-02-19 02:13:19 +01:00
return s;
2022-01-13 05:24:03 +01:00
}
2023-02-19 02:13:19 +01:00
function surface_create_from_buffer(w, h, buff) {
var s = surface_create_valid(surface_valid_size(w), surface_valid_size(h));
buffer_set_surface(buff, s, 0);
return s;
2022-11-01 03:06:03 +01:00
}
2023-02-19 02:13:19 +01:00
function surface_create_from_sprite(spr) {
if(!sprite_exists(spr)) return noone;
2022-01-13 05:24:03 +01:00
2023-02-19 02:13:19 +01:00
if(sprite_get_number(spr) == 1)
return surface_create_from_sprite_ext(spr, 0);
2022-01-13 05:24:03 +01:00
2023-02-19 02:13:19 +01:00
var s = [];
for( var i = 0; i < sprite_get_number(spr); i++ ) {
array_push(s, surface_create_from_sprite_ext(spr, i));
}
return s;
2022-09-27 06:37:28 +02:00
}
function surface_create_from_sprite_ext(spr, ind) {
if(!sprite_exists(spr)) return noone;
var sw = sprite_get_width(spr);
var sh = sprite_get_height(spr);
2022-11-03 11:44:49 +01:00
var s = surface_create_valid(sw, sh);
2022-09-27 06:37:28 +02:00
surface_set_target(s);
2023-02-19 02:13:19 +01:00
BLEND_OVERRIDE;
2022-09-27 06:37:28 +02:00
draw_clear_alpha(0, 0);
draw_sprite(spr, ind, sprite_get_xoffset(spr), sprite_get_yoffset(spr));
BLEND_NORMAL
surface_reset_target();
return s;
}
2023-02-19 02:13:19 +01:00
function surface_size_to(surface, width, height) {
if(!is_surface(surface)) return false;
if(width < 1 && height < 1) return false;
2022-09-27 06:37:28 +02:00
2023-02-19 02:13:19 +01:00
width = surface_valid_size(width);
height = surface_valid_size(height);
2022-09-27 06:37:28 +02:00
2023-02-19 02:13:19 +01:00
var ww = surface_get_width(surface);
var hh = surface_get_height(surface);
2022-09-27 06:37:28 +02:00
2023-02-19 02:13:19 +01:00
if(ww == width && hh == height) return false;
surface_resize(surface, width, height);
return true;
2022-12-27 04:00:50 +01:00
}
2023-02-19 02:13:19 +01:00
function surface_copy_from(dst, src) {
surface_set_target(dst);
draw_clear_alpha(0, 0);
BLEND_OVERRIDE;
draw_surface_safe(src, 0, 0);
BLEND_NORMAL
surface_reset_target();
}
function surface_clone(surface, source = noone) {
if(!is_surface(surface)) return noone;
2023-02-19 13:49:20 +01:00
source = surface_verify(source, surface_get_width(surface), surface_get_height(surface));
2023-02-19 02:13:19 +01:00
surface_set_target(source);
draw_clear_alpha(0, 0);
BLEND_OVERRIDE;
draw_surface_safe(surface, 0, 0);
BLEND_NORMAL
surface_reset_target();
return source;
}
function surface_copy_size(dest, source) {
if(!is_surface(dest)) return;
if(!is_surface(source)) return;
surface_size_to(dest, surface_get_width(source), surface_get_height(source));
surface_set_target(dest);
draw_clear_alpha(0, 0);
surface_reset_target();
surface_copy_from(dest, source);
}
function surface_valid_size(s) {
if(is_infinity(s)) return 1;
return max(1, s);
2023-02-14 13:44:46 +01:00
}
function surface_array_free(arr) {
if(!is_array(arr)) {
if(is_surface(arr)) surface_free(arr);
return;
}
for( var i = 0; i < array_length(arr); i++ )
surface_array_free(arr[i]);
}
function surface_array_clone(arr) {
if(!is_array(arr)) {
if(is_surface(arr))
return surface_clone(arr);
else
return arr;
}
var _arr = [];
for( var i = 0; i < array_length(arr); i++ )
_arr[i] = surface_array_clone(arr[i]);
2023-03-02 07:59:14 +01:00
return _arr;
}
function surface_array_serialize(arr) {
var _arr = __surface_array_serialize(arr);
return json_stringify(_arr);
}
function __surface_array_serialize(arr) {
if(!is_array(arr)) {
if(is_surface(arr)) {
var buff = buffer_create(surface_get_width(arr) * surface_get_height(arr) * 4, buffer_fixed, 1);
buffer_get_surface(buff, arr, 0);
var comp = buffer_compress(buff, 0, buffer_get_size(buff));
var enc = buffer_base64_encode(comp, 0, buffer_get_size(comp));
buffer_delete(buff);
return { width: surface_get_width(arr), height: surface_get_height(arr), buffer: enc };
} else
return arr;
}
var _arr = [];
for( var i = 0; i < array_length(arr); i++ )
_arr[i] = __surface_array_serialize(arr[i]);
return _arr;
}
function surface_array_deserialize(arr, index = -1) {
2023-03-05 07:16:44 +01:00
var _arr = json_try_parse(arr);
2023-03-02 07:59:14 +01:00
return index == -1? __surface_array_deserialize(_arr, index) : __surface_array_deserialize(_arr[index]);
}
function __surface_array_deserialize(arr) {
if(!is_array(arr)) {
var buff = buffer_base64_decode(arr.buffer);
var buff = buffer_decompress(buff);
return surface_create_from_buffer(arr.width, arr.height, buff);
}
var _arr = [];
for( var i = 0; i < array_length(arr); i++ )
_arr[i] = __surface_array_deserialize(arr[i]);
2023-02-14 13:44:46 +01:00
return _arr;
2023-03-05 07:16:44 +01:00
}
function surface_encode(surface) {
if(!is_surface(surface)) return "";
var buff = buffer_create(surface_get_width(surface) * surface_get_height(surface) * 4, buffer_fixed, 1);
buffer_get_surface(buff, surface, 0);
var comp = buffer_compress(buff, 0, buffer_get_size(buff));
var enc = buffer_base64_encode(comp, 0, buffer_get_size(comp));
buffer_delete(buff);
var str = { width: surface_get_width(surface), height: surface_get_height(surface), buffer: enc };
return json_stringify(str);
}
function surface_decode(struct) {
var buff = buffer_base64_decode(struct.buffer);
var buff = buffer_decompress(buff);
return surface_create_from_buffer(struct.width, struct.height, buff);
2022-01-13 05:24:03 +01:00
}