Spruce-Basin/src/main.c
Aidan Mullen f8025b805a Implement working example
Implement a working example using the GDExtension C API.
2024-05-04 10:05:16 -04:00

123 lines
4.3 KiB
C

/**
* @author : Aidan Mullen
* @file : main
* @created : Friday May 03, 2024 18:54:44 EDT
*/
#include "main.h"
#include <stdio.h>
/* This file is included in the repository since using a Git submodule would
* requried cloning the entire Godot source. */
#include "gdextension_interface.h" /* GDExtension header for Godot. */
/*
int main()
{
printf("Starting./n");
return(0);
}
*/
/* Boilerplate provided by examples. */
/* Some lines are over 80 characters. */
typedef struct {
uint8_t godot_data_dont_touch_this[sizeof(void *)];
} String;
typedef struct {
uint8_t godot_data_dont_touch_this[sizeof(void *)];
} StringName;
typedef struct {
uint8_t godot_data_dont_touch_this[24];
} Variant;
static GDExtensionInterfaceStringNewWithUtf8Chars string_new_with_utf8_chars;
static GDExtensionInterfaceVariantGetPtrConstructor variant_get_ptr_constructor;
static GDExtensionInterfaceVariantGetPtrDestructor variant_get_ptr_destructor;
static GDExtensionInterfaceGetVariantFromTypeConstructor get_variant_from_type_constructor;
static GDExtensionInterfaceVariantGetPtrUtilityFunction variant_get_ptr_utility_function;
static GDExtensionInterfaceVariantDestroy variant_destroy;
/* GDExtension API Pointers */
static GDExtensionPtrConstructor construct_StringName_from_String;
static GDExtensionVariantFromTypeConstructorFunc construct_Variant_from_String;
static GDExtensionPtrDestructor destroy_String;
static GDExtensionPtrDestructor destroy_StringName;
static StringName print_StringName;
static GDExtensionPtrUtilityFunction print_function;
StringName construct_StringName_from_cstring(const char *text) {
String string;
string_new_with_utf8_chars(&string, text);
StringName string_name;
GDExtensionConstTypePtr constructor_arguments[1] = { &string };
construct_StringName_from_String(&string_name, constructor_arguments);
destroy_String(&string);
return string_name;
}
void print(const char *text) {
String string;
string_new_with_utf8_chars(&string, text);
Variant arg1;
construct_Variant_from_String(&arg1, &string);
GDExtensionConstVariantPtr args[1] = { &arg1 };
print_function(NULL, args, 1);
variant_destroy(&arg1);
destroy_String(&string);
}
void initialize(void *userdata, GDExtensionInitializationLevel p_level) {
if (p_level != GDEXTENSION_INITIALIZATION_SCENE) {
return;
}
construct_StringName_from_String = variant_get_ptr_constructor(GDEXTENSION_VARIANT_TYPE_STRING_NAME, 2);
construct_Variant_from_String = get_variant_from_type_constructor(GDEXTENSION_VARIANT_TYPE_STRING);
destroy_String = variant_get_ptr_destructor(GDEXTENSION_VARIANT_TYPE_STRING);
destroy_StringName = variant_get_ptr_destructor(GDEXTENSION_VARIANT_TYPE_STRING_NAME);
print_StringName = construct_StringName_from_cstring("print");
print_function = variant_get_ptr_utility_function(&print_StringName, 2648703342);
print("Starting...");
}
void deinitialize(void *userdata, GDExtensionInitializationLevel p_level) {
if (p_level != GDEXTENSION_INITIALIZATION_SCENE) {
return;
}
print("Exited.");
destroy_StringName(&print_StringName);
}
/* librtr object; called by Godot when it reads the librtr.gdextension file that
* provides a configuration using this name. */
GDExtensionBool librtr_extension_init(
GDExtensionInterfaceGetProcAddress p_get_proc_address,
GDExtensionClassLibraryPtr p_library,
GDExtensionInitialization *r_initialization
) {
r_initialization->initialize = &initialize;
r_initialization->deinitialize = &deinitialize;
string_new_with_utf8_chars = (GDExtensionInterfaceStringNewWithUtf8Chars) p_get_proc_address("string_new_with_utf8_chars");
variant_get_ptr_constructor = (GDExtensionInterfaceVariantGetPtrConstructor) p_get_proc_address("variant_get_ptr_constructor");
variant_get_ptr_destructor = (GDExtensionInterfaceVariantGetPtrDestructor) p_get_proc_address("variant_get_ptr_destructor");
get_variant_from_type_constructor = (GDExtensionInterfaceGetVariantFromTypeConstructor) p_get_proc_address("get_variant_from_type_constructor");
variant_get_ptr_utility_function = (GDExtensionInterfaceVariantGetPtrUtilityFunction) p_get_proc_address("variant_get_ptr_utility_function");
variant_destroy = (GDExtensionInterfaceVariantDestroy) p_get_proc_address("variant_destroy");
return 1;
}