Merge pull request #24 from CicholGricenchos/update-glfw-demo

update GLFWDemo for Godot 3
This commit is contained in:
Thomas Herzog
2018-05-23 13:07:44 +02:00
committed by GitHub
10 changed files with 114 additions and 80 deletions

View File

@@ -1,22 +0,0 @@
[gd_resource type="GDNativeScript" load_steps=2 format=2]
[sub_resource type="GDNativeLibrary" id=1]
platform/unix = ""
platform/x11 = "res://src/libglfw.so"
platform/server = ""
platform/android = ""
platform/blackberry = ""
platform/haiku = ""
platform/mac = "res://src/libglfw_godot.dylib"
platform/ios = ""
platform/osx = "res://src/libglfw_godot.dylib"
platform/html5 = ""
platform/windows = ""
platform/uwp = ""
[resource]
library = SubResource( 1 )
script_name = "GLFW"

17
c/GLFWDemo/GLFW.gdnlib Normal file
View File

@@ -0,0 +1,17 @@
[general]
singleton=false
load_once=true
symbol_prefix="godot_"
[entry]
X11.64="res://src/libglfw.so"
Windows.64=""
OSX.64="res://src/libglfw_godot.dylib"
[dependencies]
X11.64=[]
Windows.64=[]
OSX.64=[]

10
c/GLFWDemo/GLFW.gdns Normal file
View File

@@ -0,0 +1,10 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://GLFW.gdnlib" type="GDNativeLibrary" id=1]
[resource]
class_name = "GLFW"
library = ExtResource( 1 )
_sections_unfolded = [ "Resource" ]

View File

