diff options
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/file_access.cpp | 10 | ||||
-rw-r--r-- | core/io/file_access.h | 2 | ||||
-rw-r--r-- | core/io/marshalls.h | 10 | ||||
-rw-r--r-- | core/io/stream_peer.cpp | 26 | ||||
-rw-r--r-- | core/io/stream_peer.h | 2 |
5 files changed, 50 insertions, 0 deletions
diff --git a/core/io/file_access.cpp b/core/io/file_access.cpp index acfd680a1e..6aaafb16d8 100644 --- a/core/io/file_access.cpp +++ b/core/io/file_access.cpp @@ -270,6 +270,10 @@ uint64_t FileAccess::get_64() const { return data; } +float FileAccess::get_half() const { + return Math::half_to_float(get_16()); +} + float FileAccess::get_float() const { MarshallFloat m; m.i = get_32(); @@ -529,6 +533,10 @@ void FileAccess::store_real(real_t p_real) { } } +void FileAccess::store_half(float p_dest) { + store_16(Math::make_half_float(p_dest)); +} + void FileAccess::store_float(float p_dest) { MarshallFloat m; m.f = p_dest; @@ -835,6 +843,7 @@ void FileAccess::_bind_methods() { ClassDB::bind_method(D_METHOD("get_16"), &FileAccess::get_16); ClassDB::bind_method(D_METHOD("get_32"), &FileAccess::get_32); ClassDB::bind_method(D_METHOD("get_64"), &FileAccess::get_64); + ClassDB::bind_method(D_METHOD("get_half"), &FileAccess::get_half); ClassDB::bind_method(D_METHOD("get_float"), &FileAccess::get_float); ClassDB::bind_method(D_METHOD("get_double"), &FileAccess::get_double); ClassDB::bind_method(D_METHOD("get_real"), &FileAccess::get_real); @@ -853,6 +862,7 @@ void FileAccess::_bind_methods() { ClassDB::bind_method(D_METHOD("store_16", "value"), &FileAccess::store_16); ClassDB::bind_method(D_METHOD("store_32", "value"), &FileAccess::store_32); ClassDB::bind_method(D_METHOD("store_64", "value"), &FileAccess::store_64); + ClassDB::bind_method(D_METHOD("store_half", "value"), &FileAccess::store_half); ClassDB::bind_method(D_METHOD("store_float", "value"), &FileAccess::store_float); ClassDB::bind_method(D_METHOD("store_double", "value"), &FileAccess::store_double); ClassDB::bind_method(D_METHOD("store_real", "value"), &FileAccess::store_real); diff --git a/core/io/file_access.h b/core/io/file_access.h index 20b5f26d9f..3cca8ba4f0 100644 --- a/core/io/file_access.h +++ b/core/io/file_access.h @@ -150,6 +150,7 @@ public: virtual uint32_t get_32() const; ///< get 32 bits uint virtual uint64_t get_64() const; ///< get 64 bits uint + virtual float get_half() const; virtual float get_float() const; virtual double get_double() const; virtual real_t get_real() const; @@ -181,6 +182,7 @@ public: virtual void store_32(uint32_t p_dest); ///< store 32 bits uint virtual void store_64(uint64_t p_dest); ///< store 64 bits uint + virtual void store_half(float p_dest); virtual void store_float(float p_dest); virtual void store_double(double p_dest); virtual void store_real(real_t p_real); diff --git a/core/io/marshalls.h b/core/io/marshalls.h index 3da24b905f..399a1baf56 100644 --- a/core/io/marshalls.h +++ b/core/io/marshalls.h @@ -86,6 +86,12 @@ static inline unsigned int encode_uint32(uint32_t p_uint, uint8_t *p_arr) { return sizeof(uint32_t); } +static inline unsigned int encode_half(float p_float, uint8_t *p_arr) { + encode_uint16(Math::make_half_float(p_float), p_arr); + + return sizeof(uint16_t); +} + static inline unsigned int encode_float(float p_float, uint8_t *p_arr) { MarshallFloat mf; mf.f = p_float; @@ -174,6 +180,10 @@ static inline uint32_t decode_uint32(const uint8_t *p_arr) { return u; } +static inline float decode_half(const uint8_t *p_arr) { + return Math::half_to_float(decode_uint16(p_arr)); +} + static inline float decode_float(const uint8_t *p_arr) { MarshallFloat mf; mf.i = decode_uint32(p_arr); diff --git a/core/io/stream_peer.cpp b/core/io/stream_peer.cpp index 2b5a44a27c..c5f35e9650 100644 --- a/core/io/stream_peer.cpp +++ b/core/io/stream_peer.cpp @@ -180,6 +180,18 @@ void StreamPeer::put_64(int64_t p_val) { put_data(buf, 8); } +void StreamPeer::put_half(float p_val) { + uint8_t buf[2]; + + encode_half(p_val, buf); + uint16_t *p16 = (uint16_t *)buf; + if (big_endian) { + *p16 = BSWAP16(*p16); + } + + put_data(buf, 2); +} + void StreamPeer::put_float(float p_val) { uint8_t buf[4]; @@ -296,6 +308,18 @@ int64_t StreamPeer::get_64() { return r; } +float StreamPeer::get_half() { + uint8_t buf[2]; + get_data(buf, 2); + + uint16_t *p16 = (uint16_t *)buf; + if (big_endian) { + *p16 = BSWAP16(*p16); + } + + return decode_half(buf); +} + float StreamPeer::get_float() { uint8_t buf[4]; get_data(buf, 4); @@ -387,6 +411,7 @@ void StreamPeer::_bind_methods() { ClassDB::bind_method(D_METHOD("put_u32", "value"), &StreamPeer::put_u32); ClassDB::bind_method(D_METHOD("put_64", "value"), &StreamPeer::put_64); ClassDB::bind_method(D_METHOD("put_u64", "value"), &StreamPeer::put_u64); + ClassDB::bind_method(D_METHOD("put_half", "value"), &StreamPeer::put_half); ClassDB::bind_method(D_METHOD("put_float", "value"), &StreamPeer::put_float); ClassDB::bind_method(D_METHOD("put_double", "value"), &StreamPeer::put_double); ClassDB::bind_method(D_METHOD("put_string", "value"), &StreamPeer::put_string); @@ -401,6 +426,7 @@ void StreamPeer::_bind_methods() { ClassDB::bind_method(D_METHOD("get_u32"), &StreamPeer::get_u32); ClassDB::bind_method(D_METHOD("get_64"), &StreamPeer::get_64); ClassDB::bind_method(D_METHOD("get_u64"), &StreamPeer::get_u64); + ClassDB::bind_method(D_METHOD("get_half"), &StreamPeer::get_half); ClassDB::bind_method(D_METHOD("get_float"), &StreamPeer::get_float); ClassDB::bind_method(D_METHOD("get_double"), &StreamPeer::get_double); ClassDB::bind_method(D_METHOD("get_string", "bytes"), &StreamPeer::get_string, DEFVAL(-1)); diff --git a/core/io/stream_peer.h b/core/io/stream_peer.h index 682a6affa0..15d9ac97b9 100644 --- a/core/io/stream_peer.h +++ b/core/io/stream_peer.h @@ -75,6 +75,7 @@ public: void put_u32(uint32_t p_val); void put_64(int64_t p_val); void put_u64(uint64_t p_val); + void put_half(float p_val); void put_float(float p_val); void put_double(double p_val); void put_string(const String &p_string); @@ -89,6 +90,7 @@ public: int32_t get_32(); uint64_t get_u64(); int64_t get_64(); + float get_half(); float get_float(); double get_double(); String get_string(int p_bytes = -1); |