From 33b5c571995cce60a21784ac33fcf958640ed1e2 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Mon, 24 Feb 2020 15:20:53 -0300 Subject: Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT. - Renames PackedIntArray to PackedInt32Array. - Renames PackedFloatArray to PackedFloat32Array. - Adds PackedInt64Array and PackedFloat64Array. - Renames Variant::REAL to Variant::FLOAT for consistency. Packed arrays are for storing large amount of data and creating stuff like meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of memory. That said, many users requested the ability to have 64 bits packed arrays for their games, so this is just an optional added type. For Variant, the float datatype is always 64 bits, and exposed as `float`. We still have `real_t` which is the datatype that can change from 32 to 64 bits depending on a compile flag (not entirely working right now, but that's the idea). It affects math related datatypes and code only. Neither Variant nor PackedArray make use of real_t, which is only intended for math precision, so the term is removed from there to keep only float. --- core/variant_parser.cpp | 116 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 96 insertions(+), 20 deletions(-) (limited to 'core/variant_parser.cpp') diff --git a/core/variant_parser.cpp b/core/variant_parser.cpp index 56d33c10f4..12fd9976bd 100644 --- a/core/variant_parser.cpp +++ b/core/variant_parser.cpp @@ -941,20 +941,41 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, return OK; - } else if (id == "PackedIntArray" || id == "PoolIntArray" || id == "IntArray") { + } else if (id == "PackedInt32Array" || id == "PackedIntArray" || id == "PoolIntArray" || id == "IntArray") { - Vector args; - Error err = _parse_construct(p_stream, args, line, r_err_str); + Vector args; + Error err = _parse_construct(p_stream, args, line, r_err_str); if (err) return err; - Vector arr; + Vector arr; { - int len = args.size(); + int32_t len = args.size(); arr.resize(len); - int *w = arr.ptrw(); - for (int i = 0; i < len; i++) { - w[i] = int(args[i]); + int32_t *w = arr.ptrw(); + for (int32_t i = 0; i < len; i++) { + w[i] = int32_t(args[i]); + } + } + + value = arr; + + return OK; + + } else if (id == "PackedInt64Array") { + + Vector args; + Error err = _parse_construct(p_stream, args, line, r_err_str); + if (err) + return err; + + Vector arr; + { + int64_t len = args.size(); + arr.resize(len); + int64_t *w = arr.ptrw(); + for (int64_t i = 0; i < len; i++) { + w[i] = int64_t(args[i]); } } @@ -962,7 +983,7 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, return OK; - } else if (id == "PackedRealArray" || id == "PoolRealArray" || id == "FloatArray") { + } else if (id == "PackedFloat32Array" || id == "PackedRealArray" || id == "PoolRealArray" || id == "FloatArray") { Vector args; Error err = _parse_construct(p_stream, args, line, r_err_str); @@ -981,6 +1002,26 @@ Error VariantParser::parse_value(Token &token, Variant &value, Stream *p_stream, value = arr; + return OK; + } else if (id == "PackedFloat64Array") { + + Vector args; + Error err = _parse_construct(p_stream, args, line, r_err_str); + if (err) + return err; + + Vector arr; + { + int len = args.size(); + arr.resize(len); + double *w = arr.ptrw(); + for (int i = 0; i < len; i++) { + w[i] = args[i]; + } + } + + value = arr; + return OK; } else if (id == "PackedStringArray" || id == "PoolStringArray" || id == "StringArray") { @@ -1455,7 +1496,7 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud, itos(p_variant.operator int64_t())); } break; - case Variant::REAL: { + case Variant::FLOAT: { String s = rtosfix(p_variant.operator real_t()); if (s.find(".") == -1 && s.find("e") == -1) @@ -1715,14 +1756,14 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud, " )"); } break; - case Variant::PACKED_INT_ARRAY: { + case Variant::PACKED_INT32_ARRAY: { - p_store_string_func(p_store_string_ud, "PackedIntArray( "); - Vector data = p_variant; - int len = data.size(); - const int *ptr = data.ptr(); + p_store_string_func(p_store_string_ud, "PackedInt32Array( "); + Vector data = p_variant; + int32_t len = data.size(); + const int32_t *ptr = data.ptr(); - for (int i = 0; i < len; i++) { + for (int32_t i = 0; i < len; i++) { if (i > 0) p_store_string_func(p_store_string_ud, ", "); @@ -1733,12 +1774,47 @@ Error VariantWriter::write(const Variant &p_variant, StoreStringFunc p_store_str p_store_string_func(p_store_string_ud, " )"); } break; - case Variant::PACKED_REAL_ARRAY: { + case Variant::PACKED_INT64_ARRAY: { + + p_store_string_func(p_store_string_ud, "PackedInt64Array( "); + Vector data = p_variant; + int64_t len = data.size(); + const int64_t *ptr = data.ptr(); + + for (int64_t i = 0; i < len; i++) { + + if (i > 0) + p_store_string_func(p_store_string_ud, ", "); + + p_store_string_func(p_store_string_ud, itos(ptr[i])); + } + + p_store_string_func(p_store_string_ud, " )"); + + } break; + case Variant::PACKED_FLOAT32_ARRAY: { + + p_store_string_func(p_store_string_ud, "PackedFloat32Array( "); + Vector data = p_variant; + int len = data.size(); + const float *ptr = data.ptr(); + + for (int i = 0; i < len; i++) { + + if (i > 0) + p_store_string_func(p_store_string_ud, ", "); + p_store_string_func(p_store_string_ud, rtosfix(ptr[i])); + } + + p_store_string_func(p_store_string_ud, " )"); + + } break; + case Variant::PACKED_FLOAT64_ARRAY: { - p_store_string_func(p_store_string_ud, "PackedRealArray( "); - Vector data = p_variant; + p_store_string_func(p_store_string_ud, "PackedFloat64Array( "); + Vector data = p_variant; int len = data.size(); - const real_t *ptr = data.ptr(); + const double *ptr = data.ptr(); for (int i = 0; i < len; i++) { -- cgit v1.2.3