Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from m4gr3d/customize_oculus_runtime_parameters
Browse files Browse the repository at this point in the history
Access vrapi display refresh rate config api.
  • Loading branch information
m4gr3d authored Aug 28, 2019
2 parents 0b92cb8 + a738b8d commit 3639380
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 5 deletions.
8 changes: 8 additions & 0 deletions demo/addons/godot_ovrmobile/OvrDisplayRefreshRate.gdns
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="NativeScript" load_steps=2 format=2]

[ext_resource path="res://addons/godot_ovrmobile/godot_ovrmobile.gdnlib" type="GDNativeLibrary" id=1]

[resource]
resource_name = "OvrDisplayRefreshRate"
class_name = "OvrDisplayRefreshRate"
library = ExtResource( 1 )
18 changes: 18 additions & 0 deletions src/config/config_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "config_common.h"

void *init_ovr_config_data_struct() {
ovr_config_data_struct *ovr_config_data;

ovr_config_data = (ovr_config_data_struct *)api->godot_alloc(sizeof(ovr_config_data_struct));
if (ovr_config_data != NULL) {
ovr_config_data->ovr_mobile_session = ovrmobile::OvrMobileSession::get_singleton_instance();
}

return ovr_config_data;
}

void reset_ovr_config_data_struct(ovr_config_data_struct *ovr_config_data) {
if (ovr_config_data && ovr_config_data->ovr_mobile_session) {
ovr_config_data->ovr_mobile_session = NULL;
}
}
13 changes: 9 additions & 4 deletions src/config/config_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ typedef struct ovr_config_data_struct {
ovrmobile::OvrMobileSession *ovr_mobile_session;
} ovr_config_data_struct;

void *init_ovr_config_data_struct();

void reset_ovr_config_data_struct(ovr_config_data_struct *ovr_config_data);

