Skip to content

Commit

Permalink
fix array.random, reactphysics, minor misc
Browse files Browse the repository at this point in the history
* Array.random should properly be registered as a const function.

* On mobile platforms, sounds are not preloaded by default.

* Tiny amount of reactphysics work.
  • Loading branch information
samtupy committed Nov 16, 2024
1 parent b74f1ea commit 8b9b544
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
6 changes: 3 additions & 3 deletions src/nvgt_angelscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,9 @@ int ConfigureEngine(asIScriptEngine* engine) {
engine->BeginConfigGroup("serialization");
RegisterSerializationFunctions(engine);
engine->EndConfigGroup();
engine->BeginConfigGroup("xplatform");
RegisterXplatform(engine);
engine->EndConfigGroup();
engine->SetDefaultAccessMask(NVGT_SUBSYSTEM_SOUND);
engine->BeginConfigGroup("sound");
RegisterScriptSound(engine);
Expand Down Expand Up @@ -494,9 +497,6 @@ int ConfigureEngine(asIScriptEngine* engine) {
engine->BeginConfigGroup("unsorted");
RegisterUnsorted(engine);
engine->EndConfigGroup();
engine->BeginConfigGroup("xplatform");
RegisterXplatform(engine);
engine->EndConfigGroup();
engine->SetDefaultAccessMask(NVGT_SUBSYSTEM_UNCLASSIFIED);
g_ctxMgr->RegisterThreadSupport(engine);
g_ctxMgr->RegisterCoRoutineSupport(engine);
Expand Down
10 changes: 5 additions & 5 deletions src/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,37 +111,37 @@ void RegisterScriptRandom(asIScriptEngine* engine) {
engine->RegisterGlobalFunction(_O("int random(int, int)"), WRAP_FN_PR(random, (int, int), int), asCALL_GENERIC);
engine->RegisterGlobalFunction(_O("bool random_bool(int = 50)"), asFUNCTION(random_bool), asCALL_CDECL);
engine->RegisterGlobalFunction(_O("string random_character(const string& in, const string& in)"), asFUNCTION(random_character), asCALL_CDECL);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random()"), asFUNCTION(random_choice), asCALL_CDECL_OBJFIRST);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random() const"), asFUNCTION(random_choice), asCALL_CDECL_OBJFIRST);
engine->RegisterObjectType(_O("random_pcg"), sizeof(rnd_pcg_t), asOBJ_VALUE | asOBJ_POD);
engine->RegisterObjectBehaviour(_O("random_pcg"), asBEHAVE_CONSTRUCT, _O("void f()"), WRAP_OBJ_FIRST(rnd_pcg_construct), asCALL_GENERIC);
engine->RegisterObjectBehaviour(_O("random_pcg"), asBEHAVE_CONSTRUCT, _O("void f(uint)"), WRAP_OBJ_FIRST(rnd_pcg_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_pcg"), _O("void seed(uint = random_seed())"), WRAP_OBJ_FIRST(rnd_pcg_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_pcg"), _O("uint next()"), WRAP_OBJ_FIRST(rnd_pcg_next), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_pcg"), _O("float nextf()"), WRAP_OBJ_FIRST(rnd_pcg_nextf), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_pcg"), _O("int range(int, int)"), WRAP_OBJ_FIRST(rnd_pcg_range), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_pcg&in generator)"), WRAP_OBJ_FIRST(rnd_pcg_choice), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_pcg&in generator) const"), WRAP_OBJ_FIRST(rnd_pcg_choice), asCALL_GENERIC);
engine->RegisterObjectType(_O("random_well"), sizeof(rnd_well_t), asOBJ_VALUE | asOBJ_POD);
engine->RegisterObjectBehaviour(_O("random_well"), asBEHAVE_CONSTRUCT, _O("void f()"), WRAP_OBJ_FIRST(rnd_well_construct), asCALL_GENERIC);
engine->RegisterObjectBehaviour(_O("random_well"), asBEHAVE_CONSTRUCT, _O("void f(uint = random_seed())"), WRAP_OBJ_FIRST(rnd_well_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_well"), _O("void seed(uint = random_seed())"), WRAP_OBJ_FIRST(rnd_well_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_well"), _O("uint next()"), WRAP_OBJ_FIRST(rnd_well_next), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_well"), _O("float nextf()"), WRAP_OBJ_FIRST(rnd_well_nextf), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_well"), _O("int range(int, int)"), WRAP_OBJ_FIRST(rnd_well_range), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_well&in generator)"), WRAP_OBJ_FIRST(rnd_well_choice), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_well&in generator) const"), WRAP_OBJ_FIRST(rnd_well_choice), asCALL_GENERIC);
engine->RegisterObjectType(_O("random_gamerand"), sizeof(rnd_gamerand_t), asOBJ_VALUE | asOBJ_POD);
engine->RegisterObjectBehaviour(_O("random_gamerand"), asBEHAVE_CONSTRUCT, _O("void f()"), WRAP_OBJ_FIRST(rnd_gamerand_construct), asCALL_GENERIC);
engine->RegisterObjectBehaviour(_O("random_gamerand"), asBEHAVE_CONSTRUCT, _O("void f(uint = random_seed())"), WRAP_OBJ_FIRST(rnd_gamerand_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_gamerand"), _O("void seed(uint = random_seed())"), WRAP_OBJ_FIRST(rnd_gamerand_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_gamerand"), _O("uint next()"), WRAP_OBJ_FIRST(rnd_gamerand_next), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_gamerand"), _O("float nextf()"), WRAP_OBJ_FIRST(rnd_gamerand_nextf), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_gamerand"), _O("int range(int, int)"), WRAP_OBJ_FIRST(rnd_gamerand_range), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_gamerand&in generator)"), WRAP_OBJ_FIRST(rnd_gamerand_choice), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_gamerand&in generator) const"), WRAP_OBJ_FIRST(rnd_gamerand_choice), asCALL_GENERIC);
engine->RegisterObjectType(_O("random_xorshift"), sizeof(rnd_xorshift_t), asOBJ_VALUE | asOBJ_POD);
engine->RegisterObjectBehaviour(_O("random_xorshift"), asBEHAVE_CONSTRUCT, _O("void f()"), WRAP_OBJ_FIRST(rnd_xorshift_construct), asCALL_GENERIC);
engine->RegisterObjectBehaviour(_O("random_xorshift"), asBEHAVE_CONSTRUCT, _O("void f(uint64 = random_seed64())"), WRAP_OBJ_FIRST(rnd_xorshift_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_xorshift"), _O("void seed(uint64 = random_seed64())"), WRAP_OBJ_FIRST(rnd_xorshift_seed), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_xorshift"), _O("uint64 next()"), WRAP_OBJ_FIRST(rnd_xorshift_next), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_xorshift"), _O("float nextf()"), WRAP_OBJ_FIRST(rnd_xorshift_nextf), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("random_xorshift"), _O("int range(int, int)"), WRAP_OBJ_FIRST(rnd_xorshift_range), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_xorshift&in generator)"), WRAP_OBJ_FIRST(rnd_xorshift_choice), asCALL_GENERIC);
engine->RegisterObjectMethod(_O("array<T>"), _O("const T& random(const random_xorshift&in generator) const"), WRAP_OBJ_FIRST(rnd_xorshift_choice), asCALL_GENERIC);
}
21 changes: 19 additions & 2 deletions src/reactphysics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,17 @@ AABB aabb_from_triangle(CScriptArray* points) {
}

