Various improvements:

Framebuffer resize queued to next frame to fix flickering
Wren VM init moved to on play event
Added new Wren modules
Added missing components to the editor
Changed how the wren scripts gets initialized
This commit is contained in:
Antoine Pilote
2021-06-12 13:01:07 -04:00
parent 855bb1edd6
commit cdfee1981c
25 changed files with 451 additions and 108 deletions

View File

@@ -27,9 +27,9 @@ namespace ScriptAPI
static void Sqrt(WrenVM* vm)
{
float x = wrenGetSlotDouble(vm, 0);
float y = wrenGetSlotDouble(vm, 0);
float z = wrenGetSlotDouble(vm, 0);
float x = wrenGetSlotDouble(vm, 1);
float y = wrenGetSlotDouble(vm, 2);
float z = wrenGetSlotDouble(vm, 3);
float result = glm::sqrt((x * x) + (y * y) + (z * z));
wrenSetSlotDouble(vm, 0, result);
}

View File

@@ -12,6 +12,7 @@
#include <src/Scene/Entities/Components/QuakeMap.h>
#include <src/Scene/Entities/Components/CameraComponent.h>
#include <src/Scene/Entities/Components/RigidbodyComponent.h>
#include <src/Scene/Entities/Components/CharacterControllerComponent.h>
namespace ScriptAPI
{
@@ -30,6 +31,10 @@ namespace ScriptAPI
RegisterMethod("EntityHasComponent(_,_)", (void*)EntityHasComponent);
RegisterMethod("SetLightIntensity_(_,_)", (void*)SetLightIntensity);
RegisterMethod("GetLightIntensity_(_)", (void*)GetLightIntensity);
RegisterMethod("SetCameraDirection_(_,_,_,_)", (void*)SetCameraDirection);
RegisterMethod("GetCameraDirection_(_)", (void*)GetCameraDirection);
RegisterMethod("GetCameraRight_(_)", (void*)GetCameraRight);
RegisterMethod("MoveAndSlide_(_,_,_,_)", (void*)MoveAndSlide);
}
static void GetEntity(WrenVM* vm)
@@ -99,7 +104,73 @@ namespace ScriptAPI
static void SetCameraDirection(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
cam.CameraInstance->SetDirection(Vector3(x, y, z));
}
static void GetCameraDirection(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
Vector3 dir = cam.CameraInstance->GetDirection();
wrenEnsureSlots(vm, 4);
// set the slots
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenSetSlotDouble(vm, 1, dir.x);
wrenSetSlotDouble(vm, 2, dir.y);
wrenSetSlotDouble(vm, 3, dir.z);
wrenInsertInList(vm, 0, -1, 1);
wrenInsertInList(vm, 0, -1, 2);
wrenInsertInList(vm, 0, -1, 3);
}
static void GetCameraRight(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& cam = ent.GetComponent<CameraComponent>();
Vector3 right = cam.CameraInstance->cameraRight;
// set the slots
wrenSetSlotDouble(vm, 1, right.x);
wrenSetSlotDouble(vm, 2, right.y);
wrenSetSlotDouble(vm, 3, right.z);
// Fill the list
wrenSetSlotNewList(vm, 0);
wrenInsertInList(vm, 0, 0, 1);
wrenInsertInList(vm, 0, 1, 2);
wrenInsertInList(vm, 0, 2, 3);
}
static void MoveAndSlide(WrenVM* vm)
{
int handle = wrenGetSlotDouble(vm, 1);
float x = wrenGetSlotDouble(vm, 2);
float y = wrenGetSlotDouble(vm, 3);
float z = wrenGetSlotDouble(vm, 4);
Entity ent = Entity((entt::entity)handle, Engine::GetCurrentScene().get());
auto& characterController = ent.GetComponent<CharacterControllerComponent>();
characterController.CharacterController->MoveAndSlide(Vector3(x, y, z));
}
};
}

