├── .editorconfig ├── .github └── FUNDING.yml ├── .gitignore ├── Doxyfile ├── README.md ├── UNLICENSE └── hgdn.h /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = spaces 5 | indent_size = 2 6 | end_of_line = lf 7 | insert_final_newline = true 8 | 9 | [*.{c,h,cpp,hpp}] 10 | indent_size = 4 11 | 12 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: gilzoide # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: gilzoide # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://www.buymeacoffee.com/gilzoide'] # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | docs/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # High level GDNative C/C++ API (HGDN) 2 | Single header [GDNative](https://docs.godotengine.org/en/stable/tutorials/plugins/gdnative/gdnative-c-example.html) 3 | high level API for C/C++. 4 | 5 | - Single header: just copy `hgdn.h` to your project, put `#define HGDN_IMPLEMENTATION` 6 | in a single C/C++ source file before `#include`ing it and compile. 7 | - Depends only on [godot-headers](https://github.com/godotengine/godot-headers), 8 | so GDNative libraries can be built with a single compiler invocation. 9 | No need to generate Godot API bindings if you only use core GDNative stuff. 10 | - `hgdn_gdnative_init` fetches all current GDNative APIs. 11 | - Useful definitions for all math types, including Vector2, Vector3 and Color. 12 | - Wrappers around strings and pool arrays with pointer and size available. 13 | - Functions to get values from method arguments or native calls 14 | argument arrays. 15 | - Functions to create Variants, Strings, Arrays, Pool Arrays and Dictionaries 16 | in single calls. 17 | - Overloaded macro/functions to create Variants, available in C11 and C++. 18 | - Macros to assert arguments preconditions, like expected argument count and 19 | (TODO) expected argument types. 20 | 21 | 22 | ## Documentation 23 | Code is documented using [Doxygen](https://www.doxygen.nl) and is available [online here](https://gilzoide.github.io/high-level-gdnative/). 24 | 25 | 26 | ## Usage example 27 | For a working example with full Godot project, check out the 28 | [high-level-gdnative-example](https://github.com/gilzoide/high-level-gdnative-example) 29 | repository. 30 | 31 | ```c 32 | // example.c 33 | 34 | // 1) #define HGDN_IMPLEMENTATION on exactly one C/C++ file and include hgdn.h 35 | // Optionally #define other compile-time options, check out hgdn.h for documentation 36 | #define HGDN_STATIC 37 | #define HGDN_IMPLEMENTATION 38 | #include "hgdn.h" 39 | 40 | // 2.a) Declare native functions to be used by script code, if there are any 41 | // Any function with prototype `godot_variant (godot_array *)` can be called in script with 42 | // `gdnative_instance.call_native("standard_varcall", "in_editor ? " in editor" : ""); 79 | } 80 | 81 | // 3.b) Add `godot_gdnative_terminate`, the function that will be called when Godot 82 | // unloads this GDNativeLibrary, and call `hgdn_gdnative_terminate` from it. 83 | GDN_EXPORT void godot_gdnative_terminate(godot_gdnative_terminate_options *options) { 84 | hgdn_gdnative_terminate(options); 85 | } 86 | 87 | // 4) Add `godot_nativescript_init`, the function that will be called when Godot 88 | // initializes a NativeScript with this GDNativeLibrary, and register classes, 89 | // if there are any. 90 | GDN_EXPORT void godot_nativescript_init(void *desc) { 91 | // TODO 92 | } 93 | ``` 94 | 95 | ```gdscript 96 | # example.gd 97 | extends Reference 98 | 99 | func _ready() -> void: 100 | var example = GDNative.new() 101 | example.library = preload("res://path_to_gdnativelibrary.gdnlib") 102 | example.initialize() # --> "GDNative initialized" 103 | print(example.call_native("standard_varcall", "get_message", [])) # --> "Hello world!" 104 | print(example.call_native("standard_varcall", "square", [5])) # --> 25 105 | print(example.call_native("standard_varcall", "sum_ints", [[1, 2.5, 3]])) # --> 6 106 | ``` 107 | 108 | -------------------------------------------------------------------------------- /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /hgdn.h: -------------------------------------------------------------------------------- 1 | /** @file 2 | * hgdn.h -- High level GDNative C/C++ API 3 | * 4 | * Project URL: https://github.com/gilzoide/high-level-gdnative 5 | * 6 | * Do this: 7 | * ```c 8 | * #define HGDN_IMPLEMENTATION 9 | * ``` 10 | * before you include this file in *one* C or C++ file to create the implementation. 11 | * 12 | * i.e.: 13 | * ```c 14 | * #include ... 15 | * #include ... 16 | * #define HGDN_IMPLEMENTATION 17 | * #include "hgdn.h" 18 | * ``` 19 | * 20 | * Optionally provide the following defines with your own implementations: 21 | * 22 | * - HGDN_STATIC: 23 | * If defined and HGDN_DECL is not defined, functions will be declared `static` instead of `extern` 24 | * - HGDN_DECL: 25 | * Function declaration prefix (default: `extern` or `static` depending on HGDN_STATIC) 26 | * - HGDN_STRING_FORMAT_BUFFER_SIZE: 27 | * Size of the global char buffer used for `hgdn_print*` functions. Defaults to 1024 28 | * - HGDN_NO_CORE_1_1: 29 | * - HGDN_NO_CORE_1_2: 30 | * - HGDN_NO_CORE_1_3: 31 | * - HGDN_NO_EXT_NATIVESCRIPT: 32 | * - HGDN_NO_EXT_PLUGINSCRIPT: 33 | * - HGDN_NO_EXT_ANDROID: 34 | * - HGDN_NO_EXT_ARVR: 35 | * - HGDN_NO_EXT_VIDEOCODER: 36 | * - HGDN_NO_EXT_NET: 37 | * Disable global pointers to extensions. If NativeScript is disabled, its helper functions will not be available as well. 38 | */ 39 | #ifndef __HGDN_H__ 40 | #define __HGDN_H__ 41 | 42 | #include 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | 49 | #ifndef HGDN_DECL 50 | #ifdef HGDN_STATIC 51 | #define HGDN_DECL static 52 | #else 53 | #define HGDN_DECL extern 54 | #endif 55 | #endif 56 | 57 | #ifndef HGDN_STRING_FORMAT_BUFFER_SIZE 58 | #define HGDN_STRING_FORMAT_BUFFER_SIZE 1024 59 | #endif 60 | 61 | #if defined(__cplusplus) && __cplusplus >= 201103L // `constexpr` is a C++11 feature 62 | #define HGDN_CONSTEXPR constexpr 63 | #else 64 | #define HGDN_CONSTEXPR 65 | #endif 66 | 67 | #ifndef HGDN_METHOD_ARGUMENTS_INFO_MAX 68 | #define HGDN_METHOD_ARGUMENTS_INFO_MAX 16 69 | #endif 70 | 71 | // Macro magic to get the number of variable arguments 72 | // Ref: https://groups.google.com/g/comp.std.c/c/d-6Mj5Lko_s 73 | #define HGDN__NARG(...) HGDN__NARG_(__VA_ARGS__, HGDN__NARG_RSEQ_N()) 74 | #define HGDN__NARG_(...) HGDN__NARG_N(__VA_ARGS__) 75 | #define HGDN__NARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,_21,_22,_23,_24,_25,_26,_27,_28,_29,_30,_31,_32,_33,_34,_35,_36,_37,_38,_39,_40,_41,_42,_43,_44,_45,_46,_47,_48,_49,_50,_51,_52,_53,_54,_55,_56,_57,_58,_59,_60,_61,_62,_63,N,...) N 76 | #define HGDN__NARG_RSEQ_N() 63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0 77 | 78 | // Macro magic to apply a function macro to variadic arguments 79 | // Ref: https://stackoverflow.com/questions/6707148/foreach-macro-on-macros-arguments/13459454#13459454 80 | #define HGDN__EVAL0(...) __VA_ARGS__ 81 | #define HGDN__EVAL1(...) HGDN__EVAL0(HGDN__EVAL0(HGDN__EVAL0(__VA_ARGS__))) 82 | #define HGDN__EVAL2(...) HGDN__EVAL1(HGDN__EVAL1(HGDN__EVAL1(__VA_ARGS__))) 83 | #define HGDN__EVAL3(...) HGDN__EVAL2(HGDN__EVAL2(HGDN__EVAL2(__VA_ARGS__))) 84 | #define HGDN__EVAL4(...) HGDN__EVAL3(HGDN__EVAL3(HGDN__EVAL3(__VA_ARGS__))) 85 | #define HGDN__EVAL(...) HGDN__EVAL4(HGDN__EVAL4(HGDN__EVAL4(__VA_ARGS__))) 86 | #define HGDN__MAP_END(...) 87 | #define HGDN__MAP_OUT 88 | #define HGDN__MAP_COMMA , 89 | #define HGDN__MAP_GET_END() 0, HGDN__MAP_END 90 | #define HGDN__MAP_NEXT0(item, next, ...) next HGDN__MAP_OUT 91 | #define HGDN__MAP_NEXT1(item, next) HGDN__MAP_NEXT0(item, HGDN__MAP_COMMA next, 0) 92 | #define HGDN__MAP_NEXT(item, next) HGDN__MAP_NEXT1(HGDN__MAP_GET_END item, next) 93 | #define HGDN__MAP0(f, x, peek, ...) f(x) HGDN__MAP_NEXT(peek, HGDN__MAP1)(f, peek, __VA_ARGS__) 94 | #define HGDN__MAP1(f, x, peek, ...) f(x) HGDN__MAP_NEXT(peek, HGDN__MAP0)(f, peek, __VA_ARGS__) 95 | #define HGDN__MAP(f, ...) HGDN__EVAL(HGDN__MAP1(f, __VA_ARGS__, (), 0)) 96 | 97 | 98 | 99 | /// @defgroup custom_math_types Custom math types 100 | /// Useful definitions for Godot math types 101 | /// @{ 102 | typedef union hgdn_vector2 { 103 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 104 | uint8_t data[sizeof(float[2])]; 105 | // float elements 106 | float elements[2]; 107 | // xy 108 | struct { float x, y; }; 109 | // rg 110 | struct { float r, g; }; 111 | // st 112 | struct { float s, t; }; 113 | // uv 114 | struct { float u, v; }; 115 | // Size: width/height 116 | struct { float width, height; }; 117 | } hgdn_vector2; 118 | 119 | #ifndef GODOT_CORE_API_GODOT_VECTOR2_TYPE_DEFINED 120 | typedef hgdn_vector2 godot_vector2; 121 | #define GODOT_CORE_API_GODOT_VECTOR2_TYPE_DEFINED 122 | #endif 123 | 124 | typedef union hgdn_vector3 { 125 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 126 | uint8_t data[sizeof(float[3])]; 127 | // float elements 128 | float elements[3]; 129 | // xyz 130 | struct { float x, y, z; }; 131 | struct { hgdn_vector2 xy; float _0; }; 132 | struct { float _1; hgdn_vector2 yz; }; 133 | // rgb 134 | struct { float r, g, b; }; 135 | struct { hgdn_vector2 rg; float _2; }; 136 | struct { float _3; hgdn_vector2 gb; }; 137 | // stp 138 | struct { float s, t, p; }; 139 | struct { hgdn_vector2 st; float _6; }; 140 | struct { float _7; hgdn_vector2 tp; }; 141 | // uv 142 | struct { float u, v, _4; }; 143 | struct { hgdn_vector2 uv; float _5; }; 144 | // 3D Size: width/height/depth 145 | struct { float width, height, depth; }; 146 | } hgdn_vector3; 147 | 148 | #ifndef GODOT_CORE_API_GODOT_VECTOR3_TYPE_DEFINED 149 | typedef hgdn_vector3 godot_vector3; 150 | #define GODOT_CORE_API_GODOT_VECTOR3_TYPE_DEFINED 151 | #endif 152 | 153 | typedef union hgdn_vector4 { 154 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 155 | uint8_t data[sizeof(float[4])]; 156 | // float elements 157 | float elements[4]; 158 | // xyzw 159 | struct { float x, y, z, w; }; 160 | struct { hgdn_vector2 xy; hgdn_vector2 zw; }; 161 | struct { float _0; hgdn_vector2 yz; float _1; }; 162 | struct { hgdn_vector3 xyz; float _2; }; 163 | struct { float _3; hgdn_vector3 yzw; }; 164 | // rgba 165 | struct { float r, g, b, a; }; 166 | struct { hgdn_vector2 rg; hgdn_vector2 ba; }; 167 | struct { float _4; hgdn_vector2 gb; float _5; }; 168 | struct { hgdn_vector3 rgb; float _6; }; 169 | struct { float _7; hgdn_vector3 gba; }; 170 | // stpq 171 | struct { float s, t, p, q; }; 172 | struct { hgdn_vector2 st; hgdn_vector2 pq; }; 173 | struct { float _8; hgdn_vector2 tp; float _9; }; 174 | struct { hgdn_vector3 stp; float _10; }; 175 | struct { float _11; hgdn_vector3 tpq; }; 176 | // uv 177 | struct { float u, v; float _12[2]; }; 178 | struct { hgdn_vector2 uv; float _13[2]; }; 179 | } hgdn_vector4; 180 | 181 | #ifndef GODOT_CORE_API_GODOT_COLOR_TYPE_DEFINED 182 | /// Color is present on Pool Arrays and as MultiMesh instance data, so it's convenient having a full vector4 definition for it 183 | typedef hgdn_vector4 godot_color; 184 | #define GODOT_CORE_API_GODOT_COLOR_TYPE_DEFINED 185 | #endif 186 | 187 | typedef union hgdn_rect2 { 188 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 189 | uint8_t data[sizeof(float[4])]; 190 | float elements[4]; 191 | struct { float x, y, width, height; }; 192 | struct { hgdn_vector2 position; hgdn_vector2 size; }; 193 | } hgdn_rect2; 194 | 195 | #ifndef GODOT_CORE_API_GODOT_RECT2_TYPE_DEFINED 196 | typedef hgdn_rect2 godot_rect2; 197 | #define GODOT_CORE_API_GODOT_RECT2_TYPE_DEFINED 198 | #endif 199 | 200 | typedef union hgdn_plane { 201 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 202 | uint8_t data[sizeof(float[4])]; 203 | float elements[4]; 204 | struct { hgdn_vector3 normal; float d; }; 205 | } hgdn_plane; 206 | 207 | #ifndef GODOT_CORE_API_GODOT_PLANE_TYPE_DEFINED 208 | typedef hgdn_plane godot_plane; 209 | #define GODOT_CORE_API_GODOT_PLANE_TYPE_DEFINED 210 | #endif 211 | 212 | typedef union hgdn_quat { 213 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 214 | uint8_t data[sizeof(float[4])]; 215 | float elements[4]; 216 | struct { float x, y, z, w; }; 217 | struct { hgdn_vector2 xy; hgdn_vector2 zw; }; 218 | struct { float _0; hgdn_vector2 yz; float _1; }; 219 | struct { hgdn_vector3 xyz; float _2; }; 220 | struct { float _3; hgdn_vector3 yzw; }; 221 | } hgdn_quat; 222 | #define HGDN_QUAT_IDENTITY ((hgdn_quat){ .elements = {0, 0, 0, 1} }) 223 | 224 | #ifndef GODOT_CORE_API_GODOT_QUAT_TYPE_DEFINED 225 | typedef hgdn_quat godot_quat; 226 | #define GODOT_CORE_API_GODOT_QUAT_TYPE_DEFINED 227 | #endif 228 | 229 | typedef union hgdn_basis { 230 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 231 | uint8_t data[sizeof(float[9])]; 232 | float elements[9]; 233 | hgdn_vector3 rows[3]; 234 | } hgdn_basis; 235 | #define HGDN_BASIS_IDENTITY ((hgdn_basis){ .elements = {1, 0, 0, 0, 1, 0, 0, 0, 1} }) 236 | 237 | #ifndef GODOT_CORE_API_GODOT_BASIS_TYPE_DEFINED 238 | typedef hgdn_basis godot_basis; 239 | #define GODOT_CORE_API_GODOT_BASIS_TYPE_DEFINED 240 | #endif 241 | 242 | typedef union hgdn_aabb { 243 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 244 | uint8_t data[sizeof(float[6])]; 245 | float elements[6]; 246 | struct { hgdn_vector3 position, size; }; 247 | } hgdn_aabb; 248 | 249 | #ifndef GODOT_CORE_API_GODOT_AABB_TYPE_DEFINED 250 | typedef hgdn_aabb godot_aabb; 251 | #define GODOT_CORE_API_GODOT_AABB_TYPE_DEFINED 252 | #endif 253 | 254 | typedef union hgdn_transform2d { 255 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 256 | uint8_t data[sizeof(float[6])]; 257 | float elements[6]; 258 | hgdn_vector2 columns[3]; 259 | struct { hgdn_vector2 x, y, origin; }; 260 | } hgdn_transform2d; 261 | #define HGDN_TRANSFORM2D_IDENTITY ((hgdn_transform2d){ .elements = {1, 0, 0, 1, 0, 0} }) 262 | 263 | #ifndef GODOT_CORE_API_GODOT_TRANSFORM2D_TYPE_DEFINED 264 | typedef hgdn_transform2d godot_transform2d; 265 | #define GODOT_CORE_API_GODOT_TRANSFORM2D_TYPE_DEFINED 266 | #endif 267 | 268 | typedef union hgdn_transform { 269 | // raw data, must be the first field for guaranteeing ABI compatibility with Godot 270 | uint8_t data[sizeof(float[12])]; 271 | float elements[12]; 272 | struct { hgdn_basis basis; hgdn_vector3 origin; }; 273 | } hgdn_transform; 274 | #define HGDN_TRANSFORM3D_IDENTITY ((hgdn_transform){ .elements = {1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0} }) 275 | 276 | #ifndef GODOT_CORE_API_GODOT_TRANSFORM_TYPE_DEFINED 277 | typedef hgdn_transform godot_transform; 278 | #define GODOT_CORE_API_GODOT_TRANSFORM_TYPE_DEFINED 279 | #endif 280 | /// @} 281 | 282 | #include "gdnative_api_struct.gen.h" 283 | 284 | /// @defgroup global Global GDNative pointers 285 | /// Global API structs and GDNativeLibrary pointers 286 | /// @{ 287 | extern const godot_gdnative_core_api_struct *hgdn_core_api; 288 | #ifndef HGDN_NO_CORE_1_1 289 | extern const godot_gdnative_core_1_1_api_struct *hgdn_core_1_1_api; 290 | #endif 291 | #ifndef HGDN_NO_CORE_1_2 292 | extern const godot_gdnative_core_1_2_api_struct *hgdn_core_1_2_api; 293 | #endif 294 | #ifndef HGDN_NO_CORE_1_3 295 | extern const godot_gdnative_core_1_3_api_struct *hgdn_core_1_3_api; 296 | #endif 297 | #ifndef HGDN_NO_EXT_NATIVESCRIPT 298 | extern const godot_gdnative_ext_nativescript_api_struct *hgdn_nativescript_api; 299 | extern const godot_gdnative_ext_nativescript_1_1_api_struct *hgdn_nativescript_1_1_api; 300 | #endif 301 | #ifndef HGDN_NO_EXT_PLUGINSCRIPT 302 | extern const godot_gdnative_ext_pluginscript_api_struct *hgdn_pluginscript_api; 303 | #endif 304 | #ifndef HGDN_NO_EXT_ANDROID 305 | extern const godot_gdnative_ext_android_api_struct *hgdn_android_api; 306 | #endif 307 | #ifndef HGDN_NO_EXT_ARVR 308 | extern const godot_gdnative_ext_arvr_api_struct *hgdn_arvr_api; 309 | extern const godot_gdnative_ext_arvr_1_2_api_struct *hgdn_arvr_1_2_api; 310 | #endif 311 | #ifndef HGDN_NO_EXT_VIDEOCODER 312 | extern const godot_gdnative_ext_videodecoder_api_struct *hgdn_videodecoder_api; 313 | #endif 314 | #ifndef HGDN_NO_EXT_NET 315 | extern const godot_gdnative_ext_net_api_struct *hgdn_net_api; 316 | extern const godot_gdnative_ext_net_3_2_api_struct *hgdn_net_3_2_api; 317 | #endif 318 | extern godot_object *hgdn_library; ///< GDNativeLibrary object being initialized 319 | extern godot_method_bind *hgdn_method_Object_callv; 320 | /// @} 321 | 322 | 323 | /// @defgroup init_deinit Initialization and deinitialization 324 | /// Initialize and deinitialize library, to be called on your own `godot_gdnative_init` and `godot_gdnative_terminate` functions. 325 | /// @{ 326 | HGDN_DECL void hgdn_gdnative_init(const godot_gdnative_init_options *options); 327 | HGDN_DECL void hgdn_gdnative_terminate(const godot_gdnative_terminate_options *options); 328 | /// @} 329 | 330 | 331 | /// @defgroup memory Memory related functions 332 | /// `stdlib.h` compatible functions that track memory usage when Godot is running in debug mode 333 | /// @{ 334 | HGDN_DECL void *hgdn_alloc(size_t size); ///< Compatible with `malloc` 335 | HGDN_DECL void *hgdn_realloc(void *ptr, size_t size); ///< Compatible with `realloc` 336 | /// Compatible with `free`. It is safe to pass NULL without triggering an error message. 337 | HGDN_DECL void hgdn_free(void *ptr); 338 | /// @} 339 | 340 | 341 | /// @defgroup print Printing functions 342 | /// Functions that print a `printf` formatted message to Godot's output 343 | /// @{ 344 | HGDN_DECL void hgdn_print(const char *fmt, ...); 345 | HGDN_DECL void hgdn_print_warning(const char *funcname, const char *filename, int line, const char *fmt, ...); 346 | HGDN_DECL void hgdn_print_error(const char *funcname, const char *filename, int line, const char *fmt, ...); 347 | #ifdef _MSC_VER 348 | #define HGDN_PRINT_WARNING(fmt, ...) (hgdn_print_warning(__FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__)) 349 | #define HGDN_PRINT_ERROR(fmt, ...) (hgdn_print_error(__FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__)) 350 | #else 351 | #define HGDN_PRINT_WARNING(fmt, ...) (hgdn_print_warning(__PRETTY_FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__)) 352 | #define HGDN_PRINT_ERROR(fmt, ...) (hgdn_print_error(__PRETTY_FUNCTION__, __FILE__, __LINE__, fmt, ##__VA_ARGS__)) 353 | #endif 354 | /// @} 355 | 356 | /// @defgroup assert Runtime assertions 357 | /// Macros that check for a condition, aborting current function if this condition is false 358 | /// 359 | /// If condition is false, prints an error message and return a nil variant, so 360 | /// they are meant to be used in GDNative functions that return `godot_variant` 361 | /// like native calls or methods. 362 | /// @{ 363 | #define HGDN_ASSERT(cond) HGDN_ASSERT_MSG((cond), "Assertion error: !(" #cond ")") 364 | /// If `cond` is false, print formatted error message and return nil Variant 365 | #define HGDN_ASSERT_MSG(cond, fmt, ...) if(!(cond)){ HGDN_PRINT_ERROR(fmt, ##__VA_ARGS__); return hgdn_new_nil_variant(); } 366 | /// If `arr` doesn't have at least `min_size` elements, print error message and return nil Variant 367 | #define HGDN_ASSERT_ARRAY_SIZE(arr, min_size) HGDN_ASSERT_MSG(hgdn_core_api->godot_array_size((arr)) >= (min_size), "Error: array should have size of at least " #min_size ", got %d", hgdn_core_api->godot_array_size((arr))) 368 | /// If `argc` isn't at least `min_size`, print error message and return nil Variant 369 | #define HGDN_ASSERT_ARGS_SIZE(argc, min_size) HGDN_ASSERT_MSG((argc) >= (min_size), "Error: expected at least " #min_size " arguments, got %d", argc) 370 | /// @} 371 | 372 | /// @defgroup string_wrapper String wrapper 373 | /// Wrapper around String/CharStrings with pointer and length 374 | /// @{ 375 | typedef struct hgdn_string { 376 | godot_char_string gd_char_string; 377 | const char *ptr; 378 | godot_int length; 379 | } hgdn_string; 380 | HGDN_DECL hgdn_string hgdn_string_get(const godot_string *str); 381 | HGDN_DECL hgdn_string hgdn_string_get_own(godot_string str); 382 | HGDN_DECL void hgdn_string_destroy(hgdn_string *str); 383 | 384 | typedef struct hgdn_wide_string { 385 | godot_string gd_string; 386 | const wchar_t *ptr; 387 | godot_int length; 388 | } hgdn_wide_string; 389 | HGDN_DECL hgdn_wide_string hgdn_wide_string_get(const godot_string *str); 390 | HGDN_DECL hgdn_wide_string hgdn_wide_string_get_own(godot_string str); 391 | HGDN_DECL void hgdn_wide_string_destroy(hgdn_wide_string *str); 392 | /// @} 393 | 394 | 395 | /// @defgroup pool_array_wrapper Pool*Array wrapper 396 | /// Wrapper around Pool*Array types with pointer and array size 397 | /// @{ 398 | typedef struct hgdn_byte_array { 399 | godot_pool_byte_array_read_access *gd_read_access; 400 | const uint8_t *ptr; 401 | godot_int size; 402 | } hgdn_byte_array; 403 | HGDN_DECL hgdn_byte_array hgdn_byte_array_get(const godot_pool_byte_array *array); 404 | HGDN_DECL hgdn_byte_array hgdn_byte_array_get_own(godot_pool_byte_array array); 405 | HGDN_DECL void hgdn_byte_array_destroy(hgdn_byte_array *array); 406 | 407 | typedef struct hgdn_int_array { 408 | godot_pool_int_array_read_access *gd_read_access; 409 | const godot_int *ptr; 410 | godot_int size; 411 | } hgdn_int_array; 412 | HGDN_DECL hgdn_int_array hgdn_int_array_get(const godot_pool_int_array *array); 413 | HGDN_DECL hgdn_int_array hgdn_int_array_get_own(godot_pool_int_array array); 414 | HGDN_DECL void hgdn_int_array_destroy(hgdn_int_array *array); 415 | 416 | typedef struct hgdn_real_array { 417 | godot_pool_real_array_read_access *gd_read_access; 418 | const godot_real *ptr; 419 | godot_int size; 420 | } hgdn_real_array; 421 | HGDN_DECL hgdn_real_array hgdn_real_array_get(const godot_pool_real_array *array); 422 | HGDN_DECL hgdn_real_array hgdn_real_array_get_own(godot_pool_real_array array); 423 | HGDN_DECL void hgdn_real_array_destroy(hgdn_real_array *array); 424 | 425 | typedef struct hgdn_vector2_array { 426 | godot_pool_vector2_array_read_access *gd_read_access; 427 | const godot_vector2 *ptr; 428 | godot_int size; 429 | } hgdn_vector2_array; 430 | HGDN_DECL hgdn_vector2_array hgdn_vector2_array_get(const godot_pool_vector2_array *array); 431 | HGDN_DECL hgdn_vector2_array hgdn_vector2_array_get_own(godot_pool_vector2_array array); 432 | HGDN_DECL void hgdn_vector2_array_destroy(hgdn_vector2_array *array); 433 | 434 | typedef struct hgdn_vector3_array { 435 | godot_pool_vector3_array_read_access *gd_read_access; 436 | const godot_vector3 *ptr; 437 | godot_int size; 438 | } hgdn_vector3_array; 439 | HGDN_DECL hgdn_vector3_array hgdn_vector3_array_get(const godot_pool_vector3_array *array); 440 | HGDN_DECL hgdn_vector3_array hgdn_vector3_array_get_own(godot_pool_vector3_array array); 441 | HGDN_DECL void hgdn_vector3_array_destroy(hgdn_vector3_array *array); 442 | 443 | typedef struct hgdn_color_array { 444 | godot_pool_color_array_read_access *gd_read_access; 445 | const godot_color *ptr; 446 | godot_int size; 447 | } hgdn_color_array; 448 | HGDN_DECL hgdn_color_array hgdn_color_array_get(const godot_pool_color_array *array); 449 | HGDN_DECL hgdn_color_array hgdn_color_array_get_own(godot_pool_color_array array); 450 | HGDN_DECL void hgdn_color_array_destroy(hgdn_color_array *array); 451 | 452 | typedef struct hgdn_string_array { 453 | hgdn_string *strings; 454 | const char **ptr; 455 | godot_int size; 456 | } hgdn_string_array; 457 | HGDN_DECL hgdn_string_array hgdn_string_array_get(const godot_pool_string_array *array); 458 | HGDN_DECL hgdn_string_array hgdn_string_array_get_own(godot_pool_string_array array); 459 | HGDN_DECL void hgdn_string_array_destroy(hgdn_string_array *array); 460 | /// @} 461 | 462 | 463 | /// @defgroup variant_get Typed values from Variants 464 | /// Helper functions to get values directly from a `godot_variant` 465 | /// 466 | /// The `*_own` functions own the passed argument, destroying it. 467 | /// @{ 468 | HGDN_DECL godot_bool hgdn_variant_get_bool(const godot_variant *var); 469 | HGDN_DECL uint64_t hgdn_variant_get_uint(const godot_variant *var); 470 | HGDN_DECL int64_t hgdn_variant_get_int(const godot_variant *var); 471 | HGDN_DECL double hgdn_variant_get_real(const godot_variant *var); 472 | HGDN_DECL godot_vector2 hgdn_variant_get_vector2(const godot_variant *var); 473 | HGDN_DECL godot_vector3 hgdn_variant_get_vector3(const godot_variant *var); 474 | HGDN_DECL godot_rect2 hgdn_variant_get_rect2(const godot_variant *var); 475 | HGDN_DECL godot_plane hgdn_variant_get_plane(const godot_variant *var); 476 | HGDN_DECL godot_quat hgdn_variant_get_quat(const godot_variant *var); 477 | HGDN_DECL godot_aabb hgdn_variant_get_aabb(const godot_variant *var); 478 | HGDN_DECL godot_basis hgdn_variant_get_basis(const godot_variant *var); 479 | HGDN_DECL godot_transform2d hgdn_variant_get_transform2d(const godot_variant *var); 480 | HGDN_DECL godot_transform hgdn_variant_get_transform(const godot_variant *var); 481 | HGDN_DECL godot_color hgdn_variant_get_color(const godot_variant *var); 482 | HGDN_DECL godot_node_path hgdn_variant_get_node_path(const godot_variant *var); 483 | HGDN_DECL godot_rid hgdn_variant_get_rid(const godot_variant *var); 484 | HGDN_DECL godot_object *hgdn_variant_get_object(const godot_variant *var); 485 | HGDN_DECL godot_dictionary hgdn_variant_get_dictionary(const godot_variant *var); 486 | HGDN_DECL godot_array hgdn_variant_get_array(const godot_variant *var); 487 | 488 | HGDN_DECL hgdn_string hgdn_variant_get_string(const godot_variant *var); 489 | HGDN_DECL hgdn_wide_string hgdn_variant_get_wide_string(const godot_variant *var); 490 | HGDN_DECL hgdn_byte_array hgdn_variant_get_byte_array(const godot_variant *var); 491 | HGDN_DECL hgdn_int_array hgdn_variant_get_int_array(const godot_variant *var); 492 | HGDN_DECL hgdn_real_array hgdn_variant_get_real_array(const godot_variant *var); 493 | HGDN_DECL hgdn_vector2_array hgdn_variant_get_vector2_array(const godot_variant *var); 494 | HGDN_DECL hgdn_vector3_array hgdn_variant_get_vector3_array(const godot_variant *var); 495 | HGDN_DECL hgdn_color_array hgdn_variant_get_color_array(const godot_variant *var); 496 | HGDN_DECL hgdn_string_array hgdn_variant_get_string_array(const godot_variant *var); 497 | 498 | HGDN_DECL godot_bool hgdn_variant_get_bool_own(godot_variant var); 499 | HGDN_DECL uint64_t hgdn_variant_get_uint_own(godot_variant var); 500 | HGDN_DECL int64_t hgdn_variant_get_int_own(godot_variant var); 501 | HGDN_DECL double hgdn_variant_get_real_own(godot_variant var); 502 | HGDN_DECL godot_vector2 hgdn_variant_get_vector2_own(godot_variant var); 503 | HGDN_DECL godot_vector3 hgdn_variant_get_vector3_own(godot_variant var); 504 | HGDN_DECL godot_rect2 hgdn_variant_get_rect2_own(godot_variant var); 505 | HGDN_DECL godot_plane hgdn_variant_get_plane_own(godot_variant var); 506 | HGDN_DECL godot_quat hgdn_variant_get_quat_own(godot_variant var); 507 | HGDN_DECL godot_aabb hgdn_variant_get_aabb_own(godot_variant var); 508 | HGDN_DECL godot_basis hgdn_variant_get_basis_own(godot_variant var); 509 | HGDN_DECL godot_transform2d hgdn_variant_get_transform2d_own(godot_variant var); 510 | HGDN_DECL godot_transform hgdn_variant_get_transform_own(godot_variant var); 511 | HGDN_DECL godot_color hgdn_variant_get_color_own(godot_variant var); 512 | HGDN_DECL godot_node_path hgdn_variant_get_node_path_own(godot_variant var); 513 | HGDN_DECL godot_rid hgdn_variant_get_rid_own(godot_variant var); 514 | HGDN_DECL godot_object *hgdn_variant_get_object_own(godot_variant var); 515 | HGDN_DECL godot_dictionary hgdn_variant_get_dictionary_own(godot_variant var); 516 | HGDN_DECL godot_array hgdn_variant_get_array_own(godot_variant var); 517 | 518 | HGDN_DECL hgdn_string hgdn_variant_get_string_own(godot_variant var); 519 | HGDN_DECL hgdn_wide_string hgdn_variant_get_wide_string_own(godot_variant var); 520 | HGDN_DECL hgdn_byte_array hgdn_variant_get_byte_array_own(godot_variant var); 521 | HGDN_DECL hgdn_int_array hgdn_variant_get_int_array_own(godot_variant var); 522 | HGDN_DECL hgdn_real_array hgdn_variant_get_real_array_own(godot_variant var); 523 | HGDN_DECL hgdn_vector2_array hgdn_variant_get_vector2_array_own(godot_variant var); 524 | HGDN_DECL hgdn_vector3_array hgdn_variant_get_vector3_array_own(godot_variant var); 525 | HGDN_DECL hgdn_color_array hgdn_variant_get_color_array_own(godot_variant var); 526 | HGDN_DECL hgdn_string_array hgdn_variant_get_string_array_own(godot_variant var); 527 | /// @} 528 | 529 | 530 | /// @defgroup array_get Typed values from Arrays 531 | /// Helper functions to get values directly from a `godot_array` position 532 | /// @{ 533 | HGDN_DECL godot_bool hgdn_array_get_bool(const godot_array *array, const godot_int index); 534 | HGDN_DECL uint64_t hgdn_array_get_uint(const godot_array *array, const godot_int index); 535 | HGDN_DECL int64_t hgdn_array_get_int(const godot_array *array, const godot_int index); 536 | HGDN_DECL double hgdn_array_get_real(const godot_array *array, const godot_int index); 537 | HGDN_DECL godot_vector2 hgdn_array_get_vector2(const godot_array *array, const godot_int index); 538 | HGDN_DECL godot_vector3 hgdn_array_get_vector3(const godot_array *array, const godot_int index); 539 | HGDN_DECL godot_rect2 hgdn_array_get_rect2(const godot_array *array, const godot_int index); 540 | HGDN_DECL godot_plane hgdn_array_get_plane(const godot_array *array, const godot_int index); 541 | HGDN_DECL godot_quat hgdn_array_get_quat(const godot_array *array, const godot_int index); 542 | HGDN_DECL godot_aabb hgdn_array_get_aabb(const godot_array *array, const godot_int index); 543 | HGDN_DECL godot_basis hgdn_array_get_basis(const godot_array *array, const godot_int index); 544 | HGDN_DECL godot_transform2d hgdn_array_get_transform2d(const godot_array *array, const godot_int index); 545 | HGDN_DECL godot_transform hgdn_array_get_transform(const godot_array *array, const godot_int index); 546 | HGDN_DECL godot_color hgdn_array_get_color(const godot_array *array, const godot_int index); 547 | HGDN_DECL godot_node_path hgdn_array_get_node_path(const godot_array *array, const godot_int index); 548 | HGDN_DECL godot_rid hgdn_array_get_rid(const godot_array *array, const godot_int index); 549 | HGDN_DECL godot_object *hgdn_array_get_object(const godot_array *array, const godot_int index); 550 | HGDN_DECL godot_dictionary hgdn_array_get_dictionary(const godot_array *array, const godot_int index); 551 | HGDN_DECL godot_array hgdn_array_get_array(const godot_array *array, const godot_int index); 552 | 553 | HGDN_DECL hgdn_string hgdn_array_get_string(const godot_array *array, const godot_int index); 554 | HGDN_DECL hgdn_wide_string hgdn_array_get_wide_string(const godot_array *array, const godot_int index); 555 | HGDN_DECL hgdn_byte_array hgdn_array_get_byte_array(const godot_array *array, const godot_int index); 556 | HGDN_DECL hgdn_int_array hgdn_array_get_int_array(const godot_array *array, const godot_int index); 557 | HGDN_DECL hgdn_real_array hgdn_array_get_real_array(const godot_array *array, const godot_int index); 558 | HGDN_DECL hgdn_vector2_array hgdn_array_get_vector2_array(const godot_array *array, const godot_int index); 559 | HGDN_DECL hgdn_vector3_array hgdn_array_get_vector3_array(const godot_array *array, const godot_int index); 560 | HGDN_DECL hgdn_color_array hgdn_array_get_color_array(const godot_array *array, const godot_int index); 561 | HGDN_DECL hgdn_string_array hgdn_array_get_string_array(const godot_array *array, const godot_int index); 562 | /// @} 563 | 564 | 565 | /// @defgroup args_get Typed values from method arguments 566 | /// Helper functions to get values directly from method arguments 567 | /// @{ 568 | HGDN_DECL godot_bool hgdn_args_get_bool(godot_variant **args, const godot_int index); 569 | HGDN_DECL uint64_t hgdn_args_get_uint(godot_variant **args, const godot_int index); 570 | HGDN_DECL int64_t hgdn_args_get_int(godot_variant **args, const godot_int index); 571 | HGDN_DECL double hgdn_args_get_real(godot_variant **args, const godot_int index); 572 | HGDN_DECL godot_vector2 hgdn_args_get_vector2(godot_variant **args, const godot_int index); 573 | HGDN_DECL godot_vector3 hgdn_args_get_vector3(godot_variant **args, const godot_int index); 574 | HGDN_DECL godot_rect2 hgdn_args_get_rect2(godot_variant **args, const godot_int index); 575 | HGDN_DECL godot_plane hgdn_args_get_plane(godot_variant **args, const godot_int index); 576 | HGDN_DECL godot_quat hgdn_args_get_quat(godot_variant **args, const godot_int index); 577 | HGDN_DECL godot_aabb hgdn_args_get_aabb(godot_variant **args, const godot_int index); 578 | HGDN_DECL godot_basis hgdn_args_get_basis(godot_variant **args, const godot_int index); 579 | HGDN_DECL godot_transform2d hgdn_args_get_transform2d(godot_variant **args, const godot_int index); 580 | HGDN_DECL godot_transform hgdn_args_get_transform(godot_variant **args, const godot_int index); 581 | HGDN_DECL godot_color hgdn_args_get_color(godot_variant **args, const godot_int index); 582 | HGDN_DECL godot_node_path hgdn_args_get_node_path(godot_variant **args, const godot_int index); 583 | HGDN_DECL godot_rid hgdn_args_get_rid(godot_variant **args, const godot_int index); 584 | HGDN_DECL godot_object *hgdn_args_get_object(godot_variant **args, const godot_int index); 585 | HGDN_DECL godot_dictionary hgdn_args_get_dictionary(godot_variant **args, const godot_int index); 586 | HGDN_DECL godot_array hgdn_args_get_array(godot_variant **args, const godot_int index); 587 | 588 | HGDN_DECL hgdn_string hgdn_args_get_string(godot_variant **args, const godot_int index); 589 | HGDN_DECL hgdn_wide_string hgdn_args_get_wide_string(godot_variant **args, const godot_int index); 590 | HGDN_DECL hgdn_byte_array hgdn_args_get_byte_array(godot_variant **args, const godot_int index); 591 | HGDN_DECL hgdn_int_array hgdn_args_get_int_array(godot_variant **args, const godot_int index); 592 | HGDN_DECL hgdn_real_array hgdn_args_get_real_array(godot_variant **args, const godot_int index); 593 | HGDN_DECL hgdn_vector2_array hgdn_args_get_vector2_array(godot_variant **args, const godot_int index); 594 | HGDN_DECL hgdn_vector3_array hgdn_args_get_vector3_array(godot_variant **args, const godot_int index); 595 | HGDN_DECL hgdn_color_array hgdn_args_get_color_array(godot_variant **args, const godot_int index); 596 | HGDN_DECL hgdn_string_array hgdn_args_get_string_array(godot_variant **args, const godot_int index); 597 | /// @} 598 | 599 | 600 | /// @defgroup dictionary_get Typed values from Dictionaries 601 | /// Helper functions to get values directly from a `godot_dictionary` with Variant or String key 602 | /// @{ 603 | HGDN_DECL godot_bool hgdn_dictionary_get_bool(const godot_dictionary *dict, const godot_variant *key); 604 | HGDN_DECL uint64_t hgdn_dictionary_get_uint(const godot_dictionary *dict, const godot_variant *key); 605 | HGDN_DECL int64_t hgdn_dictionary_get_int(const godot_dictionary *dict, const godot_variant *key); 606 | HGDN_DECL double hgdn_dictionary_get_real(const godot_dictionary *dict, const godot_variant *key); 607 | HGDN_DECL godot_vector2 hgdn_dictionary_get_vector2(const godot_dictionary *dict, const godot_variant *key); 608 | HGDN_DECL godot_vector3 hgdn_dictionary_get_vector3(const godot_dictionary *dict, const godot_variant *key); 609 | HGDN_DECL godot_rect2 hgdn_dictionary_get_rect2(const godot_dictionary *dict, const godot_variant *key); 610 | HGDN_DECL godot_plane hgdn_dictionary_get_plane(const godot_dictionary *dict, const godot_variant *key); 611 | HGDN_DECL godot_quat hgdn_dictionary_get_quat(const godot_dictionary *dict, const godot_variant *key); 612 | HGDN_DECL godot_aabb hgdn_dictionary_get_aabb(const godot_dictionary *dict, const godot_variant *key); 613 | HGDN_DECL godot_basis hgdn_dictionary_get_basis(const godot_dictionary *dict, const godot_variant *key); 614 | HGDN_DECL godot_transform2d hgdn_dictionary_get_transform2d(const godot_dictionary *dict, const godot_variant *key); 615 | HGDN_DECL godot_transform hgdn_dictionary_get_transform(const godot_dictionary *dict, const godot_variant *key); 616 | HGDN_DECL godot_color hgdn_dictionary_get_color(const godot_dictionary *dict, const godot_variant *key); 617 | HGDN_DECL godot_node_path hgdn_dictionary_get_node_path(const godot_dictionary *dict, const godot_variant *key); 618 | HGDN_DECL godot_rid hgdn_dictionary_get_rid(const godot_dictionary *dict, const godot_variant *key); 619 | HGDN_DECL godot_object *hgdn_dictionary_get_object(const godot_dictionary *dict, const godot_variant *key); 620 | HGDN_DECL godot_dictionary hgdn_dictionary_get_dictionary(const godot_dictionary *dict, const godot_variant *key); 621 | HGDN_DECL godot_array hgdn_dictionary_get_array(const godot_dictionary *dict, const godot_variant *key); 622 | 623 | HGDN_DECL hgdn_string hgdn_dictionary_get_string(const godot_dictionary *dict, const godot_variant *key); 624 | HGDN_DECL hgdn_wide_string hgdn_dictionary_get_wide_string(const godot_dictionary *dict, const godot_variant *key); 625 | HGDN_DECL hgdn_byte_array hgdn_dictionary_get_byte_array(const godot_dictionary *dict, const godot_variant *key); 626 | HGDN_DECL hgdn_int_array hgdn_dictionary_get_int_array(const godot_dictionary *dict, const godot_variant *key); 627 | HGDN_DECL hgdn_real_array hgdn_dictionary_get_real_array(const godot_dictionary *dict, const godot_variant *key); 628 | HGDN_DECL hgdn_vector2_array hgdn_dictionary_get_vector2_array(const godot_dictionary *dict, const godot_variant *key); 629 | HGDN_DECL hgdn_vector3_array hgdn_dictionary_get_vector3_array(const godot_dictionary *dict, const godot_variant *key); 630 | HGDN_DECL hgdn_color_array hgdn_dictionary_get_color_array(const godot_dictionary *dict, const godot_variant *key); 631 | HGDN_DECL hgdn_string_array hgdn_dictionary_get_string_array(const godot_dictionary *dict, const godot_variant *key); 632 | 633 | HGDN_DECL godot_bool hgdn_dictionary_string_get_bool(const godot_dictionary *dict, const char *key); 634 | HGDN_DECL uint64_t hgdn_dictionary_string_get_uint(const godot_dictionary *dict, const char *key); 635 | HGDN_DECL int64_t hgdn_dictionary_string_get_int(const godot_dictionary *dict, const char *key); 636 | HGDN_DECL double hgdn_dictionary_string_get_real(const godot_dictionary *dict, const char *key); 637 | HGDN_DECL godot_vector2 hgdn_dictionary_string_get_vector2(const godot_dictionary *dict, const char *key); 638 | HGDN_DECL godot_vector3 hgdn_dictionary_string_get_vector3(const godot_dictionary *dict, const char *key); 639 | HGDN_DECL godot_rect2 hgdn_dictionary_string_get_rect2(const godot_dictionary *dict, const char *key); 640 | HGDN_DECL godot_plane hgdn_dictionary_string_get_plane(const godot_dictionary *dict, const char *key); 641 | HGDN_DECL godot_quat hgdn_dictionary_string_get_quat(const godot_dictionary *dict, const char *key); 642 | HGDN_DECL godot_aabb hgdn_dictionary_string_get_aabb(const godot_dictionary *dict, const char *key); 643 | HGDN_DECL godot_basis hgdn_dictionary_string_get_basis(const godot_dictionary *dict, const char *key); 644 | HGDN_DECL godot_transform2d hgdn_dictionary_string_get_transform2d(const godot_dictionary *dict, const char *key); 645 | HGDN_DECL godot_transform hgdn_dictionary_string_get_transform(const godot_dictionary *dict, const char *key); 646 | HGDN_DECL godot_color hgdn_dictionary_string_get_color(const godot_dictionary *dict, const char *key); 647 | HGDN_DECL godot_node_path hgdn_dictionary_string_get_node_path(const godot_dictionary *dict, const char *key); 648 | HGDN_DECL godot_rid hgdn_dictionary_string_get_rid(const godot_dictionary *dict, const char *key); 649 | HGDN_DECL godot_object *hgdn_dictionary_string_get_object(const godot_dictionary *dict, const char *key); 650 | HGDN_DECL godot_dictionary hgdn_dictionary_string_get_dictionary(const godot_dictionary *dict, const char *key); 651 | HGDN_DECL godot_array hgdn_dictionary_string_get_array(const godot_dictionary *dict, const char *key); 652 | 653 | HGDN_DECL hgdn_string hgdn_dictionary_string_get_string(const godot_dictionary *dict, const char *key); 654 | HGDN_DECL hgdn_wide_string hgdn_dictionary_string_get_wide_string(const godot_dictionary *dict, const char *key); 655 | HGDN_DECL hgdn_byte_array hgdn_dictionary_string_get_byte_array(const godot_dictionary *dict, const char *key); 656 | HGDN_DECL hgdn_int_array hgdn_dictionary_string_get_int_array(const godot_dictionary *dict, const char *key); 657 | HGDN_DECL hgdn_real_array hgdn_dictionary_string_get_real_array(const godot_dictionary *dict, const char *key); 658 | HGDN_DECL hgdn_vector2_array hgdn_dictionary_string_get_vector2_array(const godot_dictionary *dict, const char *key); 659 | HGDN_DECL hgdn_vector3_array hgdn_dictionary_string_get_vector3_array(const godot_dictionary *dict, const char *key); 660 | HGDN_DECL hgdn_color_array hgdn_dictionary_string_get_color_array(const godot_dictionary *dict, const char *key); 661 | HGDN_DECL hgdn_string_array hgdn_dictionary_string_get_string_array(const godot_dictionary *dict, const char *key); 662 | /// @} 663 | 664 | 665 | /// @defgroup new_variant Variant constructors 666 | /// Helper functions to create Variant values 667 | /// 668 | /// The `*_own` functions own the passed argument, destroying it. Useful 669 | /// when you create the object just for creating a Variant of it, enabling 670 | /// the idiom `hgdn_new_string_variant_own(hgdn_new_string("..."))`. 671 | /// @{ 672 | HGDN_DECL godot_variant hgdn_new_variant_copy(const godot_variant *value); 673 | HGDN_DECL godot_variant hgdn_new_nil_variant(); 674 | HGDN_DECL godot_variant hgdn_new_bool_variant(const godot_bool value); 675 | HGDN_DECL godot_variant hgdn_new_uint_variant(const uint64_t value); 676 | HGDN_DECL godot_variant hgdn_new_int_variant(const int64_t value); 677 | HGDN_DECL godot_variant hgdn_new_real_variant(const double value); 678 | HGDN_DECL godot_variant hgdn_new_string_variant(const godot_string *value); 679 | HGDN_DECL godot_variant hgdn_new_cstring_variant(const char *value); 680 | HGDN_DECL godot_variant hgdn_new_wide_string_variant(const wchar_t *value); 681 | HGDN_DECL godot_variant hgdn_new_vector2_variant(const godot_vector2 value); 682 | HGDN_DECL godot_variant hgdn_new_vector3_variant(const godot_vector3 value); 683 | HGDN_DECL godot_variant hgdn_new_rect2_variant(const godot_rect2 value); 684 | HGDN_DECL godot_variant hgdn_new_plane_variant(const godot_plane value); 685 | HGDN_DECL godot_variant hgdn_new_quat_variant(const godot_quat value); 686 | HGDN_DECL godot_variant hgdn_new_aabb_variant(const godot_aabb value); 687 | HGDN_DECL godot_variant hgdn_new_basis_variant(const godot_basis value); 688 | HGDN_DECL godot_variant hgdn_new_transform2d_variant(const godot_transform2d value); 689 | HGDN_DECL godot_variant hgdn_new_transform_variant(const godot_transform value); 690 | HGDN_DECL godot_variant hgdn_new_color_variant(const godot_color value); 691 | HGDN_DECL godot_variant hgdn_new_node_path_variant(const godot_node_path *value); 692 | HGDN_DECL godot_variant hgdn_new_rid_variant(const godot_rid *value); 693 | HGDN_DECL godot_variant hgdn_new_object_variant(const godot_object *value); 694 | HGDN_DECL godot_variant hgdn_new_dictionary_variant(const godot_dictionary *value); 695 | HGDN_DECL godot_variant hgdn_new_array_variant(const godot_array *value); 696 | HGDN_DECL godot_variant hgdn_new_pool_byte_array_variant(const godot_pool_byte_array *value); 697 | HGDN_DECL godot_variant hgdn_new_pool_int_array_variant(const godot_pool_int_array *value); 698 | HGDN_DECL godot_variant hgdn_new_pool_real_array_variant(const godot_pool_real_array *value); 699 | HGDN_DECL godot_variant hgdn_new_pool_vector2_array_variant(const godot_pool_vector2_array *value); 700 | HGDN_DECL godot_variant hgdn_new_pool_vector3_array_variant(const godot_pool_vector3_array *value); 701 | HGDN_DECL godot_variant hgdn_new_pool_color_array_variant(const godot_pool_color_array *value); 702 | HGDN_DECL godot_variant hgdn_new_pool_string_array_variant(const godot_pool_string_array *value); 703 | 704 | HGDN_DECL godot_variant hgdn_new_string_variant_own(godot_string value); 705 | HGDN_DECL godot_variant hgdn_new_node_path_variant_own(godot_node_path value); 706 | HGDN_DECL godot_variant hgdn_new_dictionary_variant_own(godot_dictionary value); 707 | HGDN_DECL godot_variant hgdn_new_array_variant_own(godot_array value); 708 | HGDN_DECL godot_variant hgdn_new_pool_byte_array_variant_own(godot_pool_byte_array value); 709 | HGDN_DECL godot_variant hgdn_new_pool_int_array_variant_own(godot_pool_int_array value); 710 | HGDN_DECL godot_variant hgdn_new_pool_real_array_variant_own(godot_pool_real_array value); 711 | HGDN_DECL godot_variant hgdn_new_pool_vector2_array_variant_own(godot_pool_vector2_array value); 712 | HGDN_DECL godot_variant hgdn_new_pool_vector3_array_variant_own(godot_pool_vector3_array value); 713 | HGDN_DECL godot_variant hgdn_new_pool_color_array_variant_own(godot_pool_color_array value); 714 | HGDN_DECL godot_variant hgdn_new_pool_string_array_variant_own(godot_pool_string_array value); 715 | 716 | #ifdef __cplusplus 717 | extern "C++" { 718 | HGDN_DECL godot_variant hgdn_new_variant(const godot_bool value); 719 | HGDN_DECL godot_variant hgdn_new_variant(const unsigned int value); 720 | HGDN_DECL godot_variant hgdn_new_variant(const uint64_t value); 721 | HGDN_DECL godot_variant hgdn_new_variant(const int value); 722 | HGDN_DECL godot_variant hgdn_new_variant(const int64_t value); 723 | HGDN_DECL godot_variant hgdn_new_variant(const double value); 724 | HGDN_DECL godot_variant hgdn_new_variant(const godot_string *value); 725 | HGDN_DECL godot_variant hgdn_new_variant(const char *value); 726 | HGDN_DECL godot_variant hgdn_new_variant(const wchar_t *value); 727 | HGDN_DECL godot_variant hgdn_new_variant(const godot_vector2 value); 728 | HGDN_DECL godot_variant hgdn_new_variant(const godot_vector3 value); 729 | HGDN_DECL godot_variant hgdn_new_variant(const godot_rect2 value); 730 | HGDN_DECL godot_variant hgdn_new_variant(const godot_plane value); 731 | HGDN_DECL godot_variant hgdn_new_variant(const godot_quat value); 732 | HGDN_DECL godot_variant hgdn_new_variant(const godot_aabb value); 733 | HGDN_DECL godot_variant hgdn_new_variant(const godot_basis value); 734 | HGDN_DECL godot_variant hgdn_new_variant(const godot_transform2d value); 735 | HGDN_DECL godot_variant hgdn_new_variant(const godot_transform value); 736 | HGDN_DECL godot_variant hgdn_new_variant(const godot_color value); 737 | HGDN_DECL godot_variant hgdn_new_variant(const godot_node_path *value); 738 | HGDN_DECL godot_variant hgdn_new_variant(const godot_rid *value); 739 | HGDN_DECL godot_variant hgdn_new_variant(const godot_object *value); 740 | HGDN_DECL godot_variant hgdn_new_variant(const godot_dictionary *value); 741 | HGDN_DECL godot_variant hgdn_new_variant(const godot_array *value); 742 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_byte_array *value); 743 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_int_array *value); 744 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_real_array *value); 745 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_vector2_array *value); 746 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_vector3_array *value); 747 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_color_array *value); 748 | HGDN_DECL godot_variant hgdn_new_variant(const godot_pool_string_array *value); 749 | HGDN_DECL godot_variant hgdn_new_variant(godot_string value); 750 | HGDN_DECL godot_variant hgdn_new_variant(godot_node_path value); 751 | HGDN_DECL godot_variant hgdn_new_variant(godot_dictionary value); 752 | HGDN_DECL godot_variant hgdn_new_variant(godot_array value); 753 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_byte_array value); 754 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_int_array value); 755 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_real_array value); 756 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_vector2_array value); 757 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_vector3_array value); 758 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_color_array value); 759 | HGDN_DECL godot_variant hgdn_new_variant(godot_pool_string_array value); 760 | HGDN_DECL godot_variant hgdn_new_variant(const godot_variant *value); 761 | HGDN_DECL HGDN_CONSTEXPR godot_variant hgdn_new_variant(godot_variant value); 762 | } 763 | #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L // C11 764 | /// Overloaded function/macro for creating Variants from any values. Available in C++ and C11. 765 | #define hgdn_new_variant(value) \ 766 | (_Generic((value), \ 767 | godot_bool: hgdn_new_bool_variant, \ 768 | unsigned int: hgdn_new_uint_variant, \ 769 | uint64_t: hgdn_new_uint_variant, \ 770 | int: hgdn_new_int_variant, \ 771 | int64_t: hgdn_new_int_variant, \ 772 | float: hgdn_new_real_variant, \ 773 | double: hgdn_new_real_variant, \ 774 | godot_string*: hgdn_new_string_variant, const godot_string*: hgdn_new_string_variant, \ 775 | char*: hgdn_new_cstring_variant, const char*: hgdn_new_cstring_variant, \ 776 | wchar_t*: hgdn_new_wide_string_variant, const wchar_t*: hgdn_new_wide_string_variant, \ 777 | godot_vector2: hgdn_new_vector2_variant, \ 778 | godot_vector3: hgdn_new_vector3_variant, \ 779 | godot_rect2: hgdn_new_rect2_variant, \ 780 | godot_plane: hgdn_new_plane_variant, \ 781 | godot_quat: hgdn_new_quat_variant, \ 782 | godot_aabb: hgdn_new_aabb_variant, \ 783 | godot_basis: hgdn_new_basis_variant, \ 784 | godot_transform2d: hgdn_new_transform2d_variant, \ 785 | godot_transform: hgdn_new_transform_variant, \ 786 | godot_color: hgdn_new_color_variant, \ 787 | godot_node_path*: hgdn_new_node_path_variant, const godot_node_path*: hgdn_new_node_path_variant, \ 788 | godot_rid*: hgdn_new_rid_variant, const godot_rid*: hgdn_new_rid_variant, \ 789 | godot_object*: hgdn_new_object_variant, const godot_object*: hgdn_new_object_variant, \ 790 | godot_dictionary*: hgdn_new_dictionary_variant, const godot_dictionary*: hgdn_new_dictionary_variant, \ 791 | godot_array*: hgdn_new_array_variant, const godot_array*: hgdn_new_array_variant, \ 792 | godot_pool_byte_array*: hgdn_new_pool_byte_array_variant, const godot_pool_byte_array*: hgdn_new_pool_byte_array_variant, \ 793 | godot_pool_int_array*: hgdn_new_pool_int_array_variant, const godot_pool_int_array*: hgdn_new_pool_int_array_variant, \ 794 | godot_pool_real_array*: hgdn_new_pool_real_array_variant, const godot_pool_real_array*: hgdn_new_pool_real_array_variant, \ 795 | godot_pool_vector2_array*: hgdn_new_pool_vector2_array_variant, const godot_pool_vector2_array*: hgdn_new_pool_vector2_array_variant, \ 796 | godot_pool_vector3_array*: hgdn_new_pool_vector3_array_variant, const godot_pool_vector3_array*: hgdn_new_pool_vector3_array_variant, \ 797 | godot_pool_color_array*: hgdn_new_pool_color_array_variant, const godot_pool_color_array*: hgdn_new_pool_color_array_variant, \ 798 | godot_pool_string_array*: hgdn_new_pool_string_array_variant, const godot_pool_string_array*: hgdn_new_pool_string_array_variant, \ 799 | godot_string: hgdn_new_string_variant_own, \ 800 | godot_node_path: hgdn_new_node_path_variant_own, \ 801 | godot_dictionary: hgdn_new_dictionary_variant_own, \ 802 | godot_array: hgdn_new_array_variant_own, \ 803 | godot_pool_byte_array: hgdn_new_pool_byte_array_variant_own, \ 804 | godot_pool_int_array: hgdn_new_pool_int_array_variant_own, \ 805 | godot_pool_real_array: hgdn_new_pool_real_array_variant_own, \ 806 | godot_pool_vector2_array: hgdn_new_pool_vector2_array_variant_own, \ 807 | godot_pool_vector3_array: hgdn_new_pool_vector3_array_variant_own, \ 808 | godot_pool_color_array: hgdn_new_pool_color_array_variant_own, \ 809 | godot_pool_string_array: hgdn_new_pool_string_array_variant_own, \ 810 | godot_variant*: hgdn_new_variant_copy, const godot_variant*: hgdn_new_variant_copy, \ 811 | godot_variant: hgdn__variant_return \ 812 | )(value)) 813 | HGDN_DECL godot_variant hgdn__variant_return(godot_variant value); 814 | #else 815 | #define hgdn_new_variant(value) (value) // No transformations in C without C11 support 816 | #endif 817 | /// @} 818 | 819 | 820 | /// @defgroup string String creation 821 | /// Helper functions to create Strings 822 | /// @{ 823 | HGDN_DECL godot_string hgdn_new_string(const char *cstr); 824 | HGDN_DECL godot_string hgdn_new_string_with_len(const char *cstr, const godot_int len); 825 | /// @param fmt A `printf` compatible format 826 | HGDN_DECL godot_string hgdn_new_formatted_string(const char *fmt, ...); 827 | 828 | HGDN_DECL godot_string hgdn_new_wide_string(const wchar_t *wstr); 829 | HGDN_DECL godot_string hgdn_new_wide_string_with_len(const wchar_t *wstr, const godot_int len); 830 | /// @} 831 | 832 | 833 | /// @defgroup array Pool*Array/Array creation 834 | /// Helper functions to create Pool*Array/Array objects from sized buffers 835 | /// 836 | /// The `*_args` variadic functions/macros construct a temporary array and call 837 | /// the functions. On C++11 they are implemented using templates with parameter pack. 838 | /// @{ 839 | HGDN_DECL godot_pool_byte_array hgdn_new_byte_array(const uint8_t *buffer, const godot_int size); 840 | HGDN_DECL godot_pool_int_array hgdn_new_int_array(const godot_int *buffer, const godot_int size); 841 | HGDN_DECL godot_pool_real_array hgdn_new_real_array(const godot_real *buffer, const godot_int size); 842 | HGDN_DECL godot_pool_vector2_array hgdn_new_vector2_array(const godot_vector2 *buffer, const godot_int size); 843 | HGDN_DECL godot_pool_vector3_array hgdn_new_vector3_array(const godot_vector3 *buffer, const godot_int size); 844 | HGDN_DECL godot_pool_color_array hgdn_new_color_array(const godot_color *buffer, const godot_int size); 845 | /// @note All strings must be NULL terminated. 846 | HGDN_DECL godot_pool_string_array hgdn_new_string_array(const char *const *buffer, const godot_int size); 847 | HGDN_DECL godot_array hgdn_new_array(const godot_variant *const *buffer, const godot_int size); 848 | /// @note Variants in `buffer` will be destroyed, convenient if you create Variants only for constructing the Array 849 | HGDN_DECL godot_array hgdn_new_array_own(godot_variant *buffer, const godot_int size); 850 | 851 | #if defined(__cplusplus) && __cplusplus >= 201103L // Parameter pack is a C++11 feature 852 | extern "C++" { 853 | template godot_pool_byte_array hgdn_new_byte_array_args(Args... args) { 854 | uint8_t buffer[] = { args... }; 855 | return hgdn_new_byte_array(buffer, sizeof...(args)); 856 | } 857 | template godot_pool_int_array hgdn_new_int_array_args(Args... args) { 858 | godot_int buffer[] = { args... }; 859 | return hgdn_new_int_array(buffer, sizeof...(args)); 860 | } 861 | template godot_pool_real_array hgdn_new_real_array_args(Args... args) { 862 | godot_real buffer[] = { args... }; 863 | return hgdn_new_real_array(buffer, sizeof...(args)); 864 | } 865 | template godot_pool_vector2_array hgdn_new_vector2_array_args(Args... args) { 866 | godot_vector2 buffer[] = { args... }; 867 | return hgdn_new_vector2_array(buffer, sizeof...(args)); 868 | } 869 | template godot_pool_vector3_array hgdn_new_vector3_array_args(Args... args) { 870 | godot_vector3 buffer[] = { args... }; 871 | return hgdn_new_vector3_array(buffer, sizeof...(args)); 872 | } 873 | template godot_pool_color_array hgdn_new_color_array_args(Args... args) { 874 | godot_color buffer[] = { args... }; 875 | return hgdn_new_color_array(buffer, sizeof...(args)); 876 | } 877 | template godot_pool_string_array hgdn_new_string_array_args(Args... args) { 878 | const char *const buffer[] = { args... }; 879 | return hgdn_new_string_array(buffer, sizeof...(args)); 880 | } 881 | template godot_array hgdn_new_array_args(Args... args) { 882 | godot_variant buffer[] = { hgdn_new_variant(args)... }; 883 | return hgdn_new_array_own(buffer, sizeof...(args)); 884 | } 885 | } 886 | #else 887 | #define hgdn_new_byte_array_args(...) (hgdn_new_byte_array((const uint8_t[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 888 | #define hgdn_new_int_array_args(...) (hgdn_new_int_array((const godot_int[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 889 | #define hgdn_new_real_array_args(...) (hgdn_new_real_array((const godot_real[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 890 | #define hgdn_new_vector2_array_args(...) (hgdn_new_vector2_array((const godot_vector2[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 891 | #define hgdn_new_vector3_array_args(...) (hgdn_new_vector3_array((const godot_vector3[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 892 | #define hgdn_new_color_array_args(...) (hgdn_new_color_array((const godot_color[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 893 | #define hgdn_new_string_array_args(...) (hgdn_new_string_array((const char *const []){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 894 | /// @note In C++ and C11 the arguments passed are transformed by `hgdn_new_variant`, so primitive C data can be passed directly 895 | #define hgdn_new_array_args(...) (hgdn_new_array_own((godot_variant[]){ HGDN__MAP(hgdn_new_variant, __VA_ARGS__) }, HGDN__NARG(__VA_ARGS__))) 896 | #endif 897 | /// @} 898 | 899 | 900 | /// @defgroup dictionary Dictionary creation 901 | /// Helper functions to create Dictionaries 902 | /// 903 | /// The `*_own` functions own the passed Variants, destroying them. Useful 904 | /// when you create the Variants just for creating a Dictionary with them. 905 | /// @{ 906 | typedef struct hgdn_dictionary_entry { 907 | godot_variant *key, *value; 908 | } hgdn_dictionary_entry; 909 | 910 | typedef struct hgdn_dictionary_entry_own { 911 | godot_variant key, value; 912 | } hgdn_dictionary_entry_own; 913 | 914 | typedef struct hgdn_dictionary_entry_string { 915 | const char *key; 916 | godot_variant *value; 917 | } hgdn_dictionary_entry_string; 918 | 919 | typedef struct hgdn_dictionary_entry_string_own { 920 | const char *key; 921 | godot_variant value; 922 | } hgdn_dictionary_entry_string_own; 923 | 924 | typedef struct hgdn_dictionary_entry_string_string { 925 | const char *key, *value; 926 | } hgdn_dictionary_entry_string_string; 927 | 928 | typedef struct hgdn_dictionary_entry_string_int { 929 | const char *key; 930 | godot_int value; 931 | } hgdn_dictionary_entry_string_int; 932 | 933 | HGDN_DECL godot_dictionary hgdn_new_dictionary(const hgdn_dictionary_entry *buffer, const godot_int size); 934 | HGDN_DECL godot_dictionary hgdn_new_dictionary_string(const hgdn_dictionary_entry_string *buffer, const godot_int size); 935 | HGDN_DECL godot_dictionary hgdn_new_dictionary_string_int(const hgdn_dictionary_entry_string_int *buffer, const godot_int size); 936 | HGDN_DECL godot_dictionary hgdn_new_dictionary_string_string(const hgdn_dictionary_entry_string_string *buffer, const godot_int size); 937 | HGDN_DECL godot_dictionary hgdn_new_dictionary_own(hgdn_dictionary_entry_own *buffer, const godot_int size); 938 | HGDN_DECL godot_dictionary hgdn_new_dictionary_string_own(hgdn_dictionary_entry_string_own *buffer, const godot_int size); 939 | 940 | #define hgdn_new_dictionary_args(...) (hgdn_new_dictionary((const hgdn_dictionary_entry[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 941 | #define hgdn_new_dictionary_string_args(...) (hgdn_new_dictionary_string((const hgdn_dictionary_entry_string[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 942 | #define hgdn_new_dictionary_string_int_args(...) (hgdn_new_dictionary_string_int((const hgdn_dictionary_entry_string_int[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 943 | #define hgdn_new_dictionary_string_string_args(...) (hgdn_new_dictionary_string_string((const hgdn_dictionary_entry_string_string[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 944 | #define hgdn_new_dictionary_own_args(...) (hgdn_new_dictionary_own((hgdn_dictionary_entry_own[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 945 | #define hgdn_new_dictionary_string_own_args(...) (hgdn_new_dictionary_string_own((hgdn_dictionary_entry_string_own[]){ __VA_ARGS__ }, HGDN__NARG(__VA_ARGS__))) 946 | /// @} 947 | 948 | 949 | /// @defgroup object Object functions 950 | /// Helper functions to work with `godot_object` values 951 | /// @{ 952 | HGDN_DECL godot_variant hgdn_object_callv(godot_object *instance, const char *method, const godot_array *args); 953 | HGDN_DECL godot_variant hgdn_object_callv_own(godot_object *instance, const char *method, godot_array args); 954 | #define hgdn_object_get(instance, property) (hgdn_object_call(instance, "get", property)) 955 | #define hgdn_object_set(instance, property, value) (hgdn_object_call(instance, "set", property, value)) 956 | 957 | #if defined(__cplusplus) && __cplusplus >= 201103L // Parameter pack is a C++11 feature 958 | extern "C++" template godot_variant hgdn_object_call(godot_object *instance, const char *method, Args... args) { 959 | godot_array args_array = hgdn_new_array_args(args...); 960 | return hgdn_object_callv_own(instance, method, args_array); 961 | } 962 | #else 963 | /// @note In C++ and C11 the arguments passed are transformed by `hgdn_new_variant`, so primitive C data can be passed directly 964 | #define hgdn_object_call(instance, method, ...) (hgdn_object_callv_own((instance), (method), hgdn_new_array_args(__VA_ARGS__))) 965 | #endif 966 | /// @} 967 | 968 | 969 | /// @defgroup nativescript NativeScript helpers 970 | /// Definitions that help registering classes in Godot, focusing on wrapping C structs 971 | /// @{ 972 | #ifndef HGDN_NO_EXT_NATIVESCRIPT 973 | typedef struct hgdn_property_info { 974 | const char *path; 975 | godot_property_set_func setter; 976 | godot_property_get_func getter; 977 | // godot_property_attributes 978 | godot_int type; 979 | godot_property_hint hint; 980 | const char *hint_string; 981 | godot_property_usage_flags usage; 982 | godot_variant default_value; 983 | godot_method_rpc_mode rset_type; 984 | /// NativeScript 1.1 documentation 985 | const char *documentation; 986 | } hgdn_property_info; 987 | 988 | typedef struct hgdn_method_argument_info { 989 | const char *name; 990 | godot_variant_type type; 991 | godot_property_hint hint; 992 | const char *hint_string; 993 | } hgdn_method_argument_info; 994 | 995 | typedef struct hgdn_method_info { 996 | const char *name; 997 | godot_instance_method method; 998 | // godot_method_attributes 999 | godot_method_rpc_mode rpc_type; 1000 | /// NativeScript 1.1 documentation 1001 | const char *documentation; 1002 | /// NULL-terminated array of argument info. If NULL, no documentation is registered. 1003 | /// When a property with NULL `name` is encountered, stops registering. 1004 | /// @see @ref hgdn_method_arguments 1005 | hgdn_method_argument_info *arguments_info; 1006 | } hgdn_method_info; 1007 | 1008 | typedef struct hgdn_signal_argument_info { 1009 | const char *name; 1010 | godot_int type; 1011 | godot_property_hint hint; 1012 | const char *hint_string; 1013 | godot_variant default_value; 1014 | } hgdn_signal_argument_info; 1015 | 1016 | typedef struct hgdn_signal_info { 1017 | const char *name; 1018 | hgdn_signal_argument_info *arguments_info; 1019 | int num_default_args; 1020 | /// NativeScript 1.1 documentation 1021 | const char *documentation; 1022 | } hgdn_signal_info; 1023 | 1024 | typedef struct hgdn_class_info { 1025 | const char *name; 1026 | const char *base; 1027 | godot_instance_create_func create; 1028 | godot_instance_destroy_func destroy; 1029 | /// NULL-terminated array of properties. If NULL, no properties are registered. 1030 | /// When a property with NULL `path` is encountered, stops registering. 1031 | /// @see @ref hgdn_properties 1032 | hgdn_property_info *properties; 1033 | /// NULL-terminated array of methods. If NULL, no methods are registered. 1034 | /// When a method with NULL `name` is encountered, stops registering. 1035 | /// @see @ref hgdn_methods 1036 | hgdn_method_info *methods; 1037 | /// NULL-terminated array of signals. If NULL, no signals are registered. 1038 | /// When a signal with NULL `name` is encountered, stops registering. 1039 | /// @see @ref hgdn_signals 1040 | hgdn_signal_info *signals; 1041 | godot_bool tool; 1042 | /// NativeScript 1.1 documentation 1043 | const char *documentation; 1044 | } hgdn_class_info; 1045 | 1046 | /// Helper for a literal NULL-terminated array of `hgdn_property_info` 1047 | #define hgdn_properties(...) ((hgdn_property_info[]){ __VA_ARGS__, {} }) 1048 | /// Helper for a literal NULL-terminated array of `hgdn_method_info` 1049 | #define hgdn_methods(...) ((hgdn_method_info[]){ __VA_ARGS__, {} }) 1050 | /// Helper for a literal NULL-terminated array of `hgdn_method_argument_info` 1051 | #define hgdn_method_arguments(...) ((hgdn_method_argument_info[]){ __VA_ARGS__, {} }) 1052 | /// Helper for a literal NULL-terminated array of `hgdn_signal_info` 1053 | #define hgdn_signals(...) ((hgdn_signal_info[]){ __VA_ARGS__, {} }) 1054 | /// Helper for a literal NULL-terminated array of `hgdn_signal_argument_info` 1055 | #define hgdn_signal_arguments(...) ((hgdn_signal_argument_info[]){ __VA_ARGS__, {} }) 1056 | 1057 | HGDN_DECL void hgdn_register_class(void *gdnative_handle, const hgdn_class_info *class_info); 1058 | 1059 | /// Function that allocates and returns zero-initialized `alloc_size` bytes, to be used as instance create function 1060 | HGDN_DECL void *hgdn_instance_alloc(godot_object *instance, void *alloc_size); 1061 | /// Create a `godot_instance_create_func` that allocates a zero-initialized `ctype` 1062 | #define hgdn_instance_create_func_alloc(ctype) ((const godot_instance_create_func){ &hgdn_instance_alloc, (void *) sizeof(ctype) }) 1063 | /// Function that frees data with `hgdn_free`, to be used as instance destroy function 1064 | HGDN_DECL void hgdn_instance_free(godot_object *instance, void *method_data, void *data); 1065 | /// Create a `godot_instance_destroy_func` that frees instance data 1066 | #define hgdn_instance_destroy_func_free() ((const godot_instance_destroy_func){ &hgdn_instance_free }) 1067 | 1068 | HGDN_DECL godot_variant *hgdn_property_constant_alloc(godot_variant value); 1069 | HGDN_DECL void hgdn_property_constant_free(void *value); 1070 | HGDN_DECL godot_variant hgdn_property_constant_get(godot_object *instance, void *value, void *data); 1071 | /// @note In C++ and C11 the value passed is transformed by `hgdn_new_variant`, so primitive C data can be passed directly 1072 | #define hgdn_property_constant(value) ((const godot_property_get_func){ &hgdn_property_constant_get, (void *) hgdn_property_constant_alloc(hgdn_new_variant((value))), &hgdn_property_constant_free }) 1073 | #endif // HGDN_NO_EXT_NATIVESCRIPT 1074 | /// @} 1075 | 1076 | #ifdef __cplusplus 1077 | } 1078 | #endif 1079 | 1080 | #endif // __HGDN_H__ 1081 | 1082 | /////////////////////////////////////////////////////////////////////////////// 1083 | 1084 | #ifdef HGDN_IMPLEMENTATION 1085 | 1086 | #include 1087 | #include 1088 | #include 1089 | 1090 | const godot_gdnative_core_api_struct *hgdn_core_api; 1091 | #ifndef HGDN_NO_CORE_1_1 1092 | const godot_gdnative_core_1_1_api_struct *hgdn_core_1_1_api; 1093 | #endif 1094 | #ifndef HGDN_NO_CORE_1_2 1095 | const godot_gdnative_core_1_2_api_struct *hgdn_core_1_2_api; 1096 | #endif 1097 | #ifndef HGDN_NO_CORE_1_3 1098 | const godot_gdnative_core_1_3_api_struct *hgdn_core_1_3_api; 1099 | #endif 1100 | #ifndef HGDN_NO_EXT_NATIVESCRIPT 1101 | const godot_gdnative_ext_nativescript_api_struct *hgdn_nativescript_api; 1102 | const godot_gdnative_ext_nativescript_1_1_api_struct *hgdn_nativescript_1_1_api; 1103 | #endif 1104 | #ifndef HGDN_NO_EXT_PLUGINSCRIPT 1105 | const godot_gdnative_ext_pluginscript_api_struct *hgdn_pluginscript_api; 1106 | #endif 1107 | #ifndef HGDN_NO_EXT_ANDROID 1108 | const godot_gdnative_ext_android_api_struct *hgdn_android_api; 1109 | #endif 1110 | #ifndef HGDN_NO_EXT_ARVR 1111 | const godot_gdnative_ext_arvr_api_struct *hgdn_arvr_api; 1112 | const godot_gdnative_ext_arvr_1_2_api_struct *hgdn_arvr_1_2_api; 1113 | #endif 1114 | #ifndef HGDN_NO_EXT_VIDEOCODER 1115 | const godot_gdnative_ext_videodecoder_api_struct *hgdn_videodecoder_api; 1116 | #endif 1117 | #ifndef HGDN_NO_EXT_NET 1118 | const godot_gdnative_ext_net_api_struct *hgdn_net_api; 1119 | const godot_gdnative_ext_net_3_2_api_struct *hgdn_net_3_2_api; 1120 | #endif 1121 | godot_object *hgdn_library; 1122 | godot_method_bind *hgdn_method_Object_callv; 1123 | 1124 | static char hgdn__format_string_buffer[HGDN_STRING_FORMAT_BUFFER_SIZE]; 1125 | #define HGDN__FILL_FORMAT_BUFFER(fmt, ...) \ 1126 | va_list args; \ 1127 | va_start(args, fmt); \ 1128 | godot_int size = vsnprintf(hgdn__format_string_buffer, HGDN_STRING_FORMAT_BUFFER_SIZE, fmt, args); \ 1129 | if (size > HGDN_STRING_FORMAT_BUFFER_SIZE) { \ 1130 | size = HGDN_STRING_FORMAT_BUFFER_SIZE; \ 1131 | } \ 1132 | va_end(args) 1133 | 1134 | static godot_array hgdn__empty_array; 1135 | 1136 | // Init and terminate 1137 | void hgdn_gdnative_init(const godot_gdnative_init_options *options) { 1138 | hgdn_library = options->gd_native_library; 1139 | hgdn_core_api = options->api_struct; 1140 | for (const godot_gdnative_api_struct *ext = hgdn_core_api->next; ext; ext = ext->next) { 1141 | #ifndef HGDN_NO_CORE_1_1 1142 | if (ext->version.major == 1 && ext->version.minor == 1) { 1143 | hgdn_core_1_1_api = (const godot_gdnative_core_1_1_api_struct *) ext; 1144 | } 1145 | #endif 1146 | #ifndef HGDN_NO_CORE_1_2 1147 | if (ext->version.major == 1 && ext->version.minor == 2) { 1148 | hgdn_core_1_2_api = (const godot_gdnative_core_1_2_api_struct *) ext; 1149 | } 1150 | #endif 1151 | #ifndef HGDN_NO_CORE_1_3 1152 | if (ext->version.major == 1 && ext->version.minor == 3) { 1153 | hgdn_core_1_3_api = (const godot_gdnative_core_1_3_api_struct *) ext; 1154 | } 1155 | #endif 1156 | } 1157 | 1158 | for (unsigned int i = 0; i < hgdn_core_api->num_extensions; i++) { 1159 | switch(hgdn_core_api->extensions[i]->type) { 1160 | #ifndef HGDN_NO_EXT_NATIVESCRIPT 1161 | case GDNATIVE_EXT_NATIVESCRIPT: 1162 | hgdn_nativescript_api = (const godot_gdnative_ext_nativescript_api_struct *) hgdn_core_api->extensions[i]; 1163 | for (const godot_gdnative_api_struct *ext = hgdn_nativescript_api->next; ext; ext = ext->next) { 1164 | if (ext->version.major == 1 && ext->version.minor == 1) { 1165 | hgdn_nativescript_1_1_api = (const godot_gdnative_ext_nativescript_1_1_api_struct *) ext; 1166 | } 1167 | } 1168 | break; 1169 | #endif 1170 | #ifndef HGDN_NO_EXT_PLUGINSCRIPT 1171 | case GDNATIVE_EXT_PLUGINSCRIPT: 1172 | hgdn_pluginscript_api = (const godot_gdnative_ext_pluginscript_api_struct *) hgdn_core_api->extensions[i]; 1173 | break; 1174 | #endif 1175 | #ifndef HGDN_NO_EXT_ANDROID 1176 | case GDNATIVE_EXT_ANDROID: 1177 | hgdn_android_api = (const godot_gdnative_ext_android_api_struct *) hgdn_core_api->extensions[i]; 1178 | break; 1179 | #endif 1180 | #ifndef HGDN_NO_EXT_ARVR 1181 | case GDNATIVE_EXT_ARVR: 1182 | hgdn_arvr_api = (const godot_gdnative_ext_arvr_api_struct *) hgdn_core_api->extensions[i]; 1183 | for (const godot_gdnative_api_struct *ext = hgdn_arvr_api->next; ext; ext = ext->next) { 1184 | if (ext->version.major == 1 && ext->version.minor == 2) { 1185 | hgdn_arvr_1_2_api = (const godot_gdnative_ext_arvr_1_2_api_struct *) ext; 1186 | } 1187 | } 1188 | break; 1189 | #endif 1190 | #ifndef HGDN_NO_EXT_VIDEOCODER 1191 | case GDNATIVE_EXT_VIDEODECODER: 1192 | hgdn_videodecoder_api = (const godot_gdnative_ext_videodecoder_api_struct *) hgdn_core_api->extensions[i]; 1193 | break; 1194 | #endif 1195 | #ifndef HGDN_NO_EXT_NET 1196 | case GDNATIVE_EXT_NET: 1197 | hgdn_net_api = (const godot_gdnative_ext_net_api_struct *) hgdn_core_api->extensions[i]; 1198 | for (const godot_gdnative_api_struct *ext = hgdn_net_api->next; ext; ext = ext->next) { 1199 | if (ext->version.major == 3 && ext->version.minor == 2) { 1200 | hgdn_net_3_2_api = (const godot_gdnative_ext_net_3_2_api_struct *) ext; 1201 | } 1202 | } 1203 | break; 1204 | #endif 1205 | default: 1206 | break; 1207 | } 1208 | } 1209 | 1210 | hgdn_method_Object_callv = hgdn_core_api->godot_method_bind_get_method("Object", "callv"); 1211 | hgdn_core_api->godot_array_new(&hgdn__empty_array); 1212 | } 1213 | 1214 | void hgdn_gdnative_terminate(const godot_gdnative_terminate_options *options) { 1215 | hgdn_core_api->godot_array_destroy(&hgdn__empty_array); 1216 | } 1217 | 1218 | // Memory API 1219 | void *hgdn_alloc(size_t size) { 1220 | return hgdn_core_api->godot_alloc(size); 1221 | } 1222 | 1223 | void *hgdn_realloc(void *ptr, size_t size) { 1224 | return hgdn_core_api->godot_realloc(ptr, size); 1225 | } 1226 | 1227 | void hgdn_free(void *ptr) { 1228 | if (ptr) { 1229 | hgdn_core_api->godot_free(ptr); 1230 | } 1231 | } 1232 | 1233 | // Print functions 1234 | void hgdn_print(const char *fmt, ...) { 1235 | HGDN__FILL_FORMAT_BUFFER(fmt, ...); 1236 | godot_string str = hgdn_new_string_with_len(hgdn__format_string_buffer, size); 1237 | hgdn_core_api->godot_print(&str); 1238 | hgdn_core_api->godot_string_destroy(&str); 1239 | } 1240 | 1241 | void hgdn_print_warning(const char *funcname, const char *filename, int line, const char *fmt, ...) { 1242 | HGDN__FILL_FORMAT_BUFFER(fmt, ...); 1243 | hgdn_core_api->godot_print_warning(hgdn__format_string_buffer, funcname, filename, line); 1244 | } 1245 | 1246 | void hgdn_print_error(const char *funcname, const char *filename, int line, const char *fmt, ...) { 1247 | HGDN__FILL_FORMAT_BUFFER(fmt, ...); 1248 | hgdn_core_api->godot_print_error(hgdn__format_string_buffer, funcname, filename, line); 1249 | } 1250 | 1251 | // String creation API 1252 | godot_string hgdn_new_wide_string(const wchar_t *wstr) { 1253 | godot_string str; 1254 | hgdn_core_api->godot_string_new_with_wide_string(&str, wstr, -1); 1255 | return str; 1256 | } 1257 | 1258 | godot_string hgdn_new_wide_string_with_len(const wchar_t *wstr, const godot_int len) { 1259 | godot_string str; 1260 | hgdn_core_api->godot_string_new_with_wide_string(&str, wstr, len); 1261 | return str; 1262 | } 1263 | 1264 | godot_string hgdn_new_string(const char *cstr) { 1265 | return hgdn_core_api->godot_string_chars_to_utf8(cstr); 1266 | } 1267 | 1268 | godot_string hgdn_new_string_with_len(const char *cstr, const godot_int len) { 1269 | return hgdn_core_api->godot_string_chars_to_utf8_with_len(cstr, len); 1270 | } 1271 | 1272 | godot_string hgdn_new_formatted_string(const char *fmt, ...) { 1273 | HGDN__FILL_FORMAT_BUFFER(fmt, ...); 1274 | return hgdn_new_string_with_len(hgdn__format_string_buffer, size); 1275 | } 1276 | 1277 | // Array creation API 1278 | #define HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(kind, ctype) \ 1279 | godot_pool_##kind##_array hgdn_new_##kind##_array(const ctype *buffer, const godot_int size) { \ 1280 | godot_pool_##kind##_array array; \ 1281 | hgdn_core_api->godot_pool_##kind##_array_new(&array); \ 1282 | hgdn_core_api->godot_pool_##kind##_array_resize(&array, size); \ 1283 | godot_pool_##kind##_array_write_access *write = hgdn_core_api->godot_pool_##kind##_array_write(&array); \ 1284 | memcpy(hgdn_core_api->godot_pool_##kind##_array_write_access_ptr(write), buffer, size * sizeof(ctype)); \ 1285 | hgdn_core_api->godot_pool_##kind##_array_write_access_destroy(write); \ 1286 | return array; \ 1287 | } 1288 | 1289 | HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(byte, uint8_t) // hgdn_new_byte_array 1290 | HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(int, godot_int) // hgdn_new_int_array 1291 | HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(real, godot_real) // hgdn_new_real_array 1292 | HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(vector2, godot_vector2) // hgdn_new_vector2_array 1293 | HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(vector3, godot_vector3) // hgdn_new_vector3_array 1294 | HGDN_DECLARE_NEW_POOL_ARRAY_FUNC(color, godot_color) // hgdn_new_color_array 1295 | 1296 | godot_pool_string_array hgdn_new_string_array(const char *const *buffer, const godot_int size) { 1297 | godot_pool_string_array array; 1298 | hgdn_core_api->godot_pool_string_array_new(&array); 1299 | hgdn_core_api->godot_pool_string_array_resize(&array, size); 1300 | for (godot_int i = 0; i < size; i++) { 1301 | godot_string str = hgdn_core_api->godot_string_chars_to_utf8(buffer[i]); 1302 | hgdn_core_api->godot_pool_string_array_set(&array, i, &str); 1303 | hgdn_core_api->godot_string_destroy(&str); 1304 | } 1305 | return array; 1306 | } 1307 | 1308 | #undef HGDN_DECLARE_NEW_POOL_ARRAY_FUNC 1309 | 1310 | godot_array hgdn_new_array(const godot_variant *const *buffer, const godot_int size) { 1311 | godot_array array; 1312 | hgdn_core_api->godot_array_new(&array); 1313 | hgdn_core_api->godot_array_resize(&array, size); 1314 | for (godot_int i = 0; i < size; i++) { 1315 | hgdn_core_api->godot_array_set(&array, i, buffer[i]); 1316 | } 1317 | return array; 1318 | } 1319 | 1320 | godot_array hgdn_new_array_own(godot_variant *buffer, const godot_int size) { 1321 | godot_array array; 1322 | hgdn_core_api->godot_array_new(&array); 1323 | hgdn_core_api->godot_array_resize(&array, size); 1324 | for (godot_int i = 0; i < size; i++) { 1325 | godot_variant *var = &buffer[i]; 1326 | hgdn_core_api->godot_array_set(&array, i, var); 1327 | hgdn_core_api->godot_variant_destroy(var); 1328 | } 1329 | return array; 1330 | } 1331 | 1332 | // Dictionary creation API 1333 | godot_dictionary hgdn_new_dictionary(const hgdn_dictionary_entry *buffer, const godot_int size) { 1334 | godot_dictionary dict; 1335 | hgdn_core_api->godot_dictionary_new(&dict); 1336 | for (godot_int i = 0; i < size; i++) { 1337 | hgdn_core_api->godot_dictionary_set(&dict, buffer[i].key, buffer[i].value); 1338 | } 1339 | return dict; 1340 | } 1341 | 1342 | godot_dictionary hgdn_new_dictionary_string(const hgdn_dictionary_entry_string *buffer, const godot_int size) { 1343 | godot_dictionary dict; 1344 | hgdn_core_api->godot_dictionary_new(&dict); 1345 | for (godot_int i = 0; i < size; i++) { 1346 | godot_variant key = hgdn_new_string_variant_own(hgdn_new_string(buffer[i].key)); 1347 | hgdn_core_api->godot_dictionary_set(&dict, &key, buffer[i].value); 1348 | hgdn_core_api->godot_variant_destroy(&key); 1349 | } 1350 | return dict; 1351 | } 1352 | 1353 | godot_dictionary hgdn_new_dictionary_string_int(const hgdn_dictionary_entry_string_int *buffer, const godot_int size) { 1354 | godot_dictionary dict; 1355 | hgdn_core_api->godot_dictionary_new(&dict); 1356 | for (godot_int i = 0; i < size; i++) { 1357 | godot_variant key = hgdn_new_string_variant_own(hgdn_new_string(buffer[i].key)); 1358 | godot_variant value = hgdn_new_int_variant(buffer[i].value); 1359 | hgdn_core_api->godot_dictionary_set(&dict, &key, &value); 1360 | hgdn_core_api->godot_variant_destroy(&key); 1361 | hgdn_core_api->godot_variant_destroy(&value); 1362 | } 1363 | return dict; 1364 | } 1365 | 1366 | godot_dictionary hgdn_new_dictionary_string_string(const hgdn_dictionary_entry_string_string *buffer, const godot_int size) { 1367 | godot_dictionary dict; 1368 | hgdn_core_api->godot_dictionary_new(&dict); 1369 | for (godot_int i = 0; i < size; i++) { 1370 | godot_variant key = hgdn_new_string_variant_own(hgdn_new_string(buffer[i].key)); 1371 | godot_variant value = hgdn_new_string_variant_own(hgdn_new_string(buffer[i].value)); 1372 | hgdn_core_api->godot_dictionary_set(&dict, &key, &value); 1373 | hgdn_core_api->godot_variant_destroy(&key); 1374 | hgdn_core_api->godot_variant_destroy(&value); 1375 | } 1376 | return dict; 1377 | } 1378 | 1379 | godot_dictionary hgdn_new_dictionary_own(hgdn_dictionary_entry_own *buffer, const godot_int size) { 1380 | godot_dictionary dict; 1381 | hgdn_core_api->godot_dictionary_new(&dict); 1382 | for (godot_int i = 0; i < size; i++) { 1383 | hgdn_core_api->godot_dictionary_set(&dict, &buffer[i].key, &buffer[i].value); 1384 | hgdn_core_api->godot_variant_destroy(&buffer[i].key); 1385 | hgdn_core_api->godot_variant_destroy(&buffer[i].value); 1386 | } 1387 | return dict; 1388 | } 1389 | 1390 | godot_dictionary hgdn_new_dictionary_string_own(hgdn_dictionary_entry_string_own *buffer, const godot_int size) { 1391 | godot_dictionary dict; 1392 | hgdn_core_api->godot_dictionary_new(&dict); 1393 | for (godot_int i = 0; i < size; i++) { 1394 | godot_variant key = hgdn_new_string_variant_own(hgdn_new_string(buffer[i].key)); 1395 | hgdn_core_api->godot_dictionary_set(&dict, &key, &buffer[i].value); 1396 | hgdn_core_api->godot_variant_destroy(&key); 1397 | hgdn_core_api->godot_variant_destroy(&buffer[i].value); 1398 | } 1399 | return dict; 1400 | } 1401 | 1402 | // String helpers 1403 | hgdn_wide_string hgdn_wide_string_get(const godot_string *str) { 1404 | godot_string new_str; 1405 | hgdn_core_api->godot_string_new_copy(&new_str, str); 1406 | return hgdn_wide_string_get_own(new_str); 1407 | } 1408 | 1409 | hgdn_wide_string hgdn_wide_string_get_own(godot_string str) { 1410 | hgdn_wide_string wrapper = { 1411 | str, 1412 | hgdn_core_api->godot_string_wide_str(&str), 1413 | hgdn_core_api->godot_string_length(&str), 1414 | }; 1415 | return wrapper; 1416 | } 1417 | 1418 | void hgdn_wide_string_destroy(hgdn_wide_string *str) { 1419 | hgdn_core_api->godot_string_destroy(&str->gd_string); 1420 | } 1421 | 1422 | hgdn_string hgdn_string_get(const godot_string *str) { 1423 | godot_char_string char_string = hgdn_core_api->godot_string_utf8(str); 1424 | hgdn_string wrapper = { 1425 | char_string, 1426 | hgdn_core_api->godot_char_string_get_data(&char_string), 1427 | hgdn_core_api->godot_char_string_length(&char_string), 1428 | }; 1429 | return wrapper; 1430 | } 1431 | 1432 | hgdn_string hgdn_string_get_own(godot_string str) { 1433 | hgdn_string wrapper = hgdn_string_get(&str); 1434 | hgdn_core_api->godot_string_destroy(&str); 1435 | return wrapper; 1436 | } 1437 | 1438 | void hgdn_string_destroy(hgdn_string *str) { 1439 | hgdn_core_api->godot_char_string_destroy(&str->gd_char_string); 1440 | } 1441 | 1442 | // Pool String helpers 1443 | #define HGDN_DECLARE_POOL_ARRAY_API(kind, ctype) \ 1444 | hgdn_##kind##_array hgdn_##kind##_array_get(const godot_pool_##kind##_array *array) { \ 1445 | godot_pool_##kind##_array_read_access *access = hgdn_core_api->godot_pool_##kind##_array_read(array); \ 1446 | hgdn_##kind##_array wrapper = { \ 1447 | access, \ 1448 | hgdn_core_api->godot_pool_##kind##_array_read_access_ptr(access), \ 1449 | hgdn_core_api->godot_pool_##kind##_array_size(array), \ 1450 | }; \ 1451 | return wrapper; \ 1452 | } \ 1453 | hgdn_##kind##_array hgdn_##kind##_array_get_own(godot_pool_##kind##_array array) { \ 1454 | hgdn_##kind##_array result = hgdn_##kind##_array_get(&array); \ 1455 | hgdn_core_api->godot_pool_##kind##_array_destroy(&array); \ 1456 | return result; \ 1457 | } \ 1458 | void hgdn_##kind##_array_destroy(hgdn_##kind##_array *array) { \ 1459 | hgdn_core_api->godot_pool_##kind##_array_read_access_destroy(array->gd_read_access); \ 1460 | } 1461 | 1462 | HGDN_DECLARE_POOL_ARRAY_API(byte, uint8_t) // hgdn_byte_array_get, hgdn_byte_array_get_own, hgdn_byte_array_destroy 1463 | HGDN_DECLARE_POOL_ARRAY_API(int, godot_int) // hgdn_int_array_get, hgdn_int_array_get_own, hgdn_int_array_destroy 1464 | HGDN_DECLARE_POOL_ARRAY_API(real, godot_real) // hgdn_real_array_get, hgdn_real_array_get_own, hgdn_real_array_destroy 1465 | HGDN_DECLARE_POOL_ARRAY_API(vector2, godot_vector2) // hgdn_vector2_array_get, hgdn_vector2_array_get_own, hgdn_vector2_array_destroy 1466 | HGDN_DECLARE_POOL_ARRAY_API(vector3, godot_vector3) // hgdn_vector3_array_get, hgdn_vector3_array_get_own, hgdn_vector3_array_destroy 1467 | HGDN_DECLARE_POOL_ARRAY_API(color, godot_color) // hgdn_color_array_get, hgdn_color_array_get_own, hgdn_color_array_destroy 1468 | 1469 | #undef HGDN_DECLARE_POOL_ARRAY_API 1470 | 1471 | hgdn_string_array hgdn_string_array_get(const godot_pool_string_array *array) { 1472 | godot_int size = hgdn_core_api->godot_pool_string_array_size(array); 1473 | godot_pool_string_array_read_access *access = hgdn_core_api->godot_pool_string_array_read(array); 1474 | hgdn_string_array wrapper = {0}; 1475 | if ((wrapper.strings = (hgdn_string *) hgdn_alloc(size * sizeof(hgdn_string))) == NULL) { 1476 | return wrapper; 1477 | } 1478 | if ((wrapper.ptr = (const char **) hgdn_alloc(size * sizeof(char *))) == NULL) { 1479 | hgdn_free(wrapper.strings); 1480 | return wrapper; 1481 | } 1482 | wrapper.size = size; 1483 | const godot_string *gd_strings = hgdn_core_api->godot_pool_string_array_read_access_ptr(access); 1484 | for (godot_int i = 0; i < size; i++) { 1485 | hgdn_string str = hgdn_string_get(&gd_strings[i]); 1486 | wrapper.strings[i] = str; 1487 | wrapper.ptr[i] = str.ptr; 1488 | } 1489 | hgdn_core_api->godot_pool_string_array_read_access_destroy(access); 1490 | return wrapper; 1491 | } 1492 | 1493 | hgdn_string_array hgdn_string_array_get_own(godot_pool_string_array array) { 1494 | hgdn_string_array result = hgdn_string_array_get(&array); 1495 | hgdn_core_api->godot_pool_string_array_destroy(&array); 1496 | return result; 1497 | } 1498 | 1499 | void hgdn_string_array_destroy(hgdn_string_array *array) { 1500 | for (godot_int i = 0; i < array->size; i++) { 1501 | hgdn_string_destroy(&array->strings[i]); 1502 | } 1503 | hgdn_free(array->strings); 1504 | hgdn_free((void *) array->ptr); 1505 | } 1506 | 1507 | // Get values from Variant, Array, Dictionary and method arguments helpers 1508 | #define HGDN_DECLARE_VARIANT_GET(kind, ctype) \ 1509 | ctype hgdn_variant_get_##kind(const godot_variant *var) { \ 1510 | return hgdn_core_api->godot_variant_as_##kind(var); \ 1511 | } 1512 | #define HGDN_DECLARE_VARIANT_GET_OWN(kind, ctype) \ 1513 | ctype hgdn_variant_get_##kind##_own(godot_variant var) { \ 1514 | ctype result = hgdn_variant_get_##kind(&var); \ 1515 | hgdn_core_api->godot_variant_destroy(&var); \ 1516 | return result; \ 1517 | } 1518 | #define HGDN_DECLARE_ARRAY_GET(kind, ctype) \ 1519 | ctype hgdn_array_get_##kind(const godot_array *array, const godot_int index) { \ 1520 | return hgdn_variant_get_##kind(hgdn_core_api->godot_array_operator_index_const(array, index)); \ 1521 | } 1522 | #define HGDN_DECLARE_ARGS_GET(kind, ctype) \ 1523 | ctype hgdn_args_get_##kind(godot_variant **args, const godot_int index) { \ 1524 | return hgdn_variant_get_##kind(args[index]); \ 1525 | } 1526 | #define HGDN_DECLARE_DICTIONARY_GET(kind, ctype) \ 1527 | ctype hgdn_dictionary_get_##kind(const godot_dictionary *dict, const godot_variant *key) { \ 1528 | return hgdn_variant_get_##kind(hgdn_core_api->godot_dictionary_operator_index_const(dict, key)); \ 1529 | } \ 1530 | ctype hgdn_dictionary_string_get_##kind(const godot_dictionary *dict, const char *key) { \ 1531 | godot_variant key_var = hgdn_new_cstring_variant(key); \ 1532 | ctype value = hgdn_dictionary_get_##kind(dict, &key_var); \ 1533 | hgdn_core_api->godot_variant_destroy(&key_var); \ 1534 | return value; \ 1535 | } 1536 | 1537 | HGDN_DECLARE_VARIANT_GET(bool, godot_bool) // hgdn_variant_get_bool 1538 | HGDN_DECLARE_VARIANT_GET_OWN(bool, godot_bool) // hgdn_variant_get_bool_own 1539 | HGDN_DECLARE_ARRAY_GET(bool, godot_bool) // hgdn_array_get_bool 1540 | HGDN_DECLARE_ARGS_GET(bool, godot_bool) // hgdn_args_get_bool 1541 | HGDN_DECLARE_DICTIONARY_GET(bool, godot_bool) // hgdn_dictionary_get_bool, hgdn_dictionary_string_get_bool 1542 | 1543 | #define HGDN_DECLARE_VARIANT_API(kind, ctype) \ 1544 | HGDN_DECLARE_VARIANT_GET(kind, ctype) \ 1545 | HGDN_DECLARE_VARIANT_GET_OWN(kind, ctype) \ 1546 | HGDN_DECLARE_ARRAY_GET(kind, ctype) \ 1547 | HGDN_DECLARE_ARGS_GET(kind, ctype) \ 1548 | HGDN_DECLARE_DICTIONARY_GET(kind, ctype) 1549 | 1550 | HGDN_DECLARE_VARIANT_API(uint, uint64_t) // hgdn_variant_get_uint, hgdn_variant_get_uint_own, hgdn_array_get_uint, hgdn_args_get_uint, hgdn_dictionary_get_uint, hgdn_dictionary_string_get_uint 1551 | HGDN_DECLARE_VARIANT_API(int, int64_t) // hgdn_variant_get_int, hgdn_variant_get_int_own, hgdn_array_get_int, hgdn_args_get_int, hgdn_dictionary_get_int, hgdn_dictionary_string_get_int 1552 | HGDN_DECLARE_VARIANT_API(real, double) // hgdn_variant_get_real, hgdn_variant_get_real_own, hgdn_array_get_real, hgdn_args_get_real, hgdn_dictionary_get_real, hgdn_dictionary_string_get_real 1553 | HGDN_DECLARE_VARIANT_API(vector2, godot_vector2) // hgdn_variant_get_vector2, hgdn_variant_get_vector2_own, hgdn_array_get_vector2, hgdn_args_get_vector2, hgdn_dictionary_get_vector2, hgdn_dictionary_string_get_vector2 1554 | HGDN_DECLARE_VARIANT_API(vector3, godot_vector3) // hgdn_variant_get_vector3, hgdn_variant_get_vector3_own, hgdn_array_get_vector3, hgdn_args_get_vector3, hgdn_dictionary_get_vector3, hgdn_dictionary_string_get_vector3 1555 | HGDN_DECLARE_VARIANT_API(rect2, godot_rect2) // hgdn_variant_get_rect2, hgdn_variant_get_rect2_own, hgdn_array_get_rect2, hgdn_args_get_rect2, hgdn_dictionary_get_rect2, hgdn_dictionary_string_get_rect2 1556 | HGDN_DECLARE_VARIANT_API(plane, godot_plane) // hgdn_variant_get_plane, hgdn_variant_get_plane_own, hgdn_array_get_plane, hgdn_args_get_plane, hgdn_dictionary_get_plane, hgdn_dictionary_string_get_plane 1557 | HGDN_DECLARE_VARIANT_API(quat, godot_quat) // hgdn_variant_get_quat, hgdn_variant_get_quat_own, hgdn_array_get_quat, hgdn_args_get_quat, hgdn_dictionary_get_quat, hgdn_dictionary_string_get_quat 1558 | HGDN_DECLARE_VARIANT_API(aabb, godot_aabb) // hgdn_variant_get_aabb, hgdn_variant_get_aabb_own, hgdn_array_get_aabb, hgdn_args_get_aabb, hgdn_dictionary_get_aabb, hgdn_dictionary_string_get_aabb 1559 | HGDN_DECLARE_VARIANT_API(basis, godot_basis) // hgdn_variant_get_basis, hgdn_variant_get_basis_own, hgdn_array_get_basis, hgdn_args_get_basis, hgdn_dictionary_get_basis, hgdn_dictionary_string_get_basis 1560 | HGDN_DECLARE_VARIANT_API(transform2d, godot_transform2d) // hgdn_variant_get_transform2d, hgdn_variant_get_transform2d_own, hgdn_array_get_transform2d, hgdn_args_get_transform2d, hgdn_dictionary_get_transform2d, hgdn_dictionary_string_get_transform2d 1561 | HGDN_DECLARE_VARIANT_API(transform, godot_transform) // hgdn_variant_get_transform, hgdn_variant_get_transform_own, hgdn_array_get_transform, hgdn_args_get_transform, hgdn_dictionary_get_transform, hgdn_dictionary_string_get_transform 1562 | HGDN_DECLARE_VARIANT_API(color, godot_color) // hgdn_variant_get_color, hgdn_variant_get_color_own, hgdn_array_get_color, hgdn_args_get_color, hgdn_dictionary_get_color, hgdn_dictionary_string_get_color 1563 | HGDN_DECLARE_VARIANT_API(node_path, godot_node_path) // hgdn_variant_get_node_path, hgdn_variant_get_node_path_own, hgdn_array_get_node_path, hgdn_args_get_node_path, hgdn_dictionary_get_node_path, hgdn_dictionary_string_get_node_path 1564 | HGDN_DECLARE_VARIANT_API(rid, godot_rid) // hgdn_variant_get_rid, hgdn_variant_get_rid_own, hgdn_array_get_rid, hgdn_args_get_rid, hgdn_dictionary_get_rid, hgdn_dictionary_string_get_rid 1565 | HGDN_DECLARE_VARIANT_API(object, godot_object *) // hgdn_variant_get_object, hgdn_variant_get_object_own, hgdn_array_get_object, hgdn_args_get_object, hgdn_dictionary_get_object, hgdn_dictionary_string_get_object 1566 | HGDN_DECLARE_VARIANT_API(dictionary, godot_dictionary) // hgdn_variant_get_dictionary, hgdn_variant_get_dictionary_own, hgdn_array_get_dictionary, hgdn_args_get_dictionary, hgdn_dictionary_get_dictionary, hgdn_dictionary_string_get_dictionary 1567 | HGDN_DECLARE_VARIANT_API(array, godot_array) // hgdn_variant_get_array, hgdn_variant_get_array_own, hgdn_array_get_array, hgdn_args_get_array, hgdn_dictionary_get_array, hgdn_dictionary_string_get_array 1568 | 1569 | #undef HGDN_DECLARE_VARIANT_GET 1570 | 1571 | hgdn_string hgdn_variant_get_string(const godot_variant *var) { 1572 | return hgdn_string_get_own(hgdn_core_api->godot_variant_as_string(var)); 1573 | } 1574 | HGDN_DECLARE_VARIANT_GET_OWN(string, hgdn_string) // hgdn_variant_get_string_own 1575 | HGDN_DECLARE_ARRAY_GET(string, hgdn_string) // hgdn_array_get_string 1576 | HGDN_DECLARE_ARGS_GET(string, hgdn_string) // hgdn_args_get_string 1577 | HGDN_DECLARE_DICTIONARY_GET(string, hgdn_string) // hgdn_dictionary_get_string 1578 | 1579 | hgdn_wide_string hgdn_variant_get_wide_string(const godot_variant *var) { 1580 | return hgdn_wide_string_get_own(hgdn_core_api->godot_variant_as_string(var)); 1581 | } 1582 | HGDN_DECLARE_VARIANT_GET_OWN(wide_string, hgdn_wide_string) // hgdn_variant_get_wide_string_own 1583 | HGDN_DECLARE_ARRAY_GET(wide_string, hgdn_wide_string) // hgdn_array_get_wide_string 1584 | HGDN_DECLARE_ARGS_GET(wide_string, hgdn_wide_string) // hgdn_args_get_wide_string 1585 | HGDN_DECLARE_DICTIONARY_GET(wide_string, hgdn_wide_string) // hgdn_dictionary_get_wide_string 1586 | 1587 | #define HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(kind, ctype) \ 1588 | ctype hgdn_variant_get_##kind(const godot_variant *var) { \ 1589 | return hgdn_##kind##_get_own(hgdn_core_api->godot_variant_as_pool_##kind(var)); \ 1590 | } \ 1591 | HGDN_DECLARE_VARIANT_GET_OWN(kind, ctype) \ 1592 | HGDN_DECLARE_ARRAY_GET(kind, ctype) \ 1593 | HGDN_DECLARE_ARGS_GET(kind, ctype) \ 1594 | HGDN_DECLARE_DICTIONARY_GET(kind, ctype) 1595 | 1596 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(byte_array, hgdn_byte_array) // hgdn_variant_get_byte_array, hgdn_variant_get_byte_array_own, hgdn_array_get_byte_array, hgdn_args_get_byte_array, hgdn_dictionary_get_byte_array, hgdn_dictionary_string_get_byte_array 1597 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(int_array, hgdn_int_array) // hgdn_variant_get_int_array, hgdn_variant_get_int_array_own, hgdn_array_get_int_array, hgdn_args_get_int_array, hgdn_dictionary_get_int_array, hgdn_dictionary_string_get_int_array 1598 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(real_array, hgdn_real_array) // hgdn_variant_get_real_array, hgdn_variant_get_real_array_own, hgdn_array_get_real_array, hgdn_args_get_real_array, hgdn_dictionary_get_real_array, hgdn_dictionary_string_get_real_array 1599 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(string_array, hgdn_string_array) // hgdn_variant_get_string_array, hgdn_variant_get_string_array_own, hgdn_array_get_string_array, hgdn_args_get_string_array, hgdn_dictionary_get_string_array, hgdn_dictionary_string_get_string_array 1600 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(vector2_array, hgdn_vector2_array) // hgdn_variant_get_vector2_array, hgdn_variant_get_vector2_array_own, hgdn_array_get_vector2_array, hgdn_args_get_vector2_array, hgdn_dictionary_get_vector2_array, hgdn_dictionary_string_get_vector2_array 1601 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(vector3_array, hgdn_vector3_array) // hgdn_variant_get_vector3_array, hgdn_variant_get_vector3_array_own, hgdn_array_get_vector3_array, hgdn_args_get_vector3_array, hgdn_dictionary_get_vector3_array, hgdn_dictionary_string_get_vector3_array 1602 | HGDN_DECLARE_VARIANT_GET_POOL_ARRAY(color_array, hgdn_color_array) // hgdn_variant_get_color_array, hgdn_variant_get_color_array_own, hgdn_array_get_color_array, hgdn_args_get_color_array, hgdn_dictionary_get_color_array, hgdn_dictionary_string_get_color_array 1603 | 1604 | #undef HGDN_DECLARE_VARIANT_GET_POOL_ARRAY 1605 | 1606 | #undef HGDN_DECLARE_DICTIONARY_GET 1607 | #undef HGDN_DECLARE_ARGS_GET 1608 | #undef HGDN_DECLARE_ARRAY_GET 1609 | #undef HGDN_DECLARE_VARIANT_GET_OWN 1610 | 1611 | // Object helpers 1612 | godot_variant hgdn_object_callv(godot_object *instance, const char *method, const godot_array *args_array) { 1613 | if (!args_array) { 1614 | args_array = &hgdn__empty_array; 1615 | } 1616 | godot_variant result; 1617 | godot_string method_str = hgdn_new_string(method); 1618 | const void *args[] = { &method_str, args_array }; 1619 | hgdn_core_api->godot_method_bind_ptrcall(hgdn_method_Object_callv, instance, args, &result); 1620 | hgdn_core_api->godot_string_destroy(&method_str); 1621 | return result; 1622 | } 1623 | 1624 | godot_variant hgdn_object_callv_own(godot_object *instance, const char *method, godot_array args) { 1625 | godot_variant result = hgdn_object_callv(instance, method, &args); 1626 | hgdn_core_api->godot_array_destroy(&args); 1627 | return result; 1628 | } 1629 | 1630 | // Create variants 1631 | godot_variant hgdn_new_variant_copy(const godot_variant *value) { 1632 | godot_variant var; 1633 | hgdn_core_api->godot_variant_new_copy(&var, value); 1634 | return var; 1635 | } 1636 | 1637 | godot_variant hgdn_new_nil_variant() { 1638 | godot_variant var; 1639 | hgdn_core_api->godot_variant_new_nil(&var); 1640 | return var; 1641 | } 1642 | 1643 | #define HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(kind, ctype) \ 1644 | godot_variant hgdn_new_##kind##_variant(const ctype value) { \ 1645 | godot_variant var; \ 1646 | hgdn_core_api->godot_variant_new_##kind(&var, value); \ 1647 | return var; \ 1648 | } 1649 | 1650 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(bool, godot_bool) // hgdn_new_bool_variant 1651 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(uint, uint64_t) // hgdn_new_uint_variant 1652 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(int, int64_t) // hgdn_new_int_variant 1653 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(real, double) // hgdn_new_real_variant 1654 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(string, godot_string *) // hgdn_new_string_variant 1655 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(dictionary, godot_dictionary *) // hgdn_new_dictionary_variant 1656 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(array, godot_array *) // hgdn_new_array_variant 1657 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_byte_array, godot_pool_byte_array *) // hgdn_new_pool_byte_array_variant 1658 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_int_array, godot_pool_int_array *) // hgdn_new_pool_int_array_variant 1659 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_real_array, godot_pool_real_array *) // hgdn_new_pool_real_array_variant 1660 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_vector2_array, godot_pool_vector2_array *) // hgdn_new_pool_vector2_array_variant 1661 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_vector3_array, godot_pool_vector3_array *) // hgdn_new_pool_vector3_array_variant 1662 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_color_array, godot_pool_color_array *) // hgdn_new_pool_color_array_variant 1663 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(pool_string_array, godot_pool_string_array *) // hgdn_new_pool_string_array_variant 1664 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(node_path, godot_node_path *) // hgdn_new_node_path_variant 1665 | HGDN_DECLARE_NEW_PRIMITIVE_VARIANT(rid, godot_rid *) // hgdn_new_rid_variant 1666 | 1667 | #undef HGDN_DECLARE_NEW_PRIMITIVE_VARIANT 1668 | 1669 | godot_variant hgdn_new_object_variant(const godot_object *value) { 1670 | godot_variant var; 1671 | if (value) { 1672 | hgdn_core_api->godot_variant_new_object(&var, value); 1673 | } 1674 | else { 1675 | hgdn_core_api->godot_variant_new_nil(&var); 1676 | } 1677 | return var; 1678 | } 1679 | 1680 | godot_variant hgdn_new_cstring_variant(const char *value) { 1681 | return hgdn_new_string_variant_own(hgdn_new_string(value)); 1682 | } 1683 | 1684 | godot_variant hgdn_new_wide_string_variant(const wchar_t *value) { 1685 | return hgdn_new_string_variant_own(hgdn_new_wide_string(value)); 1686 | } 1687 | 1688 | #define HGDN_DECLARE_NEW_COMPOUND_VARIANT(kind, ctype) \ 1689 | godot_variant hgdn_new_##kind##_variant(const ctype value) { \ 1690 | godot_variant var; \ 1691 | hgdn_core_api->godot_variant_new_##kind(&var, &value); \ 1692 | return var; \ 1693 | } 1694 | 1695 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(vector2, godot_vector2) // hgdn_new_vector2_variant 1696 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(vector3, godot_vector3) // hgdn_new_vector3_variant 1697 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(rect2, godot_rect2) // hgdn_new_rect2_variant 1698 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(plane, godot_plane) // hgdn_new_plane_variant 1699 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(quat, godot_quat) // hgdn_new_quat_variant 1700 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(aabb, godot_aabb) // hgdn_new_aabb_variant 1701 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(basis, godot_basis) // hgdn_new_basis_variant 1702 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(transform2d, godot_transform2d) // hgdn_new_transform2d_variant 1703 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(transform, godot_transform) // hgdn_new_transform_variant 1704 | HGDN_DECLARE_NEW_COMPOUND_VARIANT(color, godot_color) // hgdn_new_color_variant 1705 | 1706 | #undef HGDN_DECLARE_NEW_COMPOUND_VARIANT 1707 | 1708 | #define HGDN_DECLARE_NEW_OWNED_VARIANT(kind, ctype) \ 1709 | godot_variant hgdn_new_##kind##_variant_own(ctype value) { \ 1710 | godot_variant var; \ 1711 | hgdn_core_api->godot_variant_new_##kind(&var, &value); \ 1712 | hgdn_core_api->ctype##_destroy(&value); \ 1713 | return var; \ 1714 | } 1715 | 1716 | HGDN_DECLARE_NEW_OWNED_VARIANT(string, godot_string) // hgdn_new_string_variant_own 1717 | HGDN_DECLARE_NEW_OWNED_VARIANT(dictionary, godot_dictionary) // hgdn_new_dictionary_variant_own 1718 | HGDN_DECLARE_NEW_OWNED_VARIANT(array, godot_array) // hgdn_new_array_variant_own 1719 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_byte_array, godot_pool_byte_array) // hgdn_new_pool_byte_array_variant_own 1720 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_int_array, godot_pool_int_array) // hgdn_new_pool_int_array_variant_own 1721 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_real_array, godot_pool_real_array) // hgdn_new_pool_real_array_variant_own 1722 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_vector2_array, godot_pool_vector2_array) // hgdn_new_pool_vector2_array_variant_own 1723 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_vector3_array, godot_pool_vector3_array) // hgdn_new_pool_vector3_array_variant_own 1724 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_color_array, godot_pool_color_array) // hgdn_new_pool_color_array_variant_own 1725 | HGDN_DECLARE_NEW_OWNED_VARIANT(pool_string_array, godot_pool_string_array) // hgdn_new_pool_string_array_variant_own 1726 | HGDN_DECLARE_NEW_OWNED_VARIANT(node_path, godot_node_path) // hgdn_new_node_path_variant_own 1727 | 1728 | #undef HGDN_DECLARE_NEW_OWNED_VARIANT 1729 | 1730 | #ifdef __cplusplus 1731 | godot_variant hgdn_new_variant(const godot_bool value) { return hgdn_new_bool_variant(value); } 1732 | godot_variant hgdn_new_variant(const unsigned int value) { return hgdn_new_uint_variant(value); } 1733 | godot_variant hgdn_new_variant(const uint64_t value) { return hgdn_new_uint_variant(value); } 1734 | godot_variant hgdn_new_variant(const int value) { return hgdn_new_int_variant(value); } 1735 | godot_variant hgdn_new_variant(const int64_t value) { return hgdn_new_int_variant(value); } 1736 | godot_variant hgdn_new_variant(const double value) { return hgdn_new_real_variant(value); } 1737 | godot_variant hgdn_new_variant(const godot_string *value) { return hgdn_new_string_variant(value); } 1738 | godot_variant hgdn_new_variant(const char *value) { return hgdn_new_cstring_variant(value); } 1739 | godot_variant hgdn_new_variant(const wchar_t *value) { return hgdn_new_wide_string_variant(value); } 1740 | godot_variant hgdn_new_variant(const godot_vector2 value) { return hgdn_new_vector2_variant(value); } 1741 | godot_variant hgdn_new_variant(const godot_vector3 value) { return hgdn_new_vector3_variant(value); } 1742 | godot_variant hgdn_new_variant(const godot_rect2 value) { return hgdn_new_rect2_variant(value); } 1743 | godot_variant hgdn_new_variant(const godot_plane value) { return hgdn_new_plane_variant(value); } 1744 | godot_variant hgdn_new_variant(const godot_quat value) { return hgdn_new_quat_variant(value); } 1745 | godot_variant hgdn_new_variant(const godot_aabb value) { return hgdn_new_aabb_variant(value); } 1746 | godot_variant hgdn_new_variant(const godot_basis value) { return hgdn_new_basis_variant(value); } 1747 | godot_variant hgdn_new_variant(const godot_transform2d value) { return hgdn_new_transform2d_variant(value); } 1748 | godot_variant hgdn_new_variant(const godot_transform value) { return hgdn_new_transform_variant(value); } 1749 | godot_variant hgdn_new_variant(const godot_color value) { return hgdn_new_color_variant(value); } 1750 | godot_variant hgdn_new_variant(const godot_node_path *value) { return hgdn_new_node_path_variant(value); } 1751 | godot_variant hgdn_new_variant(const godot_rid *value) { return hgdn_new_rid_variant(value); } 1752 | godot_variant hgdn_new_variant(const godot_object *value) { return hgdn_new_object_variant(value); } 1753 | godot_variant hgdn_new_variant(const godot_dictionary *value) { return hgdn_new_dictionary_variant(value); } 1754 | godot_variant hgdn_new_variant(const godot_array *value) { return hgdn_new_array_variant(value); } 1755 | godot_variant hgdn_new_variant(const godot_pool_byte_array *value) { return hgdn_new_pool_byte_array_variant(value); } 1756 | godot_variant hgdn_new_variant(const godot_pool_int_array *value) { return hgdn_new_pool_int_array_variant(value); } 1757 | godot_variant hgdn_new_variant(const godot_pool_real_array *value) { return hgdn_new_pool_real_array_variant(value); } 1758 | godot_variant hgdn_new_variant(const godot_pool_vector2_array *value) { return hgdn_new_pool_vector2_array_variant(value); } 1759 | godot_variant hgdn_new_variant(const godot_pool_vector3_array *value) { return hgdn_new_pool_vector3_array_variant(value); } 1760 | godot_variant hgdn_new_variant(const godot_pool_color_array *value) { return hgdn_new_pool_color_array_variant(value); } 1761 | godot_variant hgdn_new_variant(const godot_pool_string_array *value) { return hgdn_new_pool_string_array_variant(value); } 1762 | godot_variant hgdn_new_variant(godot_string value) { return hgdn_new_string_variant_own(value); } 1763 | godot_variant hgdn_new_variant(godot_node_path value) { return hgdn_new_node_path_variant_own(value); } 1764 | godot_variant hgdn_new_variant(godot_dictionary value) { return hgdn_new_dictionary_variant_own(value); } 1765 | godot_variant hgdn_new_variant(godot_array value) { return hgdn_new_array_variant_own(value); } 1766 | godot_variant hgdn_new_variant(godot_pool_byte_array value) { return hgdn_new_pool_byte_array_variant_own(value); } 1767 | godot_variant hgdn_new_variant(godot_pool_int_array value) { return hgdn_new_pool_int_array_variant_own(value); } 1768 | godot_variant hgdn_new_variant(godot_pool_real_array value) { return hgdn_new_pool_real_array_variant_own(value); } 1769 | godot_variant hgdn_new_variant(godot_pool_vector2_array value) { return hgdn_new_pool_vector2_array_variant_own(value); } 1770 | godot_variant hgdn_new_variant(godot_pool_vector3_array value) { return hgdn_new_pool_vector3_array_variant_own(value); } 1771 | godot_variant hgdn_new_variant(godot_pool_color_array value) { return hgdn_new_pool_color_array_variant_own(value); } 1772 | godot_variant hgdn_new_variant(godot_pool_string_array value) { return hgdn_new_pool_string_array_variant_own(value); } 1773 | godot_variant hgdn_new_variant(const godot_variant *value) { return hgdn_new_variant_copy(value); } 1774 | HGDN_CONSTEXPR godot_variant hgdn_new_variant(godot_variant value) { return value; } 1775 | #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L // C11 1776 | godot_variant hgdn__variant_return(godot_variant value) { return value; } 1777 | #endif // __cplusplus 1778 | 1779 | // NativeScript 1780 | #ifndef HGDN_NO_EXT_NATIVESCRIPT 1781 | void hgdn_register_class(void *handle, const hgdn_class_info *class_info) { 1782 | if (class_info->tool) { 1783 | hgdn_nativescript_api->godot_nativescript_register_tool_class(handle, class_info->name, class_info->base, class_info->create, class_info->destroy); 1784 | } 1785 | else { 1786 | hgdn_nativescript_api->godot_nativescript_register_class(handle, class_info->name, class_info->base, class_info->create, class_info->destroy); 1787 | } 1788 | 1789 | if (hgdn_nativescript_1_1_api && class_info->documentation) { 1790 | godot_string documentation = hgdn_new_string(class_info->documentation); 1791 | hgdn_nativescript_1_1_api->godot_nativescript_set_class_documentation(handle, class_info->name, documentation); 1792 | hgdn_core_api->godot_string_destroy(&documentation); 1793 | } 1794 | 1795 | if (class_info->properties) { 1796 | for (hgdn_property_info *property = class_info->properties; property->path; property++) { 1797 | godot_property_attributes attr = { 1798 | property->rset_type, 1799 | property->type, 1800 | property->hint, 1801 | hgdn_new_string(property->hint_string), 1802 | property->usage, 1803 | property->default_value, 1804 | }; 1805 | hgdn_nativescript_api->godot_nativescript_register_property(handle, class_info->name, property->path, &attr, property->setter, property->getter); 1806 | hgdn_core_api->godot_string_destroy(&attr.hint_string); 1807 | if (hgdn_nativescript_1_1_api && property->documentation) { 1808 | godot_string documentation = hgdn_new_string(property->documentation); 1809 | hgdn_nativescript_1_1_api->godot_nativescript_set_property_documentation(handle, class_info->name, property->path, documentation); 1810 | hgdn_core_api->godot_string_destroy(&documentation); 1811 | } 1812 | } 1813 | } 1814 | 1815 | if (class_info->methods) { 1816 | for (hgdn_method_info *method = class_info->methods; method->name; method++) { 1817 | godot_method_attributes attr = { method->rpc_type }; 1818 | hgdn_nativescript_api->godot_nativescript_register_method(handle, class_info->name, method->name, attr, method->method); 1819 | if (hgdn_nativescript_1_1_api) { 1820 | if (method->documentation) { 1821 | godot_string documentation = hgdn_new_string(method->documentation); 1822 | hgdn_nativescript_1_1_api->godot_nativescript_set_method_documentation(handle, class_info->name, method->name, documentation); 1823 | hgdn_core_api->godot_string_destroy(&documentation); 1824 | } 1825 | if (method->arguments_info) { 1826 | godot_method_arg gd_args[HGDN_METHOD_ARGUMENTS_INFO_MAX]; 1827 | int num_args = 0; 1828 | for (hgdn_method_argument_info *argument = method->arguments_info; argument->name; argument++) { 1829 | gd_args[num_args] = (godot_method_arg){ 1830 | hgdn_new_string(argument->name), 1831 | argument->type, 1832 | argument->hint, 1833 | hgdn_new_string(argument->hint_string), 1834 | }; 1835 | num_args++; 1836 | } 1837 | hgdn_nativescript_1_1_api->godot_nativescript_set_method_argument_information(handle, class_info->name, method->name, num_args, gd_args); 1838 | for (int i = 0; i < num_args; i++) { 1839 | hgdn_core_api->godot_string_destroy(&gd_args[i].name); 1840 | hgdn_core_api->godot_string_destroy(&gd_args[i].hint_string); 1841 | } 1842 | } 1843 | } 1844 | } 1845 | } 1846 | 1847 | if (class_info->signals) { 1848 | for (hgdn_signal_info *signal = class_info->signals; signal->name; signal++) { 1849 | godot_signal_argument gd_args[HGDN_METHOD_ARGUMENTS_INFO_MAX]; 1850 | int num_args = 0; 1851 | for (hgdn_signal_argument_info *argument = signal->arguments_info; argument->name; argument++) { 1852 | gd_args[num_args] = (godot_signal_argument){ 1853 | hgdn_new_string(argument->name), 1854 | argument->type, 1855 | argument->hint, 1856 | hgdn_new_string(argument->hint_string), 1857 | (godot_property_usage_flags) 0, 1858 | argument->default_value, 1859 | }; 1860 | num_args++; 1861 | } 1862 | godot_string signal_name = hgdn_new_string(signal->name); 1863 | godot_signal gd_signal = { 1864 | signal_name, 1865 | num_args, 1866 | gd_args, 1867 | signal->num_default_args, 1868 | NULL, 1869 | }; 1870 | hgdn_nativescript_api->godot_nativescript_register_signal(handle, class_info->name, &gd_signal); 1871 | hgdn_core_api->godot_string_destroy(&signal_name); 1872 | for (int i = 0; i < num_args; i++) { 1873 | hgdn_core_api->godot_string_destroy(&gd_args[i].name); 1874 | hgdn_core_api->godot_string_destroy(&gd_args[i].hint_string); 1875 | } 1876 | if (hgdn_nativescript_1_1_api && signal->documentation) { 1877 | godot_string documentation = hgdn_new_string(signal->documentation); 1878 | hgdn_nativescript_1_1_api->godot_nativescript_set_signal_documentation(handle, class_info->name, signal->name, documentation); 1879 | hgdn_core_api->godot_string_destroy(&documentation); 1880 | } 1881 | } 1882 | } 1883 | } 1884 | 1885 | void *hgdn_instance_alloc(godot_object *instance, void *alloc_size) { 1886 | void *buffer = hgdn_alloc((uintptr_t) alloc_size); 1887 | if (buffer) { 1888 | memset(buffer, 0, (uintptr_t) alloc_size); 1889 | } 1890 | return buffer; 1891 | } 1892 | 1893 | void hgdn_instance_free(godot_object *instance, void *method_data, void *data) { 1894 | hgdn_free(data); 1895 | } 1896 | 1897 | godot_variant *hgdn_property_constant_alloc(godot_variant value) { 1898 | godot_variant *buffer = (godot_variant *) hgdn_alloc(sizeof(godot_variant)); 1899 | if (buffer) { 1900 | *buffer = value; 1901 | } 1902 | return buffer; 1903 | } 1904 | 1905 | void hgdn_property_constant_free(void *value) { 1906 | if (value) { 1907 | hgdn_core_api->godot_variant_destroy((godot_variant *) value); 1908 | hgdn_core_api->godot_free(value); 1909 | } 1910 | } 1911 | 1912 | godot_variant hgdn_property_constant_get(godot_object *instance, void *value, void *data) { 1913 | return hgdn_new_variant_copy((const godot_variant *) value); 1914 | } 1915 | #endif // HGDN_NO_EXT_NATIVESCRIPT 1916 | 1917 | #undef HGDN__FILL_FORMAT_BUFFER 1918 | 1919 | #endif // HGDN_IMPLEMENTATION 1920 | --------------------------------------------------------------------------------