@@ -5,7 +5,7 @@
script/source = "extends Node2D
# load the GLFW library
onready var window = preload(\"res://GLFW.gdn\").new()
onready var window = preload(\"res://GLFW.gdns\").new()
func _ready():
@@ -35,7 +35,7 @@ func _notification(what):
window.close()
"
[node name="Node2D" type="Node2D"]
[node name="Node2D" type="Node2D" index="0"]
script = SubResource( 1 )

View File

@@ -16,6 +16,10 @@ To compile the library, do
clang -std=c11 -fPIC -c -I/PATH/TO/GODOT/HEADERS init.c -o init.os
clang -shared -lglfw init.os -o libglfw.so
#or use gcc
gcc -std=c11 -fPIC -c -I/PATH/TO/GODOT/HEADERS init.c -o init.os
gcc -shared init.os -o libglfw.so -lglfw
This creates the file `libglfw.so` in your `src` directory.
For windows you need to find out what compiler flags need to be used, I don't know which ones. (If you do, feel free to fork and update this project and README)
@@ -26,7 +30,7 @@ On mac:
clang -dynamiclib libglfw.dylib init.os -o libglfw_godot.dylib -arch i386 -arch x86_64 -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo -Wl,-undefined,dynamic_lookup
This creates the file 'libglfw_godot.dylib' as a universal binary (or alternatively remove one of the -arch options from both commands if you want to just compile for one architecture).
If you want to statically link GLFW in just replace libglfw.dylib by libglfw.a, make sure to copy these files from GLFW.
If you want to statically link GLFW in just replace libglfw.dylib by libglfw.a, make sure to copy these files from GLFW.
## Usage

View File

@@ -4,10 +4,20 @@ importer="texture"
type="StreamTexture"
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
[deps]
source_file="res://icon.png"
source_md5="86aa1d9e8afaf5ec9cabb7e89945c7a8"
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
dest_md5="3364e3cd572841d7e22cf649a135919e"
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
@@ -15,6 +25,8 @@ flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=true
process/HDR_as_SRGB=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0

View File

@@ -1,6 +1,16 @@
; Engine configuration file.
; It's best edited using the editor UI and not directly,
; since the parameters that go here are not all obvious.
;
; Format:
; [section] ; section goes between []
; param=value ; assign values to parameters
config_version=3
[application]
run/main_scene="res://Main.tscn"
name="GLFWDemo"
main_scene="res://Main.tscn"
icon="res://icon.png"

View File

@@ -1,4 +1,4 @@
#include <godot.h>
#include <gdnative_api_struct.gen.h>
#include <stdio.h>
#include <GLFW/glfw3.h>
@@ -7,8 +7,8 @@ struct glfw {
GLFWwindow *window;
};
const godot_gdnative_core_api_struct *api = NULL;
const godot_gdnative_ext_nativescript_api_struct *nativescript_api = NULL;
void window_close_callback(GLFWwindow* window)
{
@@ -21,46 +21,42 @@ void window_close_callback(GLFWwindow* window)
// Object.callv("emit_signal", ["closed"])
static godot_method_bind *mb = NULL;
if (!mb) {
mb = godot_method_bind_get_method("Object", "callv");
mb = api->godot_method_bind_get_method("Object", "callv");
}
godot_variant v;
godot_array args;
godot_array_new(&args);
api->godot_array_new(&args);
godot_string name;
godot_string_new_data(&name, "emit_signal", 11);
api->godot_string_new(&name);
api->godot_string_parse_utf8(&name, "emit_signal");
{
godot_string signal_name;
godot_string_new_data(&signal_name, "closed", 6);
api->godot_string_new(&signal_name);
api->godot_string_parse_utf8(&signal_name, "closed");
godot_variant signal_name_var;
godot_variant_new_string(&signal_name_var, &signal_name);
api->godot_variant_new_string(&signal_name_var, &signal_name);
godot_array_append(&args, &signal_name_var);
api->godot_array_append(&args, &signal_name_var);
godot_variant_destroy(&signal_name_var);
godot_string_destroy(&signal_name);
api->godot_variant_destroy(&signal_name_var);
api->godot_string_destroy(&signal_name);
}
const void *c_args[] = {
&name,
&args
};
godot_method_bind_ptrcall(mb, instance, c_args, &v);
api->godot_method_bind_ptrcall(mb, instance, c_args, &v);
godot_string_destroy(&name);
godot_array_destroy(&args);
api->godot_string_destroy(&name);
api->godot_array_destroy(&args);
}
}
void *glfw_constructor(godot_object *instance, void *method_data);
void glfw_destructor(godot_object *instance, void *method_data, struct glfw *user_data);
@@ -82,7 +78,22 @@ godot_variant glfw_is_closed(godot_object *instance, void *method_data,
int num_args, godot_variant **args);
void godot_native_init(godot_native_init_options *options)
void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *p_options)
{
api = p_options->api_struct;
// now find our extensions
for (int i = 0; i < api->num_extensions; i++) {
switch (api->extensions[i]->type) {
case GDNATIVE_EXT_NATIVESCRIPT: {
nativescript_api = (godot_gdnative_ext_nativescript_api_struct *)api->extensions[i];
}; break;
default: break;
};
};
}
void GDN_EXPORT godot_nativescript_init(void *p_handle)
{
if (!glfwInit()) {
fprintf(stderr, "can't initialize GLFW\n");
@@ -95,7 +106,7 @@ void godot_native_init(godot_native_init_options *options)
godot_instance_destroy_func destroy = {};
destroy.destroy_func = &glfw_destructor;
godot_script_register_class("GLFW", "Reference", create, destroy);
nativescript_api->godot_nativescript_register_class(p_handle, "GLFW", "Reference", create, destroy);
{
godot_instance_method create_window = {};
@@ -104,7 +115,7 @@ void godot_native_init(godot_native_init_options *options)
godot_method_attributes attributes = {};
attributes.rpc_type = GODOT_METHOD_RPC_MODE_DISABLED;
godot_script_register_method("GLFW", "create", attributes, create_window);
nativescript_api->godot_nativescript_register_method(p_handle, "GLFW", "create", attributes, create_window);
}
{
@@ -114,7 +125,7 @@ void godot_native_init(godot_native_init_options *options)
godot_method_attributes attributes = {};
attributes.rpc_type = GODOT_METHOD_RPC_MODE_DISABLED;
godot_script_register_method("GLFW", "close", attributes, close_window);
nativescript_api->godot_nativescript_register_method(p_handle, "GLFW", "close", attributes, close_window);
}
@@ -125,7 +136,7 @@ void godot_native_init(godot_native_init_options *options)
godot_method_attributes attributes = {};
attributes.rpc_type = GODOT_METHOD_RPC_MODE_DISABLED;
godot_script_register_method("GLFW", "poll_events", attributes, poll_events);
nativescript_api->godot_nativescript_register_method(p_handle, "GLFW", "poll_events", attributes, poll_events);
}
{
@@ -135,62 +146,54 @@ void godot_native_init(godot_native_init_options *options)
godot_method_attributes attributes = {};
attributes.rpc_type = GODOT_METHOD_RPC_MODE_DISABLED;
godot_script_register_method("GLFW", "is_closed", attributes, is_closed);
nativescript_api->godot_nativescript_register_method(p_handle, "GLFW", "is_closed", attributes, is_closed);
}
{
godot_signal signal = {};
signal.num_args = 0;
signal.num_default_args = 0;
godot_string_new_data(&signal.name, "closed", 6);
api->godot_string_new(&signal.name);
api->godot_string_parse_utf8(&signal.name, "closed");
godot_script_register_signal("GLFW", &signal);
nativescript_api->godot_nativescript_register_signal(p_handle, "GLFW", &signal);
godot_string_destroy(&signal.name);
api->godot_string_destroy(&signal.name);
}
}
void godot_native_terminate(godot_native_terminate_options *options)
void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *options)
{
glfwTerminate();
}
godot_variant glfw_create_window(godot_object *instance, void *method_data,
struct glfw *user_data,
int num_args, godot_variant **args)
{
godot_variant ret;
godot_variant_new_nil(&ret);
api->godot_variant_new_nil(&ret);
int width = godot_variant_as_int(args[0]);
int height = godot_variant_as_int(args[1]);;
godot_string title = godot_variant_as_string(args[2]);
int width = api->godot_variant_as_int(args[0]);
int height = api->godot_variant_as_int(args[1]);
godot_string title = api->godot_variant_as_string(args[2]);
printf("create window: %dx%d\n", width, height);
{
int length;
godot_string_get_data(&title, NULL, &length);
char c_title[length + 1];
c_title[length] = 0;
godot_string_get_data(&title, c_title, &length);
godot_char_string title_char_string = api->godot_string_utf8(&title);
const char *c_title = api->godot_char_string_get_data(&title_char_string);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
user_data->window = glfwCreateWindow(width, height, c_title, NULL, NULL);
api->godot_char_string_destroy(&title_char_string);
glfwSetWindowUserPointer(user_data->window, instance);
glfwSetWindowCloseCallback(user_data->window, window_close_callback);
}
return ret;
}
@@ -201,7 +204,7 @@ godot_variant glfw_close(godot_object *instance, void *method_data,
int num_args, godot_variant **args)
{
godot_variant ret;
godot_variant_new_nil(&ret);
api->godot_variant_new_nil(&ret);
if (user_data->window) {
glfwDestroyWindow(user_data->window);
@@ -217,7 +220,7 @@ godot_variant glfw_is_closed(godot_object *instance, void *method_data,
int num_args, godot_variant **args)
{
godot_variant ret;
godot_variant_new_bool(&ret, user_data->window == NULL);
api->godot_variant_new_bool(&ret, user_data->window == NULL);
return ret;
}
@@ -228,7 +231,7 @@ godot_variant glfw_poll_events(godot_object *instance, void *method_data,
int num_args, godot_variant **args)
{
godot_variant ret;
godot_variant_new_nil(&ret);
api->godot_variant_new_nil(&ret);
glfwPollEvents();
@@ -242,8 +245,8 @@ godot_variant glfw_poll_events(godot_object *instance, void *method_data,
void *glfw_constructor(godot_object *instance, void *method_data)
{
printf("GLFW._init()\n");
struct glfw *user_data = godot_alloc(sizeof(struct glfw));
struct glfw *user_data = api->godot_alloc(sizeof(struct glfw));
user_data->window = NULL;
return user_data;
@@ -252,11 +255,11 @@ void *glfw_constructor(godot_object *instance, void *method_data)
void glfw_destructor(godot_object *instance, void *method_data, struct glfw *user_data)
{
// destroy window
if (user_data->window) {
glfwDestroyWindow(user_data->window);
user_data->window = NULL;
}
godot_free(user_data);
api->godot_free(user_data);
}

Binary file not shown.

Binary file not shown.