diff --git a/WickedEngine/MainComponent.cpp b/WickedEngine/MainComponent.cpp index c4f50e210..ccf02975c 100644 --- a/WickedEngine/MainComponent.cpp +++ b/WickedEngine/MainComponent.cpp @@ -6,7 +6,7 @@ #include "wiCpuInfo.h" #include "wiInputManager.h" #include "wiBackLog.h" - +#include "MainComponent_BindLua.h" MainComponent::MainComponent() { @@ -18,6 +18,8 @@ MainComponent::MainComponent() setFrameSkip(true); setTargetFrameRate(60); setApplicationControlLostThreshold(10); + + MainComponent_BindLua::Bind(); } @@ -26,6 +28,11 @@ MainComponent::~MainComponent() activeComponent->Unload(); } +void MainComponent::Initialize() +{ + wiLua::GetGlobal()->RegisterObject(MainComponent_BindLua::className, "main", new MainComponent_BindLua(this)); +} + void MainComponent::activateComponent(RenderableComponent* component) { if (component == nullptr) @@ -41,7 +48,6 @@ void MainComponent::activateComponent(RenderableComponent* component) void MainComponent::run() { - static wiTimer timer = wiTimer(); static double accumulator = 0.0; diff --git a/WickedEngine/MainComponent.h b/WickedEngine/MainComponent.h index 28cf211b9..ef4f0ceb8 100644 --- a/WickedEngine/MainComponent.h +++ b/WickedEngine/MainComponent.h @@ -33,7 +33,7 @@ public: int getTargetFrameRate(){ return targetFrameRate; } void setApplicationControlLostThreshold(int value){ applicationControlLostThreshold = value; } - virtual void Initialize(){}; + virtual void Initialize(); virtual void Update(); virtual void Render(); virtual void Compose(); diff --git a/WickedEngine/MainComponent_BindLua.cpp b/WickedEngine/MainComponent_BindLua.cpp new file mode 100644 index 000000000..355e3b778 --- /dev/null +++ b/WickedEngine/MainComponent_BindLua.cpp @@ -0,0 +1,75 @@ +#include "MainComponent_BindLua.h" +#include "Renderable3DComponent_BindLua.h" + +const char MainComponent_BindLua::className[] = "MainComponent"; + +Luna::FunctionType MainComponent_BindLua::methods[] = { + lunamethod(MainComponent_BindLua, GetActiveComponent), + lunamethod(MainComponent_BindLua, SetActiveComponent), + lunamethod(MainComponent_BindLua, SetFrameSkip), + { NULL, NULL } +}; +Luna::PropertyType MainComponent_BindLua::properties[] = { + { NULL, NULL } +}; + +MainComponent_BindLua::MainComponent_BindLua(MainComponent* component) :component(component) +{ +} + +MainComponent_BindLua::MainComponent_BindLua(lua_State *L) +{ + component = nullptr; +} + +MainComponent_BindLua::~MainComponent_BindLua() +{ +} + +int MainComponent_BindLua::GetActiveComponent(lua_State *L) +{ + Renderable3DComponent* comp = dynamic_cast(component->getActiveComponent()); + if (comp != nullptr) + { + Luna::push(L, new Renderable3DComponent_BindLua(comp)); + return 1; + } + + wiLua::SError(L, "GetActiveComponent() Warning: type of active component not registered with lua!"); + return 0; +} +int MainComponent_BindLua::SetActiveComponent(lua_State *L) +{ + int argc = wiLua::SGetArgCount(L); + if (argc > 1) + { + //TODO + } + else + { + wiLua::SError(L, "SetActiveComponent(Renderable3DComponent component) not enought arguments!"); + return 0; + } + return 0; +} +int MainComponent_BindLua::SetFrameSkip(lua_State *L) +{ + int argc = wiLua::SGetArgCount(L); + if (argc > 1) + { + component->setFrameSkip(wiLua::SGetBool(L, 2)); + } + else + wiLua::SError(L, "SetFrameSkip(bool enabled) not enought arguments!"); + return 0; +} + +void MainComponent_BindLua::Bind() +{ + static bool initialized = false; + if (!initialized) + { + initialized = true; + Luna::Register(wiLua::GetGlobal()->GetLuaState()); + } +} diff --git a/WickedEngine/MainComponent_BindLua.h b/WickedEngine/MainComponent_BindLua.h new file mode 100644 index 000000000..24bfa4bb3 --- /dev/null +++ b/WickedEngine/MainComponent_BindLua.h @@ -0,0 +1,25 @@ +#pragma once +#include "wiLua.h" +#include "wiLuna.h" +#include "MainComponent.h" + +class MainComponent_BindLua +{ +private: + MainComponent *component; +public: + static const char className[]; + static Luna::FunctionType methods[]; + static Luna::PropertyType properties[]; + + MainComponent_BindLua(MainComponent* component = nullptr); + MainComponent_BindLua(lua_State *L); + ~MainComponent_BindLua(); + + int GetActiveComponent(lua_State *L); + int SetActiveComponent(lua_State *L); + int SetFrameSkip(lua_State *L); + + static void Bind(); +}; + diff --git a/WickedEngine/Renderable3DComponent_BindLua.cpp b/WickedEngine/Renderable3DComponent_BindLua.cpp index 8b09b07bf..9be986e34 100644 --- a/WickedEngine/Renderable3DComponent_BindLua.cpp +++ b/WickedEngine/Renderable3DComponent_BindLua.cpp @@ -1,8 +1,10 @@ #include "Renderable3DComponent_BindLua.h" +#include "wiResourceManager_BindLua.h" const char Renderable3DComponent_BindLua::className[] = "Renderable3DComponent"; Luna::FunctionType Renderable3DComponent_BindLua::methods[] = { + lunamethod(Renderable3DComponent_BindLua,GetContent), { NULL, NULL } }; Luna::PropertyType Renderable3DComponent_BindLua::properties[] = { @@ -22,6 +24,12 @@ Renderable3DComponent_BindLua::~Renderable3DComponent_BindLua() { } +int Renderable3DComponent_BindLua::GetContent(lua_State *L) +{ + Luna::push(L, new wiResourceManager_BindLua(&component->Content)); + return 1; +} + void Renderable3DComponent_BindLua::Bind() { static bool initialized = false; diff --git a/WickedEngine/Renderable3DComponent_BindLua.h b/WickedEngine/Renderable3DComponent_BindLua.h index dcb6ff7ce..39d1d4a58 100644 --- a/WickedEngine/Renderable3DComponent_BindLua.h +++ b/WickedEngine/Renderable3DComponent_BindLua.h @@ -16,6 +16,8 @@ public: Renderable3DComponent_BindLua(lua_State* L); ~Renderable3DComponent_BindLua(); + int GetContent(lua_State *L); + static void Bind(); }; diff --git a/WickedEngine/WickedEngine.vcxproj b/WickedEngine/WickedEngine.vcxproj index a5198c4e6..8ba06d10a 100644 --- a/WickedEngine/WickedEngine.vcxproj +++ b/WickedEngine/WickedEngine.vcxproj @@ -210,6 +210,7 @@ + @@ -409,6 +410,7 @@ + diff --git a/WickedEngine/WickedEngine.vcxproj.filters b/WickedEngine/WickedEngine.vcxproj.filters index 96b5e1b06..c8cdf9e3f 100644 --- a/WickedEngine/WickedEngine.vcxproj.filters +++ b/WickedEngine/WickedEngine.vcxproj.filters @@ -749,6 +749,9 @@ ScriptBinders + + ScriptBinders + @@ -1669,6 +1672,9 @@ ScriptBinders + + ScriptBinders + diff --git a/WickedEngine/wiResourceManager.h b/WickedEngine/wiResourceManager.h index 2e60d359c..99f2b6a32 100644 --- a/WickedEngine/wiResourceManager.h +++ b/WickedEngine/wiResourceManager.h @@ -19,7 +19,6 @@ public: COMPUTESHADER, }; -protected: struct Resource { void* data; @@ -27,13 +26,15 @@ protected: Resource(void* newData, Data_Type newType) :data(newData), type(newType){}; }; + typedef unordered_map container; + container resources; + +protected: typedef map filetypes; static filetypes types; static wiResourceManager* globalResources; static void SetUp(); -typedef unordered_map container; -container resources; public: wiResourceManager(); diff --git a/WickedEngine/wiResourceManager_BindLua.cpp b/WickedEngine/wiResourceManager_BindLua.cpp index 5a981d477..f6ca21ce4 100644 --- a/WickedEngine/wiResourceManager_BindLua.cpp +++ b/WickedEngine/wiResourceManager_BindLua.cpp @@ -1,6 +1,5 @@ #include "wiResourceManager_BindLua.h" #include "wiSound_BindLua.h" - #include "wiHelper.h" @@ -17,9 +16,12 @@ Luna::PropertyType wiResourceManager_BindLua::propert { NULL, NULL } }; +wiResourceManager_BindLua::wiResourceManager_BindLua(wiResourceManager* resources) :resources(resources) +{ +} wiResourceManager_BindLua::wiResourceManager_BindLua(lua_State *L) { - wiResourceManager::wiResourceManager(); + resources = nullptr; } wiResourceManager_BindLua::~wiResourceManager_BindLua() { @@ -31,13 +33,13 @@ int wiResourceManager_BindLua::Get(lua_State *L) if (argc > 1) { string name = wiLua::SGetString(L, 2); - const Resource* data = get(name); + const wiResourceManager::Resource* data = resources->get(name); if (data != nullptr) { switch (data->type) { - case Data_Type::MUSIC: - case Data_Type::SOUND: + case wiResourceManager::Data_Type::MUSIC: + case wiResourceManager::Data_Type::SOUND: Luna::push(L, new wiSound_BindLua((wiSound*)data->data)); return 1; break; @@ -64,16 +66,16 @@ int wiResourceManager_BindLua::Add(lua_State *L) if (argc > 1) { string name = wiLua::SGetString(L, 2); - Data_Type type = Data_Type::DYNAMIC; + wiResourceManager::Data_Type type = wiResourceManager::Data_Type::DYNAMIC; if (argc > 2) //type info also provided in this case { string typeStr = wiHelper::toUpper( wiLua::SGetString(L, 3) ); if (!typeStr.compare("SOUND")) - type = Data_Type::SOUND; + type = wiResourceManager::Data_Type::SOUND; else if (!typeStr.compare("MUSIC")) - type = Data_Type::MUSIC; + type = wiResourceManager::Data_Type::MUSIC; } - void* data = add(name, type); + void* data = resources->add(name, type); wiLua::SSetString(L, (data != nullptr ? "ok" : "not found")); return 1; } @@ -89,7 +91,7 @@ int wiResourceManager_BindLua::Del(lua_State *L) if (argc > 1) { string name = wiLua::SGetString(L, 2); - wiLua::SSetString(L, (del(name) ? "ok" : "not found")); + wiLua::SSetString(L, (resources->del(name) ? "ok" : "not found")); return 1; } else @@ -102,7 +104,7 @@ int wiResourceManager_BindLua::List(lua_State *L) { stringstream ss(""); ss << "Resources of: " << wiLua::SGetString(L, 1) << endl; - for (auto& x : resources) + for (auto& x : resources->resources) { ss << x.first << endl; } @@ -116,7 +118,7 @@ void wiResourceManager_BindLua::Bind() if (!initialized) { Luna::Register(wiLua::GetGlobal()->GetLuaState()); - wiLua::GetGlobal()->RegisterObject(className, "globalResources", wiResourceManager::GetGlobal()); + wiLua::GetGlobal()->RegisterObject(className, "globalResources", new wiResourceManager_BindLua(wiResourceManager::GetGlobal())); initialized = true; } } diff --git a/WickedEngine/wiResourceManager_BindLua.h b/WickedEngine/wiResourceManager_BindLua.h index acb68eead..83cd1c052 100644 --- a/WickedEngine/wiResourceManager_BindLua.h +++ b/WickedEngine/wiResourceManager_BindLua.h @@ -5,13 +5,16 @@ //extends the resourcemanager to be accessed by lua scripts using the Luna wrapper -class wiResourceManager_BindLua:public wiResourceManager +class wiResourceManager_BindLua : public wiResourceManager { +private: + wiResourceManager* resources; public: static const char className[]; static Luna::FunctionType methods[]; static Luna::PropertyType properties[]; + wiResourceManager_BindLua(wiResourceManager* resources = nullptr); wiResourceManager_BindLua(lua_State *L); ~wiResourceManager_BindLua();