// registration templates
template <class T> void RegisterCollisionShape() {

template <class T> void RegisterCollisionShape(asIScriptEngine* engine, const string& type) {
engine->RegisterObjectMethod(type.c_str(), "physics_shape_name get_name() const property", asMETHOD(T, getName), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "physics_shape_type get_type() const property", asMETHOD(T, getType), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "bool get_is_convex() const property", asMETHOD(T, isConvex), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "bool get_is_polyhedron() const property", asMETHOD(T, isPolyhedron), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "aabb get_local_bounds() const", asMETHOD(T, getLocalBounds), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "int get_id() const property", asMETHOD(T, getId), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "vector get_local_inertia_tensor(float mass) const", asMETHOD(T, getLocalInertiaTensor), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "float get_volume() const property", asMETHOD(T, getVolume), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "aabb compute_transformed_aabb(const transform&in transform) const", asMETHOD(T, computeTransformedAABB), asCALL_THISCALL);
engine->RegisterObjectMethod(type.c_str(), "string opImplConv() const", asMETHOD(T, to_string), asCALL_THISCALL);
}

void RegisterReactphysics(asIScriptEngine* engine) {
Expand All @@ -58,6 +67,14 @@ void RegisterReactphysics(asIScriptEngine* engine) {
engine->RegisterEnumValue("physics_shape_type", "SHAPE_TYPE_CAPSULE", int(CollisionShapeType::CAPSULE));
engine->RegisterEnumValue("physics_shape_type", "SHAPE_TYPE_CONVEX_POLYHEDRON", int(CollisionShapeType::CONVEX_POLYHEDRON));
engine->RegisterEnumValue("physics_shape_type", "SHAPE_TYPE_CONCAVE", int(CollisionShapeType::CONCAVE_SHAPE));
engine->RegisterEnum("physics_shape_name");
engine->RegisterEnumValue("physics_shape_name", "SHAPE_TRIANGLE", int(CollisionShapeName::TRIANGLE));
engine->RegisterEnumValue("physics_shape_name", "SHAPE_SPHERE", int(CollisionShapeName::SPHERE));
engine->RegisterEnumValue("physics_shape_name", "SHAPE_CAPSULE", int(CollisionShapeName::CAPSULE));
engine->RegisterEnumValue("physics_shape_name", "SHAPE_BOX", int(CollisionShapeName::BOX));
engine->RegisterEnumValue("physics_shape_name", "SHAPE_CONVEX_MESH", int(CollisionShapeName::CONVEX_MESH));
engine->RegisterEnumValue("physics_shape_name", "SHAPE_TRIANGLE_MESH", int(CollisionShapeName::TRIANGLE_MESH));
engine->RegisterEnumValue("physics_shape_name", "SHAPE_HEIGHTFIELD", int(CollisionShapeName::HEIGHTFIELD));
engine->RegisterGlobalProperty("const float EPSILON", (void*)&MACHINE_EPSILON);
engine->RegisterObjectType("vector", sizeof(Vector3), asOBJ_VALUE | asOBJ_POD | asGetTypeTraits<Vector3>() | asOBJ_APP_CLASS_ALLFLOATS);
engine->RegisterObjectBehaviour("vector", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(rp_construct<Vector3>), asCALL_CDECL_OBJFIRST);
Expand Down
25 changes: 13 additions & 12 deletions src/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "sound.h"
#include <scriptarray.h>
#include "timestuff.h" //ticks() sound preloading
#include "xplatform.h" // running_on_mobile
#include <system_error>
#include <fast_float.h>
#include <Poco/StringTokenizer.h>
Expand Down Expand Up @@ -2007,23 +2008,23 @@ void RegisterScriptSound(asIScriptEngine* engine) {
engine->RegisterObjectBehaviour("sound", asBEHAVE_RELEASE, "void f()", asMETHOD(sound, Release), asCALL_THISCALL);
engine->RegisterObjectProperty("sound", "const string loaded_filename", asOFFSET(sound, loaded_filename));
engine->RegisterObjectMethod("sound", "bool close()", asMETHOD(sound, close), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load(const string &in, pack@ = sound_default_pack, bool = true)", asMETHOD(sound, load), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load(const string &in filename, pack@ pack_file = sound_default_pack, bool allow_preloads = !system_is_mobile)", asMETHOD(sound, load), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load(sound_close_callback@, sound_length_callback@, sound_read_callback@, sound_seek_callback@, const string &in, const string&in = \"\")", asMETHOD(sound, load_script), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load(string&, uint, const string&in = \"\", bool = false)", asMETHOD(sound, load_memstream), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load_url(const string &in)", asMETHOD(sound, load_url), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool stream(const string &in, pack@ = sound_default_pack)", asMETHOD(sound, stream), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool push_memory(const string &in, bool = false, int = 0, int = 0)", asMETHOD(sound, push_string), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool set_position(float, float, float, float, float, float, float, float, float)", asMETHOD(sound, set_position), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool set_mixer(mixer@ = null)", asMETHOD(sound, set_mixer), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "void set_hrtf(bool = true)", asMETHOD(sound, set_hrtf), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "void set_length(float = 0.0)", asMETHOD(sound, set_length), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool set_fx(const string &in, int = -1)", asMETHOD(sound, set_fx), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool play(bool = true)", asMETHOD(sound, play), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load(string& data, uint size, const string&in preload_filename = \"\", bool legacy_encrypt = false)", asMETHOD(sound, load_memstream), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool load_url(const string &in url)", asMETHOD(sound, load_url), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool stream(const string &in filename, pack@ containing_pack = sound_default_pack)", asMETHOD(sound, stream), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool push_memory(const string &in data, bool end_stream = false, int pcm_rate = 0, int pcm_channels = 0)", asMETHOD(sound, push_string), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool set_position(float listener_x, float listener_y, float listener_z, float sound_x, float sound_y, float sound_z, float rotation = 0.0, float pan_step = 1.0, float volume_step = 1.0)", asMETHOD(sound, set_position), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool set_mixer(mixer@ mixer = null)", asMETHOD(sound, set_mixer), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "void set_hrtf(bool enable = true)", asMETHOD(sound, set_hrtf), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "void set_length(float length = 0.0)", asMETHOD(sound, set_length), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool set_fx(const string &in fx, int index = -1)", asMETHOD(sound, set_fx), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool play(bool reset_loop_state = true)", asMETHOD(sound, play), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool play_wait()", asMETHOD(sound, play_wait), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool play_looped()", asMETHOD(sound, play_looped), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool pause()", asMETHOD(sound, pause), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool stop()", asMETHOD(sound, stop), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool seek(float)", asMETHOD(sound, seek), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool seek(float position)", asMETHOD(sound, seek), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool get_active() const property", asMETHOD(sound, is_active), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool get_playing() const property", asMETHOD(sound, is_playing), asCALL_THISCALL);
engine->RegisterObjectMethod("sound", "bool get_paused() const property", asMETHOD(sound, is_paused), asCALL_THISCALL);
Expand Down
13 changes: 8 additions & 5 deletions src/xplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ std::string android_get_main_shared_object() {
// Usually this involves defining no-op versions of functions that are only available on certain platforms, though can sometimes include wrappers as well to get around char* and other things that we can't directly register.
#ifndef SDL_PLATFORM_LINUX
bool SDL_SetLinuxThreadPriority(Sint64 threadID, int priority) { return false; }
bool SDL_SetLinuxThreadPriorityAndPolocy(Sint64 threadID, int priority, int schedPolocy) { return false; }
bool SDL_SetLinuxThreadPriorityAndPolicy(Sint64 threadID, int priority, int schedPolocy) { return false; }
#endif
#ifndef SDL_PLATFORM_ANDROID
int SDL_GetAndroidSDKVersion() { return -1; }
Expand Down Expand Up @@ -181,17 +181,20 @@ std::string get_directory_temp() {
}
#endif
void RegisterXplatform(asIScriptEngine* engine) {
engine->SetDefaultAccessMask(NVGT_SUBSYSTEM_OS);
engine->RegisterGlobalFunction("string set_linux_thread_priority(int64 thread_id, int priority)", asFUNCTION(SDL_SetLinuxThreadPriority), asCALL_CDECL);
engine->RegisterGlobalFunction("string set_linux_thread_priority_and_polocy(int64 thread_id, int priority, int polocy)", asFUNCTION(SDL_SetLinuxThreadPriorityAndPolocy), asCALL_CDECL);
engine->RegisterGlobalFunction("int get_ANDROID_SDK_VERSION() property", asFUNCTION(SDL_GetAndroidSDKVersion), asCALL_CDECL);
engine->RegisterGlobalFunction("bool get_system_is_chromebook() property", asFUNCTION(SDL_IsChromebook), asCALL_CDECL);
engine->RegisterGlobalFunction("bool get_system_is_DeX_mode() property", asFUNCTION(SDL_IsDeXMode), asCALL_CDECL);
engine->RegisterGlobalFunction("string set_linux_thread_priority_and_policy(int64 thread_id, int priority, int policy)", asFUNCTION(SDL_SetLinuxThreadPriorityAndPolicy), asCALL_CDECL);
engine->RegisterGlobalFunction("void android_send_back_button()", asFUNCTION(SDL_SendAndroidBackButton), asCALL_CDECL);
engine->RegisterFuncdef("void android_permission_request_callback(string permission, bool granted, string user_data)");
engine->RegisterGlobalFunction("bool android_request_permission(const string&in permission, android_permission_request_callback@ callback = null, const string&in callback_data = \"\")", asFUNCTION(request_android_permission), asCALL_CDECL);
engine->RegisterGlobalFunction("bool android_show_toast(const string&in message, int duration, int gravity = -1, int x_offset = 0, int y_offset = 0)", asFUNCTION(show_android_toast), asCALL_CDECL);
engine->SetDefaultAccessMask(NVGT_SUBSYSTEM_GENERAL);
engine->RegisterGlobalFunction("int get_ANDROID_SDK_VERSION() property", asFUNCTION(SDL_GetAndroidSDKVersion), asCALL_CDECL);
engine->RegisterGlobalFunction("bool get_system_is_chromebook() property", asFUNCTION(SDL_IsChromebook), asCALL_CDECL);
engine->RegisterGlobalFunction("bool get_system_is_DeX_mode() property", asFUNCTION(SDL_IsDeXMode), asCALL_CDECL);
engine->RegisterGlobalFunction("bool get_system_is_tablet() property", asFUNCTION(SDL_IsTablet), asCALL_CDECL);
//engine->RegisterGlobalFunction("bool get_system_is_tv() property", asFUNCTION(SDL_IsTV), asCALL_CDECL);
engine->RegisterGlobalFunction(_O("string get_DIRECTORY_APPDATA() property"), asFUNCTION(get_directory_appdata), asCALL_CDECL);
engine->RegisterGlobalFunction(_O("string get_DIRECTORY_TEMP() property"), asFUNCTION(get_directory_temp), asCALL_CDECL);
engine->RegisterGlobalFunction("bool get_system_is_mobile() property", asFUNCTION(running_on_mobile), asCALL_CDECL);
}

0 comments on commit 8b9b544

Please sign in to comment.