/*
to avoid code duplication and have consistent error handling this define is used in each function to check
for use data and instance in each function the inner _codeblock_ contains the actual implementation and
needs to set the variabl 'ret' correctly on success
needs to set the variable 'ret' correctly on success
*/
#undef CHECK_OVR
#define CHECK_OVR(_codeblock_) \
godot_variant ret; \
if (p_user_data != NULL) { \
ovr_config_data_struct *ovr_tracking_space_data = (ovr_config_data_struct *) p_user_data; \
if (ovr_tracking_space_data->ovr_mobile_session != NULL) { \
ovrMobile* ovr = ovr_tracking_space_data->ovr_mobile_session->get_ovr_mobile_context(); \
ovr_config_data_struct *ovr_config_data = (ovr_config_data_struct *) p_user_data; \
if (ovr_config_data->ovr_mobile_session != NULL) { \
ovrMobile* ovr = ovr_config_data->ovr_mobile_session->get_ovr_mobile_context(); \
const ovrJava* ovr_java = ovr_config_data->ovr_mobile_session->get_ovr_java(); \
_codeblock_ \
} \
} \
Expand Down
69 changes: 69 additions & 0 deletions src/config/ovr_display_refresh_rate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "ovr_display_refresh_rate.h"
#include "config_common.h"
#include <vector>

static const char *kClassName = "OvrDisplayRefreshRate";

void register_gdnative_display_refresh_rate(void *handle) {
// register the constructor and destructor of the OvrDisplayRefreshRate class for use in GDScript
godot_instance_create_func create = { NULL, NULL, NULL };
create.create_func = &ovr_display_refresh_rate_constructor;

godot_instance_destroy_func destroy = { NULL, NULL, NULL };
destroy.destroy_func = &ovr_display_refresh_rate_destructor;

nativescript_api->godot_nativescript_register_class(handle, kClassName, "Reference", create, destroy);

// register all the functions that we want to expose via the OvrDisplayRefreshRate class in GDScript
godot_instance_method method = { NULL, NULL, NULL };
godot_method_attributes attributes = { GODOT_METHOD_RPC_MODE_DISABLED };

method.method = &get_supported_display_refresh_rates;
nativescript_api->godot_nativescript_register_method(handle, kClassName, "get_supported_display_refresh_rates", attributes, method);

method.method = &set_display_refresh_rate;
nativescript_api->godot_nativescript_register_method(handle, kClassName, "set_display_refresh_rate", attributes, method);
}

GDCALLINGCONV void *ovr_display_refresh_rate_constructor(godot_object *instance, void *method_data) {
return init_ovr_config_data_struct();
}

GDCALLINGCONV void ovr_display_refresh_rate_destructor(godot_object *instance, void *method_data, void *user_data) {
if (user_data) {
reset_ovr_config_data_struct(static_cast<ovr_config_data_struct *>(user_data));
}
}

GDCALLINGCONV godot_variant get_supported_display_refresh_rates(godot_object *instance, void *method_data, void *p_user_data, int num_args, godot_variant **args) {
CHECK_OVR(
const int refresh_rates_count = vrapi_GetSystemPropertyInt(ovr_java, VRAPI_SYS_PROP_NUM_SUPPORTED_DISPLAY_REFRESH_RATES);
std::vector<float> supported_refresh_rates(refresh_rates_count, 0.0F);
vrapi_GetSystemPropertyFloatArray(ovr_java, VRAPI_SYS_PROP_SUPPORTED_DISPLAY_REFRESH_RATES, supported_refresh_rates.data(), refresh_rates_count);

godot_array gd_return_array;
api->godot_array_new(&gd_return_array);

for (int i = 0; i < refresh_rates_count; i++) {
godot_variant refresh_rate;
api->godot_variant_new_real(&refresh_rate, supported_refresh_rates[i]);
api->godot_array_push_back(&gd_return_array, &refresh_rate);
api->godot_variant_destroy(&refresh_rate);
}

api->godot_variant_new_array(&ret, &gd_return_array);
api->godot_array_destroy(&gd_return_array);

)
}

GDCALLINGCONV godot_variant set_display_refresh_rate(godot_object *instance, void *method_data, void *p_user_data, int num_args, godot_variant **args) {
CHECK_OVR(
const double refresh_rate = api->godot_variant_as_real(args[0]);
ovrResult result = vrapi_SetDisplayRefreshRate(ovr, refresh_rate);
if (result == ovrSuccess) {
api->godot_variant_new_bool(&ret, true);
}

)
}
32 changes: 32 additions & 0 deletions src/config/ovr_display_refresh_rate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
////////////////////////////////////////////////////////////////////////////////////////////////
// GDNative module that exposes part of the oculus api.
// Each exported class implements one section of the VrApi.h.
// This implements the <Display refresh rate> section.

#ifndef OVR_DISPLAY_REFRESH_RATE_H
#define OVR_DISPLAY_REFRESH_RATE_H

#include "../godot_calls.h"

#ifdef __cplusplus
extern "C" {
#endif

// Registers the OvrDisplayRefreshRate class and functions to GDNative.
// This method should be called from godot_ovrmobile_nativescript_init.
void register_gdnative_display_refresh_rate(void *handle);

GDCALLINGCONV void *ovr_display_refresh_rate_constructor(godot_object *instance, void *method_data);
GDCALLINGCONV void ovr_display_refresh_rate_destructor(godot_object *instance, void *method_data, void *user_data);

// implements VrApi.h: ovrResult vrapi_SetDisplayRefreshRate(ovrMobile * ovr, const float refreshRate);
GDCALLINGCONV godot_variant set_display_refresh_rate(godot_object *instance, void *method_data, void *user_data, int num_args, godot_variant **args);

// Utility method to access the display refresh rates supported by the device.
GDCALLINGCONV godot_variant get_supported_display_refresh_rates(godot_object *instance, void *method_data, void *user_data, int num_args, godot_variant **args);

#ifdef __cplusplus
}
#endif

#endif // OVR_DISPLAY_REFRESH_RATE_H
4 changes: 3 additions & 1 deletion src/godot_ovrmobile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

#include "arvr_interface.h"

#include "config/ovr_tracking_transform.h"
#include "config/ovr_display_refresh_rate.h"
#include "config/ovr_guardian_system.h"
#include "config/ovr_tracking_transform.h"

void GDN_EXPORT godot_ovrmobile_gdnative_singleton() {
if (arvr_api != NULL) {
Expand All @@ -22,6 +23,7 @@ void GDN_EXPORT godot_ovrmobile_nativescript_init(void *p_handle) {
return;
}

register_gdnative_display_refresh_rate(p_handle);
register_gdnative_guardian_system(p_handle);
register_gdnative_tracking_transform(p_handle);
}
4 changes: 4 additions & 0 deletions src/ovr_mobile_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class OvrMobileSession {

ovrMobile* get_ovr_mobile_context() {return ovr;};

const ovrJava *get_ovr_java() {
return &java;
}

private:

OvrMobileSession();
Expand Down

0 comments on commit 3639380

Please sign in to comment.