View File

@@ -40,11 +40,25 @@ void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
bool hasEnding(std::string const& fullString, std::string const& ending) {
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare(fullString.length() - ending.length(), ending.length(), ending));
}
else {
return false;
}
}
WrenLoadModuleResult myLoadModule(WrenVM* vm, const char* name) {
WrenLoadModuleResult result = { 0 };
std::string str = FileSystem::ReadFile("resources/" + std::string(name) + ".wren", true);
std::string path = "resources/" + std::string(name);
if(!hasEnding(path, ".wren"))
path += ".wren";
std::string str = FileSystem::ReadFile(path, true);
char* c = strcpy(new char[str.length() + 1], str.c_str());
result.source = c;
return result;
}

View File

@@ -1,20 +1,36 @@
#include "WrenScript.h"
#include "../Core/FileSystem.h"
#include <src/Vendors/wren/src/include/wren.h>
WrenScript::WrenScript(const std::string& path, const std::string& mod)
{
WrenScript::WrenScript(const std::string& path, const std::string& mod, bool isEntity)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
// Import statement
std::string source = "import \"" + path + "\" for " + mod;
// Import file as module
wrenInterpret(vm, "main", source.c_str());
// Get handle to class
wrenEnsureSlots(vm, 1);
wrenGetVariable(vm, "main", mod.c_str(), 0);
WrenHandle* classHandle = wrenGetSlotHandle(vm, 0);
// Call the constructor
WrenHandle* constructHandle = wrenMakeCallHandle(vm, "new()");
wrenCall(vm, constructHandle);
// Retreive value of constructor
this->m_Instance = wrenGetSlotHandle(vm, 0);
// Create handles to the instance methods.
this->m_OnInitHandle = wrenMakeCallHandle(vm, "init()");
this->m_OnUpdateHandle = wrenMakeCallHandle(vm, "update(_)");
this->m_OnExitHandle = wrenMakeCallHandle(vm, "exit()");
if (isEntity)
this->m_SetEntityIDHandle = wrenMakeCallHandle(vm, "SetEntityId(_)");
}
void WrenScript::CallInit()
@@ -26,7 +42,6 @@ void WrenScript::CallInit()
void WrenScript::CallUpdate(float timestep)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 2);
wrenSetSlotHandle(vm, 0, this->m_Instance);
@@ -37,6 +52,7 @@ void WrenScript::CallUpdate(float timestep)
void WrenScript::CallExit()
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenEnsureSlots(vm, 1);
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenInterpretResult result = wrenCall(vm, this->m_OnExitHandle);
}
@@ -56,7 +72,16 @@ void WrenScript::CallMethod(const std::string& signature)
// Not found. maybe try to register it?
if (methods.find(signature) == methods.end())
return;
wrenSetSlotHandle(vm, 0, this->m_Instance);
WrenHandle* handle = methods[signature];
WrenInterpretResult result = wrenCall(vm, handle);
}
void WrenScript::SetScriptableEntityID(int id)
{
WrenVM* vm = ScriptingEngine::GetWrenVM();
wrenSetSlotHandle(vm, 0, this->m_Instance);
wrenSetSlotDouble(vm, 1, id);
WrenInterpretResult result = wrenCall(vm, this->m_SetEntityIDHandle);
}

View File

@@ -12,8 +12,9 @@ public:
WrenHandle* m_OnInitHandle;
WrenHandle* m_OnUpdateHandle;
WrenHandle* m_OnExitHandle;
WrenHandle* m_SetEntityIDHandle;
WrenScript(const std::string& path, const std::string& mod);
WrenScript(const std::string& path, const std::string& mod, bool isEntity = false);
void CallInit();
void CallUpdate(float timestep);
@@ -21,4 +22,6 @@ public:
void RegisterMethod(const std::string& signature);
void CallMethod(const std::string& signature);
void SetScriptableEntityID(int id);
};