From 893ab7b6f505ed109c68cdc8cb3744b1845ca09b Mon Sep 17 00:00:00 2001 From: touky Date: Sun, 23 Oct 2016 18:54:46 -0400 Subject: [PATCH 01/21] first little pass, everything is broken. Will resume on other computer. --- .vscode/c_cpp_properties.json | 28 +++++++++++++ build/Lol (vs2015).sln | 15 +++++++ doc/tutorial/14_lol_lua.cpp | 22 ++++++---- doc/tutorial/imgui.ini | 4 +- src/lolua/baselua.h | 75 +++++++++-------------------------- 5 files changed, 78 insertions(+), 66 deletions(-) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..972446b0 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,28 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": ["/usr/include"], + "browse" : { + "limitSymbolsToIncludedHeaders" : true, + "databaseFilename" : "" + } + }, + { + "name": "Linux", + "includePath": ["/usr/include"], + "browse" : { + "limitSymbolsToIncludedHeaders" : true, + "databaseFilename" : "" + } + }, + { + "name": "Win32", + "includePath": ["c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include", "src"], + "browse" : { + "limitSymbolsToIncludedHeaders" : true, + "databaseFilename" : "" + } + } + ] +} diff --git a/build/Lol (vs2015).sln b/build/Lol (vs2015).sln index 22c0ffe0..d24d0a1d 100644 --- a/build/Lol (vs2015).sln +++ b/build/Lol (vs2015).sln @@ -66,6 +66,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{B629 EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "15_lolimgui", "..\doc\tutorial\15_lolimgui.vcxproj", "{81C83B42-D00A-4FA3-9A3D-80F9D46524BF}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "14_lol_lua", "..\doc\tutorial\14_lol_lua.vcxproj", "{31B96262-1C41-43B9-BA38-27AA385B05DB}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|ORBIS = Debug|ORBIS @@ -308,6 +310,18 @@ Global {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|Win32.Build.0 = Release|Win32 {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|x64.ActiveCfg = Release|x64 {81C83B42-D00A-4FA3-9A3D-80F9D46524BF}.Release|x64.Build.0 = Release|x64 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Debug|ORBIS.ActiveCfg = Debug|ORBIS + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Debug|ORBIS.Build.0 = Debug|ORBIS + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Debug|Win32.ActiveCfg = Debug|Win32 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Debug|Win32.Build.0 = Debug|Win32 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Debug|x64.ActiveCfg = Debug|x64 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Debug|x64.Build.0 = Debug|x64 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Release|ORBIS.ActiveCfg = Release|ORBIS + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Release|ORBIS.Build.0 = Release|ORBIS + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Release|Win32.ActiveCfg = Release|Win32 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Release|Win32.Build.0 = Release|Win32 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Release|x64.ActiveCfg = Release|x64 + {31B96262-1C41-43B9-BA38-27AA385B05DB}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -341,5 +355,6 @@ Global {73F1A804-1116-46C3-922A-9C0ADEB33F52} = {4C4BD478-3767-4C27-BD91-DAAFE7CD03A2} {B6297FF2-63D0-41EE-BE13-EFF720C9B0FA} = {1AFD580B-98B8-4689-B661-38C41132C60E} {81C83B42-D00A-4FA3-9A3D-80F9D46524BF} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} + {31B96262-1C41-43B9-BA38-27AA385B05DB} = {E74CF679-CA2A-47E9-B1F4-3779D6AC6B04} EndGlobalSection EndGlobal diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index 653354c6..e9311ac3 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -37,18 +37,26 @@ public: //------------------------------------------------------------------------- static int AddFive(lua_State* l) { - LuaInt32 i; i.Get(l, 1); + LuaStack stack(l); + int32_t i = stack.GetVar(); + //LuaInt32 i; i.Get(l, 1); i += 5; - return i.Return(l); - } + //return i.Return(l); + return stack.SetVar(i).Return(); + } static int AddTenInstance(lua_State* l) { LuaStack stack(l); - LuaDemoObjectPtr obj; - LuaFloat f; - stack >> obj >> f; + DemoObject* obj = nullptr; // stack.GetPtr(); + float f = stack.GetVar(); + + //LuaDemoObjectPtr obj; + //LuaFloat f; + //stack >> obj >> f; f = obj->AddTenMethod(f); - return f.Return(l); + + //return f.Return(l); + return 0;// stack.SetVar(f).SetPtr().Return(); } float AddTenMethod(float f) { diff --git a/doc/tutorial/imgui.ini b/doc/tutorial/imgui.ini index c39d8823..f11877b3 100644 --- a/doc/tutorial/imgui.ini +++ b/doc/tutorial/imgui.ini @@ -4,12 +4,12 @@ Size=400,400 Collapsed=0 [testature] -Pos=266,15 +Pos=187,98 Size=494,359 Collapsed=0 [SO FUN !!] -Pos=30,24 +Pos=16,24 Size=113,99 Collapsed=0 diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index 54cc511b..aaf6cb84 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -883,6 +883,24 @@ public: return lua_gettop(m_state); } + //------------------------------------------------------------------------- + template + T GetVar(T defaultValue = 0, bool isOptional = false) + { + return Var(defaultValue, m_state, m_index, isOptional).GetValue(); + } + + //------------------------------------------------------------------------- + template + Stack& SetVar(T& value) + { + m_result += Var(value, m_state, m_index).Return(m_state); + return *this; + } + + //------------------------------------------------------------------------- + int32_t Return() { return m_result; } + //------------------------------------------------------------------------- template Stack& operator>>(T& var) @@ -891,42 +909,6 @@ public: return *this; } - /* - template - Stack& operator>>(Var& var) - { - var = Var(var.GetValue(), m_state, m_index, var.IsOptional()); - return *this; - } - template - Stack& operator>>(Var& var) - { - var = Var(var.GetValue(), m_state, m_index, var.IsOptional()); - return *this; - } - template - Stack& operator>>(VarPtr& var) - { - var = VarPtr(m_state, m_index); - return *this; - } - */ - /* - template - Stack& operator>>(T& var) - { - Var ret(m_state, m_index); - var = ret.GetValue(); - return *this; - } - template - Stack& operator>>(VarPtrLight& var) - { - var = VarPtrLight(m_state, m_index); - return *this; - } - */ - //------------------------------------------------------------------------- template Stack& operator<<(T& var) @@ -934,27 +916,6 @@ public: m_result += var.Return(m_state); return *this; } - /* - template - Stack& operator<<(T& var) - { - Var ret(var, false); - m_result += ret.Return(m_state); - return *this; - } - template - Stack& operator<<(VarPtr& var) - { - m_result += var.Return(m_state); - return *this; - } - template - Stack& operator<<(VarPtrLight& var) - { - m_result += var.Return(m_state); - return *this; - } - */ private: lua_State* m_state = nullptr; From 4d78d62a70e78e3db6c4ea2a5a87ef2479b459ab Mon Sep 17 00:00:00 2001 From: touky Date: Sat, 29 Oct 2016 23:33:08 -0400 Subject: [PATCH 02/21] Lolua refactor 2nd pass: New version is much simpler and practical Next pass is clean-up and fix of all the old usage --- doc/samples/meshviewer/scenesetup.cpp | 6 +- doc/samples/meshviewer/scenesetup.h | 2 +- doc/tutorial/14_lol_lua.cpp | 80 ++-- src/easymesh/easymeshlua.cpp | 6 +- src/easymesh/easymeshlua.h | 2 +- src/lolimgui.cpp | 2 +- src/lolua/baselua.h | 501 +++++++++++++++++++++----- src/utils.h | 10 +- 8 files changed, 465 insertions(+), 144 deletions(-) diff --git a/doc/samples/meshviewer/scenesetup.cpp b/doc/samples/meshviewer/scenesetup.cpp index 46806349..74e942dd 100644 --- a/doc/samples/meshviewer/scenesetup.cpp +++ b/doc/samples/meshviewer/scenesetup.cpp @@ -242,10 +242,10 @@ int SceneSetupLuaObject::Toggle(lua_State* l) } //----------------------------------------------------------------------------- -const LuaObjectLib* SceneSetupLuaObject::GetLib() +const LuaObjectLibrary* SceneSetupLuaObject::GetLib() { typedef SceneSetupLuaObject SSLO; - static const LuaObjectLib lib = LuaObjectLib( + static const LuaObjectLibrary lib = LuaObjectLibrary( "SceneSetup", //Statics { { nullptr, nullptr } }, @@ -273,7 +273,7 @@ SceneSetupLuaLoader::SceneSetupLuaLoader() : LuaLoader() { lua_State* l = GetLuaState(); - LuaObjectDef::Register(l); + LuaObjectHelper::Register(l); } //----------------------------------------------------------------------------- diff --git a/doc/samples/meshviewer/scenesetup.h b/doc/samples/meshviewer/scenesetup.h index 9b6ffc29..39165715 100644 --- a/doc/samples/meshviewer/scenesetup.h +++ b/doc/samples/meshviewer/scenesetup.h @@ -116,7 +116,7 @@ public: //------------------------------------------------------------------------- static SceneSetupLuaObject* New(lua_State* l, int arg_nb); - static const LuaObjectLib* GetLib(); + static const LuaObjectLibrary* GetLib(); //------------------------------------------------------------------------- public: diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index e9311ac3..de5b5cd0 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -37,27 +37,25 @@ public: //------------------------------------------------------------------------- static int AddFive(lua_State* l) { - LuaStack stack(l); - int32_t i = stack.GetVar(); - //LuaInt32 i; i.Get(l, 1); + auto stack = LuaStack::Begin(l); + int32_t i = stack.Get(); + i += 5; - //return i.Return(l); - return stack.SetVar(i).Return(); - } + + return (stack << i).End(); + } + static int AddTenInstance(lua_State* l) { - LuaStack stack(l); - DemoObject* obj = nullptr; // stack.GetPtr(); - float f = stack.GetVar(); + auto stack = LuaStack::Begin(l); + DemoObject* obj = stack.GetPtr(); + float f = stack.Get(); - //LuaDemoObjectPtr obj; - //LuaFloat f; - //stack >> obj >> f; f = obj->AddTenMethod(f); - //return f.Return(l); - return 0;// stack.SetVar(f).SetPtr().Return(); + return (stack << f).End(); } + float AddTenMethod(float f) { return (f + 10); @@ -65,27 +63,30 @@ public: static int GetX(lua_State* l) { - LuaStack stack(l); - LuaDemoObjectPtr obj; - LuaInt32 i; - stack >> obj; + auto stack = LuaStack::Begin(l); + DemoObject* obj = stack.GetPtr(); + auto i = stack.Get(); + i = obj->m_x; - return stack << i; + + return (stack << i).End(); } + static int SetX(lua_State* l) { - LuaStack stack(l); - LuaDemoObjectPtr obj; - LuaInt32 i; - stack >> obj >> i; + auto stack = LuaStack::Begin(l); + DemoObject* obj = stack.GetPtr(); + auto i = stack.Get(); + obj->m_x = i; - return 0; + + return stack.End(); } //------------------------------------------------------------------------- - static const LuaObjectLib* GetLib() + static const LuaObjectLibrary* GetLib() { - static const LuaObjectLib lib = LuaObjectLib( + static const LuaObjectLibrary lib = LuaObjectLibrary( "LoluaDemo", { { "AddFive", &DemoObject::AddFive } }, { { "AddTenInstance", &DemoObject::AddTenInstance } }, @@ -99,9 +100,12 @@ public: //----------------------------------------------------------------------------- static int GlobalAddString(lua_State* l) { - LuaString s; s.Get(l, 1); - s() += "_added"; - return s.Return(l); + auto stack = LuaStack::Begin(l); + auto s = stack.Get(); + + s += "_added"; + + return (stack << s).End(); } //----------------------------------------------------------------------------- @@ -113,7 +117,7 @@ public: lua_State* l = GetLuaState(); //Registering demo object - LuaObjectDef::Register(l); + LuaObjectHelper::Register(l); //Registering function LuaFunction add_string(l, "GlobalAddString", &GlobalAddString); @@ -166,18 +170,18 @@ public: demo_loader->TestStuff(); //Grab global test values - float testvalue_num = demo_loader->GetVar("testvalue_num"); - int32_t testvalue_int = demo_loader->GetVar("testvalue_int"); - uint32_t testvalue_uint = demo_loader->GetVar("testvalue_uint"); - String testvalue_str = demo_loader->GetVar("testvalue_str"); + float testvalue_num = demo_loader->Get("testvalue_num"); + int32_t testvalue_int = demo_loader->Get("testvalue_int"); + uint32_t testvalue_uint = demo_loader->Get("testvalue_uint"); + String testvalue_str = demo_loader->Get("testvalue_str"); //Grab string modified with function - String function_return = demo_loader->GetVar("function_return"); + String function_return = demo_loader->Get("function_return"); //Grab global values modified with DemoObject - int32_t loluademo_return = demo_loader->GetVar("loluademo_return"); - int32_t loluademo_getx = demo_loader->GetVar("loluademo_getx"); - float loluademo_inst_return = demo_loader->GetVar("loluademo_inst_return"); + int32_t loluademo_return = demo_loader->Get("loluademo_return"); + int32_t loluademo_getx = demo_loader->Get("loluademo_getx"); + float loluademo_inst_return = demo_loader->Get("loluademo_inst_return"); DemoObject* loluademo_inst = demo_loader->GetPtr("loluademo_inst"); msg::info("Lua Vars: \ diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp index 258b3182..a68c6a7e 100644 --- a/src/easymesh/easymeshlua.cpp +++ b/src/easymesh/easymeshlua.cpp @@ -27,7 +27,7 @@ EasyMeshLuaLoader::EasyMeshLuaLoader() : LuaLoader() lua_State* l = GetLuaState(); //Registering demo object - LuaObjectDef::Register(l); + LuaObjectHelper::Register(l); } //----------------------------------------------------------------------------- @@ -88,10 +88,10 @@ EasyMeshLuaObject* EasyMeshLuaObject::New(lua_State* l, int arg_nb) } //----------------------------------------------------------------------------- -const LuaObjectLib* EasyMeshLuaObject::GetLib() +const LuaObjectLibrary* EasyMeshLuaObject::GetLib() { typedef EasyMeshLuaObject EMLO; - static const LuaObjectLib lib = LuaObjectLib( + static const LuaObjectLibrary lib = LuaObjectLibrary( "EasyMesh", //Statics { { nullptr, nullptr } }, diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h index 3632e2d4..35e76140 100644 --- a/src/easymesh/easymeshlua.h +++ b/src/easymesh/easymeshlua.h @@ -28,7 +28,7 @@ public: //------------------------------------------------------------------------- static EasyMeshLuaObject* New(lua_State* l, int arg_nb); - static const LuaObjectLib* GetLib(); + static const LuaObjectLibrary* GetLib(); //------------------------------------------------------------------------- static int AppendCylinder(lua_State* l) diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index ab47cc89..70a612b1 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -375,7 +375,7 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) m_vdecl->SetStream(vbo, m_attribs[0], m_attribs[1], m_attribs[2]); const ImDrawIdx* idx_buffer_offset = 0; - for (size_t cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[(int)cmd_i]; #ifdef SHOW_IMGUI_DEBUG diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index aaf6cb84..035a641b 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -16,6 +16,9 @@ #pragma once +#define OLD_VAR_SYSTEM true +#define NEW_VAR_SYSTEM true + namespace lol { @@ -33,69 +36,70 @@ typedef struct ClassVar } ClassVar; //----------------------------------------------------------------------------- -struct ObjectLib +class Object { - typedef struct ClassVarStr +public: + //----------------------------------------------------------------------------- + struct Library { - ClassVarStr() { } - ClassVarStr(String var_name, lua_CFunction get, lua_CFunction set) + typedef struct ClassVarStr { - m_get_name = String("Get") + var_name; - m_set_name = String("Set") + var_name; - m_get = get; - m_set = set; - } - String m_get_name = ""; - String m_set_name = ""; - lua_CFunction m_get = nullptr; - lua_CFunction m_set = nullptr; - } ClassVarStr; - - ObjectLib(String class_name, - array const& statics, - array const& methods, - array const& variables) - { - m_class_name = class_name; - m_static_name = class_name + "_lib"; - m_method_name = class_name + "_inst"; - - m_statics = statics; - if (m_statics.count() == 0 - || m_statics.last().name != nullptr - || m_statics.last().func != nullptr) - m_statics.push({ nullptr, nullptr }); - - m_methods = methods; - if (m_methods.count() == 0 - || m_methods.last().name != nullptr - || m_methods.last().func != nullptr) - m_methods.push({ nullptr, nullptr }); - - for (ClassVar const& cv : variables) + ClassVarStr() { } + ClassVarStr(String var_name, lua_CFunction get, lua_CFunction set) + { + m_get_name = String("Get") + var_name; + m_set_name = String("Set") + var_name; + m_get = get; + m_set = set; + } + String m_get_name = ""; + String m_set_name = ""; + lua_CFunction m_get = nullptr; + lua_CFunction m_set = nullptr; + } ClassVarStr; + + Library(String class_name, + array const& statics, + array const& methods, + array const& variables) { - if (cv.name && cv.get && cv.set) + m_class_name = class_name; + m_static_name = class_name + "_lib"; + m_method_name = class_name + "_inst"; + + m_statics = statics; + if (m_statics.count() == 0 + || m_statics.last().name != nullptr + || m_statics.last().func != nullptr) + m_statics.push({ nullptr, nullptr }); + + m_methods = methods; + if (m_methods.count() == 0 + || m_methods.last().name != nullptr + || m_methods.last().func != nullptr) + m_methods.push({ nullptr, nullptr }); + + for (ClassVar const& cv : variables) { - m_variables.push({ cv.name, cv.get, cv.set }); + if (cv.name && cv.get && cv.set) + { + m_variables.push({ cv.name, cv.get, cv.set }); + } } + if (m_variables.count() == 0 + || variables.last().name != nullptr + || variables.last().get != nullptr + || variables.last().set != nullptr) + m_variables.push(ClassVarStr()); } - if (m_variables.count() == 0 - || variables.last().name != nullptr - || variables.last().get != nullptr - || variables.last().set != nullptr) - m_variables.push(ClassVarStr()); - } - String m_class_name = ""; - String m_static_name = ""; - String m_method_name = ""; - array m_statics; - array m_methods; - array m_variables; -}; + String m_class_name = ""; + String m_static_name = ""; + String m_method_name = ""; + array m_statics; + array m_methods; + array m_variables; + }; -//----------------------------------------------------------------------------- -class Object -{ public: Object() { } virtual ~Object() { } @@ -106,19 +110,20 @@ public: ASSERT(false); return nullptr; } - static const ObjectLib* GetLib() { ASSERT(false); return nullptr; } + static const Library* GetLib() { ASSERT(false); return nullptr; } }; //----------------------------------------------------------------------------- // Class available to link C++ class to Lua methods //-- -class ObjectDef +class ObjectHelper { -public: +private: //------------------------------------------------------------------------- - ObjectDef() { } - virtual ~ObjectDef() { } + ObjectHelper() { } + virtual ~ObjectHelper() { } +public: //------------------------------------------------------------------------- template static void Register(lua_State *l) @@ -163,8 +168,8 @@ public: lua_setfield(l, -1, "__index"); //Create variables Get/Set - const array& variables = GetVariables(); - for (const ObjectLib::ClassVarStr& var : variables) + const array& variables = GetVariables(); + for (const Object::Library::ClassVarStr& var : variables) { if (!var.m_get || !var.m_set) continue; @@ -183,27 +188,29 @@ public: } private: + //------------------------------------------------------------------------- template - static const ObjectLib* GetLib() + static const Object::Library* GetLibrary() { - const ObjectLib* lib = TLuaClass::GetLib(); + const Object::Library* lib = TLuaClass::GetLib(); ASSERT(lib); return lib; } public: + //------------------------------------------------------------------------- template - static const char* GetObjectName() { return GetLib()->m_class_name.C(); } + static const char* GetObjectName() { return GetLibrary()->m_class_name.C(); } template - static const char* GetStaticName() { return GetLib()->m_static_name.C(); } + static const char* GetStaticName() { return GetLibrary()->m_static_name.C(); } template - static const char* GetMethodName() { return GetLib()->m_method_name.C(); } + static const char* GetMethodName() { return GetLibrary()->m_method_name.C(); } template - static const ClassMethod* GetStaticMethods() { return GetLib()->m_statics.data(); } + static const ClassMethod* GetStaticMethods() { return GetLibrary()->m_statics.data(); } template - static const ClassMethod* GetInstanceMethods() { return GetLib()->m_methods.data(); } + static const ClassMethod* GetInstanceMethods() { return GetLibrary()->m_methods.data(); } template - static const array& GetVariables() { return GetLib()->m_variables; } + static const array& GetVariables() { return GetLibrary()->m_variables; } protected: //------------------------------------------------------------------------- @@ -241,6 +248,9 @@ public: } }; +//----------------------------------------------------------------------------- +#if OLD_VAR_SYSTEM + //----------------------------------------------------------------------------- template class VarPtr @@ -311,7 +321,7 @@ protected: } virtual void InnerGet(lua_State* l, int& index) { - T** obj = static_cast(luaL_checkudata(l, index++, ObjectDef::GetMethodName())); + T** obj = static_cast(luaL_checkudata(l, index++, ObjectHelper::GetMethodName())); m_value = obj ? *obj : nullptr; } void InnerPush(lua_State* l) @@ -339,6 +349,7 @@ protected: } }; */ + //----------------------------------------------------------------------------- template class Var @@ -431,6 +442,7 @@ protected: }; //----------------------------------------------------------------------------- +#ifndef BASE_TYPES template<> inline bool Var::InnerIsValid(lua_State* l, int index) { return lua_isboolean(l, index); @@ -638,8 +650,10 @@ template<> inline int Var::InnerPush(lua_State* l) Var w = m_value.w; return (x.Return(l) + y.Return(l) + z.Return(l) + w.Return(l)); } +#endif //BASE_TYPES //----------------------------------------------------------------------------- +#ifndef CUSTOM_TYPES class VarColor { protected: @@ -866,42 +880,150 @@ protected: return s.Return(l); } }; +#endif //CUSTOM_TYPES + +#endif //OLD_VAR_SYSTEM +//----------------------------------------------------------------------------- +// Stack: Main class that encapsulates everything ----------------------------- //----------------------------------------------------------------------------- class Stack { public: + +#if NEW_VAR_SYSTEM + + //------------------------------------------------------------------------- + static Stack Begin(lua_State* state, int32_t start_index = 1) + { + return Stack(state, start_index); + } + + //------------------------------------------------------------------------- + int32_t End() + { + return m_result; + } + +#if !OLD_VAR_SYSTEM +protected: +#endif //!OLD_VAR_SYSTEM + +#endif //NEW_VAR_SYSTEM + Stack(lua_State* l, int32_t start_index = 1) { m_state = l; m_index = start_index; } virtual ~Stack() { } - inline operator int32_t() { return m_result; } + int32_t GetArgs() { return lua_gettop(m_state); } - //------------------------------------------------------------------------- - template - T GetVar(T defaultValue = 0, bool isOptional = false) - { - return Var(defaultValue, m_state, m_index, isOptional).GetValue(); - } +#if NEW_VAR_SYSTEM - //------------------------------------------------------------------------- - template - Stack& SetVar(T& value) - { - m_result += Var(value, m_state, m_index).Return(m_state); - return *this; - } +public: + //------------------------------------------------------------------------- + //The encapsulating struct for pointers + template + struct Ptr + { + public: + T* m_value = nullptr; + bool m_throw_error = false; + + //--------------------------------------------------------------------- + //m_value: Your stored value + //throw_error: If true, lua will throw an error if the value get fails + Ptr(T* value, bool throw_error = false) + { + m_value = value; + m_throw_error = throw_error; + } + Ptr(const T*& value) { m_value = value; } + inline operator T*() { return m_value; } + inline Ptr& operator=(T const*& value) { m_value = value; return *this; } + }; //------------------------------------------------------------------------- - int32_t Return() { return m_result; } + template T Get(bool isOptional = false) { return Get(InnerDefault(), isOptional); } + template SafeEnum GetEnum(bool isOptional = false) { return GetEnum(InnerDefaultSafeEnum(), isOptional); } + template Ptr

GetPtr(bool isOptional = false) { return GetPtr(InnerDefaultPtr

(), isOptional); } + + //------------------------------------------------------------------------- +#define DECLARE_STACK_GET(T0, T1, GET_NAME, INNER_IS_VALID, INNER_GET) \ + template \ + T1 GET_NAME(T1 value, bool isOptional = false) \ + { \ + bool is_nil = lua_isnil(m_state, m_index); \ + if (!isOptional || (!is_nil && INNER_IS_VALID())) \ + { \ + ASSERT(!is_nil); /* touky: should assert, though ? */ \ + return INNER_GET(value); \ + } \ + return value; \ + } + DECLARE_STACK_GET(T, T, Get, InnerIsValid, InnerGet); + DECLARE_STACK_GET(E, SafeEnum, GetEnum, InnerIsValidSafeEnum, InnerGetSafeEnum); + DECLARE_STACK_GET(P, Ptr

, GetPtr, InnerIsValidPtr, InnerGetPtr); + +#undef DECLARE_STACK_GET + + //------------------------------------------------------------------------- + template Stack& operator<<(T& value) { m_result += InnerPush(value); return *this; } + template Stack& operator<<(SafeEnum& value) { m_result += InnerPushSafeEnum(value); return *this; } + template Stack& operator<<(Ptr

& value) { m_result += InnerPushPtr

(value); return *this; } + +protected: + #define INNER_ERROR "Your type is not implemented. For pointers, use LuaPtr()" + template T InnerDefault() { return T(0); } + template bool InnerIsValid() { ASSERT(false, INNER_ERROR); return false; } + template T InnerGet(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return InnerDefault(); } + template int InnerPush(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return 0; } + +#ifndef INNER_T_T + //template class C> C InnerDefault() { return C(); } + //template bool InnerIsValid() { return InnerIsValid(); } + //template SafeEnum InnerGet(SafeEnum value) { return FindValue >(InnerGet(value.ToString().C())); } + //template int InnerPush(SafeEnum value) { return InnerPush(value.ToString.C()); } +#endif //STACK_STRING + +#ifndef INNER_SAFE_ENUM + template SafeEnum InnerDefaultSafeEnum() { return SafeEnum(); } + template bool InnerIsValidSafeEnum() { return InnerIsValid(); } + template SafeEnum InnerGetSafeEnum(SafeEnum value) { return FindValue >(InnerGet(value.ToString())); } + template int InnerPushSafeEnum(SafeEnum value) { return InnerPush(value.ToString()); } +#endif //STACK_STRING + +#ifndef INNER_PTR + template inline Ptr

InnerDefaultPtr() { return Ptr

(nullptr); } + template inline bool InnerIsValidPtr() { return !!lua_isuserdata(m_state, m_index); } + template inline Ptr

InnerGetPtr(Ptr

value) + { + P** obj = static_cast(value.m_throw_error + ? luaL_checkudata(m_state, m_index++, ObjectHelper::GetMethodName

()) + : luaL_testudata(m_state, m_index++, ObjectHelper::GetMethodName

()) ); + return Ptr

(obj ? *obj : value.m_value); + } + template inline int InnerPushPtr(Ptr

value) + { + P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); + *data = value.m_value; + } +#endif //STACK_STRING + +#endif //NEW_VAR_SYSTEM + + //------------------------------------------------------------------------- +#if OLD_VAR_SYSTEM +public: + inline operator int32_t() { return m_result; } //------------------------------------------------------------------------- + template Stack& operator>>(T& var) { @@ -909,6 +1031,8 @@ public: return *this; } +#if !NEW_VAR_SYSTEM + //------------------------------------------------------------------------- template Stack& operator<<(T& var) @@ -917,16 +1041,152 @@ public: return *this; } +#endif //NEW_VAR_SYSTEM + +#endif //OLD_VAR_SYSTEM + private: lua_State* m_state = nullptr; int32_t m_index = 1; int32_t m_result = 0; }; +//----------------------------------------------------------------------------- +#if NEW_VAR_SYSTEM + +#ifndef REGION_STACK_VAR + +//#if OLD_VAR_SYSTEM && NEW_VAR_SYSTEM +// +////------------------------------------------------------------------------- +//template +//Stack& Stack::operator<<(Var& var) +//{ +// m_result += var.Return(m_state); +// return *this; +//} +// +//#endif //OLD_VAR_SYSTEM && NEW_VAR_SYSTEM + +#ifndef STACK_BOOL +template<> inline bool Stack::InnerIsValid() { return lua_isboolean(m_state, m_index); } +template<> inline bool Stack::InnerGet(bool value) { UNUSED(value); return !!lua_toboolean(m_state, m_index++); } +template<> inline int Stack::InnerPush(bool value) { lua_pushboolean(m_state, value); return 1; } +#endif // STACK_BOOL + +//----------------------------------------------------------------------------- +#ifndef STACK_CHAR_CONST +template<> inline bool Stack::InnerIsValid() { return !!lua_isstring(m_state, m_index); } +template<> inline char const* Stack::InnerGet(char const* value) { UNUSED(value); return lua_tostring(m_state, m_index++); } +template<> inline int Stack::InnerPush(char const* value) { lua_pushstring(m_state, value); return 1; } +#endif // STACK_CHAR_CONST + +//----------------------------------------------------------------------------- +#ifndef STACK_STRING +template<> inline String Stack::InnerDefault() { return String(); } +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline String Stack::InnerGet(String value) { return String(InnerGet(value.C())); } +template<> inline int Stack::InnerPush(String value) { return InnerPush(value.C()); } +#endif //STACK_STRING + +//----------------------------------------------------------------------------- +#ifndef STACK_STRING +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline double Stack::InnerGet(double value) { UNUSED(value); return lua_tonumber(m_state, m_index++); } +template<> inline int Stack::InnerPush(double value) { lua_pushnumber(m_state, value); return 1; } +#endif //STACK_STRING + +//----------------------------------------------------------------------------- +#ifndef STACK_FLOAT +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline float Stack::InnerGet(float value) { return (float)InnerGet((double)value); } +template<> inline int Stack::InnerPush(float value) { return InnerPush((double)value); } +#endif //STACK_FLOAT + +//----------------------------------------------------------------------------- +//DO NOT REMOVE: IT IS THE BASIS FOR INT 32 +#ifndef STACK_INT64 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline int64_t Stack::InnerGet(int64_t value) { UNUSED(value); return lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(int64_t value) { lua_pushinteger(m_state, value); return 1; } +#endif //STACK_INT64 + +//----------------------------------------------------------------------------- +#ifndef STACK_UINT64 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline uint64_t Stack::InnerGet(uint64_t value) { UNUSED(value); return (uint64_t)lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(uint64_t value) { lua_pushinteger(m_state, (lua_Unsigned)value); return 1; } +#endif //STACK_UINT64 + +//----------------------------------------------------------------------------- +#ifndef STACK_INT32 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline int32_t Stack::InnerGet(int32_t value) { UNUSED(value); return (int32_t)lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(int32_t value) { lua_pushinteger(m_state, (lua_Integer)value); return 1; } +#endif STACK_INT32 + +//----------------------------------------------------------------------------- +#ifndef STACK_UINT32 +template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } +template<> inline uint32_t Stack::InnerGet(uint32_t value) { UNUSED(value); return (uint32_t)(lua_Unsigned)lua_tointeger(m_state, m_index++); } +template<> inline int Stack::InnerPush(uint32_t value) { lua_pushinteger(m_state, (lua_Unsigned)value); return 1; } +#endif //STACK_UINT32 + +//----------------------------------------------------------------------------- +#ifndef STACK_VEC2 +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline vec2 Stack::InnerGet(vec2 value) { return vec2(InnerGet(value.x), Get(value.y, true)); } +template<> inline int Stack::InnerPush(vec2 value) { return (InnerPush(value.x) + InnerPush(value.y)); } +#endif //STACK_VEC2 + +//----------------------------------------------------------------------------- +#ifndef STACK_VEC3 +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline vec3 Stack::InnerGet(vec3 value) { return vec3(InnerGet(value.x), Get(value.y, true), Get(value.z, true)); } +template<> inline int Stack::InnerPush(vec3 value) { return (InnerPush(value.x) + InnerPush(value.y) + InnerPush(value.z)); } +#endif //STACK_VEC3 + +//----------------------------------------------------------------------------- +#ifndef STACK_VEC4 +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline vec4 Stack::InnerGet(vec4 value) { return vec4(InnerGet(value.x), Get(value.y, true), Get(value.z, true), Get(value.w, true)); } +template<> inline int Stack::InnerPush(vec4 value) { return (InnerPush(value.x) + InnerPush(value.y) + InnerPush(value.z) + InnerPush(value.w)); } +#endif STACK_VEC4 + +//----------------------------------------------------------------------------- +#ifndef STACK_SAFE_ENUM +//template inline SafeEnum Stack::InnerDefault() { return SafeEnum(); } +//template inline bool Stack::InnerIsValid >() { return InnerIsValid(); } +//template inline SafeEnum Stack::InnerGet >(SafeEnum value) { return FindValue >(InnerGet(value.ToString().C())); } +//template inline int Stack::InnerPush >(SafeEnum value) { return InnerPush(value.ToString.C()); } +#endif //STACK_STRING + +//----------------------------------------------------------------------------- +#ifndef STACK_PTR +//template inline Stack::Ptr

Stack::InnerDefault >() { return Stack::Ptr

(nullptr); } +//template inline bool Stack::InnerIsValid >() { return !!lua_isuserdata(m_state, m_index); } +//template inline Stack::Ptr

Stack::InnerGet >(Stack::Ptr

value) +//{ +// P** obj = static_cast(value.m_throw_error +// ? luaL_checkudata(m_state, m_index++, ObjectHelper::GetMethodName

()) +// : luaL_testudata(m_state, m_index++, ObjectHelper::GetMethodName

()) ); +// return Stack::Ptr

(obj ? *obj : value.m_value); +//} +//template inline int Stack::InnerPush >(Stack::Ptr

value) +//{ +// P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); +// *data = value.m_value; +//} +#endif //STACK_STRING + +#endif //REGION_STACK_VAR + +#endif //NEW_VAR_SYSTEM + //----------------------------------------------------------------------------- class Loader { - friend class ObjectDef; + friend class ObjectHelper; public: Loader(); virtual ~Loader(); @@ -934,6 +1194,27 @@ public: bool ExecLuaFile(String const &lua); bool ExecLuaCode(String const &lua); +#if NEW_VAR_SYSTEM + //------------------------------------------------------------------------- +#define DECLARE_LOADER_GET(T0, T1, GET_NAME) \ + template \ + T1 GET_NAME(String const &name) \ + { \ + lua_getglobal(m_lua_state, name.C()); \ + auto stack = LuaStack::Begin(m_lua_state, -1); \ + auto result = stack.GET_NAME(); \ + lua_pop(m_lua_state, 1); \ + return result; \ + } + + DECLARE_LOADER_GET(T, T, Get); + DECLARE_LOADER_GET(E, SafeEnum, GetEnum); + DECLARE_LOADER_GET(P, P*, GetPtr); + +#undef DECLARE_LOADER_GET + +#else //OLD_VAR_SYSTEM + template T GetVar(String const &name) { @@ -951,6 +1232,8 @@ public: return var(); } +#endif //OLD_VAR_SYSTEM + protected: lua_State* GetLuaState(); static void Store(lua_State* l, Loader* loader); @@ -964,10 +1247,33 @@ private: }; //----------------------------------------------------------------------------- -// ObjectDef member implementations that require VarPtr +// ObjectHelper member implementations that require VarPtr +#if NEW_VAR_SYSTEM template -int ObjectDef::Store(lua_State * l) +int ObjectHelper::Store(lua_State * l) +{ + auto stack = LuaStack::Begin(l); + auto obj = stack.Get >(); + ASSERT(obj); + Loader::StoreObject(l, obj); + return 0; +} + +template +int ObjectHelper::Del(lua_State * l) +{ + auto stack = LuaStack::Begin(l); + auto obj = stack.Get >(); + ASSERT(obj); + delete obj; + return 0; +} + +#else //OLD_VAR_SYSTEM + +template +int ObjectHelper::Store(lua_State * l) { VarPtr obj; obj.Get(l, 1); @@ -977,7 +1283,7 @@ int ObjectDef::Store(lua_State * l) } template -int ObjectDef::Del(lua_State * l) +int ObjectHelper::Del(lua_State * l) { VarPtr obj; obj.Get(l, 1); @@ -986,13 +1292,22 @@ int ObjectDef::Del(lua_State * l) return 0; } +#endif //OLD_VAR_SYSTEM + + } /* namespace Lolua */ +//TYPEDEFS typedef Lolua::Function LuaFunction; -typedef Lolua::ObjectDef LuaObjectDef; +typedef Lolua::ObjectHelper LuaObjectHelper; typedef Lolua::Object LuaObject; -typedef Lolua::ObjectLib LuaObjectLib; +typedef Lolua::Object::Library LuaObjectLibrary; typedef Lolua::Loader LuaLoader; +typedef Lolua::Stack LuaStack; +template using LuaPtr = Lolua::Stack::Ptr

; +//template +//typedef Lolua::Stack::Ptr

LuaPtr

; +#if OLD_VAR_SYSTEM typedef Lolua::Var LuaBool; typedef Lolua::Var LuaCharPtr; typedef Lolua::Var LuaString; @@ -1006,6 +1321,6 @@ typedef Lolua::Var LuaVec2; typedef Lolua::Var LuaVec3; typedef Lolua::Var LuaVec4; typedef Lolua::VarColor LuaColor; -typedef Lolua::Stack LuaStack; +#endif //OLD_VAR_SYSTEM } /* namespace lol */ diff --git a/src/utils.h b/src/utils.h index 3faf9d84..5929394e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -61,6 +61,12 @@ template< class T > inline int GetRandom(array src) // Gets the value for the given enum type. template inline T FindValue(const char* name) +{ + auto str = String(name); + return FindValue(str); +} + +template inline T FindValue(String const& name) { String n = name; n.to_lower(); @@ -72,10 +78,6 @@ template inline T FindValue(const char* name) } return T::Max; } -template inline T FindValue(String const& name) -{ - return FindValue(name.C()); -} } /* namespace lol */ From 0cdeb424d95d4608817b03424f9596d195d5d57d Mon Sep 17 00:00:00 2001 From: touky Date: Sun, 30 Oct 2016 19:56:23 -0400 Subject: [PATCH 03/21] 2nd pass, removed all old code, and fixed all the wrong usages --- doc/samples/meshviewer/scenesetup.cpp | 82 ++- doc/samples/meshviewer/scenesetup.h | 2 - doc/tutorial/14_lol_lua.cpp | 1 - src/easymesh/easymeshlua.cpp | 7 +- src/easymesh/easymeshlua.h | 642 ++++++++++---------- src/lolua/baselua.cpp | 14 +- src/lolua/baselua.h | 844 ++------------------------ 7 files changed, 394 insertions(+), 1198 deletions(-) diff --git a/doc/samples/meshviewer/scenesetup.cpp b/doc/samples/meshviewer/scenesetup.cpp index 74e942dd..a86ff561 100644 --- a/doc/samples/meshviewer/scenesetup.cpp +++ b/doc/samples/meshviewer/scenesetup.cpp @@ -159,86 +159,76 @@ SceneSetupLuaObject::~SceneSetupLuaObject() //----------------------------------------------------------------------------- SceneSetupLuaObject* SceneSetupLuaObject::New(lua_State* l, int arg_nb) { - UNUSED(l); UNUSED(arg_nb); - LuaStack s(l); - LuaString n; - s >> n; - return new SceneSetupLuaObject(n()); + auto s = LuaStack::Begin(l); + auto n = s.Get(); + return new SceneSetupLuaObject(n); } //-- Setup command ------------------------------------------------------------ int SceneSetupLuaObject::AddLight(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaString t; - s >> o >> t; - o->m_setup->AddLight(FindValue(t().C())); - return 0; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto t = s.Get(); + o->m_setup->AddLight(FindValue(t.C())); + return s.End(); } int SceneSetupLuaObject::SetupScene(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - s >> o; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); o->m_setup->SetupScene(); - return 0; + return s.End(); } //-- main funcs --------------------------------------------------------------- int SceneSetupLuaObject::SetPosition(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaVec3 c; - s >> o >> c; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto c = s.Get(); o->m_setup->SetPosition(c); - return 0; + return s.End(); } int SceneSetupLuaObject::SetLookAt(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaVec3 c; - s >> o >> c; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto c = s.Get(); o->m_setup->SetLookAt(c); - return 0; + return s.End(); } int SceneSetupLuaObject::SetColor(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaColor c; - s >> o >> c; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto c = s.Get(); o->m_setup->SetColor(c); - return 0; + return s.End(); } int SceneSetupLuaObject::Show(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaDisplay e; - s >> o >> e; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto e = s.GetEnum(); o->m_setup->Show(e); - return 0; + return s.End(); } int SceneSetupLuaObject::Hide(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaDisplay e; - s >> o >> e; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto e = s.GetEnum(); o->m_setup->Hide(e); - return 0; + return s.End(); } int SceneSetupLuaObject::Toggle(lua_State* l) { - LuaStack s(l); - LuaSSetupPtr o; - LuaDisplay e; - s >> o >> e; + auto s = LuaStack::Begin(l); + auto o = s.GetPtr(); + auto e = s.GetEnum(); o->m_setup->Toggle(e); - return 0; + return s.End(); } //----------------------------------------------------------------------------- @@ -264,7 +254,7 @@ const LuaObjectLibrary* SceneSetupLuaObject::GetLib() }, //Variables { { nullptr, nullptr, nullptr } }); - return &lib; + return &lib; } //----------------------------------------------------------------------------- diff --git a/doc/samples/meshviewer/scenesetup.h b/doc/samples/meshviewer/scenesetup.h index 39165715..23694a7b 100644 --- a/doc/samples/meshviewer/scenesetup.h +++ b/doc/samples/meshviewer/scenesetup.h @@ -103,12 +103,10 @@ public: bool m_show_gizmo; bool m_show_lights; }; -typedef Lolua::VarEnum LuaDisplay; //----------------------------------------------------------------------------- class SceneSetupLuaObject : public LuaObject { - typedef Lolua::VarPtr LuaSSetupPtr; public: //------------------------------------------------------------------------- SceneSetupLuaObject(String& name); diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index de5b5cd0..ce0348ab 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -23,7 +23,6 @@ using namespace lol; //----------------------------------------------------------------------------- class DemoObject : public LuaObject { - typedef Lolua::VarPtr LuaDemoObjectPtr; public: DemoObject() : LuaObject() {} virtual ~DemoObject() {} diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp index a68c6a7e..a36ab4d8 100644 --- a/src/easymesh/easymeshlua.cpp +++ b/src/easymesh/easymeshlua.cpp @@ -81,10 +81,9 @@ EasyMeshLuaObject* EasyMeshLuaObject::New(lua_State* l, int arg_nb) { UNUSED(l); UNUSED(arg_nb); - LuaStack s(l); - LuaString n("", true); - s >> n; - return new EasyMeshLuaObject(n()); + LuaStack s = LuaStack::Begin(l); + String str = s.Get("", true); + return new EasyMeshLuaObject(str); } //----------------------------------------------------------------------------- diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h index 35e76140..5fb32ea8 100644 --- a/src/easymesh/easymeshlua.h +++ b/src/easymesh/easymeshlua.h @@ -18,7 +18,6 @@ namespace lol //----------------------------------------------------------------------------- class EasyMeshLuaObject : public LuaObject { - typedef Lolua::VarPtr EzMeshPtr; EasyMesh m_instance; public: //------------------------------------------------------------------------- @@ -33,604 +32,583 @@ public: //------------------------------------------------------------------------- static int AppendCylinder(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 nsides; - LuaFloat h, d1, d2; - LuaBool dualside(false, true), smooth(false, true), close(false, true); - s >> m >> nsides >> h >> d1 >> d2 >> dualside >> smooth >> close; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto nsides = s.Get(); + auto h = s.Get(); + auto d1 = s.Get(); + auto d2 = s.Get(); + auto dualside = s.Get(false, true); + auto smooth = s.Get(false, true); + auto close = s.Get(false, true); m->m_instance.AppendCylinder(nsides, h, d1, d2, dualside, smooth, close); - return 0; + return s.End(); } static int AppendSphere(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 ndivisions; - LuaFloat d; - s >> m >> ndivisions >> d; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto ndivisions = s.Get(); + auto d = s.Get(); m->m_instance.AppendSphere(ndivisions, d); - return 0; + return s.End(); } static int AppendCapsule(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 ndivisions; - LuaFloat h, d; - s >> m >> ndivisions >> h >> d; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto ndivisions = s.Get(); + auto h = s.Get(); + auto d = s.Get(); m->m_instance.AppendCapsule(ndivisions, h, d); - return 0; + return s.End(); } static int AppendTorus(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 ndivisions; - LuaFloat d1, d2; - s >> m >> ndivisions >> d1 >> d2; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto ndivisions = s.Get(); + auto d1 = s.Get(); + auto d2 = s.Get(); m->m_instance.AppendTorus(ndivisions, d1, d2); - return 0; + return s.End(); } static int AppendBox(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaVec3 size; - LuaFloat chamf(0.f, true); - LuaBool smooth(false, true); - s >> m >> size >> chamf >> smooth; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto size = s.Get(); + auto chamf = s.Get(0.f, true); + auto smooth = s.Get(false, true); m->m_instance.AppendBox(size, chamf, smooth); - return 0; + return s.End(); } static int AppendStar(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 nbranches; - LuaFloat d1, d2; - LuaBool fade(false, true), fade2(false, true); - s >> m >> nbranches >> d1 >> d2 >> fade >> fade2; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto nbranches = s.Get(); + auto d1 = s.Get(); + auto d2 = s.Get(); + auto fade = s.Get(false, true); + auto fade2 = s.Get(false, true); m->m_instance.AppendStar(nbranches, d1, d2, fade, fade2); - return 0; + return s.End(); } static int AppendExpandedStar(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 nbranches; - LuaFloat d1, d2, extrad(0.f, true); - s >> m >> nbranches >> d1 >> d2 >> extrad; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto nbranches = s.Get(); + auto d1 = s.Get(); + auto d2 = s.Get(); + auto extrad = s.Get(0.f, true); m->m_instance.AppendExpandedStar(nbranches, d1, d2, extrad); - return 0; + return s.End(); } static int AppendDisc(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 nsides; - LuaFloat d; - LuaBool fade(false, true); - s >> m >> nsides >> d >> fade; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto nsides = s.Get(); + auto d = s.Get(); + auto fade = s.Get(false, true); m->m_instance.AppendDisc(nsides, d, fade); - return 0; + return s.End(); } static int AppendSimpleTriangle(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat d; - LuaBool fade(false, true); - s >> m >> d >> fade; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto d = s.Get(); + auto fade = s.Get(false, true); m->m_instance.AppendSimpleTriangle(d, fade); - return 0; + return s.End(); } static int AppendSimpleQuad(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat size; - LuaBool fade(false, true); - s >> m >> size >> fade; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto size = s.Get(); + auto fade = s.Get(false, true); m->m_instance.AppendSimpleQuad(size, fade); - return 0; + return s.End(); } static int AppendCog(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 nbsides; - LuaFloat h, sidemul(0.f, true); - LuaVec2 d0, d1, d2; - LuaBool offset(false, true); - s >> m >> nbsides >> h >> d0 >> d1 >> d2 >> sidemul >> offset; - m->m_instance.AppendCog(nbsides, h, d0().x, d0().y, d1().x, d1().y, d2().x, d2().y, sidemul, offset); - return 0; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto nbsides = s.Get(); + auto h = s.Get(); + auto sidemul = s.Get(0.f, true); + auto d0 = s.Get(); + auto d1 = s.Get(); + auto d2 = s.Get(); + auto offset = s.Get(false, true); + m->m_instance.AppendCog(nbsides, h, d0.x, d0.y, d1.x, d1.y, d2.x, d2.y, sidemul, offset); + return s.End(); } //------------------------------------------------------------------------- static int TranslateX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat f; - s >> m >> f; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto f = s.Get(); m->m_instance.TranslateX(f); - return 0; + return s.End(); } static int TranslateY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat f; - s >> m >> f; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto f = s.Get(); m->m_instance.TranslateY(f); - return 0; + return s.End(); } static int TranslateZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat f; - s >> m >> f; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto f = s.Get(); m->m_instance.TranslateZ(f); - return 0; + return s.End(); } static int Translate(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaVec3 v; - s >> m >> v; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto v = s.Get(); m->m_instance.Translate(v); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int RotateX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat a; - s >> m >> a; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto a = s.Get(); m->m_instance.RotateX(a); - return 0; + return s.End(); } static int RotateY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat a; - s >> m >> a; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto a = s.Get(); m->m_instance.RotateY(a); - return 0; + return s.End(); } static int RotateZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat a; - s >> m >> a; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto a = s.Get(); m->m_instance.RotateZ(a); - return 0; + return s.End(); } static int Rotate(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat a; - LuaVec3 v; - s >> m >> a >> v; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto a = s.Get(); + auto v = s.Get(); m->m_instance.Rotate(a, v); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int ScaleX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x; - s >> m >> x; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); m->m_instance.ScaleX(x); - return 0; + return s.End(); } static int ScaleY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat y; - s >> m >> y; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto y = s.Get(); m->m_instance.ScaleY(y); - return 0; + return s.End(); } static int ScaleZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat z; - s >> m >> z; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto z = s.Get(); m->m_instance.ScaleZ(z); - return 0; + return s.End(); } static int Scale(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaVec3 v; - s >> m >> v; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto v = s.Get(); m->m_instance.Scale(v); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int RadialJitter(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat f; - s >> m >> f; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto f = s.Get(); m->m_instance.RadialJitter(f); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int TaperX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat y, z, xoff(0.f, true); - LuaBool abs(true, true); - s >> m >> y >> z >> xoff >> abs; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto y = s.Get(); + auto z = s.Get(); + auto xoff = s.Get(0.f, true); + auto abs = s.Get(true, true); m->m_instance.TaperX(y, z, xoff, abs); - return 0; + return s.End(); } static int TaperY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x, z, yoff(0.f, true); - LuaBool abs(true, true); - s >> m >> x >> z >> yoff >> abs; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); + auto z = s.Get(); + auto yoff = s.Get(0.f, true); + auto abs = s.Get(true, true); m->m_instance.TaperY(x, z, yoff, abs); - return 0; + return s.End(); } static int TaperZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x, y, zoff(0.f, true); - LuaBool abs(true, true); - s >> m >> x >> y >> zoff >> abs; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); + auto y = s.Get(); + auto zoff = s.Get(0.f, true); + auto abs = s.Get(true, true); m->m_instance.TaperZ(x, y, zoff, abs); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int TwistX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.TwistX(t, toff); - return 0; + return s.End(); } static int TwistY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.TwistY(t, toff); - return 0; + return s.End(); } static int TwistZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.TwistZ(t, toff); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int ShearX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat y, z, xoff(0.f, true); - LuaBool abs(true, true); - s >> m >> y >> z >> xoff >> abs; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto y = s.Get(); + auto z = s.Get(); + auto xoff = s.Get(0.f, true); + auto abs = s.Get(true, true); m->m_instance.ShearX(y, z, xoff, abs); - return 0; + return s.End(); } static int ShearY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x, z, yoff(0.f, true); - LuaBool abs(true, true); - s >> m >> x >> z >> yoff >> abs; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); + auto z = s.Get(); + auto yoff = s.Get(0.f, true); + auto abs = s.Get(true, true); m->m_instance.ShearY(x, z, yoff, abs); - return 0; + return s.End(); } static int ShearZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x, y, zoff(0.f, true); - LuaBool abs(true, true); - s >> m >> x >> y >> zoff >> abs; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); + auto y = s.Get(); + auto zoff = s.Get(0.f, true); + auto abs = s.Get(true, true); m->m_instance.ShearZ(x, y, zoff, abs); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int StretchX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat y, z, xoff(0.f, true); - s >> m >> y >> z >> xoff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto y = s.Get(); + auto z = s.Get(); + auto xoff = s.Get(0.f, true); m->m_instance.StretchX(y, z, xoff); - return 0; + return s.End(); } static int StretchY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x, z, yoff(0.f, true); - s >> m >> x >> z >> yoff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); + auto z = s.Get(); + auto yoff = s.Get(0.f, true); m->m_instance.StretchY(x, z, yoff); - return 0; + return s.End(); } static int StretchZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat x, y, zoff(0.f, true); - s >> m >> x >> y >> zoff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto x = s.Get(); + auto y = s.Get(); + auto zoff = s.Get(0.f, true); m->m_instance.StretchZ(x, y, zoff); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int BendXY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.BendXY(t, toff); - return 0; + return s.End(); } static int BendXZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.BendXZ(t, toff); - return 0; + return s.End(); } static int BendYX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.BendYX(t, toff); - return 0; + return s.End(); } static int BendYZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.BendYZ(t, toff); - return 0; + return s.End(); } static int BendZX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.BendZX(t, toff); - return 0; + return s.End(); } static int BendZY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat t, toff(0.f, true); - s >> m >> t >> toff; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto t = s.Get(); + auto toff = s.Get(0.f, true); m->m_instance.BendZY(t, toff); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int MirrorX(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.MirrorX(); - return 0; + return s.End(); } static int MirrorY(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.MirrorY(); - return 0; + return s.End(); } static int MirrorZ(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.MirrorZ(); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int LoopStart(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 loopnb; - s >> m >> loopnb; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto loopnb = s.Get(); m->m_instance.LoopStart(loopnb); - return 0; + return s.End(); } static int LoopEnd(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.LoopEnd(); - return 0; + return s.End(); } static int OpenBrace(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.OpenBrace(); - return 0; + return s.End(); } static int CloseBrace(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.CloseBrace(); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int ToggleScaleWinding(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.ToggleScaleWinding(); - return 0; + return s.End(); } static int ToggleQuadWeighting(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.ToggleQuadWeighting(); - return 0; + return s.End(); } static int TogglePostBuildNormal(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.TogglePostBuildNormal(); - return 0; + return s.End(); } static int ToggleVerticeNoCleanup(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.ToggleVerticeNoCleanup(); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int VerticesMerge(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.VerticesMerge(); - return 0; + return s.End(); } static int VerticesSeparate(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.VerticesSeparate(); - return 0; + return s.End(); } static int VerticesCleanup(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - s >> m; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); m->m_instance.VerticesCleanup(); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int Duplicate(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaVec3 ds(vec3(1.f)); - s >> m >> ds; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto ds = s.Get(vec3(1.f)); m->m_instance.DupAndScale(ds, true); - return 0; + return s.End(); } static int Smooth(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 pass, split_per_pass, smooth_per_pass; - s >> m >> pass >> split_per_pass >> smooth_per_pass; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto pass = s.Get(); + auto split_per_pass = s.Get(); + auto smooth_per_pass = s.Get(); m->m_instance.SmoothMesh(pass, split_per_pass, smooth_per_pass); - return 0; + return s.End(); } static int SplitTriangles(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaInt32 pass; - s >> m >> pass; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto pass = s.Get(); m->m_instance.SplitTriangles(pass); - return 0; + return s.End(); } static int Chamfer(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaFloat f; - s >> m >> f; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto f = s.Get(); m->m_instance.Chamfer(f); - return 0; + return s.End(); } //------------------------------------------------------------------------- static int SetCurColor(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaColor c; - s >> m >> c; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto c = s.Get(); m->m_instance.SetCurColor(c); - return 0; + return s.End(); } static int SetCurColorA(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaColor c; - s >> m >> c; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto c = s.Get(); m->m_instance.SetCurColorA(c); - return 0; + return s.End(); } static int SetCurColorB(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaColor c; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto c = s.Get(); m->m_instance.SetCurColorB(c); - return 0; + return s.End(); } static int SetVertColor(lua_State* l) { - LuaStack s(l); - EzMeshPtr m; - LuaColor c; - s >> m >> c; + auto s = LuaStack::Begin(l); + auto m = s.GetPtr(); + auto c = s.Get(); m->m_instance.SetVertColor(c); - return 0; + return s.End(); } /* (csgu|csgunion) { return token::T_CSGUNION; } diff --git a/src/lolua/baselua.cpp b/src/lolua/baselua.cpp index 8a1c035d..faffef0f 100644 --- a/src/lolua/baselua.cpp +++ b/src/lolua/baselua.cpp @@ -40,8 +40,9 @@ class LuaBaseData int status = luaL_dostring(l, s.C()); if (status == 1) { - LuaString error; error.Get(l, -1); - msg::error("Lua error %s\n", error().C()); + auto stack = LuaStack::Begin(l, -1); + auto error = stack.Get(); + msg::error("Lua error %s\n", error.C()); lua_pop(l, 1); } return status; @@ -53,8 +54,8 @@ class LuaBaseData if (lua_isnoneornil(l, 1)) return LUA_ERRFILE; - LuaCharPtr var; var.Get(l, 1); - char const *filename = var;// lua_tostring(l, 1); + auto stack = LuaStack::Begin(l); + char const *filename = stack.Get(); int status = LUA_ERRFILE; File f; @@ -76,8 +77,9 @@ class LuaBaseData msg::error("could not find Lua file %s\n", filename); else if (status == 1) { - LuaString error; error.Get(l, -1); - msg::error("Lua error %s\n", error().C()); + stack.SetIndex(-1); + auto error = stack.Get(); + msg::error("Lua error %s\n", error.C()); lua_pop(l, 1); } diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index 035a641b..c4316558 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -16,9 +16,6 @@ #pragma once -#define OLD_VAR_SYSTEM true -#define NEW_VAR_SYSTEM true - namespace lol { @@ -134,20 +131,19 @@ public: { "New", New }, { "Store", Store }, { "__gc", Del }, + { "__tostring", ToString }, + { "__add", OpAdd }, + { "__sub", OpSubstract }, + { "__mul", OpMultiply }, + { "__div", OpDivide }, + { "__mod", OpModulo }, + { "__unm", OpUnaryNeg }, + { "__concat", OpConcat }, + { "__eq", CmpEqual }, + { "__lt", CmpLessThan }, + { "__le", CmpLessEqual }, { NULL, NULL } }; - //TODO: Touky: Implement that - //__tostring : ToString - //__add : Addition(+) - //__sub : Subtraction(-) - //__mul : Multiplication(*) - //__div : Division(/ ) - //__mod : Modulos(%) - //__unm : Unary - , used for negation on numbers - //__concat : Concatenation(..) - //__eq : Equality(== ) - //__lt : Less than(<) - //__le : Less than or equal to(<= ) //Create Static metatable luaL_newmetatable(l, GetStaticName()); @@ -235,6 +231,18 @@ protected: //------------------------------------------------------------------------- template static int Store(lua_State * l); template static int Del(lua_State * l); + //------------------------------------------------------------------------- + template static int ToString(lua_State* l) { ASSERT(false); return 0; } + template static int OpAdd(lua_State* l) { ASSERT(false); return 0; } + template static int OpSubstract(lua_State* l) { ASSERT(false); return 0; } + template static int OpMultiply(lua_State* l) { ASSERT(false); return 0; } + template static int OpDivide(lua_State* l) { ASSERT(false); return 0; } + template static int OpModulo(lua_State* l) { ASSERT(false); return 0; } + template static int OpUnaryNeg(lua_State* l) { ASSERT(false); return 0; } + template static int OpConcat(lua_State* l) { ASSERT(false); return 0; } + template static int CmpEqual(lua_State* l) { ASSERT(false); return 0; } + template static int CmpLessThan(lua_State* l) { ASSERT(false); return 0; } + template static int CmpLessEqual(lua_State* l) { ASSERT(false); return 0; } }; //----------------------------------------------------------------------------- @@ -248,683 +256,47 @@ public: } }; -//----------------------------------------------------------------------------- -#if OLD_VAR_SYSTEM - -//----------------------------------------------------------------------------- -template -class VarPtr -{ -protected: - T* m_value = nullptr; - bool m_optional = false; - -public: - VarPtr(bool optional = false) - { - m_optional = optional; - } - VarPtr(T* value, bool optional = false) - : VarPtr(optional) - { - m_value = value; - } - VarPtr(lua_State* l, int& index, bool optional = false) - : VarPtr(optional) - { - GetInc(l, index); - } - VarPtr(T* value, lua_State* l, int& index, bool optional = false) - : VarPtr(value, optional) - { - GetInc(l, index); - } - - inline T* operator ()() { return m_value; } - inline T* operator ->() { return m_value; } - inline T* operator=(T* value) { m_value = value; } - inline T* GetValue() { return m_value; } - inline bool IsValid(lua_State* l, int index) - { - return InnerIsValid(l, index); - } - inline bool IsOptional() - { - return m_optional; - } -private: - inline void GetInc(lua_State* l, int& index) - { - bool is_nil = lua_isnil(l, index); - if (!m_optional || (!is_nil && InnerIsValid(l, index))) - { - ASSERT(!is_nil); - InnerGet(l, index); - } - } -public: - inline void Get(lua_State* l, int index) - { - int idx = index; - GetInc(l, idx); - } - inline int Return(lua_State* l) - { - InnerPush(l); - return 1; - } - -protected: - virtual bool InnerIsValid(lua_State* l, int index) - { - return !!lua_isuserdata(l, index); - } - virtual void InnerGet(lua_State* l, int& index) - { - T** obj = static_cast(luaL_checkudata(l, index++, ObjectHelper::GetMethodName())); - m_value = obj ? *obj : nullptr; - } - void InnerPush(lua_State* l) - { - T** data = (T**)lua_newuserdata(l, sizeof(T*)); - *data = m_value; - } -}; - -//----------------------------------------------------------------------------- -/* TODO: FIX THAT TOUKY !! -template -class VarPtrLight -{ -public: - VarPtrLight(bool optional = false) : VarPtr(optional) { } - VarPtrLight(T* value, bool optional = false) : VarPtr(value, optional) { } - VarPtrLight(lua_State* l, int& index, bool optional = false) : VarPtr(l, index, optional) { } - VarPtrLight(T* value, lua_State* l, int& index, bool optional = false) : VarPtr(value, l, index, optional) { } -protected: - virtual void InnerGet(lua_State* l, int& index) - { - T** obj = static_cast(luaL_testudata(l, index++, ObjectDef::GetMethodName())); - m_value = obj ? *obj : nullptr; - } -}; -*/ - -//----------------------------------------------------------------------------- -template -class Var -{ -protected: - bool m_optional = false; - T m_value; - -public: - Var(bool optional = false) - { - m_optional = optional; - InnerInit(); - } - Var(T value, bool optional = false) - { - m_optional = optional; - m_value = value; - } - Var(lua_State* l, int& index, bool optional = false) - { - m_optional = optional; - GetInc(l, index); - } - Var(T value, lua_State* l, int& index, bool optional = false) - { - m_optional = optional; - m_value = value; - GetInc(l, index); - } - inline operator T() { return m_value; } - inline T& operator ()() { return m_value; } - inline T& GetValue() { return m_value; } - inline bool IsValid(lua_State* l, int index) - { - return InnerIsValid(l, index); - } - inline bool IsOptional() - { - return m_optional; - } -private: - void GetInc(lua_State* l, int& index) - { - bool is_nil = lua_isnil(l, index); - if (!m_optional || (!is_nil && InnerIsValid(l, index))) - { - ASSERT(!is_nil); - InnerGet(l, index); - } - } -public: - inline void Get(lua_State* l, int index) - { - int idx = index; - GetInc(l, idx); - } - inline int Return(lua_State* l) - { - return InnerPush(l); - } - inline Var& operator-(const T& value) { m_value - value; return *this; } - inline Var& operator+(const T& value) { m_value + value; return *this; } - inline Var& operator*(const T& value) { m_value * value; return *this; } - inline Var& operator/(const T& value) { m_value / value; return *this; } - inline Var& operator=(const T& value) { m_value = value; return *this; } - inline Var& operator-=(const T& value) { m_value -= value; return *this; } - inline Var& operator+=(const T& value) { m_value += value; return *this; } - inline Var& operator*=(const T& value) { m_value *= value; return *this; } - inline Var& operator/=(const T& value) { m_value /= value; return *this; } - inline Var& operator-(const Var& o) { m_value - o.m_value; return *this; } - inline Var& operator+(const Var& o) { m_value + o.m_value; return *this; } - inline Var& operator*(const Var& o) { m_value * o.m_value; return *this; } - inline Var& operator/(const Var& o) { m_value / o.m_value; return *this; } - inline Var& operator=(const Var& o) { m_value = o.m_value; return *this; } - inline Var& operator-=(const Var& o) { m_value -= o.m_value; return *this; } - inline Var& operator+=(const Var& o) { m_value += o.m_value; return *this; } - inline Var& operator*=(const Var& o) { m_value *= o.m_value; return *this; } - inline Var& operator/=(const Var& o) { m_value /= o.m_value; return *this; } - inline bool operator==(const T& value) { return m_value == value; } - inline bool operator!=(const T& value) { return m_value != value; } - inline bool operator==(const Var& o) { return m_value == o.m_value; } - inline bool operator!=(const Var& o) { return m_value != o.m_value; } - -protected: - void InnerInit() { m_value = T(0); } - bool InnerIsValid(lua_State* l, int index) { UNUSED(l); UNUSED(index); ASSERT(false); return false; } - void InnerGet(lua_State* l, int& index) { UNUSED(l); UNUSED(index); ASSERT(false); } - int InnerPush(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } -}; - -//----------------------------------------------------------------------------- -#ifndef BASE_TYPES -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - return lua_isboolean(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - m_value = !!lua_toboolean(l, index++); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - lua_pushboolean(l, m_value); - return 1; -} -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - return !!lua_isstring(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - m_value = lua_tostring(l, index++); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - lua_pushstring(l, m_value); - return 1; -} -//----------------------------------------------------------------------------- -template<> inline void Var::InnerInit() -{ - m_value = String(); -} -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var v; - return v.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var v(l, index); - m_value = v(); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var v; - v = m_value.C(); - return v.Return(l); -} -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - return !!lua_isnumber(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - m_value = lua_tonumber(l, index++); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - lua_pushnumber(l, m_value); - return 1; -} -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var v; - return v.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var v(l, index); - m_value = (float)v(); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var v = (double)m_value; - return v.Return(l); -} - -#if 0 -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - return !!lua_isnumber(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - m_value = lua_tointeger(l, index++); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - lua_pushinteger(l, m_value); - return 1; -} -#endif - -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var v; - return v.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var v(l, index); - m_value = (int32_t)v(); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var v = (int64_t)m_value; - return v.Return(l); -} - -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - return !!lua_isnumber(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - m_value = (uint32_t)(lua_Unsigned)lua_tointeger(l, index++); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - lua_pushinteger(l, (lua_Integer)m_value); - return 1; -} - -#if 0 -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var v; - return v.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var v(l, index); - m_value = (uint64_t)v(); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var v = (uint32_t)m_value; - return v.Return(l); -} -#endif - -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var x; - return x.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var x(l, index); - Var y(x(), l, index, true); - m_value = vec2(x, y); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var x = m_value.x; - Var y = m_value.y; - return (x.Return(l) + y.Return(l)); -} -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var x; - return x.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var x(l, index); - Var y(x(), l, index, true); - Var z(x(), l, index, true); - m_value = vec3(x, y, z); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var x = m_value.x; - Var y = m_value.y; - Var z = m_value.z; - return (x.Return(l) + y.Return(l) + z.Return(l)); -} -//----------------------------------------------------------------------------- -template<> inline bool Var::InnerIsValid(lua_State* l, int index) -{ - Var x; - return x.IsValid(l, index); -} -template<> inline void Var::InnerGet(lua_State* l, int& index) -{ - Var x(l, index); - Var y(x(), l, index, true); - Var z(x(), l, index, true); - Var w(x(), l, index, true); - m_value = vec4(x, y, z, w); -} -template<> inline int Var::InnerPush(lua_State* l) -{ - Var x = m_value.x; - Var y = m_value.y; - Var z = m_value.z; - Var w = m_value.w; - return (x.Return(l) + y.Return(l) + z.Return(l) + w.Return(l)); -} -#endif //BASE_TYPES - -//----------------------------------------------------------------------------- -#ifndef CUSTOM_TYPES -class VarColor -{ -protected: - Var m_value; - -public: - VarColor(bool optional = false) - { - m_value = Var(optional); - InnerInit(); - } - VarColor(vec4 value, bool optional = false) - { - m_value = Var(value, optional); - } - VarColor(lua_State* l, int& index, bool optional = false) - : VarColor(optional) - { - GetInc(l, index); - } - VarColor(vec4 value, lua_State* l, int& index, bool optional = false) - : VarColor(value, optional) - { - GetInc(l, index); - } - - inline operator vec4() { return m_value; } - inline vec4& operator ()() { return m_value(); } - inline vec4& GetValue() { return m_value.GetValue(); } - inline bool IsValid(lua_State* l, int index) - { - return InnerIsValid(l, index); - } - inline bool IsOptional() - { - return m_value.IsOptional(); - } -private: - void GetInc(lua_State* l, int& index) - { - bool is_nil = lua_isnil(l, index); - if (!m_value.IsOptional() || (!is_nil && InnerIsValid(l, index))) - { - ASSERT(!is_nil); - InnerGet(l, index); - } - } -public: - inline void Get(lua_State* l, int index) - { - int idx = index; - GetInc(l, idx); - } - inline int Return(lua_State* l) - { - return InnerPush(l); - } - inline VarColor& operator-(const vec4& value) { m_value - value; return *this; } - inline VarColor& operator+(const vec4& value) { m_value + value; return *this; } - inline VarColor& operator*(const vec4& value) { m_value * value; return *this; } - inline VarColor& operator/(const vec4& value) { m_value / value; return *this; } - inline VarColor& operator=(const vec4& value) { m_value = value; return *this; } - inline VarColor& operator-=(const vec4& value) { m_value -= value; return *this; } - inline VarColor& operator+=(const vec4& value) { m_value += value; return *this; } - inline VarColor& operator*=(const vec4& value) { m_value *= value; return *this; } - inline VarColor& operator/=(const vec4& value) { m_value /= value; return *this; } - inline VarColor& operator-(const Var& o) { m_value - o; return *this; } - inline VarColor& operator+(const Var& o) { m_value + o; return *this; } - inline VarColor& operator*(const Var& o) { m_value * o; return *this; } - inline VarColor& operator/(const Var& o) { m_value / o; return *this; } - inline VarColor& operator=(const Var& o) { m_value = o; return *this; } - inline VarColor& operator-=(const Var& o) { m_value -= o; return *this; } - inline VarColor& operator+=(const Var& o) { m_value += o; return *this; } - inline VarColor& operator*=(const Var& o) { m_value *= o; return *this; } - inline VarColor& operator/=(const Var& o) { m_value /= o; return *this; } - inline bool operator==(const vec4& value) { return m_value == value; } - inline bool operator!=(const vec4& value) { return m_value != value; } - inline bool operator==(const Var& o) { return m_value == o; } - inline bool operator!=(const Var& o) { return m_value != o; } - inline VarColor& operator-(const VarColor& v) { m_value - v.m_value; return *this; } - inline VarColor& operator+(const VarColor& v) { m_value + v.m_value; return *this; } - inline VarColor& operator*(const VarColor& v) { m_value * v.m_value; return *this; } - inline VarColor& operator/(const VarColor& v) { m_value / v.m_value; return *this; } - inline VarColor& operator=(const VarColor& v) { m_value = v.m_value; return *this; } - inline VarColor& operator-=(const VarColor& v) { m_value -= v.m_value; return *this; } - inline VarColor& operator+=(const VarColor& v) { m_value += v.m_value; return *this; } - inline VarColor& operator*=(const VarColor& v) { m_value *= v.m_value; return *this; } - inline VarColor& operator/=(const VarColor& v) { m_value /= v.m_value; return *this; } - inline bool operator==(const VarColor& v) { return m_value == v.m_value; } - inline bool operator!=(const VarColor& v) { return m_value != v.m_value; } - -protected: - void InnerInit() - { - m_value = vec4::zero; - } - bool InnerIsValid(lua_State* l, int index) - { - Var s; - return m_value.IsValid(l, index) || s.IsValid(l, index); - } - void InnerGet(lua_State* l, int& index) - { - //Try vec4 first - if (m_value.IsValid(l, index)) - { - m_value.Get(l, index); - } - else - { - Var c(l, index); - *this = Color::C8BppHexString(c); - } - } - int InnerPush(lua_State* l) - { - Var c = Color::HexString8Bpp(m_value); - return c.Return(l); - } -}; - -//----------------------------------------------------------------------------- -template -class VarEnum -{ -protected: - SafeEnum m_value; - bool m_optional = false; - -public: - VarEnum(bool optional = false) - { - m_optional = optional; - InnerInit(); - } - VarEnum(SafeEnum value, bool optional = false) - : VarEnum(optional) - { - m_value = value; - } - VarEnum(lua_State* l, int& index, bool optional = false) - : VarEnum(optional) - { - GetInc(l, index); - } - VarEnum(SafeEnum value, lua_State* l, int& index, bool optional = false) - : VarEnum(value, optional) - { - GetInc(l, index); - } - - inline operator SafeEnum() { return m_value; } - inline SafeEnum& operator ()() { return m_value; } - inline SafeEnum& GetValue() { return m_value; } - inline bool IsValid(lua_State* l, int index) - { - return InnerIsValid(l, index); - } - inline bool IsOptional() - { - return m_optional; - } -private: - void GetInc(lua_State* l, int& index) - { - bool is_nil = lua_isnil(l, index); - if (!m_optional || (!is_nil && InnerIsValid(l, index))) - { - ASSERT(!is_nil); - InnerGet(l, index); - } - } -public: - inline void Get(lua_State* l, int index) - { - int idx = index; - GetInc(l, idx); - } - inline int Return(lua_State* l) - { - return InnerPush(l); - } - inline VarEnum& operator-(const SafeEnum& value) { m_value - value; return *this; } - inline VarEnum& operator+(const SafeEnum& value) { m_value + value; return *this; } - inline VarEnum& operator*(const SafeEnum& value) { m_value * value; return *this; } - inline VarEnum& operator/(const SafeEnum& value) { m_value / value; return *this; } - inline VarEnum& operator=(const SafeEnum& value) { m_value = value; return *this; } - inline VarEnum& operator-=(const SafeEnum& value) { m_value -= value; return *this; } - inline VarEnum& operator+=(const SafeEnum& value) { m_value += value; return *this; } - inline VarEnum& operator*=(const SafeEnum& value) { m_value *= value; return *this; } - inline VarEnum& operator/=(const SafeEnum& value) { m_value /= value; return *this; } - inline bool operator==(const SafeEnum& value) { return m_value == value; } - inline bool operator!=(const SafeEnum& value) { return m_value != value; } - inline VarEnum& operator-(const VarEnum& v) { m_value - v.m_value; return *this; } - inline VarEnum& operator+(const VarEnum& v) { m_value + v.m_value; return *this; } - inline VarEnum& operator*(const VarEnum& v) { m_value * v.m_value; return *this; } - inline VarEnum& operator/(const VarEnum& v) { m_value / v.m_value; return *this; } - inline VarEnum& operator=(const VarEnum& v) { m_value = v.m_value; return *this; } - inline VarEnum& operator-=(const VarEnum& v) { m_value -= v.m_value; return *this; } - inline VarEnum& operator+=(const VarEnum& v) { m_value += v.m_value; return *this; } - inline VarEnum& operator*=(const VarEnum& v) { m_value *= v.m_value; return *this; } - inline VarEnum& operator/=(const VarEnum& v) { m_value /= v.m_value; return *this; } - inline bool operator==(const VarEnum& v) { return m_value == v.m_value; } - inline bool operator!=(const VarEnum& v) { return m_value != v.m_value; } - -protected: - void InnerInit() - { - m_value = SafeEnum(0); - } - bool InnerIsValid(lua_State* l, int index) - { - Var s; - return s.IsValid(l, index); - } - void InnerGet(lua_State* l, int& index) - { - Var c(l, index); - *this = FindValue >(c); - } - int InnerPush(lua_State* l) - { - Var s = this->GetValue().ToString(); - return s.Return(l); - } -}; -#endif //CUSTOM_TYPES - -#endif //OLD_VAR_SYSTEM - //----------------------------------------------------------------------------- // Stack: Main class that encapsulates everything ----------------------------- //----------------------------------------------------------------------------- class Stack { -public: - -#if NEW_VAR_SYSTEM - //------------------------------------------------------------------------- +public: static Stack Begin(lua_State* state, int32_t start_index = 1) { return Stack(state, start_index); } + //------------------------------------------------------------------------- + void SetIndex(int32_t index) + { + m_index = index; + } + //------------------------------------------------------------------------- int32_t End() { return m_result; } -#if !OLD_VAR_SYSTEM + //------------------------------------------------------------------------- protected: -#endif //!OLD_VAR_SYSTEM - -#endif //NEW_VAR_SYSTEM - Stack(lua_State* l, int32_t start_index = 1) { m_state = l; m_index = start_index; } + +public: virtual ~Stack() { } +protected: int32_t GetArgs() { return lua_gettop(m_state); } -#if NEW_VAR_SYSTEM - public: //------------------------------------------------------------------------- //The encapsulating struct for pointers @@ -945,6 +317,7 @@ public: } Ptr(const T*& value) { m_value = value; } inline operator T*() { return m_value; } + inline T* operator ->() { return m_value; } inline Ptr& operator=(T const*& value) { m_value = value; return *this; } }; @@ -985,13 +358,6 @@ protected: template T InnerGet(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return InnerDefault(); } template int InnerPush(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return 0; } -#ifndef INNER_T_T - //template class C> C InnerDefault() { return C(); } - //template bool InnerIsValid() { return InnerIsValid(); } - //template SafeEnum InnerGet(SafeEnum value) { return FindValue >(InnerGet(value.ToString().C())); } - //template int InnerPush(SafeEnum value) { return InnerPush(value.ToString.C()); } -#endif //STACK_STRING - #ifndef INNER_SAFE_ENUM template SafeEnum InnerDefaultSafeEnum() { return SafeEnum(); } template bool InnerIsValidSafeEnum() { return InnerIsValid(); } @@ -1016,35 +382,7 @@ protected: } #endif //STACK_STRING -#endif //NEW_VAR_SYSTEM - - //------------------------------------------------------------------------- -#if OLD_VAR_SYSTEM -public: - inline operator int32_t() { return m_result; } - //------------------------------------------------------------------------- - - template - Stack& operator>>(T& var) - { - var = T(var.GetValue(), m_state, m_index, var.IsOptional()); - return *this; - } - -#if !NEW_VAR_SYSTEM - //------------------------------------------------------------------------- - template - Stack& operator<<(T& var) - { - m_result += var.Return(m_state); - return *this; - } - -#endif //NEW_VAR_SYSTEM - -#endif //OLD_VAR_SYSTEM - private: lua_State* m_state = nullptr; int32_t m_index = 1; @@ -1052,22 +390,8 @@ private: }; //----------------------------------------------------------------------------- -#if NEW_VAR_SYSTEM - #ifndef REGION_STACK_VAR -//#if OLD_VAR_SYSTEM && NEW_VAR_SYSTEM -// -////------------------------------------------------------------------------- -//template -//Stack& Stack::operator<<(Var& var) -//{ -// m_result += var.Return(m_state); -// return *this; -//} -// -//#endif //OLD_VAR_SYSTEM && NEW_VAR_SYSTEM - #ifndef STACK_BOOL template<> inline bool Stack::InnerIsValid() { return lua_isboolean(m_state, m_index); } template<> inline bool Stack::InnerGet(bool value) { UNUSED(value); return !!lua_toboolean(m_state, m_index++); } @@ -1104,7 +428,6 @@ template<> inline int Stack::InnerPush(float value) { return InnerPush inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } template<> inline int64_t Stack::InnerGet(int64_t value) { UNUSED(value); return lua_tointeger(m_state, m_index++); } @@ -1153,36 +476,8 @@ template<> inline vec4 Stack::InnerGet(vec4 value) { return vec4(InnerGet< template<> inline int Stack::InnerPush(vec4 value) { return (InnerPush(value.x) + InnerPush(value.y) + InnerPush(value.z) + InnerPush(value.w)); } #endif STACK_VEC4 -//----------------------------------------------------------------------------- -#ifndef STACK_SAFE_ENUM -//template inline SafeEnum Stack::InnerDefault() { return SafeEnum(); } -//template inline bool Stack::InnerIsValid >() { return InnerIsValid(); } -//template inline SafeEnum Stack::InnerGet >(SafeEnum value) { return FindValue >(InnerGet(value.ToString().C())); } -//template inline int Stack::InnerPush >(SafeEnum value) { return InnerPush(value.ToString.C()); } -#endif //STACK_STRING - -//----------------------------------------------------------------------------- -#ifndef STACK_PTR -//template inline Stack::Ptr

Stack::InnerDefault >() { return Stack::Ptr

(nullptr); } -//template inline bool Stack::InnerIsValid >() { return !!lua_isuserdata(m_state, m_index); } -//template inline Stack::Ptr

Stack::InnerGet >(Stack::Ptr

value) -//{ -// P** obj = static_cast(value.m_throw_error -// ? luaL_checkudata(m_state, m_index++, ObjectHelper::GetMethodName

()) -// : luaL_testudata(m_state, m_index++, ObjectHelper::GetMethodName

()) ); -// return Stack::Ptr

(obj ? *obj : value.m_value); -//} -//template inline int Stack::InnerPush >(Stack::Ptr

value) -//{ -// P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); -// *data = value.m_value; -//} -#endif //STACK_STRING - #endif //REGION_STACK_VAR -#endif //NEW_VAR_SYSTEM - //----------------------------------------------------------------------------- class Loader { @@ -1194,7 +489,6 @@ public: bool ExecLuaFile(String const &lua); bool ExecLuaCode(String const &lua); -#if NEW_VAR_SYSTEM //------------------------------------------------------------------------- #define DECLARE_LOADER_GET(T0, T1, GET_NAME) \ template \ @@ -1213,27 +507,6 @@ public: #undef DECLARE_LOADER_GET -#else //OLD_VAR_SYSTEM - - template - T GetVar(String const &name) - { - lua_getglobal(m_lua_state, name.C()); - Var var; var.Get(m_lua_state, -1); - lua_pop(m_lua_state, 1); - return var; - } - template - T* GetPtr(String const &name) - { - lua_getglobal(m_lua_state, name.C()); - VarPtr var; var.Get(m_lua_state, -1); - lua_pop(m_lua_state, 1); - return var(); - } - -#endif //OLD_VAR_SYSTEM - protected: lua_State* GetLuaState(); static void Store(lua_State* l, Loader* loader); @@ -1249,12 +522,11 @@ private: //----------------------------------------------------------------------------- // ObjectHelper member implementations that require VarPtr -#if NEW_VAR_SYSTEM template int ObjectHelper::Store(lua_State * l) { auto stack = LuaStack::Begin(l); - auto obj = stack.Get >(); + TLuaClass* obj = stack.GetPtr(); ASSERT(obj); Loader::StoreObject(l, obj); return 0; @@ -1264,37 +536,12 @@ template int ObjectHelper::Del(lua_State * l) { auto stack = LuaStack::Begin(l); - auto obj = stack.Get >(); + TLuaClass* obj = stack.GetPtr(); ASSERT(obj); delete obj; return 0; } -#else //OLD_VAR_SYSTEM - -template -int ObjectHelper::Store(lua_State * l) -{ - VarPtr obj; - obj.Get(l, 1); - ASSERT(obj()); - Loader::StoreObject(l, obj()); - return 0; -} - -template -int ObjectHelper::Del(lua_State * l) -{ - VarPtr obj; - obj.Get(l, 1); - ASSERT(obj()); - delete obj(); - return 0; -} - -#endif //OLD_VAR_SYSTEM - - } /* namespace Lolua */ //TYPEDEFS @@ -1305,22 +552,5 @@ typedef Lolua::Object::Library LuaObjectLibrary; typedef Lolua::Loader LuaLoader; typedef Lolua::Stack LuaStack; template using LuaPtr = Lolua::Stack::Ptr

; -//template -//typedef Lolua::Stack::Ptr

LuaPtr

; -#if OLD_VAR_SYSTEM -typedef Lolua::Var LuaBool; -typedef Lolua::Var LuaCharPtr; -typedef Lolua::Var LuaString; -typedef Lolua::Var LuaDouble; -typedef Lolua::Var LuaFloat; -typedef Lolua::Var LuaInt64; -typedef Lolua::Var LuaInt32; -typedef Lolua::Var LuaUInt32; -typedef Lolua::Var LuaUInt64; -typedef Lolua::Var LuaVec2; -typedef Lolua::Var LuaVec3; -typedef Lolua::Var LuaVec4; -typedef Lolua::VarColor LuaColor; -#endif //OLD_VAR_SYSTEM } /* namespace lol */ From eb943fc5e332ac65fb5e3e2264ac23dc809fb030 Mon Sep 17 00:00:00 2001 From: touky Date: Mon, 31 Oct 2016 19:31:34 -0400 Subject: [PATCH 04/21] Lolua small simplification getter default params are better now --- doc/samples/meshviewer/imgui.ini | 10 +++ doc/tutorial/14_lol_lua.cpp | 29 ++++++-- doc/tutorial/14_lol_lua.lua | 2 +- src/easymesh/easymeshlua.cpp | 2 +- src/easymesh/easymeshlua.h | 76 ++++++++++---------- src/lolua/baselua.h | 115 ++++++++++++++++++++++++------- 6 files changed, 166 insertions(+), 68 deletions(-) create mode 100644 doc/samples/meshviewer/imgui.ini diff --git a/doc/samples/meshviewer/imgui.ini b/doc/samples/meshviewer/imgui.ini new file mode 100644 index 00000000..0e0a9153 --- /dev/null +++ b/doc/samples/meshviewer/imgui.ini @@ -0,0 +1,10 @@ +[Debug] +Pos=60,60 +Size=400,400 +Collapsed=0 + +[Camera Setup] +Pos=60,60 +Size=307,314 +Collapsed=0 + diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index ce0348ab..4c4d3d39 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -43,20 +43,24 @@ public: return (stack << i).End(); } - - static int AddTenInstance(lua_State* l) + + LOLUA_DECLARE_RETURN_METHOD(AddTenInstance, GetPtr(), AddTenMethod, Get(), Get(), Get()); + static int _AddTenInstance(lua_State* l) { auto stack = LuaStack::Begin(l); DemoObject* obj = stack.GetPtr(); - float f = stack.Get(); + float f = stack.Get(); + int32_t i = stack.Get(); + int32_t i2 = stack.Get(); - f = obj->AddTenMethod(f); + f = obj->AddTenMethod(f, i, i2); return (stack << f).End(); } - float AddTenMethod(float f) + float AddTenMethod(float f, int32_t i, int32_t i2) { + UNUSED(i, i2); return (f + 10); } @@ -71,7 +75,8 @@ public: return (stack << i).End(); } - static int SetX(lua_State* l) + LOLUA_DECLARE_VOID_METHOD(SetX, GetPtr(), SetXMethod, Get()); + static int _SetX(lua_State* l) { auto stack = LuaStack::Begin(l); DemoObject* obj = stack.GetPtr(); @@ -82,6 +87,12 @@ public: return stack.End(); } + void SetXMethod(int32_t i) + { + m_x = i; + } + + //------------------------------------------------------------------------- static const LuaObjectLibrary* GetLib() { @@ -190,6 +201,12 @@ public: function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n", function_return.C(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x); +#define /***/ _LOLUA_ARG_1(a00) (float)a00 +#define /***/ _LOLUA_ARG_2(a00, a01) _LOLUA_ARG_1(a00), _LOLUA_ARG_1(a01) +#define /***/ _LOLUA_ARG_3(a00, a01, a02) _LOLUA_ARG_1(a00), _LOLUA_ARG_2(a01, a02) +#define /***/ _LOLUA_ARG_4(a00, a01, a02, a03) _LOLUA_ARG_1(a00), _LOLUA_ARG_3(a01, a02, a03) + msg::info("_LOLUA_ARG_1: %f, %f, %f, %f\n", _LOLUA_ARG_4(0, 1, 2, 3)); + delete demo_loader; Ticker::Shutdown(); diff --git a/doc/tutorial/14_lol_lua.lua b/doc/tutorial/14_lol_lua.lua index cea94073..0b0a0428 100644 --- a/doc/tutorial/14_lol_lua.lua +++ b/doc/tutorial/14_lol_lua.lua @@ -13,4 +13,4 @@ loluademo_return = LoluaDemo.AddFive(1); loluademo_inst = LoluaDemo.New(); loluademo_inst:SetX(10); loluademo_getx = loluademo_inst:GetX(); -loluademo_inst_return = loluademo_inst:AddTenInstance(2.0); +loluademo_inst_return = loluademo_inst:AddTenInstance(2.5, 4, 6); diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp index a36ab4d8..4edd141c 100644 --- a/src/easymesh/easymeshlua.cpp +++ b/src/easymesh/easymeshlua.cpp @@ -82,7 +82,7 @@ EasyMeshLuaObject* EasyMeshLuaObject::New(lua_State* l, int arg_nb) UNUSED(l); UNUSED(arg_nb); LuaStack s = LuaStack::Begin(l); - String str = s.Get("", true); + String str = s.Get(""); return new EasyMeshLuaObject(str); } diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h index 5fb32ea8..5409fab3 100644 --- a/src/easymesh/easymeshlua.h +++ b/src/easymesh/easymeshlua.h @@ -38,12 +38,13 @@ public: auto h = s.Get(); auto d1 = s.Get(); auto d2 = s.Get(); - auto dualside = s.Get(false, true); - auto smooth = s.Get(false, true); - auto close = s.Get(false, true); + auto dualside = s.Get(false); + auto smooth = s.Get(false); + auto close = s.Get(false); m->m_instance.AppendCylinder(nsides, h, d1, d2, dualside, smooth, close); return s.End(); } + //LOLUA_DECLARE_VOID_METHOD(AppendSphere, GetPtr(), m_instance.AppendSphere, Get(), Get()); static int AppendSphere(lua_State* l) { auto s = LuaStack::Begin(l); @@ -53,6 +54,7 @@ public: m->m_instance.AppendSphere(ndivisions, d); return s.End(); } + static int AppendCapsule(lua_State* l) { auto s = LuaStack::Begin(l); @@ -78,8 +80,8 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto size = s.Get(); - auto chamf = s.Get(0.f, true); - auto smooth = s.Get(false, true); + auto chamf = s.Get(0.f); + auto smooth = s.Get(false); m->m_instance.AppendBox(size, chamf, smooth); return s.End(); } @@ -90,8 +92,8 @@ public: auto nbranches = s.Get(); auto d1 = s.Get(); auto d2 = s.Get(); - auto fade = s.Get(false, true); - auto fade2 = s.Get(false, true); + auto fade = s.Get(false); + auto fade2 = s.Get(false); m->m_instance.AppendStar(nbranches, d1, d2, fade, fade2); return s.End(); } @@ -102,7 +104,7 @@ public: auto nbranches = s.Get(); auto d1 = s.Get(); auto d2 = s.Get(); - auto extrad = s.Get(0.f, true); + auto extrad = s.Get(0.f); m->m_instance.AppendExpandedStar(nbranches, d1, d2, extrad); return s.End(); } @@ -112,7 +114,7 @@ public: auto m = s.GetPtr(); auto nsides = s.Get(); auto d = s.Get(); - auto fade = s.Get(false, true); + auto fade = s.Get(false); m->m_instance.AppendDisc(nsides, d, fade); return s.End(); } @@ -121,7 +123,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto d = s.Get(); - auto fade = s.Get(false, true); + auto fade = s.Get(false); m->m_instance.AppendSimpleTriangle(d, fade); return s.End(); } @@ -130,7 +132,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto size = s.Get(); - auto fade = s.Get(false, true); + auto fade = s.Get(false); m->m_instance.AppendSimpleQuad(size, fade); return s.End(); } @@ -140,11 +142,11 @@ public: auto m = s.GetPtr(); auto nbsides = s.Get(); auto h = s.Get(); - auto sidemul = s.Get(0.f, true); + auto sidemul = s.Get(0.f); auto d0 = s.Get(); auto d1 = s.Get(); auto d2 = s.Get(); - auto offset = s.Get(false, true); + auto offset = s.Get(false); m->m_instance.AppendCog(nbsides, h, d0.x, d0.y, d1.x, d1.y, d2.x, d2.y, sidemul, offset); return s.End(); } @@ -264,8 +266,8 @@ public: auto m = s.GetPtr(); auto y = s.Get(); auto z = s.Get(); - auto xoff = s.Get(0.f, true); - auto abs = s.Get(true, true); + auto xoff = s.Get(0.f); + auto abs = s.Get(true); m->m_instance.TaperX(y, z, xoff, abs); return s.End(); } @@ -275,8 +277,8 @@ public: auto m = s.GetPtr(); auto x = s.Get(); auto z = s.Get(); - auto yoff = s.Get(0.f, true); - auto abs = s.Get(true, true); + auto yoff = s.Get(0.f); + auto abs = s.Get(true); m->m_instance.TaperY(x, z, yoff, abs); return s.End(); } @@ -286,8 +288,8 @@ public: auto m = s.GetPtr(); auto x = s.Get(); auto y = s.Get(); - auto zoff = s.Get(0.f, true); - auto abs = s.Get(true, true); + auto zoff = s.Get(0.f); + auto abs = s.Get(true); m->m_instance.TaperZ(x, y, zoff, abs); return s.End(); } @@ -297,7 +299,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.TwistX(t, toff); return s.End(); } @@ -306,7 +308,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.TwistY(t, toff); return s.End(); } @@ -315,7 +317,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.TwistZ(t, toff); return s.End(); } @@ -326,8 +328,8 @@ public: auto m = s.GetPtr(); auto y = s.Get(); auto z = s.Get(); - auto xoff = s.Get(0.f, true); - auto abs = s.Get(true, true); + auto xoff = s.Get(0.f); + auto abs = s.Get(true); m->m_instance.ShearX(y, z, xoff, abs); return s.End(); } @@ -337,8 +339,8 @@ public: auto m = s.GetPtr(); auto x = s.Get(); auto z = s.Get(); - auto yoff = s.Get(0.f, true); - auto abs = s.Get(true, true); + auto yoff = s.Get(0.f); + auto abs = s.Get(true); m->m_instance.ShearY(x, z, yoff, abs); return s.End(); } @@ -348,8 +350,8 @@ public: auto m = s.GetPtr(); auto x = s.Get(); auto y = s.Get(); - auto zoff = s.Get(0.f, true); - auto abs = s.Get(true, true); + auto zoff = s.Get(0.f); + auto abs = s.Get(true); m->m_instance.ShearZ(x, y, zoff, abs); return s.End(); } @@ -360,7 +362,7 @@ public: auto m = s.GetPtr(); auto y = s.Get(); auto z = s.Get(); - auto xoff = s.Get(0.f, true); + auto xoff = s.Get(0.f); m->m_instance.StretchX(y, z, xoff); return s.End(); } @@ -370,7 +372,7 @@ public: auto m = s.GetPtr(); auto x = s.Get(); auto z = s.Get(); - auto yoff = s.Get(0.f, true); + auto yoff = s.Get(0.f); m->m_instance.StretchY(x, z, yoff); return s.End(); } @@ -380,7 +382,7 @@ public: auto m = s.GetPtr(); auto x = s.Get(); auto y = s.Get(); - auto zoff = s.Get(0.f, true); + auto zoff = s.Get(0.f); m->m_instance.StretchZ(x, y, zoff); return s.End(); } @@ -390,7 +392,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.BendXY(t, toff); return s.End(); } @@ -399,7 +401,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.BendXZ(t, toff); return s.End(); } @@ -408,7 +410,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.BendYX(t, toff); return s.End(); } @@ -417,7 +419,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.BendYZ(t, toff); return s.End(); } @@ -426,7 +428,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.BendZX(t, toff); return s.End(); } @@ -435,7 +437,7 @@ public: auto s = LuaStack::Begin(l); auto m = s.GetPtr(); auto t = s.Get(); - auto toff = s.Get(0.f, true); + auto toff = s.Get(0.f); m->m_instance.BendZY(t, toff); return s.End(); } diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index c4316558..bf5d25c6 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -321,37 +321,56 @@ public: inline Ptr& operator=(T const*& value) { m_value = value; return *this; } }; +private: + bool AllowGet(bool is_optional, bool value_validity) + { + bool is_nil = lua_isnil(m_state, m_index); + if (!is_optional || (!is_nil && value_validity)) + { + ASSERT(!is_nil); /* touky: should it assert, though ? */ + return true; + } + return false; + } + +public: //------------------------------------------------------------------------- - template T Get(bool isOptional = false) { return Get(InnerDefault(), isOptional); } - template SafeEnum GetEnum(bool isOptional = false) { return GetEnum(InnerDefaultSafeEnum(), isOptional); } - template Ptr

GetPtr(bool isOptional = false) { return GetPtr(InnerDefaultPtr

(), isOptional); } + template T Get() { return Get(InnerDefault(), false); } + template T Get(T default_value) { return Get(default_value, true); } + template SafeEnum GetEnum() { return GetEnum(InnerDefaultSafeEnum(), false); } + template SafeEnum GetEnum(SafeEnum default_value) { return GetEnum(default_value, true); } + template Ptr

GetPtr() { return GetPtr(InnerDefaultPtr

(), false); } + template Ptr

GetPtr(Ptr

default_value) { return GetPtr(default_value, true); } +private: //------------------------------------------------------------------------- -#define DECLARE_STACK_GET(T0, T1, GET_NAME, INNER_IS_VALID, INNER_GET) \ - template \ - T1 GET_NAME(T1 value, bool isOptional = false) \ - { \ - bool is_nil = lua_isnil(m_state, m_index); \ - if (!isOptional || (!is_nil && INNER_IS_VALID())) \ - { \ - ASSERT(!is_nil); /* touky: should assert, though ? */ \ - return INNER_GET(value); \ - } \ - return value; \ + template T Get(T default_value, bool is_optional) + { + if (AllowGet(is_optional, InnerIsValid())) + return InnerGet(default_value); + return default_value; + } + template SafeEnum GetEnum(SafeEnum default_value, bool is_optional) + { + if (AllowGet(is_optional, InnerIsValidSafeEnum())) + return InnerGetSafeEnum(default_value); + return default_value; + } + template Ptr

GetPtr(Ptr

default_value, bool is_optional) + { + if (AllowGet(is_optional, InnerIsValidPtr

())) + return InnerGetPtr(default_value); + return default_value; } - DECLARE_STACK_GET(T, T, Get, InnerIsValid, InnerGet); - DECLARE_STACK_GET(E, SafeEnum, GetEnum, InnerIsValidSafeEnum, InnerGetSafeEnum); - DECLARE_STACK_GET(P, Ptr

, GetPtr, InnerIsValidPtr, InnerGetPtr); - -#undef DECLARE_STACK_GET - +public: //------------------------------------------------------------------------- - template Stack& operator<<(T& value) { m_result += InnerPush(value); return *this; } - template Stack& operator<<(SafeEnum& value) { m_result += InnerPushSafeEnum(value); return *this; } - template Stack& operator<<(Ptr

& value) { m_result += InnerPushPtr

(value); return *this; } + template Stack& operator<<(T value) { m_result += InnerPush(value); return *this; } + template Stack& operator<<(SafeEnum value) { m_result += InnerPushSafeEnum(value); return *this; } + template Stack& operator<<(Ptr

value) { m_result += InnerPushPtr

(value); return *this; } protected: + //------------------------------------------------------------------------- #define INNER_ERROR "Your type is not implemented. For pointers, use LuaPtr()" template T InnerDefault() { return T(0); } template bool InnerIsValid() { ASSERT(false, INNER_ERROR); return false; } @@ -389,6 +408,56 @@ private: int32_t m_result = 0; }; +//----------------------------------------------------------------------------- +#define /***/ LOLUA_VAR_1(a00) auto v00 = s.a00; +#define /***/ LOLUA_VAR_2(a00, a01) LOLUA_VAR_1(a00) auto v01 = s.a01; +#define /***/ LOLUA_VAR_3(a00, a01, a02) LOLUA_VAR_2(a00, a01) auto v02 = s.a02; +#define /***/ LOLUA_VAR_4(a00, a01, a02, a03) LOLUA_VAR_3(a00, a01, a02) auto v03 = s.a03; +#define /***/ LOLUA_VAR_5(a00, a01, a02, a03, a04) LOLUA_VAR_4(a00, a01, a02, a03) auto v04 = s.a04; +#define /***/ LOLUA_VAR_6(a00, a01, a02, a03, a04, a05) LOLUA_VAR_5(a00, a01, a02, a03, a04) auto v05 = s.a05; +#define /***/ LOLUA_VAR_7(a00, a01, a02, a03, a04, a05, a06) LOLUA_VAR_6(a00, a01, a02, a03, a04, a05) auto v06 = s.a06; +#define /***/ LOLUA_VAR_8(a00, a01, a02, a03, a04, a05, a06, a07) LOLUA_VAR_7(a00, a01, a02, a03, a04, a05, a06) auto v07 = s.a07; +#define /***/ LOLUA_VAR_9(a00, a01, a02, a03, a04, a05, a06, a07, a08) LOLUA_VAR_8(a00, a01, a02, a03, a04, a05, a06, a07) auto v08 = s.a08; +#define /**/ LOLUA_VAR_10(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09) LOLUA_VAR_9(a00, a01, a02, a03, a04, a05, a06, a07, a08) auto v09 = s.a09; +#define /**/ LOLUA_VAR_11(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09, a10) LOLUA_VAR_10(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09) auto v10 = s.a10; +#define /**/ LOLUA_VAR_12(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, a11) LOLUA_VAR_11(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09) auto v11 = s.a11; +//----------------------------------------------------------------------------- +#define /***/ LOLUA_ARG_1(a00) v00 +#define /***/ LOLUA_ARG_2(a00, a01) LOLUA_ARG_1(a00), v01 +#define /***/ LOLUA_ARG_3(a00, a01, a02) LOLUA_ARG_2(a00, a01), v02 +#define /***/ LOLUA_ARG_4(a00, a01, a02, a03) LOLUA_ARG_3(a00, a01, a02), v03 +#define /***/ LOLUA_ARG_5(a00, a01, a02, a03, a04) LOLUA_ARG_4(a00, a01, a02, a03), v04 +#define /***/ LOLUA_ARG_6(a00, a01, a02, a03, a04, a05) LOLUA_ARG_5(a00, a01, a02, a03, a04), v05 +#define /***/ LOLUA_ARG_7(a00, a01, a02, a03, a04, a05, a06) LOLUA_ARG_6(a00, a01, a02, a03, a04, a05), v06 +#define /***/ LOLUA_ARG_8(a00, a01, a02, a03, a04, a05, a06, a07) LOLUA_ARG_7(a00, a01, a02, a03, a04, a05, a06), v07 +#define /***/ LOLUA_ARG_9(a00, a01, a02, a03, a04, a05, a06, a07, a08) LOLUA_ARG_8(a00, a01, a02, a03, a04, a05, a06, a07), v08 +#define /**/ LOLUA_ARG_10(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09) LOLUA_ARG_9(a00, a01, a02, a03, a04, a05, a06, a07, a08), v09 +#define /**/ LOLUA_ARG_11(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09, a10) LOLUA_ARG_10(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09), v10 +#define /**/ LOLUA_ARG_12(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09, a10, a11) LOLUA_ARG_11(a00, a01, a02, a03, a04, a05, a06, a07, a08, a09), v11 +//----------------------------------------------------------------------------- + +//----------------------------------------------------------------------------- +#define LOLUA_DECLARE_VOID_METHOD(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ + static int LUA_FUNC_NAME(lua_State* l) \ + { \ + auto s = LuaStack::Begin(l); \ + auto o = s.INSTANCE_GET; \ + LOL_CALL(LOL_CAT(LOLUA_VAR_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)) \ + o->INSTANCE_CALL(LOL_CALL(LOL_CAT(LOLUA_ARG_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__))); \ + return s.End(); \ + } + +//----------------------------------------------------------------------------- +#define LOLUA_DECLARE_RETURN_METHOD(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ + static int LUA_FUNC_NAME(lua_State* l) \ + { \ + auto s = LuaStack::Begin(l); \ + auto o = s.INSTANCE_GET; \ + LOL_CALL(LOL_CAT(LOLUA_VAR_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)) \ + s << o->INSTANCE_CALL(LOL_CALL(LOL_CAT(LOLUA_ARG_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__))); \ + return s.End(); \ + } + //----------------------------------------------------------------------------- #ifndef REGION_STACK_VAR From fab4610405ba6129528236a9bf8b47c764a6ad08 Mon Sep 17 00:00:00 2001 From: touky Date: Mon, 31 Oct 2016 20:31:48 -0400 Subject: [PATCH 05/21] lolua tweaks Added macros to help basic setup --- doc/tutorial/14_lol_lua.cpp | 6 +- src/easymesh/easymesh.h | 6 +- src/easymesh/easymeshlua.cpp | 5 +- src/easymesh/easymeshlua.h | 663 +++++------------------------------ src/lolua/baselua.h | 31 +- 5 files changed, 120 insertions(+), 591 deletions(-) diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index 4c4d3d39..412795e5 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -44,7 +44,7 @@ public: return (stack << i).End(); } - LOLUA_DECLARE_RETURN_METHOD(AddTenInstance, GetPtr(), AddTenMethod, Get(), Get(), Get()); + LOLUA_DECLARE_RETURN_METHOD_ARGS(AddTenInstance, GetPtr(), AddTenMethod, Get(), Get(), Get()); static int _AddTenInstance(lua_State* l) { auto stack = LuaStack::Begin(l); @@ -75,7 +75,7 @@ public: return (stack << i).End(); } - LOLUA_DECLARE_VOID_METHOD(SetX, GetPtr(), SetXMethod, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(SetX, GetPtr(), SetXMethod, Get()); static int _SetX(lua_State* l) { auto stack = LuaStack::Begin(l); @@ -198,7 +198,7 @@ public: testvalue_num: %.2f, testvalue_int: %i, testvalue_uint: %i, testvalue_str: %s.\n", testvalue_num, testvalue_int, testvalue_uint, testvalue_str.C()); msg::info("Lua Vars: \ - function_return: %s, loluademo_return: %i, loluademo_inst_return: %.f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n", + function_return: %s, loluademo_return: %i, loluademo_inst_return: %.2f, loluademo_getx: %i, loluademo_inst->m_x: %i.\n", function_return.C(), loluademo_return, loluademo_inst_return, loluademo_getx, loluademo_inst->m_x); #define /***/ _LOLUA_ARG_1(a00) (float)a00 diff --git a/src/easymesh/easymesh.h b/src/easymesh/easymesh.h index 8f93def3..31198e37 100644 --- a/src/easymesh/easymesh.h +++ b/src/easymesh/easymesh.h @@ -392,7 +392,11 @@ public: - offset : useless */ void AppendCog(int nbsides, float h, float d10, float d20, float d11, - float d21, float d12, float d22, float sidemul=0.f, bool offset=false); + float d21, float d12, float d22, float sidemul = 0.f, bool offset = false); + void AppendCog(int nbsides, float h, vec2 d0, vec2 d1, vec2 d2, float sidemul = 0.f, bool offset = false) + { + AppendCog(nbsides, h, d0.x, d0.y, d1.x, d1.y, d2.x, d2.y, sidemul, offset); + } //------------------------------------------------------------------------- //TODO : Mesh Bone operations diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp index 4edd141c..3c932b47 100644 --- a/src/easymesh/easymeshlua.cpp +++ b/src/easymesh/easymeshlua.cpp @@ -89,7 +89,8 @@ EasyMeshLuaObject* EasyMeshLuaObject::New(lua_State* l, int arg_nb) //----------------------------------------------------------------------------- const LuaObjectLibrary* EasyMeshLuaObject::GetLib() { - typedef EasyMeshLuaObject EMLO; +#define EMLO EasyMeshLuaObject + static const LuaObjectLibrary lib = LuaObjectLibrary( "EasyMesh", //Statics @@ -181,6 +182,8 @@ const LuaObjectLibrary* EasyMeshLuaObject::GetLib() //Variables { { nullptr, nullptr, nullptr } }); return &lib; + +#undef EMLO } //----------------------------------------------------------------------------- diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h index 5409fab3..6c786c55 100644 --- a/src/easymesh/easymeshlua.h +++ b/src/easymesh/easymeshlua.h @@ -29,589 +29,89 @@ public: static EasyMeshLuaObject* New(lua_State* l, int arg_nb); static const LuaObjectLibrary* GetLib(); - //------------------------------------------------------------------------- - static int AppendCylinder(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto nsides = s.Get(); - auto h = s.Get(); - auto d1 = s.Get(); - auto d2 = s.Get(); - auto dualside = s.Get(false); - auto smooth = s.Get(false); - auto close = s.Get(false); - m->m_instance.AppendCylinder(nsides, h, d1, d2, dualside, smooth, close); - return s.End(); - } - //LOLUA_DECLARE_VOID_METHOD(AppendSphere, GetPtr(), m_instance.AppendSphere, Get(), Get()); - static int AppendSphere(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto ndivisions = s.Get(); - auto d = s.Get(); - m->m_instance.AppendSphere(ndivisions, d); - return s.End(); - } +#define EMLO GetPtr() - static int AppendCapsule(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto ndivisions = s.Get(); - auto h = s.Get(); - auto d = s.Get(); - m->m_instance.AppendCapsule(ndivisions, h, d); - return s.End(); - } - static int AppendTorus(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto ndivisions = s.Get(); - auto d1 = s.Get(); - auto d2 = s.Get(); - m->m_instance.AppendTorus(ndivisions, d1, d2); - return s.End(); - } - static int AppendBox(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto size = s.Get(); - auto chamf = s.Get(0.f); - auto smooth = s.Get(false); - m->m_instance.AppendBox(size, chamf, smooth); - return s.End(); - } - static int AppendStar(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto nbranches = s.Get(); - auto d1 = s.Get(); - auto d2 = s.Get(); - auto fade = s.Get(false); - auto fade2 = s.Get(false); - m->m_instance.AppendStar(nbranches, d1, d2, fade, fade2); - return s.End(); - } - static int AppendExpandedStar(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto nbranches = s.Get(); - auto d1 = s.Get(); - auto d2 = s.Get(); - auto extrad = s.Get(0.f); - m->m_instance.AppendExpandedStar(nbranches, d1, d2, extrad); - return s.End(); - } - static int AppendDisc(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto nsides = s.Get(); - auto d = s.Get(); - auto fade = s.Get(false); - m->m_instance.AppendDisc(nsides, d, fade); - return s.End(); - } - static int AppendSimpleTriangle(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto d = s.Get(); - auto fade = s.Get(false); - m->m_instance.AppendSimpleTriangle(d, fade); - return s.End(); - } - static int AppendSimpleQuad(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto size = s.Get(); - auto fade = s.Get(false); - m->m_instance.AppendSimpleQuad(size, fade); - return s.End(); - } - static int AppendCog(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto nbsides = s.Get(); - auto h = s.Get(); - auto sidemul = s.Get(0.f); - auto d0 = s.Get(); - auto d1 = s.Get(); - auto d2 = s.Get(); - auto offset = s.Get(false); - m->m_instance.AppendCog(nbsides, h, d0.x, d0.y, d1.x, d1.y, d2.x, d2.y, sidemul, offset); - return s.End(); - } - //------------------------------------------------------------------------- - static int TranslateX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto f = s.Get(); - m->m_instance.TranslateX(f); - return s.End(); - } - static int TranslateY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto f = s.Get(); - m->m_instance.TranslateY(f); - return s.End(); - } - static int TranslateZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto f = s.Get(); - m->m_instance.TranslateZ(f); - return s.End(); - } - static int Translate(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto v = s.Get(); - m->m_instance.Translate(v); - return s.End(); - } - //------------------------------------------------------------------------- - static int RotateX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto a = s.Get(); - m->m_instance.RotateX(a); - return s.End(); - } - static int RotateY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto a = s.Get(); - m->m_instance.RotateY(a); - return s.End(); - } - static int RotateZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto a = s.Get(); - m->m_instance.RotateZ(a); - return s.End(); - } - static int Rotate(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto a = s.Get(); - auto v = s.Get(); - m->m_instance.Rotate(a, v); - return s.End(); - } - //------------------------------------------------------------------------- - static int ScaleX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - m->m_instance.ScaleX(x); - return s.End(); - } - static int ScaleY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto y = s.Get(); - m->m_instance.ScaleY(y); - return s.End(); - } - static int ScaleZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto z = s.Get(); - m->m_instance.ScaleZ(z); - return s.End(); - } - static int Scale(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto v = s.Get(); - m->m_instance.Scale(v); - return s.End(); - } - //------------------------------------------------------------------------- - static int RadialJitter(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto f = s.Get(); - m->m_instance.RadialJitter(f); - return s.End(); - } - //------------------------------------------------------------------------- - static int TaperX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto y = s.Get(); - auto z = s.Get(); - auto xoff = s.Get(0.f); - auto abs = s.Get(true); - m->m_instance.TaperX(y, z, xoff, abs); - return s.End(); - } - static int TaperY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - auto z = s.Get(); - auto yoff = s.Get(0.f); - auto abs = s.Get(true); - m->m_instance.TaperY(x, z, yoff, abs); - return s.End(); - } - static int TaperZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - auto y = s.Get(); - auto zoff = s.Get(0.f); - auto abs = s.Get(true); - m->m_instance.TaperZ(x, y, zoff, abs); - return s.End(); - } - //------------------------------------------------------------------------- - static int TwistX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.TwistX(t, toff); - return s.End(); - } - static int TwistY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.TwistY(t, toff); - return s.End(); - } - static int TwistZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.TwistZ(t, toff); - return s.End(); - } - //------------------------------------------------------------------------- - static int ShearX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto y = s.Get(); - auto z = s.Get(); - auto xoff = s.Get(0.f); - auto abs = s.Get(true); - m->m_instance.ShearX(y, z, xoff, abs); - return s.End(); - } - static int ShearY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - auto z = s.Get(); - auto yoff = s.Get(0.f); - auto abs = s.Get(true); - m->m_instance.ShearY(x, z, yoff, abs); - return s.End(); - } - static int ShearZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - auto y = s.Get(); - auto zoff = s.Get(0.f); - auto abs = s.Get(true); - m->m_instance.ShearZ(x, y, zoff, abs); - return s.End(); - } //------------------------------------------------------------------------- - static int StretchX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto y = s.Get(); - auto z = s.Get(); - auto xoff = s.Get(0.f); - m->m_instance.StretchX(y, z, xoff); - return s.End(); - } - static int StretchY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - auto z = s.Get(); - auto yoff = s.Get(0.f); - m->m_instance.StretchY(x, z, yoff); - return s.End(); - } - static int StretchZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto x = s.Get(); - auto y = s.Get(); - auto zoff = s.Get(0.f); - m->m_instance.StretchZ(x, y, zoff); - return s.End(); - } - //------------------------------------------------------------------------- - static int BendXY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.BendXY(t, toff); - return s.End(); - } - static int BendXZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.BendXZ(t, toff); - return s.End(); - } - static int BendYX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.BendYX(t, toff); - return s.End(); - } - static int BendYZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.BendYZ(t, toff); - return s.End(); - } - static int BendZX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.BendZX(t, toff); - return s.End(); - } - static int BendZY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto t = s.Get(); - auto toff = s.Get(0.f); - m->m_instance.BendZY(t, toff); - return s.End(); - } - //------------------------------------------------------------------------- - static int MirrorX(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.MirrorX(); - return s.End(); - } - static int MirrorY(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.MirrorY(); - return s.End(); - } - static int MirrorZ(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.MirrorZ(); - return s.End(); - } - //------------------------------------------------------------------------- - static int LoopStart(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto loopnb = s.Get(); - m->m_instance.LoopStart(loopnb); - return s.End(); - } - static int LoopEnd(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.LoopEnd(); - return s.End(); - } - static int OpenBrace(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.OpenBrace(); - return s.End(); - } - static int CloseBrace(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.CloseBrace(); - return s.End(); - } - //------------------------------------------------------------------------- - static int ToggleScaleWinding(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.ToggleScaleWinding(); - return s.End(); - } - static int ToggleQuadWeighting(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.ToggleQuadWeighting(); - return s.End(); - } - static int TogglePostBuildNormal(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.TogglePostBuildNormal(); - return s.End(); - } - static int ToggleVerticeNoCleanup(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.ToggleVerticeNoCleanup(); - return s.End(); - } - //------------------------------------------------------------------------- - static int VerticesMerge(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.VerticesMerge(); - return s.End(); - } - static int VerticesSeparate(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.VerticesSeparate(); - return s.End(); - } - static int VerticesCleanup(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - m->m_instance.VerticesCleanup(); - return s.End(); - } - //------------------------------------------------------------------------- - static int Duplicate(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto ds = s.Get(vec3(1.f)); - m->m_instance.DupAndScale(ds, true); - return s.End(); - } - static int Smooth(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto pass = s.Get(); - auto split_per_pass = s.Get(); - auto smooth_per_pass = s.Get(); - m->m_instance.SmoothMesh(pass, split_per_pass, smooth_per_pass); - return s.End(); - } - static int SplitTriangles(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto pass = s.Get(); - m->m_instance.SplitTriangles(pass); - return s.End(); - } - static int Chamfer(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto f = s.Get(); - m->m_instance.Chamfer(f); - return s.End(); - } - //------------------------------------------------------------------------- - static int SetCurColor(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto c = s.Get(); - m->m_instance.SetCurColor(c); - return s.End(); - } - static int SetCurColorA(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto c = s.Get(); - m->m_instance.SetCurColorA(c); - return s.End(); - } - static int SetCurColorB(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto c = s.Get(); - m->m_instance.SetCurColorB(c); - return s.End(); - } - static int SetVertColor(lua_State* l) - { - auto s = LuaStack::Begin(l); - auto m = s.GetPtr(); - auto c = s.Get(); - m->m_instance.SetVertColor(c); - return s.End(); - } + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendCylinder, EMLO, m_instance.AppendCylinder, Get(), Get(), Get(), Get(), Get(false), Get(false), Get(false)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendSphere, EMLO, m_instance.AppendSphere, Get(), Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendCapsule, EMLO, m_instance.AppendCapsule, Get(), Get(), Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendTorus, EMLO, m_instance.AppendTorus, Get(), Get(), Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendBox, EMLO, m_instance.AppendBox, Get(), Get(0.f), Get(false)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendStar, EMLO, m_instance.AppendStar, Get(), Get(), Get(), Get(false), Get(false)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendExpandedStar, EMLO, m_instance.AppendExpandedStar, Get(), Get(), Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendDisc, EMLO, m_instance.AppendDisc, Get(), Get(), Get(false)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendSimpleTriangle, EMLO, m_instance.AppendSimpleTriangle, Get(), Get(false)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendSimpleQuad, EMLO, m_instance.AppendSimpleQuad, Get(), Get(false)); + LOLUA_DECLARE_VOID_METHOD_ARGS(AppendCog, EMLO, m_instance.AppendCog, Get(), Get(), Get(), Get(), Get(), Get(0.f), Get(false)); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(TranslateX, EMLO, m_instance.TranslateX, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(TranslateY, EMLO, m_instance.TranslateY, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(TranslateZ, EMLO, m_instance.TranslateZ, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(Translate, EMLO, m_instance.Translate, Get()); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(RotateX, EMLO, m_instance.RotateX, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(RotateY, EMLO, m_instance.RotateY, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(RotateZ, EMLO, m_instance.RotateZ, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(Rotate, EMLO, m_instance.Rotate, Get(), Get()); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(ScaleX, EMLO, m_instance.ScaleX, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(ScaleY, EMLO, m_instance.ScaleY, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(ScaleZ, EMLO, m_instance.ScaleZ, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(Scale, EMLO, m_instance.Scale, Get()); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(RadialJitter, EMLO, m_instance.RadialJitter, Get()); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(TaperX, EMLO, m_instance.TaperX, Get(), Get(), Get(0.f), Get(true)); + LOLUA_DECLARE_VOID_METHOD_ARGS(TaperY, EMLO, m_instance.TaperY, Get(), Get(), Get(0.f), Get(true)); + LOLUA_DECLARE_VOID_METHOD_ARGS(TaperZ, EMLO, m_instance.TaperZ, Get(), Get(), Get(0.f), Get(true)); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(TwistX, EMLO, m_instance.TwistX, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(TwistY, EMLO, m_instance.TwistY, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(TwistZ, EMLO, m_instance.TwistZ, Get(), Get(0.f)); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(ShearX, EMLO, m_instance.ShearX, Get(), Get(), Get(0.f), Get(true)); + LOLUA_DECLARE_VOID_METHOD_ARGS(ShearY, EMLO, m_instance.ShearY, Get(), Get(), Get(0.f), Get(true)); + LOLUA_DECLARE_VOID_METHOD_ARGS(ShearZ, EMLO, m_instance.ShearZ, Get(), Get(), Get(0.f), Get(true)); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(StretchX, EMLO, m_instance.StretchX, Get(), Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(StretchY, EMLO, m_instance.StretchY, Get(), Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(StretchZ, EMLO, m_instance.StretchZ, Get(), Get(), Get(0.f)); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(BendXY, EMLO, m_instance.BendXY, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(BendXZ, EMLO, m_instance.BendXZ, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(BendYX, EMLO, m_instance.BendYX, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(BendYZ, EMLO, m_instance.BendYZ, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(BendZX, EMLO, m_instance.BendZX, Get(), Get(0.f)); + LOLUA_DECLARE_VOID_METHOD_ARGS(BendZY, EMLO, m_instance.BendZY, Get(), Get(0.f)); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_VOID(MirrorX, EMLO, m_instance.MirrorX); + LOLUA_DECLARE_VOID_METHOD_VOID(MirrorY, EMLO, m_instance.MirrorY); + LOLUA_DECLARE_VOID_METHOD_VOID(MirrorZ, EMLO, m_instance.MirrorZ); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(LoopStart, EMLO, m_instance.LoopStart, Get()); + LOLUA_DECLARE_VOID_METHOD_VOID(LoopEnd, EMLO, m_instance.LoopEnd); + LOLUA_DECLARE_VOID_METHOD_VOID(OpenBrace, EMLO, m_instance.OpenBrace, Get()); + LOLUA_DECLARE_VOID_METHOD_VOID(CloseBrace, EMLO, m_instance.CloseBrace, Get()); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_VOID(ToggleScaleWinding, EMLO, m_instance.ToggleScaleWinding); + LOLUA_DECLARE_VOID_METHOD_VOID(ToggleQuadWeighting, EMLO, m_instance.ToggleQuadWeighting); + LOLUA_DECLARE_VOID_METHOD_VOID(TogglePostBuildNormal, EMLO, m_instance.TogglePostBuildNormal); + LOLUA_DECLARE_VOID_METHOD_VOID(ToggleVerticeNoCleanup, EMLO, m_instance.ToggleVerticeNoCleanup); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_VOID(VerticesMerge, EMLO, m_instance.VerticesMerge); + LOLUA_DECLARE_VOID_METHOD_VOID(VerticesSeparate, EMLO, m_instance.VerticesSeparate); + LOLUA_DECLARE_VOID_METHOD_VOID(VerticesCleanup, EMLO, m_instance.VerticesCleanup); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(Duplicate, EMLO, m_instance.DupAndScale, Get(vec3(1.f)), Get(true)); + LOLUA_DECLARE_VOID_METHOD_ARGS(Smooth, EMLO, m_instance.SmoothMesh, Get(), Get(), Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(SplitTriangles, EMLO, m_instance.SplitTriangles, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(Chamfer, EMLO, m_instance.Chamfer, Get()); + //------------------------------------------------------------------------- + LOLUA_DECLARE_VOID_METHOD_ARGS(SetCurColor, EMLO, m_instance.SetCurColor, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(SetCurColorA, EMLO, m_instance.SetCurColorA, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(SetCurColorB, EMLO, m_instance.SetCurColorB, Get()); + LOLUA_DECLARE_VOID_METHOD_ARGS(SetVertColor, EMLO, m_instance.SetVertColor, Get()); + /* (csgu|csgunion) { return token::T_CSGUNION; } (csgs|CsgSub) { return token::T_CSGSUBSTRACT; } @@ -620,6 +120,7 @@ public: (csgx|csgxor) { return token::T_CSGXOR; } */ + #undef EMLO }; //----------------------------------------------------------------------------- diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index bf5d25c6..ca30fa35 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -437,18 +437,39 @@ private: //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -#define LOLUA_DECLARE_VOID_METHOD(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ +#define LOLUA_DECLARE_BEGIN(LUA_FUNC_NAME, INSTANCE_GET) \ static int LUA_FUNC_NAME(lua_State* l) \ { \ auto s = LuaStack::Begin(l); \ - auto o = s.INSTANCE_GET; \ - LOL_CALL(LOL_CAT(LOLUA_VAR_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)) \ - o->INSTANCE_CALL(LOL_CALL(LOL_CAT(LOLUA_ARG_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__))); \ + auto o = s.INSTANCE_GET; + +#define LOLUA_DECLARE_VARS(...) \ + LOL_CALL(LOL_CAT(LOLUA_VAR_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)); + +#define LOLUA_DECLARE_CALL(INSTANCE_CALL, ...) \ + o->INSTANCE_CALL(LOL_CALL(LOL_CAT(LOLUA_ARG_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__))); + +#define LOLUA_DECLARE_CALL_VOID(INSTANCE_CALL) \ + o->INSTANCE_CALL(); + +#define LOLUA_DECLARE_END \ return s.End(); \ } //----------------------------------------------------------------------------- -#define LOLUA_DECLARE_RETURN_METHOD(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ +#define LOLUA_DECLARE_VOID_METHOD_VOID(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ + LOLUA_DECLARE_BEGIN(LUA_FUNC_NAME, INSTANCE_GET); \ + LOLUA_DECLARE_CALL_VOID(INSTANCE_CALL) \ + LOLUA_DECLARE_END + +#define LOLUA_DECLARE_VOID_METHOD_ARGS(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ + LOLUA_DECLARE_BEGIN(LUA_FUNC_NAME, INSTANCE_GET); \ + LOLUA_DECLARE_VARS(__VA_ARGS__) \ + LOLUA_DECLARE_CALL(INSTANCE_CALL, __VA_ARGS__) \ + LOLUA_DECLARE_END + +//----------------------------------------------------------------------------- +#define LOLUA_DECLARE_RETURN_METHOD_ARGS(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ static int LUA_FUNC_NAME(lua_State* l) \ { \ auto s = LuaStack::Begin(l); \ From 575ef8e4970d3b552282c740bf64ba1ed0053998 Mon Sep 17 00:00:00 2001 From: touky Date: Thu, 3 Nov 2016 01:20:41 -0400 Subject: [PATCH 06/21] DebugDrawContext pass Seems to still build in lol, will check other works to be sure --- doc/tutorial/02_cube.cpp | 36 +++++ src/debug/lines.cpp | 317 +++++++++++++++++---------------------- src/lol/debug/lines.h | 237 ++++++++++++++++++----------- src/lolimgui.cpp | 2 +- src/scene.cpp | 56 +------ src/scene.h | 8 +- 6 files changed, 330 insertions(+), 326 deletions(-) diff --git a/doc/tutorial/02_cube.cpp b/doc/tutorial/02_cube.cpp index c500d3af..302abe4c 100644 --- a/doc/tutorial/02_cube.cpp +++ b/doc/tutorial/02_cube.cpp @@ -44,6 +44,22 @@ public: 4, 5, 1, 1, 0, 4, 3, 2, 6, 6, 7, 3, }), m_ready(false) { + m_camera = new Camera(); + m_camera->SetProjection(mat4::perspective(radians(30.f), 960.f, 600.f, .1f, 1000.f)); + m_camera->SetView(mat4::lookat(vec3(-15.f, 5.f, 0.f), + vec3(0.f, -1.f, 0.f), + vec3(0.f, 1.f, 0.f))); + Scene& scene = Scene::GetScene(); + scene.PushCamera(m_camera); + Ticker::Ref(m_camera); + + } + + ~Cube() + { + Scene& scene = Scene::GetScene(); + scene.PopCamera(m_camera); + Ticker::Unref(m_camera); } virtual void TickGame(float seconds) @@ -58,6 +74,25 @@ public: mat4 proj = mat4::perspective(radians(45.0f), 640.0f, 480.0f, 0.1f, 10.0f); m_matrix = proj * view * model * anim; + + { + auto context0 = Debug::DrawContext::New(Color::red); + { + auto context1 = Debug::DrawContext::New(Color::blue); + Debug::DrawBox(box3(vec3(0.f), vec3(1.2f))); + Debug::DrawGrid(vec3(0.f), vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.0f); + { + auto context2 = Debug::DrawContext::New(context0); + Debug::DrawBox(box3(vec3(0.f), vec3(1.3f))); + } + { + auto context2 = Debug::DrawContext::New(context0); + context2.SetColor(Color::yellow); + Debug::DrawBox(box3(vec3(-1.f), vec3(1.4f))); + } + } + Debug::DrawBox(box3(vec3(0.f), vec3(1.1f))); + } } virtual void TickDraw(float seconds, Scene &scene) @@ -115,6 +150,7 @@ public: } private: + Camera* m_camera = nullptr; float m_angle; mat4 m_matrix; array m_mesh; diff --git a/src/debug/lines.cpp b/src/debug/lines.cpp index d49b2d98..c7d8defa 100644 --- a/src/debug/lines.cpp +++ b/src/debug/lines.cpp @@ -12,60 +12,14 @@ namespace lol { +Debug::DrawContext::Data Debug::DrawContext::m_global = Debug::DrawContext::Data(vec4(1.f)); -//Resets draw infos -void Debug::DrawSetupReset() -{ - Scene& scene = Scene::GetScene(); - scene.SetLineTime(); - scene.SetLineMask(); -} -//Sets draw infos -void Debug::DrawSetupTime(float new_time) -{ - Scene& scene = Scene::GetScene(); - scene.SetLineTime(new_time); -} - -//-- -void Debug::DrawSetupMask(int new_mask) -{ - Scene& scene = Scene::GetScene(); - scene.SetLineMask(new_mask); -} - -//-- -void Debug::DrawSetupSegment(float new_segment_size) -{ - Scene& scene = Scene::GetScene(); - scene.SetLineSegmentSize(new_segment_size); -} - -//-- -void Debug::DrawSetupColor(vec4 color) -{ - Scene& scene = Scene::GetScene(); - scene.SetLineColor(color); -} - -//-- -void Debug::DrawSetup(float new_time, int new_mask, float segment_size, vec4 color) -{ - Debug::DrawSetupTime(new_time); - Debug::DrawSetupMask(new_mask); - Debug::DrawSetupSegment(segment_size); - Debug::DrawSetupColor(color); -} +typedef Debug::DrawContext DC; +typedef Debug::DrawContext::Data DCD; //Screen to world conversion -vec3 Debug::WorldToScreen(vec3 pos) -{ - return Debug::WorldToScreen(vec4(pos, 1.f)); -} -vec3 Debug::WorldToScreen(vec3 pos, mat4 view_proj) -{ - return Debug::WorldToScreen(vec4(pos, 1.f), view_proj); -} +vec3 Debug::WorldToScreen(vec3 pos) { return Debug::WorldToScreen(vec4(pos, 1.f)); } +vec3 Debug::WorldToScreen(vec3 pos, mat4 view_proj) { return Debug::WorldToScreen(vec4(pos, 1.f), view_proj); } vec3 Debug::WorldToScreen(vec4 pos) { Scene& scene = Scene::GetScene(); @@ -81,8 +35,7 @@ vec3 Debug::WorldToScreen(vec4 pos, mat4 view_proj) } //-- -vec3 Debug::ScreenToWorld(vec2 pos, float z) - { return Debug::ScreenToWorld(vec3(pos, z)); } +vec3 Debug::ScreenToWorld(vec2 pos, float z) { return Debug::ScreenToWorld(vec3(pos, z)); } vec3 Debug::ScreenToWorld(vec3 pos) { Scene& scene = Scene::GetScene(); @@ -91,8 +44,7 @@ vec3 Debug::ScreenToWorld(vec3 pos) mat4 const inv_view_proj = inverse(scene.GetCamera()->GetProjection() * scene.GetCamera()->GetView()); return Debug::ScreenToWorld(pos, inv_view_proj); } -vec3 Debug::ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z) - { return Debug::ScreenToWorld(vec3(pos, z), inv_view_proj); } +vec3 Debug::ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z) { return Debug::ScreenToWorld(vec3(pos, z), inv_view_proj); } vec3 Debug::ScreenToWorld(vec3 pos, mat4 inv_view_proj) { vec4 screen_pos = inv_view_proj * vec4(pos, 1.f); @@ -107,12 +59,16 @@ vec3 Debug::ScreenToWorld(vec3 pos, mat4 view, mat4 proj) //Draw stuff in World //-- LINE: 3D -2D - 3D_to_2D -------------------------------------------------- -void Debug::DrawLine(vec3 a, vec3 b) { Scene& scene = Scene::GetScene(); Debug::DrawLine(a, b, scene.GetLineColor()); } -void Debug::DrawLine(vec2 a, vec3 b, float az) { Scene& scene = Scene::GetScene(); Debug::DrawLine(a, b, scene.GetLineColor(), az); } -void Debug::DrawLine(vec2 a, vec2 b, float az, float bz) { Scene& scene = Scene::GetScene(); Debug::DrawLine(a, b, scene.GetLineColor(), az, bz); } -void Debug::DrawLine(vec3 a, vec3 b, vec4 color) { Scene& scene = Scene::GetScene(); scene.AddLine(a, b, color); } -void Debug::DrawLine(vec2 a, vec3 b, vec4 color, float az) { Scene& scene = Scene::GetScene(); scene.AddLine(vec3(a, az), b, color); } -void Debug::DrawLine(vec2 a, vec2 b, vec4 color, float az, float bz){ Scene& scene = Scene::GetScene(); scene.AddLine(vec3(a, az), vec3(b, bz), color); } +//Root func +void Debug::DrawLine(vec3 a, vec3 b, DCD data) +{ + Scene::GetScene().AddLine(a, b, data.m_color, data.m_duration, data.m_mask); +} +void Debug::DrawLine(vec2 a, vec3 b, DCD data, float az) { DrawLine(vec3(a, az), b, data); } +void Debug::DrawLine(vec2 a, vec2 b, DCD data, float az, float bz) { DrawLine(vec3(a, az), vec3(b, bz), data); } +void Debug::DrawLine(vec3 a, vec3 b) { DrawLine(a, b, DC::GetGlobalData()); } +void Debug::DrawLine(vec2 a, vec3 b, float az) { DrawLine(a, b, DC::GetGlobalData(), az); } +void Debug::DrawLine(vec2 a, vec2 b, float az, float bz) { DrawLine(a, b, DC::GetGlobalData(), az, bz); } //-- GIZMO -------------------------------------------------------------------- void Debug::DrawGizmo(vec3 pos, vec3 x, vec3 y, vec3 z, float size) @@ -131,8 +87,7 @@ void Debug::DrawGizmo(vec2 pos, vec3 x, vec3 y, vec3 z, float size, float posz) //-- GRID --------------------------------------------------------------------- void Debug::DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d) { - Scene& scene = Scene::GetScene(); - float seg_sz = scene.GetLineSegmentSize(); + float seg_sz = DC::GetGlobalData().m_segment_size; int seg_nb = lol::max((int)(size / seg_sz), 1); seg_sz = size / (float)seg_nb; @@ -165,38 +120,38 @@ void Debug::DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d) } //-- ARROW: 3D -2D - 3D_to_2D ------------------------------------------------- -void Debug::DrawArrow(vec3 a, vec3 b, vec2 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y)); } -void Debug::DrawArrow(vec2 a, vec3 b, vec2 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), az, b.z); } -void Debug::DrawArrow(vec2 a, vec2 b, vec2 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), az, bz); } -void Debug::DrawArrow(vec3 a, vec3 b, vec3 s) { Scene& scene = Scene::GetScene(); Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), scene.GetLineColor()); } -void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, float az) { Scene& scene = Scene::GetScene(); Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), scene.GetLineColor(), az, b.z); } -void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, float az, float bz) { Scene& scene = Scene::GetScene(); Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), scene.GetLineColor(), az, bz); } -void Debug::DrawArrow(vec3 a, vec3 b, vec3 s, vec4 color) +void Debug::DrawArrow(vec3 a, vec3 b, vec2 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y)); } +void Debug::DrawArrow(vec2 a, vec3 b, vec2 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), az, b.z); } +void Debug::DrawArrow(vec2 a, vec2 b, vec2 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), az, bz); } +void Debug::DrawArrow(vec3 a, vec3 b, vec3 s) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), DC::GetGlobalData()); } +void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, float az) { Debug::DrawArrow(a, b.xy, vec3(s.x, s.y, s.y), DC::GetGlobalData(), az, b.z); } +void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, float az, float bz) { Debug::DrawArrow(a, b, vec3(s.x, s.y, s.y), DC::GetGlobalData(), az, bz); } +void Debug::DrawArrow(vec3 a, vec3 b, vec3 s, DCD data) { vec3 z = s.x * normalize(b - a); vec3 x = s.z * orthonormal(b - a); vec3 y = s.y * cross(normalize(x), normalize(z)); - Debug::DrawLine(a, b, color); - Debug::DrawLine(b, b - z + x, color); - Debug::DrawLine(b, b - z - x, color); - Debug::DrawLine(b, b - z + y, color); - Debug::DrawLine(b, b - z - y, color); + Debug::DrawLine(a, b, data); + Debug::DrawLine(b, b - z + x, data); + Debug::DrawLine(b, b - z - x, data); + Debug::DrawLine(b, b - z + y, data); + Debug::DrawLine(b, b - z - y, data); - Debug::DrawLine(b - z + x, b - z + y, color); - Debug::DrawLine(b - z + x, b - z - y, color); - Debug::DrawLine(b - z - x, b - z + y, color); - Debug::DrawLine(b - z - x, b - z - y, color); + Debug::DrawLine(b - z + x, b - z + y, data); + Debug::DrawLine(b - z + x, b - z - y, data); + Debug::DrawLine(b - z - x, b - z + y, data); + Debug::DrawLine(b - z - x, b - z - y, data); - Debug::DrawLine(b - z + x, b - z - x, color); - Debug::DrawLine(b - z + y, b - z - y, color); + Debug::DrawLine(b - z + x, b - z - x, data); + Debug::DrawLine(b - z + y, b - z - y, data); } -void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, vec4 color, float az) +void Debug::DrawArrow(vec2 a, vec3 b, vec3 s, DCD data, float az) { vec3 bn = Debug::WorldToScreen(b); - DrawArrow(a, bn.xy, s, color, az, bn.z); + DrawArrow(a, bn.xy, s, data, az, bn.z); } -void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz) +void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, DCD data, float az, float bz) { vec3 an = vec3(a, az); vec3 bn = vec3(b, bz); @@ -204,40 +159,40 @@ void Debug::DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az, float bz) vec3 x = s.z * orthonormal(bn - an); vec3 y = s.y * cross(normalize(x), normalize(z)); - Debug::DrawLine(a, b, color, az, bz); - Debug::DrawLine(b, b - (z + x).xy, color, bz, bz - (z + x).z); - Debug::DrawLine(b, b - (z - x).xy, color, bz, bz - (z - x).z); - Debug::DrawLine(b, b - (z + y).xy, color, bz, bz - (z + y).z); - Debug::DrawLine(b, b - (z - y).xy, color, bz, bz - (z - y).z); + Debug::DrawLine(a, b, data, az, bz); + Debug::DrawLine(b, b - (z + x).xy, data, bz, bz - (z + x).z); + Debug::DrawLine(b, b - (z - x).xy, data, bz, bz - (z - x).z); + Debug::DrawLine(b, b - (z + y).xy, data, bz, bz - (z + y).z); + Debug::DrawLine(b, b - (z - y).xy, data, bz, bz - (z - y).z); - Debug::DrawLine(b - (z + x).xy, b - (z + y).xy, color, bz - (z + x).z, bz - (z + y).z); - Debug::DrawLine(b - (z + x).xy, b - (z - y).xy, color, bz - (z + x).z, bz - (z - y).z); - Debug::DrawLine(b - (z - x).xy, b - (z + y).xy, color, bz - (z - x).z, bz - (z + y).z); - Debug::DrawLine(b - (z - x).xy, b - (z - y).xy, color, bz - (z - x).z, bz - (z - y).z); + Debug::DrawLine(b - (z + x).xy, b - (z + y).xy, data, bz - (z + x).z, bz - (z + y).z); + Debug::DrawLine(b - (z + x).xy, b - (z - y).xy, data, bz - (z + x).z, bz - (z - y).z); + Debug::DrawLine(b - (z - x).xy, b - (z + y).xy, data, bz - (z - x).z, bz - (z + y).z); + Debug::DrawLine(b - (z - x).xy, b - (z - y).xy, data, bz - (z - x).z, bz - (z - y).z); - Debug::DrawLine(b - (z + x).xy, b - (z - x).xy, color, bz - (z + x).z, bz - (z - x).z); - Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, color, bz - (z + y).z, bz - (z - y).z); + Debug::DrawLine(b - (z + x).xy, b - (z - x).xy, data, bz - (z + x).z, bz - (z - x).z); + Debug::DrawLine(b - (z + y).xy, b - (z - y).xy, data, bz - (z + y).z, bz - (z - y).z); } //-- BOX: 3D -2D - 3D_to_2D --------------------------------------------------- -void Debug::DrawBox(box3 a) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, scene.GetLineColor()); } -void Debug::DrawBox(box2 a) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, scene.GetLineColor()); } -void Debug::DrawBox(box3 a, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, color); } -void Debug::DrawBox(box2 a, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, color); } -void Debug::DrawBox(vec3 a, vec3 b) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, b, scene.GetLineColor()); } -void Debug::DrawBox(vec2 a, vec2 b) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, b, scene.GetLineColor()); } -void Debug::DrawBox(vec2 a, float s) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, s, scene.GetLineColor()); } -void Debug::DrawBox(vec3 a, vec3 b, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, b, mat4::identity, color); } -void Debug::DrawBox(vec2 a, vec2 b, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, b, mat2::identity, color); } -void Debug::DrawBox(vec2 a, float s, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, s, mat2::identity, color); } -void Debug::DrawBox(box3 a, mat4 transform) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, transform, scene.GetLineColor()); } -void Debug::DrawBox(box2 a, mat2 transform) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, transform, scene.GetLineColor()); } -void Debug::DrawBox(box3 a, mat4 transform, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, transform, color); } -void Debug::DrawBox(box2 a, mat2 transform, vec4 color) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a.aa, a.bb, transform, color); } -void Debug::DrawBox(vec3 a, vec3 b, mat4 transform) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, b, transform, scene.GetLineColor()); } -void Debug::DrawBox(vec2 a, vec2 b, mat2 transform) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, b, transform, scene.GetLineColor()); } -void Debug::DrawBox(vec2 a, float s, mat2 transform) { Scene& scene = Scene::GetScene(); Debug::DrawBox(a, s, transform, scene.GetLineColor()); } -void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color) +void Debug::DrawBox(box3 a) { Debug::DrawBox(a.aa, a.bb, DC::GetGlobalData()); } +void Debug::DrawBox(box2 a) { Debug::DrawBox(a.aa, a.bb, DC::GetGlobalData()); } +void Debug::DrawBox(box3 a, DCD data) { Debug::DrawBox(a.aa, a.bb, data); } +void Debug::DrawBox(box2 a, DCD data) { Debug::DrawBox(a.aa, a.bb, data); } +void Debug::DrawBox(vec3 a, vec3 b) { Debug::DrawBox(a, b, DC::GetGlobalData()); } +void Debug::DrawBox(vec2 a, vec2 b) { Debug::DrawBox(a, b, DC::GetGlobalData()); } +void Debug::DrawBox(vec2 a, float s) { Debug::DrawBox(a, s, DC::GetGlobalData()); } +void Debug::DrawBox(vec3 a, vec3 b, DCD data) { Debug::DrawBox(a, b, mat4::identity, data); } +void Debug::DrawBox(vec2 a, vec2 b, DCD data) { Debug::DrawBox(a, b, mat2::identity, data); } +void Debug::DrawBox(vec2 a, float s, DCD data) { Debug::DrawBox(a, s, mat2::identity, data); } +void Debug::DrawBox(box3 a, mat4 transform) { Debug::DrawBox(a.aa, a.bb, transform, DC::GetGlobalData()); } +void Debug::DrawBox(box2 a, mat2 transform) { Debug::DrawBox(a.aa, a.bb, transform, DC::GetGlobalData()); } +void Debug::DrawBox(box3 a, mat4 transform, DCD data) { Debug::DrawBox(a.aa, a.bb, transform, data); } +void Debug::DrawBox(box2 a, mat2 transform, DCD data) { Debug::DrawBox(a.aa, a.bb, transform, data); } +void Debug::DrawBox(vec3 a, vec3 b, mat4 transform) { Debug::DrawBox(a, b, transform, DC::GetGlobalData()); } +void Debug::DrawBox(vec2 a, vec2 b, mat2 transform) { Debug::DrawBox(a, b, transform, DC::GetGlobalData()); } +void Debug::DrawBox(vec2 a, float s, mat2 transform) { Debug::DrawBox(a, s, transform, DC::GetGlobalData()); } +void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, DCD data) { vec4 v[8]; for (int i = 0; i < 8; i++) @@ -252,12 +207,12 @@ void Debug::DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color) { int j = ((i & 1) << 1) | ((i >> 1) ^ 1); - Debug::DrawLine((transform * v[i]).xyz, (transform * v[i + 4]).xyz, color); - Debug::DrawLine((transform * v[i]).xyz, (transform * v[j]).xyz, color); - Debug::DrawLine((transform * v[i + 4]).xyz, (transform * v[j + 4]).xyz, color); + Debug::DrawLine((transform * v[i]).xyz, (transform * v[i + 4]).xyz, data); + Debug::DrawLine((transform * v[i]).xyz, (transform * v[j]).xyz, data); + Debug::DrawLine((transform * v[i + 4]).xyz, (transform * v[j + 4]).xyz, data); } } -void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color) +void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, DCD data) { vec2 v[4]; v[0] = a; @@ -266,41 +221,41 @@ void Debug::DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color) v[3] = vec2(b.x, a.y); int i = 0; - Debug::DrawLine((transform * v[0]).xy, (transform * v[1]).xy, color); - Debug::DrawLine((transform * v[1]).xy, (transform * v[2]).xy, color); - Debug::DrawLine((transform * v[2]).xy, (transform * v[3]).xy, color); - Debug::DrawLine((transform * v[3]).xy, (transform * v[0]).xy, color); + Debug::DrawLine((transform * v[0]).xy, (transform * v[1]).xy, data); + Debug::DrawLine((transform * v[1]).xy, (transform * v[2]).xy, data); + Debug::DrawLine((transform * v[2]).xy, (transform * v[3]).xy, data); + Debug::DrawLine((transform * v[3]).xy, (transform * v[0]).xy, data); } -void Debug::DrawBox(vec2 a, float s, mat2 transform, vec4 color) +void Debug::DrawBox(vec2 a, float s, mat2 transform, DCD data) { vec2 b = s * vec2(1.f, Renderer::Get()->GetXYRatio()); - Debug::DrawBox(a - b, a + b, transform, color); + Debug::DrawBox(a - b, a + b, transform, data); } //-- CIRCLE ------------------------------------------------------------------- -void Debug::DrawCircle(vec2 a, float s) { Scene& scene = Scene::GetScene(); Debug::DrawCircle(a, s * vec2(1.f, Renderer::Get()->GetXYRatio()), scene.GetLineColor()); } -void Debug::DrawCircle(vec3 a, vec3 n) { Scene& scene = Scene::GetScene(); Debug::DrawCircle(a, n, scene.GetLineColor()); } -void Debug::DrawCircle(vec2 a, vec2 s) { Scene& scene = Scene::GetScene(); Debug::DrawCircle(a, s * vec2(1.f, Renderer::Get()->GetXYRatio()), scene.GetLineColor()); } -void Debug::DrawCircle(vec3 a, vec3 x, vec3 y) { Scene& scene = Scene::GetScene(); Debug::DrawCircle(a, x, y, scene.GetLineColor()); } -void Debug::DrawCircle(vec2 a, vec2 x, vec2 y) { Scene& scene = Scene::GetScene(); Debug::DrawCircle(a, x, y, scene.GetLineColor()); } -void Debug::DrawCircle(vec3 a, vec3 n, vec4 color) +void Debug::DrawCircle(vec2 a, float s) { Debug::DrawCircle(a, s * vec2(1.f, Renderer::Get()->GetXYRatio()), DC::GetGlobalData()); } +void Debug::DrawCircle(vec3 a, vec3 n) { Debug::DrawCircle(a, n, DC::GetGlobalData()); } +void Debug::DrawCircle(vec2 a, vec2 s) { Debug::DrawCircle(a, s * vec2(1.f, Renderer::Get()->GetXYRatio()), DC::GetGlobalData()); } +void Debug::DrawCircle(vec3 a, vec3 x, vec3 y) { Debug::DrawCircle(a, x, y, DC::GetGlobalData()); } +void Debug::DrawCircle(vec2 a, vec2 x, vec2 y) { Debug::DrawCircle(a, x, y, DC::GetGlobalData()); } +void Debug::DrawCircle(vec3 a, vec3 n, DCD data) { vec3 x = orthogonal(n); vec3 y = cross(normalize(n), normalize(x)) * length(n); - DrawCircle(a, x, y, color); + DrawCircle(a, x, y, data); } -void Debug::DrawCircle(vec2 a, vec2 s, vec4 color) +void Debug::DrawCircle(vec2 a, vec2 s, DCD data) { vec2 x = vec2::axis_x * s.x; vec2 y = vec2::axis_y * s.y; - DrawCircle(a, x, y, color); + DrawCircle(a, x, y, data); } //-- -void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color) +void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, DCD data) { Scene& scene = Scene::GetScene(); float size = F_PI * 2.f * lol::max(length(x), length(y)); - int segment_nb = lol::max(1, (int)((size * .25f) / scene.GetLineSegmentSize())); + int segment_nb = lol::max(1, (int)((size * .25f) / data.m_segment_size)); for (int i = 0; i < segment_nb; i++) { float a0 = (((float)i) / (float)segment_nb) * F_PI_2; @@ -308,18 +263,18 @@ void Debug::DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color) vec2 p0 = vec2(lol::cos(a0), lol::sin(a0)); vec2 p1 = vec2(lol::cos(a1), lol::sin(a1)); - Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color); - Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color); - Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color); - Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color); + Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, data); + Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, data); + Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, data); + Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, data); } } //-- -void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color) +void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, DCD data) { Scene& scene = Scene::GetScene(); float size = F_PI * 2.f * lol::max(length(x), length(y)); - int segment_nb = lol::max(1, (int)((size * .25f) / scene.GetLineSegmentSize())); + int segment_nb = lol::max(1, (int)((size * .25f) / data.m_segment_size)); for (int i = 0; i < segment_nb; i++) { float a0 = (((float)i) / (float)segment_nb) * F_PI_2; @@ -327,52 +282,52 @@ void Debug::DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color) vec2 p0 = vec2(lol::cos(a0), lol::sin(a0)); vec2 p1 = vec2(lol::cos(a1), lol::sin(a1)); - Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, color); - Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, color); - Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, color); - Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, color); + Debug::DrawLine(a + p0.x * x + p0.y * y, a + p1.x * x + p1.y * y, data); + Debug::DrawLine(a + p0.x * -x + p0.y * -y, a + p1.x * -x + p1.y * -y, data); + Debug::DrawLine(a + p0.x * x + p0.y * -y, a + p1.x * x + p1.y * -y, data); + Debug::DrawLine(a + p0.x * -x + p0.y * y, a + p1.x * -x + p1.y * y, data); } } //-- SPHERE ------------------------------------------------------------------- -void Debug::DrawSphere(vec3 a, float s) { Scene& scene = Scene::GetScene(); Debug::DrawSphere(a, s, scene.GetLineColor()); } -void Debug::DrawSphere(vec3 a, float s, vec4 color) { Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, color); } -void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z) { Scene& scene = Scene::GetScene(); Debug::DrawSphere(a, x, y, z, scene.GetLineColor()); } -void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color) -{ - Debug::DrawCircle(a, x, y, color); - Debug::DrawCircle(a, x, (y + z) * .707f, color); - Debug::DrawCircle(a, x, (y - z) * .707f, color); - Debug::DrawCircle(a, z, x, color); - Debug::DrawCircle(a, z, (x + y) * .707f, color); - Debug::DrawCircle(a, z, (x - y) * .707f, color); - Debug::DrawCircle(a, y, z, color); - Debug::DrawCircle(a, y, (z + x) * .707f, color); - Debug::DrawCircle(a, y, (z - x) * .707f, color); +void Debug::DrawSphere(vec3 a, float s) { Debug::DrawSphere(a, s, DC::GetGlobalData()); } +void Debug::DrawSphere(vec3 a, float s, DCD data) { Debug::DrawSphere(a, vec3::axis_x * s, vec3::axis_y * s, vec3::axis_z * s, data); } +void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z) { Debug::DrawSphere(a, x, y, z, DC::GetGlobalData()); } +void Debug::DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, DCD data) +{ + Debug::DrawCircle(a, x, y, data); + Debug::DrawCircle(a, x, (y + z) * .707f, data); + Debug::DrawCircle(a, x, (y - z) * .707f, data); + Debug::DrawCircle(a, z, x, data); + Debug::DrawCircle(a, z, (x + y) * .707f, data); + Debug::DrawCircle(a, z, (x - y) * .707f, data); + Debug::DrawCircle(a, y, z, data); + Debug::DrawCircle(a, y, (z + x) * .707f, data); + Debug::DrawCircle(a, y, (z - x) * .707f, data); } //-- CAPSULE ------------------------------------------------------------------ -void Debug::DrawCapsule(vec3 a, float s, vec3 h) { Scene& scene = Scene::GetScene(); Debug::DrawCapsule(a, s, h, scene.GetLineColor()); } -void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h) { Scene& scene = Scene::GetScene(); Debug::DrawCapsule(a, x, y, z, h, scene.GetLineColor()); } -void Debug::DrawCapsule(vec3 a, float s, vec3 h, vec4 color) +void Debug::DrawCapsule(vec3 a, float s, vec3 h) { Debug::DrawCapsule(a, s, h, DC::GetGlobalData()); } +void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h) { Debug::DrawCapsule(a, x, y, z, h, DC::GetGlobalData()); } +void Debug::DrawCapsule(vec3 a, float s, vec3 h, DCD data) { vec3 x = orthonormal(h) * s; vec3 y = cross(normalize(h), normalize(x)) * s; - Debug::DrawCapsule(a, x, y, normalize(h) * s, h, color); + Debug::DrawCapsule(a, x, y, normalize(h) * s, h, data); } //-- -void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color) +void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, DCD data) { Scene& scene = Scene::GetScene(); float size = F_PI * 2.f * lol::max(length(x), length(y)); - int segment_nb = lol::max(1, (int)((size * .25f) / scene.GetLineSegmentSize())); + int segment_nb = lol::max(1, (int)((size * .25f) / data.m_segment_size)); for (int i = -1; i < 2; i += 2) { vec3 b = a + h * .5f * (float)i; vec3 c = a - h * .5f * (float)i; - Debug::DrawCircle(b, x, y, color); - Debug::DrawLine(b + x * (float)i, c + x * (float)i, color); - Debug::DrawLine(b + y * (float)i, c + y * (float)i, color); + Debug::DrawCircle(b, x, y, data); + Debug::DrawLine(b + x * (float)i, c + x * (float)i, data); + Debug::DrawLine(b + y * (float)i, c + y * (float)i, data); for (int j = 0; j < segment_nb; j++) { float a0 = (((float)j) / (float)segment_nb) * F_PI_2; @@ -380,18 +335,18 @@ void Debug::DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color) vec2 p0 = vec2(lol::cos(a0), lol::sin(a0)); vec2 p1 = vec2(lol::cos(a1), lol::sin(a1)); - Debug::DrawLine(b + p0.x * x + p0.y * z * (float)i, b + p1.x * x + p1.y * z * (float)i, color); - Debug::DrawLine(b + p0.x * -x + p0.y * z * (float)i, b + p1.x * -x + p1.y * z * (float)i, color); - Debug::DrawLine(b + p0.x * y + p0.y * z * (float)i, b + p1.x * y + p1.y * z * (float)i, color); - Debug::DrawLine(b + p0.x * -y + p0.y * z * (float)i, b + p1.x * -y + p1.y * z * (float)i, color); + Debug::DrawLine(b + p0.x * x + p0.y * z * (float)i, b + p1.x * x + p1.y * z * (float)i, data); + Debug::DrawLine(b + p0.x * -x + p0.y * z * (float)i, b + p1.x * -x + p1.y * z * (float)i, data); + Debug::DrawLine(b + p0.x * y + p0.y * z * (float)i, b + p1.x * y + p1.y * z * (float)i, data); + Debug::DrawLine(b + p0.x * -y + p0.y * z * (float)i, b + p1.x * -y + p1.y * z * (float)i, data); } } } //-- VIEW PROJ ---------------------------------------------------------------- -void Debug::DrawViewProj(mat4 view, mat4 proj) { Scene& scene = Scene::GetScene(); Debug::DrawViewProj(view, proj, scene.GetLineColor()); } -void Debug::DrawViewProj(mat4 view_proj) { Scene& scene = Scene::GetScene(); Debug::DrawViewProj(view_proj, scene.GetLineColor()); } -void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color) +void Debug::DrawViewProj(mat4 view, mat4 proj) { Debug::DrawViewProj(view, proj, DC::GetGlobalData()); } +void Debug::DrawViewProj(mat4 view_proj) { Debug::DrawViewProj(view_proj, DC::GetGlobalData()); } +void Debug::DrawViewProj(mat4 view, mat4 proj, DCD data) { mat4 const view_proj = proj * view; @@ -409,13 +364,13 @@ void Debug::DrawViewProj(mat4 view, mat4 proj, vec4 color) //Draw near for (int i = 0; i < 4; i++) - Debug::DrawLine(p[i].xyz, p0, color); + Debug::DrawLine(p[i].xyz, p0, data); - Debug::DrawViewProj(view_proj, color); + Debug::DrawViewProj(view_proj, data); } //-- -void Debug::DrawViewProj(mat4 view_proj, vec4 color) +void Debug::DrawViewProj(mat4 view_proj, DCD data) { //Near plane mat4 const inv_view_proj = inverse(view_proj); @@ -433,16 +388,16 @@ void Debug::DrawViewProj(mat4 view_proj, vec4 color) //Draw near for (int i = 0; i < 4; i++) - Debug::DrawLine(p[i].xyz, p[(i + 1) % 4].xyz, color); + Debug::DrawLine(p[i].xyz, p[(i + 1) % 4].xyz, data); //Draw far for (int i = 4; i < 8; i++) - Debug::DrawLine(p[i].xyz, p[(i - 4 + 1) % 4 + 4].xyz, color); + Debug::DrawLine(p[i].xyz, p[(i - 4 + 1) % 4 + 4].xyz, data); //Draw near to far for (int i = 0; i < 4; i++) - Debug::DrawLine(p[i].xyz, p[i + 4].xyz, color); + Debug::DrawLine(p[i].xyz, p[i + 4].xyz, data); //Draw diagonal for (int i = 2; i < 6; i++) - Debug::DrawLine(p[i].xyz, p[i + ((i < 4)?(-2):(+2))].xyz, color); + Debug::DrawLine(p[i].xyz, p[i + ((i < 4)?(-2):(+2))].xyz, data); } } /* namespace lol */ diff --git a/src/lol/debug/lines.h b/src/lol/debug/lines.h index e62d0275..faaa2ebe 100644 --- a/src/lol/debug/lines.h +++ b/src/lol/debug/lines.h @@ -22,96 +22,157 @@ namespace lol namespace Debug { + class DrawContext + { + public: + class Data + { + public: + float m_duration = 0.0f; + int m_mask = 0; + float m_segment_size = 8; //Resets draw infos -void DrawSetupReset(); -//Sets draw infos -void DrawSetupTime(float new_time); -void DrawSetupMask(int new_mask); -void DrawSetupSegment(float segment_size); -void DrawSetupColor(vec4 color); -void DrawSetup(float new_time, int new_mask, float segment_size, vec4 color); - -//Screen to world conversion -vec3 WorldToScreen(vec3 pos); -vec3 WorldToScreen(vec3 pos, mat4 view_proj); -vec3 WorldToScreen(vec4 pos); -vec3 WorldToScreen(vec4 pos, mat4 view_proj); -vec3 ScreenToWorld(vec2 pos, float z=-1.f); -vec3 ScreenToWorld(vec3 pos); -vec3 ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z=-1.f); -vec3 ScreenToWorld(vec3 pos, mat4 inv_view_proj); -vec3 ScreenToWorld(vec3 pos, mat4 view, mat4 proj); - -//Draw stuff in World -//-- LINE -void DrawLine(vec3 a, vec3 b); -void DrawLine(vec2 a, vec3 b, float az=-1.f); -void DrawLine(vec2 a, vec2 b, float az=-1.f, float bz=-1.f); -void DrawLine(vec3 a, vec3 b, vec4 color); -void DrawLine(vec2 a, vec3 b, vec4 color, float az=-1.f); -void DrawLine(vec2 a, vec2 b, vec4 color, float az=-1.f, float bz=-1.f); -//-- GIZMO -void DrawGizmo(vec3 pos, vec3 x, vec3 y, vec3 z, float size); -void DrawGizmo(vec2 pos, vec3 x, vec3 y, vec3 z, float size, float posz = -1.f); -//-- GRID -void DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d=false); -//-- ARROW -void DrawArrow(vec3 a, vec3 b, vec2 s); -void DrawArrow(vec2 a, vec3 b, vec2 s, float az=-1.f); -void DrawArrow(vec2 a, vec2 b, vec2 s, float az=-1.f, float bz=-1.f); -void DrawArrow(vec3 a, vec3 b, vec3 s); -void DrawArrow(vec2 a, vec3 b, vec3 s, float az=-1.f); -void DrawArrow(vec2 a, vec2 b, vec3 s, float az=-1.f, float bz=-1.f); -void DrawArrow(vec3 a, vec3 b, vec3 s, vec4 color); -void DrawArrow(vec2 a, vec3 b, vec3 s, vec4 color, float az=-1.f); -void DrawArrow(vec2 a, vec2 b, vec3 s, vec4 color, float az=-1.f, float bz=-1.f); -//-- BOX -void DrawBox(box3 a); -void DrawBox(box2 a); -void DrawBox(box3 a, vec4 color); -void DrawBox(box2 a, vec4 color); -void DrawBox(vec3 a, vec3 b); -void DrawBox(vec2 a, vec2 b); -void DrawBox(vec2 a, float s); -void DrawBox(vec3 a, vec3 b, vec4 color); -void DrawBox(vec2 a, vec2 b, vec4 color); -void DrawBox(vec2 a, float s, vec4 color); -void DrawBox(box3 a, mat4 transform); -void DrawBox(box2 a, mat2 transform); -void DrawBox(box3 a, mat4 transform, vec4 color); -void DrawBox(box2 a, mat2 transform, vec4 color); -void DrawBox(vec3 a, vec3 b, mat4 transform); -void DrawBox(vec2 a, vec2 b, mat2 transform); -void DrawBox(vec2 a, float s, mat2 transform); -void DrawBox(vec3 a, vec3 b, mat4 transform, vec4 color); -void DrawBox(vec2 a, vec2 b, mat2 transform, vec4 color); -void DrawBox(vec2 a, float s, mat2 transform, vec4 color); -//-- CIRCLE -void DrawCircle(vec2 a, float s); -void DrawCircle(vec3 a, vec3 n); -void DrawCircle(vec2 a, vec2 s); -void DrawCircle(vec3 a, vec3 n, vec4 color); -void DrawCircle(vec2 a, vec2 s, vec4 color); -void DrawCircle(vec3 a, vec3 x, vec3 y); -void DrawCircle(vec2 a, vec2 x, vec2 y); -void DrawCircle(vec3 a, vec3 x, vec3 y, vec4 color); -void DrawCircle(vec2 a, vec2 x, vec2 y, vec4 color); -//-- SPHERE -void DrawSphere(vec3 a, float s); -void DrawSphere(vec3 a, float s, vec4 color); -void DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z); -void DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, vec4 color); -//-- CAPSULE -void DrawCapsule(vec3 a, float s, vec3 h); -void DrawCapsule(vec3 a, float s, vec3 h, vec4 color); -void DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h); -void DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, vec4 color); -//-- VIEW PROJ -void DrawViewProj(mat4 view_proj); -void DrawViewProj(mat4 view_proj, vec4 color); -void DrawViewProj(mat4 view, mat4 proj); -void DrawViewProj(mat4 view, mat4 proj, vec4 color); + Data() {} + Data(vec4 color, float duration = -1.f, int mask = 0xFFFFFFFF, float segment_size = 1.f) + { + m_color = color; + m_duration = duration; + m_mask = mask; + m_segment_size = segment_size; + } + inline Data& operator=(vec4 const& color) { m_color = color; return *this; } + }; + + private: + static Data m_global; + Data m_previous; + Data m_current; + + DrawContext(vec4 color, float duration, int mask, float segment_size) + { + //Store old datas + m_previous.m_color = m_global.m_color; + m_previous.m_duration = m_global.m_duration; + m_previous.m_mask = m_global.m_mask; + m_previous.m_segment_size = m_global.m_segment_size; + + //Create new one + SetColor(color); + SetDuration(duration); + SetMask(mask); + SetSegmentSize(segment_size); + } + + public: + ~DrawContext() + { + m_global.m_color = m_previous.m_color; + m_global.m_duration = m_previous.m_duration; + m_global.m_mask = m_previous.m_mask; + m_global.m_segment_size = m_previous.m_segment_size; + } + + static DrawContext New(vec4 color, float duration = -1.f, int mask = 0xFFFFFFFF, float segment_size = 1.f) + { + return DrawContext(color, duration, mask, segment_size); + } + + static DrawContext New(DrawContext& dc) + { + return DrawContext(dc.m_current.m_color, dc.m_current.m_duration, dc.m_current.m_mask, dc.m_current.m_segment_size); + } + + static Data GetGlobalData() { return m_global; } + + //Setup methods + void SetColor(vec4 color) { m_global.m_color = m_current.m_color = color; } + void SetDuration(float duration) { m_global.m_duration = m_current.m_duration = duration; } + void SetMask(int mask) { m_global.m_mask = m_current.m_mask = mask; } + void SetSegmentSize(float segment_size) { m_global.m_segment_size = m_current.m_segment_size = segment_size; } + }; + + //Screen to world conversion + vec3 WorldToScreen(vec3 pos); + vec3 WorldToScreen(vec3 pos, mat4 view_proj); + vec3 WorldToScreen(vec4 pos); + vec3 WorldToScreen(vec4 pos, mat4 view_proj); + vec3 ScreenToWorld(vec2 pos, float z=-1.f); + vec3 ScreenToWorld(vec3 pos); + vec3 ScreenToWorld(vec2 pos, mat4 inv_view_proj, float z=-1.f); + vec3 ScreenToWorld(vec3 pos, mat4 inv_view_proj); + vec3 ScreenToWorld(vec3 pos, mat4 view, mat4 proj); + + //Draw stuff in World + //Draw stuff in World + //-- LINE + void DrawLine(vec3 a, vec3 b, DrawContext::Data data); + void DrawLine(vec2 a, vec3 b, DrawContext::Data data, float az = -1.f); + void DrawLine(vec2 a, vec2 b, DrawContext::Data data, float az = -1.f, float bz = -1.f); + void DrawLine(vec3 a, vec3 b); + void DrawLine(vec2 a, vec3 b, float az = -1.f); + void DrawLine(vec2 a, vec2 b, float az = -1.f, float bz = -1.f); + //-- GIZMO + void DrawGizmo(vec3 pos, vec3 x, vec3 y, vec3 z, float size); + void DrawGizmo(vec2 pos, vec3 x, vec3 y, vec3 z, float size, float posz = -1.f); + //-- GRID + void DrawGrid(vec3 pos, vec3 x, vec3 y, vec3 z, float size, bool draw_3d=false); + //-- ARROW + void DrawArrow(vec3 a, vec3 b, vec2 s); + void DrawArrow(vec2 a, vec3 b, vec2 s, float az=-1.f); + void DrawArrow(vec2 a, vec2 b, vec2 s, float az=-1.f, float bz=-1.f); + void DrawArrow(vec3 a, vec3 b, vec3 s); + void DrawArrow(vec2 a, vec3 b, vec3 s, float az=-1.f); + void DrawArrow(vec2 a, vec2 b, vec3 s, float az=-1.f, float bz=-1.f); + void DrawArrow(vec3 a, vec3 b, vec3 s, DrawContext::Data data); + void DrawArrow(vec2 a, vec3 b, vec3 s, DrawContext::Data data, float az=-1.f); + void DrawArrow(vec2 a, vec2 b, vec3 s, DrawContext::Data data, float az=-1.f, float bz=-1.f); + //-- BOX + void DrawBox(box3 a); + void DrawBox(box2 a); + void DrawBox(box3 a, DrawContext::Data data); + void DrawBox(box2 a, DrawContext::Data data); + void DrawBox(vec3 a, vec3 b); + void DrawBox(vec2 a, vec2 b); + void DrawBox(vec2 a, float s); + void DrawBox(vec3 a, vec3 b, DrawContext::Data data); + void DrawBox(vec2 a, vec2 b, DrawContext::Data data); + void DrawBox(vec2 a, float s, DrawContext::Data data); + void DrawBox(box3 a, mat4 transform); + void DrawBox(box2 a, mat2 transform); + void DrawBox(box3 a, mat4 transform, DrawContext::Data data); + void DrawBox(box2 a, mat2 transform, DrawContext::Data data); + void DrawBox(vec3 a, vec3 b, mat4 transform); + void DrawBox(vec2 a, vec2 b, mat2 transform); + void DrawBox(vec2 a, float s, mat2 transform); + void DrawBox(vec3 a, vec3 b, mat4 transform, DrawContext::Data data); + void DrawBox(vec2 a, vec2 b, mat2 transform, DrawContext::Data data); + void DrawBox(vec2 a, float s, mat2 transform, DrawContext::Data data); + //-- CIRCLE + void DrawCircle(vec2 a, float s); + void DrawCircle(vec3 a, vec3 n); + void DrawCircle(vec2 a, vec2 s); + void DrawCircle(vec3 a, vec3 n, DrawContext::Data data); + void DrawCircle(vec2 a, vec2 s, DrawContext::Data data); + void DrawCircle(vec3 a, vec3 x, vec3 y); + void DrawCircle(vec2 a, vec2 x, vec2 y); + void DrawCircle(vec3 a, vec3 x, vec3 y, DrawContext::Data data); + void DrawCircle(vec2 a, vec2 x, vec2 y, DrawContext::Data data); + //-- SPHERE + void DrawSphere(vec3 a, float s); + void DrawSphere(vec3 a, float s, DrawContext::Data data); + void DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z); + void DrawSphere(vec3 a, vec3 x, vec3 y, vec3 z, DrawContext::Data data); + //-- CAPSULE + void DrawCapsule(vec3 a, float s, vec3 h); + void DrawCapsule(vec3 a, float s, vec3 h, DrawContext::Data data); + void DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h); + void DrawCapsule(vec3 a, vec3 x, vec3 y, vec3 z, vec3 h, DrawContext::Data data); + //-- VIEW PROJ + void DrawViewProj(mat4 view_proj); + void DrawViewProj(mat4 view_proj, DrawContext::Data data); + void DrawViewProj(mat4 view, mat4 proj); + void DrawViewProj(mat4 view, mat4 proj, DrawContext::Data data); } /* namespace Debug */ diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index 70a612b1..a2d0b944 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -419,7 +419,7 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) //<\Debug render> ------------------------------------------------- //----------------------------------------------------------------- #endif //SHOW_IMGUI_DEBUG - Debug::DrawLine(vec2::zero, vec2::axis_x, Color::green); + //Debug::DrawLine(vec2::zero, vec2::axis_x /*, Color::green*/); m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, pcmd->ElemCount, (const short*)idx_buffer_offset); diff --git a/src/scene.cpp b/src/scene.cpp index c741e271..bbfcc9e5 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -168,10 +168,10 @@ private: /* Old line API */ struct line_api { - float m_time, m_segment_size; - vec4 m_color; + //float m_duration, m_segment_size; + //vec4 m_color; array m_lines; - int m_mask, m_debug_mask; + int /*m_mask,*/ m_debug_mask; Shader *m_shader; VertexDeclaration *m_vdecl; } @@ -241,11 +241,6 @@ Scene::Scene(ivec2 size) data->m_line_api.m_vdecl = new VertexDeclaration(VertexStream(VertexUsage::Position, VertexUsage::Color)); data->m_line_api.m_debug_mask = 1; - - SetLineTime(); - SetLineMask(); - SetLineSegmentSize(); - SetLineColor(); } //----------------------------------------------------------------------------- @@ -533,56 +528,19 @@ void Scene::AddTile(TileSet *tileset, int id, mat4 model) } //----------------------------------------------------------------------------- -void Scene::SetLineTime(float new_time) -{ - ASSERT(!!data, "Trying to access a non-ready scene"); - - data->m_line_api.m_time = new_time; -} - -void Scene::SetLineMask(int new_mask) -{ - ASSERT(!!data, "Trying to access a non-ready scene"); - - data->m_line_api.m_mask = new_mask; -} - -void Scene::SetLineSegmentSize(float new_segment_size) -{ - ASSERT(!!data, "Trying to access a non-ready scene"); - - data->m_line_api.m_segment_size = new_segment_size; -} - -void Scene::SetLineColor(vec4 new_color) -{ - ASSERT(!!data, "Trying to access a non-ready scene"); - - data->m_line_api.m_color = new_color; -} - -//----------------------------------------------------------------------------- -float Scene::GetLineSegmentSize() -{ - ASSERT(!!data, "Trying to access a non-ready scene"); - - return data->m_line_api.m_segment_size; -} - -vec4 Scene::GetLineColor() +void Scene::AddLine(vec3 a, vec3 b, vec4 color) { ASSERT(!!data, "Trying to access a non-ready scene"); - return data->m_line_api.m_color; + data->m_line_api.m_lines.push(a, b, color, -1.f, 0xFFFFFFFF, false, false); } //----------------------------------------------------------------------------- -void Scene::AddLine(vec3 a, vec3 b, vec4 color) +void Scene::AddLine(vec3 a, vec3 b, vec4 color, float duration, int mask) { ASSERT(!!data, "Trying to access a non-ready scene"); - data->m_line_api.m_lines.push(a, b, color, - data->m_line_api.m_time, data->m_line_api.m_mask, false, false); + data->m_line_api.m_lines.push(a, b, color, duration, mask, false, false); } //----------------------------------------------------------------------------- diff --git a/src/scene.h b/src/scene.h index 50b3a471..dd0ec6e4 100644 --- a/src/scene.h +++ b/src/scene.h @@ -234,14 +234,8 @@ public: void AddTile(TileSet *tileset, int id, mat4 model); public: - void SetLineTime(float new_time = -1.f); - void SetLineMask(int new_mask = 0xFFFFFFFF); - void SetLineSegmentSize(float new_segment_size = 1.f); - void SetLineColor(vec4 new_color = vec4(1.f)); - - float GetLineSegmentSize(); - vec4 GetLineColor(); void AddLine(vec3 a, vec3 b, vec4 color); + void AddLine(vec3 a, vec3 b, vec4 color, float duration, int mask); void AddLight(Light *light); array const &GetLights(); From 6b4d1645f0aabf47af11bb5695d56dac67f919cb Mon Sep 17 00:00:00 2001 From: touky Date: Thu, 3 Nov 2016 01:21:58 -0400 Subject: [PATCH 07/21] git fuckup ...... --- src/lol/debug/lines.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lol/debug/lines.h b/src/lol/debug/lines.h index faaa2ebe..e4a8a1c5 100644 --- a/src/lol/debug/lines.h +++ b/src/lol/debug/lines.h @@ -28,11 +28,11 @@ namespace Debug class Data { public: + vec4 m_color = vec4(1.f); float m_duration = 0.0f; int m_mask = 0; float m_segment_size = 8; -//Resets draw infos Data() {} Data(vec4 color, float duration = -1.f, int mask = 0xFFFFFFFF, float segment_size = 1.f) { From 6c2aee32a4d077350b5c218e50a60d8de04c8196 Mon Sep 17 00:00:00 2001 From: touky Date: Thu, 3 Nov 2016 01:26:48 -0400 Subject: [PATCH 08/21] fixes debug draw fixes are really un-numerous in ze lol, it seems --- doc/samples/btphystest.cpp | 2 +- doc/samples/meshviewer/meshviewer.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/samples/btphystest.cpp b/doc/samples/btphystest.cpp index d093de53..53bfb94c 100644 --- a/doc/samples/btphystest.cpp +++ b/doc/samples/btphystest.cpp @@ -414,7 +414,7 @@ void BtPhysTest::TickGame(float seconds) return; } - Debug::DrawSetupSegment(1.f); + auto context = Debug::DrawContext::New(Color::white, 1.f); Debug::DrawGrid(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f); if (m_controller->WasKeyReleasedThisFrame(BtPhysTestKeyInput::KEY_QUIT)) diff --git a/doc/samples/meshviewer/meshviewer.cpp b/doc/samples/meshviewer/meshviewer.cpp index 6db6cfdb..30617968 100644 --- a/doc/samples/meshviewer/meshviewer.cpp +++ b/doc/samples/meshviewer/meshviewer.cpp @@ -384,8 +384,7 @@ void MeshViewer::TickDraw(float seconds, Scene &scene) //Draw gizmos & grid Debug::DrawGizmo(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f); - Debug::DrawSetupColor(Color::white); - Debug::DrawSetupSegment(1.f); + auto context = Debug::DrawContext::New(Color::white); Debug::DrawGrid(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f); /** OLD STUFF **/ From d8efedf4d7a34e7985c36a396ca8c0187e1cc2ea Mon Sep 17 00:00:00 2001 From: touky Date: Sun, 13 Nov 2016 00:59:53 -0500 Subject: [PATCH 09/21] sdl input tweak sdl integration now supports OS character input as per existing in sdl. Use SetTextInputActive to turn it off/on --- src/input/input.cpp | 10 ++++++++++ src/input/input.h | 3 +++ src/lolimgui.cpp | 2 ++ src/platform/sdl/sdlinput.cpp | 29 +++++++++++++++++------------ 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/input/input.cpp b/src/input/input.cpp index 75a01121..710ecef6 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -36,6 +36,16 @@ String InputDevice::GetText() return ret; } +bool InputDevice::IsTextInputActive() +{ + return m_input_active; +} + +void InputDevice::SetTextInputActive(bool status) +{ + m_input_active = status; +} + void InputDeviceInternal::AddKey(int index, const char* name) { if (index == -1) diff --git a/src/input/input.h b/src/input/input.h index f16d80fc..3a290a6b 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -98,6 +98,8 @@ public: /** Gets the latest contents of text input. */ String GetText(); + bool IsTextInputActive(); + void SetTextInputActive(bool status); /** Gets the current value of the given axis. Devices should try to * clamp this value between -1 and 1, though it is not guaranteed. */ @@ -199,6 +201,7 @@ protected: /** Text input state */ String m_text; + bool m_input_active; /** Axis states (value and sensitivity) */ array m_axis; diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index a2d0b944..10ad19bf 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -227,6 +227,8 @@ void LolImGui::TickGame(float seconds) } } + m_keyboard->SetTextInputActive(io.WantTextInput); + //Update text input String text = m_keyboard->GetText(); //text.case_change(io.KeyShift); diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index cf5648a6..795ca32b 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -251,6 +251,13 @@ void SdlInputData::Tick(float seconds) m_mouse->SetAxis(4, 0); +# if !LOL_USE_OLD_SDL + if (m_keyboard->IsTextInputActive()) + SDL_StartTextInput(); + else + SDL_StopTextInput(); +# endif + /* Handle keyboard and WM events */ SDL_Event event; while (SDL_PollEvent(&event)) @@ -306,19 +313,9 @@ void SdlInputData::Tick(float seconds) # else if (ScanCodeIsValid(sc)) { + //Set key updates the corresponding key m_keyboard->SetKey(sc, event.type == SDL_KEYDOWN); - if (event.type == SDL_KEYDOWN - && !m_keyboard->GetKey(SDLOL_RCtrl) - && !m_keyboard->GetKey(SDLOL_LCtrl) - && !m_keyboard->GetKey(SDLOL_RAlt) - && !m_keyboard->GetKey(SDLOL_LAlt)) - { - String str = ScanCodeToText(sc); - str.case_change(m_keyboard->GetKey(SDLOL_CapsLockStatus) - ^ (m_keyboard->GetKey(SDLOL_RShift) - || m_keyboard->GetKey(SDLOL_LShift))); - m_keyboard->AddText(str); - } + /* DEBUG STUFF msg::info("Repeat: 0x%02x : %s/%s/%s/%i\n", (int)m_keyboard, ScanCodeToText(sc).C(), ScanCodeToName(sc).C(), @@ -334,6 +331,14 @@ void SdlInputData::Tick(float seconds) } break; +# if !LOL_USE_OLD_SDL + //case SDL_TEXTEDITING: //TODO: handle that ? + case SDL_TEXTINPUT: + m_keyboard->AddText(event.text.text); + break; + +# endif + # if LOL_USE_OLD_SDL case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: From f52db94b51e505c13ded001976590136bfadc04e Mon Sep 17 00:00:00 2001 From: touky Date: Tue, 22 Nov 2016 20:46:24 -0500 Subject: [PATCH 10/21] imgui extension small imgui extension scene palette shader crashfix --- src/lolimgui.cpp | 22 ++++++++++++++++++++++ src/lolimgui.h | 18 +++++++++++++++++- src/scene.cpp | 7 +++++-- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index 10ad19bf..57d246b0 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -18,6 +18,28 @@ using namespace lol; +//Imgui extension --------------------------------------------------------------------------------- +namespace ImGui +{ + IMGUI_API void SetNextWindowSizeAndDock(const ImVec2& size, ImGuiSetDock dock, ImGuiSetCond cond) + { + vec2 video_size = vec2(Video::GetSize()); + ImVec2 pos = ImVec2(); + + switch (dock) + { + case ImGuiSetDock_TopLeft: pos = ImVec2(0, 0); break; + case ImGuiSetDock_TopRight: pos = ImVec2(video_size.x - size.x, 0); break; + case ImGuiSetDock_BottomLeft: pos = ImVec2(0, video_size.y - size.y); break; + case ImGuiSetDock_BottomRight: pos = ImVec2(video_size.x - size.x, video_size.y - size.y); break; + } + + ImGui::SetNextWindowPos(pos, cond); + ImGui::SetNextWindowSize(size, cond); + } +} + +//LolImGui ---------------------------------------------------------------------------------------- #define Line(s) ((s) + "\n") //----------------------------------------------------------------------------- diff --git a/src/lolimgui.h b/src/lolimgui.h index fb707b9d..379e0c82 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -14,10 +14,26 @@ #include "imgui.h" +//Imgui extension --------------------------------------------------------------------------------- +typedef int ImGuiSetDock; // condition flags for Set*() // enum ImGuiSetCond_ + +enum ImGuiSetDock_ +{ + ImGuiSetDock_TopLeft = 0, + ImGuiSetDock_TopRight = 1, + ImGuiSetDock_BottomLeft = 2, + ImGuiSetDock_BottomRight = 3, +}; + +namespace ImGui +{ + IMGUI_API void SetNextWindowSizeAndDock(const ImVec2& size, ImGuiSetDock dock, ImGuiSetCond cond = 0); +} + +//LolImGui ---------------------------------------------------------------------------------------- namespace lol { -//LolImGui -------------------------------------------------------------------- class LolImGui : public Entity { typedef Entity super; diff --git a/src/scene.cpp b/src/scene.cpp index bbfcc9e5..c33adef5 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -723,7 +723,7 @@ void Scene::render_tiles() // XXX: rename to Blit() if (!data->m_tile_api.m_shader) data->m_tile_api.m_shader = Shader::Create(LOLFX_RESOURCE_NAME(gpu_tile)); - if (!data->m_tile_api.m_palette_shader) + if (!data->m_tile_api.m_palette_shader && data->m_tile_api.m_palettes.count()) data->m_tile_api.m_palette_shader = Shader::Create(LOLFX_RESOURCE_NAME(gpu_palette)); for (int p = 0; p < 2; p++) @@ -749,7 +749,7 @@ void Scene::render_tiles() // XXX: rename to Blit() shader->SetUniform(uni_mat, mat4(1.f)); uni_tex = shader->GetUniformLocation("u_texture"); - uni_pal = data->m_tile_api.m_palette_shader->GetUniformLocation("u_palette"); + uni_pal = data->m_tile_api.m_palette_shader ? data->m_tile_api.m_palette_shader->GetUniformLocation("u_palette") : ShaderUniform(); uni_texsize = shader->GetUniformLocation("u_texsize"); for (int buf = 0, i = 0, n; i < tiles.count(); i = n, buf += 2) @@ -809,6 +809,9 @@ void Scene::render_tiles() // XXX: rename to Blit() tiles.empty(); shader->Unbind(); + + if (!data->m_tile_api.m_palette_shader) + break; } #if (defined LOL_USE_GLEW || defined HAVE_GL_2X) && !defined HAVE_GLES_2X From fd3a6a8ae69c1e09dc5365ab742b9e87f4d937dc Mon Sep 17 00:00:00 2001 From: touky Date: Tue, 22 Nov 2016 22:35:07 -0500 Subject: [PATCH 11/21] imgui + renderer + mouse scroll Added scissor mode in renderer Added scissor support in imGui Mouse scroll sensivity tweak --- src/gpu/rendercontext.cpp | 31 +++++++++++++++++++++++++++ src/gpu/renderer.cpp | 42 +++++++++++++++++++++++++++++++++++++ src/input/input.cpp | 2 +- src/lol/gpu/rendercontext.h | 4 ++++ src/lol/gpu/renderer.h | 12 +++++++++++ src/lolimgui.cpp | 8 +++++++ 6 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/gpu/rendercontext.cpp b/src/gpu/rendercontext.cpp index 316bb80a..f866fd7a 100644 --- a/src/gpu/rendercontext.cpp +++ b/src/gpu/rendercontext.cpp @@ -59,6 +59,8 @@ private: TrackedState m_depth_mask; TrackedState m_cull_mode; TrackedState m_polygon_mode; + TrackedState m_scissor_mode; + TrackedState m_scissor_rect; }; /* @@ -101,6 +103,9 @@ RenderContext::~RenderContext() if (m_data->m_polygon_mode.HasChanged()) Renderer::Get()->SetPolygonMode(m_data->m_polygon_mode.GetValue()); + if (m_data->m_scissor_mode.HasChanged()) + Renderer::Get()->SetScissorMode(m_data->m_scissor_mode.GetValue()); + delete m_data; } @@ -255,5 +260,31 @@ PolygonMode RenderContext::GetPolygonMode() return Renderer::Get()->GetPolygonMode(); } +void RenderContext::SetScissorMode(ScissorMode mode) +{ + if (!m_data->m_scissor_mode.HasChanged()) + m_data->m_scissor_mode.TrackValue(Renderer::Get()->GetScissorMode()); + + Renderer::Get()->SetScissorMode(mode); +} + +void RenderContext::SetScissorRect(vec4 rect) +{ + if (!m_data->m_scissor_rect.HasChanged()) + m_data->m_scissor_rect.TrackValue(Renderer::Get()->GetScissorRect()); + + Renderer::Get()->SetScissorRect(rect); +} + +ScissorMode RenderContext::GetScissorMode() +{ + return Renderer::Get()->GetScissorMode(); +} + +vec4 RenderContext::GetScissorRect() +{ + return Renderer::Get()->GetScissorRect(); +} + } /* namespace lol */ diff --git a/src/gpu/renderer.cpp b/src/gpu/renderer.cpp index 50bb055e..7569a246 100644 --- a/src/gpu/renderer.cpp +++ b/src/gpu/renderer.cpp @@ -54,6 +54,8 @@ private: DepthMask m_depth_mask; CullMode m_cull_mode; PolygonMode m_polygon_mode; + ScissorMode m_scissor_mode; + vec4 m_scissor_rect; }; /* @@ -107,6 +109,9 @@ Renderer::Renderer(ivec2 size) m_data->m_polygon_mode = PolygonMode::Point; SetPolygonMode(PolygonMode::Fill); + m_data->m_scissor_mode = ScissorMode::Disabled; + SetPolygonMode(PolygonMode::Fill); + /* Add some rendering states that we don't export to the user */ #if defined HAVE_GL_2X && !defined __APPLE__ glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); @@ -548,5 +553,42 @@ PolygonMode Renderer::GetPolygonMode() const return m_data->m_polygon_mode; } +/* +* Scissor test mode +*/ + +void Renderer::SetScissorMode(ScissorMode mode) +{ + if (m_data->m_scissor_mode == mode) + return; + + if (mode == ScissorMode::Enabled) + glEnable(GL_SCISSOR_TEST); + else + glDisable(GL_SCISSOR_TEST); + + m_data->m_scissor_mode = mode; +} + +void Renderer::SetScissorRect(vec4 rect) +{ + m_data->m_scissor_rect = rect; + if (m_data->m_scissor_mode == ScissorMode::Enabled) + { + glScissor((int)rect.x, (int)Video::GetSize().y - rect.w, (int)(rect.z - rect.x), (int)(rect.w - rect.y)); + //glScissor((int)rect.x, (int)rect.y, (int)(rect.z - rect.x), (int)(rect.w - rect.y)); + } +} + +ScissorMode Renderer::GetScissorMode() const +{ + return m_data->m_scissor_mode; +} + +vec4 Renderer::GetScissorRect() const +{ + return m_data->m_scissor_rect; +} + } /* namespace lol */ diff --git a/src/input/input.cpp b/src/input/input.cpp index 710ecef6..f3742609 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -114,7 +114,7 @@ InputDeviceInternal* InputDeviceInternal::CreateStandardMouse() mouse->AddAxis(g_name_mouse_axis_y.C()); mouse->AddAxis(g_name_mouse_axis_xpixel.C()); mouse->AddAxis(g_name_mouse_axis_ypixel.C()); - mouse->AddAxis(g_name_mouse_axis_scroll.C()); + mouse->AddAxis(g_name_mouse_axis_scroll.C(), .0000001f); mouse->AddCursor(g_name_mouse_cursor.C()); diff --git a/src/lol/gpu/rendercontext.h b/src/lol/gpu/rendercontext.h index 0d891db2..8d94d039 100644 --- a/src/lol/gpu/rendercontext.h +++ b/src/lol/gpu/rendercontext.h @@ -36,6 +36,8 @@ public: void SetDepthMask(DepthMask mask); void SetCullMode(CullMode mode); void SetPolygonMode(PolygonMode mode); + void SetScissorMode(ScissorMode mode); + void SetScissorRect(vec4 rect); ibox2 GetViewport(); vec4 GetClearColor(); @@ -50,6 +52,8 @@ public: DepthMask GetDepthMask(); CullMode GetCullMode(); PolygonMode GetPolygonMode(); + ScissorMode GetScissorMode(); + vec4 GetScissorRect(); private: RenderContextData *m_data; diff --git a/src/lol/gpu/renderer.h b/src/lol/gpu/renderer.h index a6e9f3cd..80687731 100644 --- a/src/lol/gpu/renderer.h +++ b/src/lol/gpu/renderer.h @@ -123,6 +123,13 @@ enum class AlphaFunc : uint8_t Always, }; +/* A safe enum to indicate the depth mask. */ +enum class ScissorMode : uint8_t +{ + Disabled, + Enabled, +}; + class Renderer { private: @@ -176,6 +183,11 @@ public: void SetPolygonMode(PolygonMode mode); PolygonMode GetPolygonMode() const; + void SetScissorMode(ScissorMode mode); + void SetScissorRect(vec4 rect); + ScissorMode GetScissorMode() const; + vec4 GetScissorRect() const; + private: RendererData *m_data; }; diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index 57d246b0..175a7630 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -366,6 +366,7 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) RenderContext rc; rc.SetCullMode(CullMode::Disabled); rc.SetDepthFunc(DepthFunc::Disabled); + rc.SetScissorMode(ScissorMode::Enabled); m_shader->Bind(); for (int n = 0; n < draw_data->CmdListsCount; n++) @@ -402,6 +403,11 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) { const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[(int)cmd_i]; + TextureImage* image = (TextureImage*)pcmd->TextureId; + if (image) image->Bind(); + + rc.SetScissorRect(vec4(pcmd->ClipRect.x, pcmd->ClipRect.y, pcmd->ClipRect.z, pcmd->ClipRect.w)); + #ifdef SHOW_IMGUI_DEBUG //----------------------------------------------------------------- // -------------------------------------------------- @@ -448,6 +454,8 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) m_vdecl->DrawIndexedElements(MeshPrimitive::Triangles, pcmd->ElemCount, (const short*)idx_buffer_offset); idx_buffer_offset += pcmd->ElemCount; + + if (image) image->Unbind(); } m_vdecl->Unbind(); From 90a7d2055d7069ea88030fd6ba77eb9a8c0c3766 Mon Sep 17 00:00:00 2001 From: touky Date: Tue, 22 Nov 2016 23:32:41 -0500 Subject: [PATCH 12/21] imgui vec conversion --- src/lolimgui.h | 13 +++++++++++++ src/textureimage.cpp | 5 +++++ src/textureimage.h | 1 + 3 files changed, 19 insertions(+) diff --git a/src/lolimgui.h b/src/lolimgui.h index 379e0c82..780c049d 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -12,8 +12,21 @@ #pragma once +#define IM_VEC2_CLASS_EXTRA ImVec2(const lol::vec2 &v) { x = v.x; y = v.y; } \ + ImVec2(const lol::ivec2 &v) : ImVec2(lol::vec2(v)) { } \ + operator lol::vec2() const { return lol::vec2(x, y); } \ + operator lol::ivec2() const { return lol::ivec2(lol::vec2(x, y)); } + +#define IM_VEC4_CLASS_EXTRA ImVec4(const lol::vec4 &v) { x = v.x; y = v.y; z = v.z; w = v.w; } \ + ImVec4(const lol::ivec4 &v) : ImVec4(lol::vec4(v)) { } \ + operator lol::vec4() { return lol::vec4(x, y, z, w); } \ + operator lol::ivec4() const { return lol::ivec4(lol::vec4(x, y, z, w)); } + #include "imgui.h" +#undef IM_VEC2_CLASS_EXTRA +#undef IM_VEC4_CLASS_EXTRA + //Imgui extension --------------------------------------------------------------------------------- typedef int ImGuiSetDock; // condition flags for Set*() // enum ImGuiSetCond_ diff --git a/src/textureimage.cpp b/src/textureimage.cpp index eb4f21e8..f3d6f3ed 100644 --- a/src/textureimage.cpp +++ b/src/textureimage.cpp @@ -162,6 +162,11 @@ Image const * TextureImage::GetImage() const return m_data->m_image; } +ivec2 TextureImage::GetImageSize() const +{ + return m_data->m_image_size; +} + ivec2 TextureImage::GetTextureSize() const { return m_data->m_texture_size; diff --git a/src/textureimage.h b/src/textureimage.h index 662586d6..307c33d7 100644 --- a/src/textureimage.h +++ b/src/textureimage.h @@ -57,6 +57,7 @@ public: Texture const * GetTexture() const; Image * GetImage(); Image const * GetImage() const; + ivec2 GetImageSize() const; ivec2 GetTextureSize() const; void Bind(); void Unbind(); From 19545aa4463abd4389fa6fc5e5460e42e25fd923 Mon Sep 17 00:00:00 2001 From: touky Date: Thu, 24 Nov 2016 00:43:27 -0500 Subject: [PATCH 13/21] imgui extension + tileset imGui docking extension tweak tileset new getters --- src/lolimgui.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++------ src/lolimgui.h | 21 +++++++++++++----- src/tileset.cpp | 10 +++++++++ src/tileset.h | 2 ++ 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index 175a7630..ca9d9006 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -21,22 +21,66 @@ using namespace lol; //Imgui extension --------------------------------------------------------------------------------- namespace ImGui { - IMGUI_API void SetNextWindowSizeAndDock(const ImVec2& size, ImGuiSetDock dock, ImGuiSetCond cond) + IMGUI_API void SetNextWindowDockingAndSize(const ImVec2& size, ImGuiSetDock dock, const ImVec2& padding, ImGuiSetCond cond) { - vec2 video_size = vec2(Video::GetSize()); - ImVec2 pos = ImVec2(); + SetNextWindowDockingAndSize(size, dock, ImVec4(vec2(padding).xyxy), cond); + } + IMGUI_API void SetNextWindowDockingAndSize(const ImVec2& size, ImGuiSetDock dock, const ImVec4& padding, ImGuiSetCond cond) + { + vec4 pdg = padding; + vec2 vsz = vec2(Video::GetSize()); + vec2 ctr = pdg.xy + (((vsz - pdg.zw) - pdg.xy) * .5f); + vec2 pos = vec2(); switch (dock) { - case ImGuiSetDock_TopLeft: pos = ImVec2(0, 0); break; - case ImGuiSetDock_TopRight: pos = ImVec2(video_size.x - size.x, 0); break; - case ImGuiSetDock_BottomLeft: pos = ImVec2(0, video_size.y - size.y); break; - case ImGuiSetDock_BottomRight: pos = ImVec2(video_size.x - size.x, video_size.y - size.y); break; + case ImGuiSetDock_Center: pos = vec2(ctr.x - (size.x * .5f), ctr.y - (size.y * .5f)); break; + case ImGuiSetDock_Top: pos = vec2(ctr.x - (size.x * .5f), pdg.y); break; + case ImGuiSetDock_TopRight: pos = vec2(vsz.x - (size.x + pdg.z), pdg.y); break; + case ImGuiSetDock_Right: pos = vec2(vsz.x - (size.x + pdg.z), ctr.y - (size.y * .5f)); break; + case ImGuiSetDock_BottomRight: pos = vec2(vsz.x - (size.x + pdg.z), vsz.y - (size.y + pdg.w)); break; + case ImGuiSetDock_Bottom: pos = vec2(ctr.x - (size.x * .5f), vsz.y - (size.y + pdg.w)); break; + case ImGuiSetDock_BottomLeft: pos = vec2(pdg.x, vsz.y - (size.y + pdg.w)); break; + case ImGuiSetDock_Left: pos = vec2(pdg.x, ctr.y - (size.y * .5f)); break; + case ImGuiSetDock_TopLeft: pos = vec2(pdg.x, pdg.y); break; } ImGui::SetNextWindowPos(pos, cond); ImGui::SetNextWindowSize(size, cond); } + + IMGUI_API void SetNextWindowDocking(ImGuiSetDock dock, const ImVec2& padding, ImGuiSetCond cond) + { + SetNextWindowDocking(dock, ImVec4(vec2(padding).xyxy), cond); + } + IMGUI_API void SetNextWindowDocking(ImGuiSetDock dock, const ImVec4& padding, ImGuiSetCond cond) + { + vec2 vsz = vec2(Video::GetSize()); + vec2 size = vec2(); + vec2 pos = vec2(); + vec4 pdg = padding; + + switch (dock) + { + case ImGuiSetDock_Center: size = vsz - vec2(pdg.x + pdg.z, pdg.y + pdg.w); break; + case ImGuiSetDock_Top: size = vec2(vsz.x - (pdg.x + pdg.z), vsz.y *.5f - pdg.y); break; + case ImGuiSetDock_TopRight: size = vec2(vsz.x *.5f - pdg.z, vsz.y *.5f - pdg.y); break; + case ImGuiSetDock_Right: size = vec2(vsz.x *.5f - pdg.z, vsz.y - (pdg.y + pdg.w)); break; + case ImGuiSetDock_BottomRight: size = vec2(vsz.x *.5f - pdg.z, vsz.y *.5f - pdg.w); break; + case ImGuiSetDock_Bottom: size = vec2(vsz.x - (pdg.x + pdg.z), vsz.y *.5f - pdg.w); break; + case ImGuiSetDock_BottomLeft: size = vec2(vsz.x *.5f - pdg.x, vsz.y *.5f - pdg.w); break; + case ImGuiSetDock_Left: size = vec2(vsz.x *.5f - pdg.x, vsz.y - (pdg.y + pdg.w)); break; + case ImGuiSetDock_TopLeft: size = vec2(vsz.x *.5f - pdg.x, vsz.y *.5f - pdg.y); break; + } + + SetNextWindowDockingAndSize(size, dock, padding, cond); + } + + IMGUI_API float GetMainMenuBarHeight() + { + ImGuiContext& g = *GImGui; + return g.FontBaseSize + g.Style.FramePadding.y * 2.0f; + } } //LolImGui ---------------------------------------------------------------------------------------- diff --git a/src/lolimgui.h b/src/lolimgui.h index 780c049d..3b82c4c9 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -19,7 +19,7 @@ #define IM_VEC4_CLASS_EXTRA ImVec4(const lol::vec4 &v) { x = v.x; y = v.y; z = v.z; w = v.w; } \ ImVec4(const lol::ivec4 &v) : ImVec4(lol::vec4(v)) { } \ - operator lol::vec4() { return lol::vec4(x, y, z, w); } \ + operator lol::vec4() const { return lol::vec4(x, y, z, w); } \ operator lol::ivec4() const { return lol::ivec4(lol::vec4(x, y, z, w)); } #include "imgui.h" @@ -32,15 +32,24 @@ typedef int ImGuiSetDock; // condition flags for Set*() // e enum ImGuiSetDock_ { - ImGuiSetDock_TopLeft = 0, - ImGuiSetDock_TopRight = 1, - ImGuiSetDock_BottomLeft = 2, - ImGuiSetDock_BottomRight = 3, + ImGuiSetDock_Center, + ImGuiSetDock_Top, + ImGuiSetDock_TopRight, + ImGuiSetDock_Right, + ImGuiSetDock_BottomRight, + ImGuiSetDock_Bottom, + ImGuiSetDock_BottomLeft, + ImGuiSetDock_Left, + ImGuiSetDock_TopLeft, }; namespace ImGui { - IMGUI_API void SetNextWindowSizeAndDock(const ImVec2& size, ImGuiSetDock dock, ImGuiSetCond cond = 0); + IMGUI_API void SetNextWindowDockingAndSize(const ImVec2& size, ImGuiSetDock dock, const ImVec2& padding, ImGuiSetCond cond = 0); + IMGUI_API void SetNextWindowDockingAndSize(const ImVec2& size, ImGuiSetDock dock, const ImVec4& padding = ImVec4(0, 0, 0, 0), ImGuiSetCond cond = 0); + IMGUI_API void SetNextWindowDocking(ImGuiSetDock dock, const ImVec2& padding, ImGuiSetCond cond = 0); + IMGUI_API void SetNextWindowDocking(ImGuiSetDock dock, const ImVec4& padding = ImVec4(0, 0, 0, 0), ImGuiSetCond cond = 0); + IMGUI_API float GetMainMenuBarHeight(); } //LolImGui ---------------------------------------------------------------------------------------- diff --git a/src/tileset.cpp b/src/tileset.cpp index 473e6441..fb952d2d 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -162,6 +162,16 @@ ivec2 TileSet::GetTileSize(int tileid) const return m_tileset_data->m_tiles[tileid].m1.extent(); } +ibox2 TileSet::GetTilePixel(int tileid) const +{ + return m_tileset_data->m_tiles[tileid].m1; +} + +box2 TileSet::GetTileTexel(int tileid) const +{ + return m_tileset_data->m_tiles[tileid].m2; +} + //Palette --------------------------------------------------------------------- void TileSet::SetPalette(TileSet* palette) { diff --git a/src/tileset.h b/src/tileset.h index 4e6b2e63..5855c5e2 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -62,6 +62,8 @@ public: void define_tile(ivec2 count); int GetTileCount() const; ivec2 GetTileSize(int tileid) const; + ibox2 GetTilePixel(int tileid) const; + box2 GetTileTexel(int tileid) const; void SetPalette(TileSet* palette); TileSet* GetPalette(); From 24d9405ff33e3e1e43467c3947092db5cdffe9b6 Mon Sep 17 00:00:00 2001 From: touky Date: Sun, 27 Nov 2016 00:29:09 -0500 Subject: [PATCH 14/21] ResourceLoader + tileset tweaks Moved ImageLoader to ResourceLoader, so that loading is extensible. i.e. here: Zed image loader now loads up a tileset instead of being a hacked image loader. Goal is to support more funky stuff (wad, pak .....) --- src/Makefile.am | 3 +- src/image/codec/android-image.cpp | 16 ++-- src/image/codec/dummy-image.cpp | 23 ++--- src/image/codec/gdiplus-image.cpp | 28 +++--- src/image/codec/ios-image.cpp | 18 ++-- src/image/codec/oric-image.cpp | 28 +++--- src/image/codec/sdl-image.cpp | 34 ++++--- src/image/codec/zed-image.cpp | 54 +++++------ src/image/codec/zed-palette-image.cpp | 24 ++--- src/image/image-private.h | 33 ------- src/image/image.cpp | 132 +++++--------------------- src/image/resource-private.h | 57 +++++++++++ src/image/resource.cpp | 121 +++++++++++++++++++++++ src/lol-core.vcxproj | 5 +- src/lol/image/all.h | 1 + src/lol/image/image.h | 3 - src/lol/image/resource.h | 74 +++++++++++++++ src/platform/sdl/sdlinput.cpp | 2 +- src/sys/init.cpp | 7 ++ src/textureimage.cpp | 14 ++- src/textureimage.h | 2 + src/tileset.cpp | 53 ++++++++--- src/tileset.h | 5 + 23 files changed, 476 insertions(+), 261 deletions(-) create mode 100644 src/image/resource-private.h create mode 100644 src/image/resource.cpp create mode 100644 src/lol/image/resource.h diff --git a/src/Makefile.am b/src/Makefile.am index 27d332cd..08e25f3c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -54,7 +54,7 @@ liblol_core_headers = \ lol/sys/threadtypes.h lol/sys/timer.h \ \ lol/image/all.h \ - lol/image/pixel.h lol/image/color.h lol/image/image.h lol/image/movie.h \ + lol/image/pixel.h lol/image/color.h lol/image/image.h lol/image/resource.h lol/image/movie.h \ \ lol/gpu/all.h \ lol/gpu/shader.h lol/gpu/indexbuffer.h lol/gpu/vertexbuffer.h \ @@ -115,6 +115,7 @@ liblol_core_sources = \ sys/init.cpp sys/file.cpp sys/hacks.cpp \ sys/thread.cpp sys/threadtypes.cpp sys/getopt.cpp \ \ + image/resource.cpp image/resource-private.h \ image/image.cpp image/image-private.h image/kernel.cpp image/pixel.cpp \ image/crop.cpp image/resample.cpp image/noise.cpp image/combine.cpp \ image/codec/gdiplus-image.cpp image/codec/imlib2-image.cpp \ diff --git a/src/image/codec/android-image.cpp b/src/image/codec/android-image.cpp index 3de43606..0364b23b 100644 --- a/src/image/codec/android-image.cpp +++ b/src/image/codec/android-image.cpp @@ -21,7 +21,7 @@ extern "C" { #include } -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -32,12 +32,12 @@ extern ANativeActivity *g_activity; * Image implementation class */ -class AndroidImageCodec : public ImageCodec +class AndroidImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); virtual bool Close(); virtual uint8_t *GetData() const; @@ -50,7 +50,7 @@ private: DECLARE_IMAGE_CODEC(AndroidImageCodec, 100) -bool AndroidImageCodec::Load(Image *image, char const *path) +ResourceCodecData* AndroidImageCodec::Load(char const *path) { JNIEnv *env; jint res = g_activity->vm->GetEnv((void **)&env, JNI_VERSION_1_2); @@ -106,12 +106,12 @@ bool AndroidImageCodec::Load(Image *image, char const *path) } m_format = PixelFormat::RGBA_8; - return true; + return new ResourceCodecData(); } -bool AndroidImageCodec::Save(Image *image, char const *path) +bool AndroidImageCodec::Save(char const *path, ResourceCodecData* data) { - UNUSED(path); + UNUSED(path, data); /* TODO: unimplemented */ } diff --git a/src/image/codec/dummy-image.cpp b/src/image/codec/dummy-image.cpp index 6f4c21e9..12319dc0 100644 --- a/src/image/codec/dummy-image.cpp +++ b/src/image/codec/dummy-image.cpp @@ -10,7 +10,7 @@ #include -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -19,12 +19,12 @@ namespace lol * Image implementation class */ -class DummyImageCodec : public ImageCodec +class DummyImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); }; //Priority 0 because it's supposed to be the last one @@ -34,11 +34,13 @@ DECLARE_IMAGE_CODEC(DummyImageCodec, 0) * Public Image class */ -bool DummyImageCodec::Load(Image *image, char const *path) +ResourceCodecData* DummyImageCodec::Load(char const *path) { - UNUSED(path); + if (strcmp("DUMMY", path)) + return nullptr; - image->SetSize(ivec2(256)); + auto data = new ResourceImageData(new Image(ivec2(256))); + auto image = data->m_image; u8vec4 *pixels = image->Lock(), *tmp = pixels; for (int j = 0; j < 256; j++) for (int i = 0; i < 256; i++) @@ -51,13 +53,12 @@ bool DummyImageCodec::Load(Image *image, char const *path) } image->Unlock(pixels); - //return false, because we're not supposed to be here. - return false; + return data; } -bool DummyImageCodec::Save(Image *image, char const *path) +bool DummyImageCodec::Save(char const *path, ResourceCodecData* data) { - UNUSED(path); + UNUSED(path, data); return false; } diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index d94b86f3..f8b2ae33 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -26,7 +26,7 @@ using std::max; #if LOL_USE_GDIPLUS -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -35,12 +35,12 @@ namespace lol * Image implementation class */ -class GdiPlusImageCodec : public ImageCodec +class GdiPlusImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(GdiPlusImageCodec, 100) @@ -49,7 +49,7 @@ DECLARE_IMAGE_CODEC(GdiPlusImageCodec, 100) * Public Image class */ -bool GdiPlusImageCodec::Load(Image *image, char const *path) +ResourceCodecData* GdiPlusImageCodec::Load(char const *path) { Gdiplus::Status status; ULONG_PTR token; @@ -58,7 +58,7 @@ bool GdiPlusImageCodec::Load(Image *image, char const *path) if (status != Gdiplus::Ok) { msg::error("error %d while initialising GDI+\n", status); - return false; + return nullptr; } array pathlist = sys::get_path_list(path); @@ -99,7 +99,7 @@ bool GdiPlusImageCodec::Load(Image *image, char const *path) if (!bitmap) { msg::error("could not load %s\n", path); - return false; + return nullptr; } ivec2 size(bitmap->GetWidth(), bitmap->GetHeight()); @@ -110,13 +110,14 @@ bool GdiPlusImageCodec::Load(Image *image, char const *path) { msg::error("could not lock bits in %s\n", path); delete bitmap; - return false; + return nullptr; } /* FIXME: GDI+ doesn't know about RGBA, only ARGB. And OpenGL doesn't * know about ARGB, only RGBA. So we swap bytes. We could also fix * this in the shader. */ - image->SetSize(size); + auto data = new ResourceImageData(new Image(ivec2(size))); + auto image = data->m_image; u8vec4 *pdst = image->Lock(); u8vec4 *psrc = static_cast(bdata.Scan0); for (int n = 0; n < size.x * size.y; n++) @@ -126,11 +127,15 @@ bool GdiPlusImageCodec::Load(Image *image, char const *path) bitmap->UnlockBits(&bdata); delete bitmap; - return true; + return data; } -bool GdiPlusImageCodec::Save(Image *image, char const *path) +bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) { + auto data_image = dynamic_cast(data); + if (data_image == nullptr) + return false; + ULONG_PTR token; Gdiplus::GdiplusStartupInput input; Gdiplus::GdiplusStartup(&token, &input, nullptr); @@ -178,6 +183,7 @@ bool GdiPlusImageCodec::Save(Image *image, char const *path) return false; } + auto image = data_image->m_image; ivec2 size = image->GetSize(); Gdiplus::Bitmap *b = new Gdiplus::Bitmap(size.x, size.y, diff --git a/src/image/codec/ios-image.cpp b/src/image/codec/ios-image.cpp index 51962450..33368308 100644 --- a/src/image/codec/ios-image.cpp +++ b/src/image/codec/ios-image.cpp @@ -16,7 +16,7 @@ #import -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -25,12 +25,12 @@ namespace lol * Image implementation class */ -class IosImageCodec : public ImageCodec +class IosImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(IosImageCodec, 100) @@ -39,7 +39,7 @@ DECLARE_IMAGE_CODEC(IosImageCodec, 100) * Public Image class */ -bool IosImageCodec::Load(Image *image, char const *path) +ResourceCodecData* IosImageCodec::Load(char const *path) { NSString *fullpath = [NSString stringWithUTF8String:path]; NSArray *chunks = [fullpath componentsSeparatedByString: @"/"]; @@ -54,7 +54,7 @@ bool IosImageCodec::Load(Image *image, char const *path) #if !LOL_BUILD_RELEASE msg::error("could not load %s\n", path); #endif - return false; + return nullptr; } int w = CGImageGetWidth(image.CGImage); @@ -75,12 +75,12 @@ bool IosImageCodec::Load(Image *image, char const *path) [image release]; [pngdata release]; - return true; + return new ResourceCodecData(); } -bool IosImageCodec::Save(Image *image, char const *path) +bool IosImageCodec::Save(char const *path, ResourceCodecData* data) { - UNUSED(path); + UNUSED(path, data); /* TODO: unimplemented */ return true; diff --git a/src/image/codec/oric-image.cpp b/src/image/codec/oric-image.cpp index a7bdf413..348b59fb 100644 --- a/src/image/codec/oric-image.cpp +++ b/src/image/codec/oric-image.cpp @@ -12,7 +12,7 @@ #include -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -27,12 +27,12 @@ namespace lol * Image implementation class */ -class OricImageCodec : public ImageCodec +class OricImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); private: static String ReadScreen(char const *name); @@ -45,7 +45,7 @@ DECLARE_IMAGE_CODEC(OricImageCodec, 100) * Public Image class */ -bool OricImageCodec::Load(Image *image, char const *path) +ResourceCodecData* OricImageCodec::Load(char const *path) { static u8vec4 const pal[8] = { @@ -61,9 +61,10 @@ bool OricImageCodec::Load(Image *image, char const *path) String screen = ReadScreen(path); if (screen.count() == 0) - return false; + return nullptr; - image->SetSize(ivec2(WIDTH, screen.count() * 6 / WIDTH)); + auto data = new ResourceImageData(new Image(ivec2(WIDTH, screen.count() * 6 / WIDTH))); + auto image = data->m_image; u8vec4 *pixels = image->Lock(); @@ -102,12 +103,16 @@ bool OricImageCodec::Load(Image *image, char const *path) image->Unlock(pixels); - return true; + return data; } -bool OricImageCodec::Save(Image *image, char const *path) +bool OricImageCodec::Save(char const *path, ResourceCodecData* data) { - int len = strlen(path); + auto data_image = dynamic_cast(data); + if (data_image == nullptr) + return false; + + int len = (int)strlen(path); if (len < 4 || path[len - 4] != '.' || toupper(path[len - 3]) != 'T' || toupper(path[len - 2]) != 'A' @@ -124,6 +129,7 @@ bool OricImageCodec::Save(Image *image, char const *path) result << (uint8_t)name[0]; result << 0; + auto image = data_image->m_image; Image tmp; ivec2 size = image->GetSize(); if (size.x != WIDTH) @@ -481,7 +487,7 @@ void OricImageCodec::WriteScreen(Image &image, array &result) for (int y = 0; y < size.y; y++) for (int x = 0; x < size.x; x++) for (int c = 0; c < 3; c++) - src[x][y][c] = 0xffff * pixels[y * size.x + x][2 - c]; + src[x][y][c] = 0xffff * (int32_t)pixels[y * size.x + x][2 - c]; /* Let the fun begin */ for (int y = 0; y < size.y; y++) diff --git a/src/image/codec/sdl-image.cpp b/src/image/codec/sdl-image.cpp index abdbc324..8b97d18e 100644 --- a/src/image/codec/sdl-image.cpp +++ b/src/image/codec/sdl-image.cpp @@ -25,7 +25,7 @@ # include #endif -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -34,19 +34,19 @@ namespace lol * Image implementation class */ -class SdlImageCodec : public ImageCodec +class SdlImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); static SDL_Surface *Create32BppSurface(ivec2 size); }; DECLARE_IMAGE_CODEC(SdlImageCodec, 50) -bool SdlImageCodec::Load(Image *image, char const *path) +ResourceCodecData* SdlImageCodec::Load(char const *path) { SDL_Surface *surface = nullptr; @@ -75,24 +75,30 @@ bool SdlImageCodec::Load(Image *image, char const *path) surface = tmp; } - image->SetSize(size); - u8vec4 *data = image->Lock(); - memcpy(data, surface->pixels, 4 * size.x * size.y); - image->Unlock(data); + auto data = new ResourceImageData(new Image(size)); + auto image = data->m_image; + u8vec4 *pixel_data = image->Lock(); + memcpy(pixel_data, surface->pixels, 4 * size.x * size.y); + image->Unlock(pixel_data); SDL_FreeSurface(surface); - return true; + return data; } -bool SdlImageCodec::Save(Image *image, char const *path) +bool SdlImageCodec::Save(char const *path, ResourceCodecData* data) { + auto data_image = dynamic_cast(data); + if (data_image == nullptr) + return false; + + auto image = data_image->m_image; ivec2 size = image->GetSize(); SDL_Surface *surface = Create32BppSurface(size); - u8vec4 *data = image->Lock(); - memcpy(surface->pixels, data, 4 * size.x * size.y); - image->Unlock(data); + u8vec4 *pixel_data = image->Lock(); + memcpy(surface->pixels, pixel_data, 4 * size.x * size.y); + image->Unlock(pixel_data); int ret = SDL_SaveBMP(surface, path); SDL_FreeSurface(surface); diff --git a/src/image/codec/zed-image.cpp b/src/image/codec/zed-image.cpp index e4a0e0e9..91ee8c6b 100644 --- a/src/image/codec/zed-image.cpp +++ b/src/image/codec/zed-image.cpp @@ -11,7 +11,7 @@ #include -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -20,24 +20,12 @@ namespace lol * Image implementation class */ -class ZedImageCodec : public ImageCodec +class ZedImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); - - virtual bool RetrieveTiles(array& tiles) - { - bool result = m_tiles.count() > 0; - tiles += m_tiles; - m_tiles.empty(); - return result; - } - -private: - // - array m_tiles; + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(ZedImageCodec, 10) @@ -46,10 +34,10 @@ DECLARE_IMAGE_CODEC(ZedImageCodec, 10) * Public Image class */ -bool ZedImageCodec::Load(Image *image, char const *path) +ResourceCodecData* ZedImageCodec::Load(char const *path) { if (!lol::String(path).ends_with(".RSC")) - return false; + return nullptr; // Compacter definition struct CompactSecondary @@ -142,7 +130,9 @@ bool ZedImageCodec::Load(Image *image, char const *path) } file_offset << file_size; - m_tiles.reserve(file_count); + // + array tiles; + tiles.reserve(file_count); Compacter2d compacter; compacter.StepSetup(8, 8, 10); @@ -151,6 +141,7 @@ bool ZedImageCodec::Load(Image *image, char const *path) array file_convert; file_convert.reserve(file_size); array available_sizes; + //got through all the files and store them for (int i = 0; i < file_count; i++) { file_pos = file_offset[i]; @@ -187,8 +178,10 @@ bool ZedImageCodec::Load(Image *image, char const *path) //Prepare buffer and tiles infos int32_t convert_pos = file_convert.count(); ivec2 size = ivec2(size_x, size_y); - compacter.Store(m_tiles.count(), ivec2(size_x, size_y)); - m_tiles.push(ivec2(file_convert.count(), data_length), ivec2(size_x, size_y)); + //store tile in compacter + compacter.Store(tiles.count(), ivec2(size_x, size_y)); + //push tile on the stack + tiles.push(ivec2(file_convert.count(), data_length), ivec2(size_x, size_y)); file_convert.resize(convert_pos + data_length); //Retrieve actual datas @@ -233,8 +226,9 @@ bool ZedImageCodec::Load(Image *image, char const *path) while (tex_size < tex_sqrt) tex_size <<= 1; - //Prepare final imqge - image->SetSize(ivec2(tex_size)); + //Prepare final image + auto data = new ResourceTilesetData(new Image(ivec2(tex_size))); + auto image = data->m_image; uint8_t *pixels = image->Lock(); //Data refactor stage @@ -255,8 +249,8 @@ bool ZedImageCodec::Load(Image *image, char const *path) compacter.m_primary[j].m_count--; int i = compacter.m_primary[j].m_secondary[k].m_tiles.pop(); - int32_t file_off = m_tiles[i].m1[0]; - ivec2 t_size = m_tiles[i].m2; + int32_t file_off = tiles[i].m1[0]; + ivec2 t_size = tiles[i].m2; ASSERT(pos.y + t_size.y < tex_size); @@ -277,7 +271,7 @@ bool ZedImageCodec::Load(Image *image, char const *path) } //Register new pos and move to next - m_tiles[i].m1 = pos; + tiles[i].m1 = pos; pos.x += t_size.x; } } @@ -292,12 +286,14 @@ bool ZedImageCodec::Load(Image *image, char const *path) } image->Unlock(pixels); - return true; + data->m_tiles = tiles; + + return data; } -bool ZedImageCodec::Save(Image *image, char const *path) +bool ZedImageCodec::Save(char const *path, ResourceCodecData* data) { - UNUSED(path); + UNUSED(path, data); /* FIXME: do we need to implement this? */ return true; } diff --git a/src/image/codec/zed-palette-image.cpp b/src/image/codec/zed-palette-image.cpp index baeac2f4..a0363962 100644 --- a/src/image/codec/zed-palette-image.cpp +++ b/src/image/codec/zed-palette-image.cpp @@ -10,7 +10,7 @@ #include -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -19,12 +19,12 @@ namespace lol * Image implementation class */ -class ZedPaletteImageCodec : public ImageCodec +class ZedPaletteImageCodec : public ResourceCodec { public: virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); }; DECLARE_IMAGE_CODEC(ZedPaletteImageCodec, 10) @@ -33,10 +33,10 @@ DECLARE_IMAGE_CODEC(ZedPaletteImageCodec, 10) * Public Image class */ -bool ZedPaletteImageCodec::Load(Image *image, char const *path) +ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) { if (!lol::String(path).ends_with(".pal")) - return false; + return nullptr; File file; file.Open(path, FileAccess::Read, true); @@ -53,13 +53,15 @@ bool ZedPaletteImageCodec::Load(Image *image, char const *path) int32_t tex_size = 2; while (tex_size < tex_sqrt) tex_size <<= 1; - image->m_data->m_size = ivec2(tex_size); + auto data = new ResourceImageData(new Image(ivec2(tex_size))); + auto image = data->m_image; #else int32_t tex_sqrt = file_size / 3; int32_t tex_size = 2; while (tex_size < tex_sqrt) tex_size <<= 1; - image->SetSize(ivec2(tex_size, 1)); + auto data = new ResourceImageData(new Image(ivec2(tex_size, 1))); + auto image = data->m_image; #endif u8vec4 *pixels = image->Lock(); @@ -73,12 +75,12 @@ bool ZedPaletteImageCodec::Load(Image *image, char const *path) } image->Unlock(pixels); - return true; + return data; } -bool ZedPaletteImageCodec::Save(Image *image, char const *path) +bool ZedPaletteImageCodec::Save(char const *path, ResourceCodecData* data) { - UNUSED(path); + UNUSED(path, data); /* FIXME: do we need to implement this? */ return true; } diff --git a/src/image/image-private.h b/src/image/image-private.h index 2c65191b..a700a857 100644 --- a/src/image/image-private.h +++ b/src/image/image-private.h @@ -66,38 +66,5 @@ public: PixelFormat m_format; }; -class ImageCodec -{ -public: - virtual char const *GetName() { return ""; } - virtual bool Load(Image *image, char const *path) = 0; - virtual bool Save(Image *image, char const *path) = 0; - - /* TODO: this should become more fine-grained */ - int m_priority; -}; - -#define REGISTER_IMAGE_CODEC(name) \ - extern ImageCodec *Register##name(); \ - { \ - /* Insert image codecs in a sorted list */ \ - ImageCodec *codec = Register##name(); \ - int i = 0, prio = codec->m_priority; \ - for ( ; i < codeclist.count(); ++i) \ - { \ - if (codeclist[i]->m_priority <= prio) \ - break; \ - } \ - codeclist.insert(codec, i); \ - } - -#define DECLARE_IMAGE_CODEC(name, priority) \ - ImageCodec *Register##name() \ - { \ - ImageCodec *ret = new name(); \ - ret->m_priority = priority; \ - return ret; \ - } - } /* namespace lol */ diff --git a/src/image/image.cpp b/src/image/image.cpp index 06f9fd65..c96bc17e 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -19,61 +19,6 @@ namespace lol { -/* HACK: We cannot make this an ImageLoader member function, because the - * REGISTER_IMAGE_CODEC macro forward-declares free functions from - * the "lol" namespace. An apparent bug in Visual Studio's compiler - * makes it think these functions are actually in the top-level - * namespace when the forward declaration is in a class member function. - * To avoid the problem, we make the forward declaration in a free - * function. - * The bug was reported to Microsoft and fixed by them, but the fix - * is not yet available. - * https://connect.microsoft.com/VisualStudio/feedback/details/730878/ */ -static bool RegisterAllCodecs(array &codeclist) -{ -#if defined __ANDROID__ - REGISTER_IMAGE_CODEC(AndroidImageCodec) -#endif -#if defined LOL_USE_GDIPLUS - REGISTER_IMAGE_CODEC(GdiPlusImageCodec) -#endif -#if defined __APPLE__ && defined __MACH__ && defined __arm__ - REGISTER_IMAGE_CODEC(IosImageCodec) -#endif -#if defined LOL_USE_SDL_IMAGE - REGISTER_IMAGE_CODEC(SdlImageCodec) -#endif -#if defined LOL_USE_IMLIB2 - REGISTER_IMAGE_CODEC(Imlib2ImageCodec) -#endif - REGISTER_IMAGE_CODEC(DummyImageCodec) - REGISTER_IMAGE_CODEC(ZedImageCodec) - REGISTER_IMAGE_CODEC(ZedPaletteImageCodec) - REGISTER_IMAGE_CODEC(OricImageCodec) - - return true; -} - -/* - * Our static image loader - */ - -static class ImageLoader -{ - friend class Image; - -public: - inline ImageLoader() - { - RegisterAllCodecs(m_codecs); - } - -private: - array m_codecs; - map m_images; -} -g_image_loader; - /* * Public Image class */ @@ -117,77 +62,59 @@ Image::~Image() delete m_data; } -void Image::DummyFill() -{ - //TODO: This is not very beautiful - for (auto codec : g_image_loader.m_codecs) - { - if (String(codec->GetName()).contains("Dummy")) - { - codec->Load(this, nullptr); - return; - } - } -} - -void Image::Copy(uint8_t* pixels, ivec2 const& size, PixelFormat fmt) +void Image::Copy(uint8_t* src_pixels, ivec2 const& size, PixelFormat fmt) { ASSERT(fmt != PixelFormat::Unknown); SetSize(size); SetFormat(fmt); - memcpy(m_data->m_pixels[(int)fmt]->data(), pixels, + memcpy(m_data->m_pixels[(int)fmt]->data(), src_pixels, size.x * size.y * BytesPerPixel(fmt)); } -void Image::Copy(Image const &other) +void Image::Copy(Image const &src) { - ivec2 size = other.GetSize(); - PixelFormat fmt = other.GetFormat(); + ivec2 size = src.GetSize(); + PixelFormat fmt = src.GetFormat(); SetSize(size); if (fmt != PixelFormat::Unknown) { SetFormat(fmt); memcpy(m_data->m_pixels[(int)fmt]->data(), - other.m_data->m_pixels[(int)fmt]->data(), + src.m_data->m_pixels[(int)fmt]->data(), size.x * size.y * BytesPerPixel(fmt)); } } +void Image::DummyFill() +{ + Load("DUMMY"); +} + bool Image::Load(char const *path) { - ImageCodec* last_codec = nullptr; - for (auto codec : g_image_loader.m_codecs) + auto resource = ResourceLoader::Load(path); + if (resource == nullptr) + return false; + + auto image_resource = dynamic_cast(resource); + if (image_resource == nullptr) { - last_codec = codec; - if (codec->Load(this, path)) - { - msg::info("Image::Load: Codec %s succesfully loaded %s.\n", codec->GetName(), path); - return true; - } + delete image_resource; + return false; } - //Log error, because we shouldn't be here - msg::error("Image::Load: Last codec %s, Error loading image %s.\n", last_codec->GetName(), path); - return false; + Copy(*image_resource->m_image); + delete image_resource; + return true; } bool Image::Save(char const *path) { - ImageCodec* last_codec = nullptr; - for (auto codec : g_image_loader.m_codecs) - { - last_codec = codec; - if (codec->Save(this, path)) - { - msg::info("Image::Save: Codec %s succesfully saved %s.\n", codec->GetName(), path); - return true; - } - } - - //Log error, because we shouldn't be here - msg::error("Image::Save: Last codec %s, Error saving image %s.\n", last_codec->GetName(), path); - return false; + auto data = new ResourceImageData(new Image(*this)); + auto result = ResourceLoader::Save(path, data); + delete data; + return result; } ivec2 Image::GetSize() const @@ -281,12 +208,5 @@ void Image::Unlock(void const *pixels) ASSERT(pixels == m_data->m_pixels[(int)m_data->m_format]->data()); } -bool Image::RetrieveTiles(array& tiles) const -{ - /* TODO: re-implement this */ - //return m_data->m_codecdata->RetrieveTiles(tiles); - return false; -} - } /* namespace lol */ diff --git a/src/image/resource-private.h b/src/image/resource-private.h new file mode 100644 index 00000000..593aea7f --- /dev/null +++ b/src/image/resource-private.h @@ -0,0 +1,57 @@ +// +// Lol Engine +// +// Copyright: (c) 20162017 Sam Hocevar +// Copyright 20162017 Benjamin Touky Huet +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the Do What The Fuck You Want To +// Public License, Version 2, as published by Sam Hocevar. See +// http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The ResourceCodecData class +// ------------------------ +// + +namespace lol +{ + + class ResourceCodec + { + public: + virtual char const *GetName() { return ""; } + virtual ResourceCodecData* Load(char const *path) = 0; + virtual bool Save(char const *path, ResourceCodecData* data) = 0; + + /* TODO: this should become more fine-grained */ + int m_priority; + }; + +#define REGISTER_IMAGE_CODEC(name) \ + extern ResourceCodec *Register##name(); \ + { \ + /* Insert image codecs in a sorted list */ \ + ResourceCodec *codec = Register##name(); \ + int i = 0, prio = codec->m_priority; \ + for ( ; i < codeclist.count(); ++i) \ + { \ + if (codeclist[i]->m_priority <= prio) \ + break; \ + } \ + codeclist.insert(codec, i); \ + } + +#define DECLARE_IMAGE_CODEC(name, priority) \ + ResourceCodec *Register##name() \ + { \ + ResourceCodec *ret = new name(); \ + ret->m_priority = priority; \ + return ret; \ + } + +} /* namespace lol */ + diff --git a/src/image/resource.cpp b/src/image/resource.cpp new file mode 100644 index 00000000..36a1b8ae --- /dev/null +++ b/src/image/resource.cpp @@ -0,0 +1,121 @@ +// +// Lol Engine +// +// Copyright 20162017 Sam Hocevar +// Copyright 20162017 Benjamin Touky Huet +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#include + +#include "resource-private.h" + +#include /* for std::swap */ + +namespace lol +{ + +/* HACK: We cannot make this an ImageLoader member function, because the +* REGISTER_IMAGE_CODEC macro forward-declares free functions from +* the "lol" namespace. An apparent bug in Visual Studio's compiler +* makes it think these functions are actually in the top-level +* namespace when the forward declaration is in a class member function. +* To avoid the problem, we make the forward declaration in a free +* function. +* The bug was reported to Microsoft and fixed by them, but the fix +* is not yet available. +* https://connect.microsoft.com/VisualStudio/feedback/details/730878/ */ +static bool RegisterAllCodecs(array &codeclist) +{ +#if defined __ANDROID__ + REGISTER_IMAGE_CODEC(AndroidImageCodec) +#endif +#if defined LOL_USE_GDIPLUS + REGISTER_IMAGE_CODEC(GdiPlusImageCodec) +#endif +#if defined __APPLE__ && defined __MACH__ && defined __arm__ + REGISTER_IMAGE_CODEC(IosImageCodec) +#endif +#if defined LOL_USE_SDL_IMAGE + REGISTER_IMAGE_CODEC(SdlImageCodec) +#endif +#if defined LOL_USE_IMLIB2 + REGISTER_IMAGE_CODEC(Imlib2ImageCodec) +#endif + REGISTER_IMAGE_CODEC(ZedImageCodec) + REGISTER_IMAGE_CODEC(ZedPaletteImageCodec) + REGISTER_IMAGE_CODEC(OricImageCodec) + + REGISTER_IMAGE_CODEC(DummyImageCodec) + + return true; +} + +/* +* Our static image loader +*/ + +static class StaticResourceLoader +{ + friend class ResourceLoader; + +public: + inline StaticResourceLoader() + { + RegisterAllCodecs(m_codecs); + } + +private: + array m_codecs; +} +g_resource_loader; + +/* +* The public resource loader +*/ + +ResourceCodecData* ResourceLoader::Load(char const *path) +{ + ResourceCodec* last_codec = nullptr; + for (auto codec : g_resource_loader.m_codecs) + { + last_codec = codec; + auto data = codec->Load(path); + if (data != nullptr) + { + msg::info("Image::Load: Codec %s succesfully loaded %s.\n", codec->GetName(), path); + return data; + } + } + + //Log error, because we shouldn't be here + msg::error("Image::Load: Last codec %s, Error loading resource %s.\n", last_codec->GetName(), path); + return false; +} + +bool ResourceLoader::Save(char const *path, ResourceCodecData* data) +{ + ResourceCodec* last_codec = nullptr; + for (auto codec : g_resource_loader.m_codecs) + { + last_codec = codec; + if (codec->Save(path, data)) + { + msg::info("Image::Save: Codec %s succesfully saved %s.\n", codec->GetName(), path); + return true; + } + } + + //Log error, because we shouldn't be here + msg::error("Image::Save: Last codec %s, Error saving resource %s.\n", last_codec->GetName(), path); + return false; +} + + +} /* namespace lol */ + diff --git a/src/lol-core.vcxproj b/src/lol-core.vcxproj index c961ddf5..7d924d56 100644 --- a/src/lol-core.vcxproj +++ b/src/lol-core.vcxproj @@ -148,6 +148,7 @@ + @@ -238,6 +239,7 @@ + @@ -285,6 +287,7 @@ + @@ -397,4 +400,4 @@ - + \ No newline at end of file diff --git a/src/lol/image/all.h b/src/lol/image/all.h index e704eb30..3375d56c 100644 --- a/src/lol/image/all.h +++ b/src/lol/image/all.h @@ -15,5 +15,6 @@ #include #include #include +#include #include diff --git a/src/lol/image/image.h b/src/lol/image/image.h index 24d06d07..42fffa15 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -106,9 +106,6 @@ public: template void Unlock2D(array2d const &); - /* XXX: this does not belong here */ - bool RetrieveTiles(array& tiles) const; - /* Image processing kernels */ static array2d BayerKernel(ivec2 size); static array2d HalftoneKernel(ivec2 size); diff --git a/src/lol/image/resource.h b/src/lol/image/resource.h new file mode 100644 index 00000000..f8be8bf5 --- /dev/null +++ b/src/lol/image/resource.h @@ -0,0 +1,74 @@ +// +// Lol Engine +// +// Copyright 20162017 Sam Hocevar +// Copyright 20162017 Benjamin Touky Huet +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The Resource class +// --------------- +// + +#include +#include +#include +#include + +namespace lol +{ + //ResourceCodecData ----------------------------------------------------------- + class ResourceCodecData + { + public: + ResourceCodecData() { } + virtual ~ResourceCodecData() { } + }; + + //ResourceImageData ----------------------------------------------------------- + class ResourceImageData : public ResourceCodecData + { + public: + ResourceImageData(Image* image) + { + m_image = image; + } + + virtual ~ResourceImageData() + { + if (m_image) + delete m_image; + } + + Image* m_image = nullptr; + }; + + //ResourceImageData ----------------------------------------------------------- + class ResourceTilesetData : public ResourceImageData + { + public: + ResourceTilesetData(Image* image) + : ResourceImageData(image) + { } + + array m_tiles; + }; + + //ResourceLoader -------------------------------------------------------------- + class ResourceLoader + { + public: + static ResourceCodecData* Load(char const *path); + static bool Save(char const *path, ResourceCodecData* data); + }; + +} /* namespace lol */ + diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index 795ca32b..74a3366b 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -278,10 +278,10 @@ void SdlInputData::Tick(float seconds) # endif { //Lock management -# if defined SDLOL_CapsLock && defined SDLOL_ScrollLock && defined SDLOL_NumLockClear case SDLOL_CapsLock: case SDLOL_ScrollLock: case SDLOL_NumLockClear: +# if defined SDLOL_CapsLock && defined SDLOL_ScrollLock && defined SDLOL_NumLockClear //Update status on key down only if (event.type == SDL_KEYDOWN) { diff --git a/src/sys/init.cpp b/src/sys/init.cpp index 6600dbdd..974aa9e1 100644 --- a/src/sys/init.cpp +++ b/src/sys/init.cpp @@ -104,6 +104,13 @@ void init(int argc, char *argv[], rootdir += "../src/"; /* FIXME: use SEPARATOR? */ add_data_dir(rootdir); + /* This data dir is for submodule support stuff */ + rootdir = solutiondir; + if (rootdir.count() && rootdir.last() != SEPARATOR) + rootdir += SEPARATOR; + rootdir += "./lol/src/"; /* FIXME: use SEPARATOR? */ + add_data_dir(rootdir); + /* This data dir is for project-specific stuff */ rootdir = projectdir; if (rootdir.count() && rootdir.last() != SEPARATOR) diff --git a/src/textureimage.cpp b/src/textureimage.cpp index f3d6f3ed..1ef46289 100644 --- a/src/textureimage.cpp +++ b/src/textureimage.cpp @@ -57,7 +57,19 @@ TextureImage::~TextureImage() void TextureImage::Init(char const *path) { - Init(path, new Image(path)); + Init(path, ResourceLoader::Load(path)); +} + +void TextureImage::Init(char const *path, ResourceCodecData* loaded_data) +{ + //Load image if available + auto image_data = dynamic_cast(loaded_data); + if (image_data != nullptr) + { + Init(path, new Image(*image_data->m_image)); + } + + delete image_data; } void TextureImage::Init(char const *path, Image* image) diff --git a/src/textureimage.h b/src/textureimage.h index 307c33d7..b798bbef 100644 --- a/src/textureimage.h +++ b/src/textureimage.h @@ -18,6 +18,7 @@ // zero, the texture is freed. // +#include #include #include @@ -43,6 +44,7 @@ public: protected: void Init(char const *path); + virtual void Init(char const *path, ResourceCodecData* loaded_data); virtual void Init(char const *path, Image* image); protected: diff --git a/src/tileset.cpp b/src/tileset.cpp index fb952d2d..0f5aa981 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -46,25 +46,25 @@ protected: */ TileSet::TileSet(char const *path) - : TextureImage(path), + : m_tileset_data(new TileSetData()), + m_palette(nullptr), + TextureImage(path) +{ +} + +TileSet::TileSet(char const *path, Image* image) + : TextureImage(path, image), m_tileset_data(new TileSetData()), m_palette(nullptr) { - array tiles; - if (m_data->m_image->RetrieveTiles(tiles)) - for (int i = 0; i < tiles.count(); i++) - define_tile(ibox2(tiles[0].m1, tiles[0].m1 + tiles[0].m2)); } -TileSet::TileSet(char const *path, Image* image) - : TextureImage(path, image), +TileSet::TileSet(char const *path, Image* image, array& tiles) + : TextureImage(path, image), m_tileset_data(new TileSetData()), m_palette(nullptr) { - array tiles; - if (m_data->m_image->RetrieveTiles(tiles)) - for (int i = 0; i < tiles.count(); i++) - define_tile(ibox2(tiles[0].m1, tiles[0].m1 + tiles[0].m2)); + define_tile(tiles); } TileSet::TileSet(char const *path, ivec2 size, ivec2 count) @@ -118,6 +118,20 @@ TileSet::~TileSet() delete m_tileset_data; } +void TileSet::Init(char const *path, ResourceCodecData* loaded_data) +{ + //Load tileset if available + auto tileset_data = dynamic_cast(loaded_data); + if (tileset_data != nullptr) + { + define_tile(tileset_data->m_tiles); + } + + m_data->m_name = String(" ") + path; + + super::Init(path, loaded_data); +} + void TileSet::Init(char const *path, Image* image) { super::Init(path, image); @@ -132,6 +146,11 @@ char const *TileSet::GetName() } //New methods ----------------------------------------------------------------- +void TileSet::clear_all() +{ + m_tileset_data->m_tiles.empty(); +} + int TileSet::define_tile(ibox2 rect) { m_tileset_data->m_tiles.push(rect, @@ -152,6 +171,18 @@ void TileSet::define_tile(ivec2 count) } } +void TileSet::define_tile(array& tiles) +{ + for (int i = 0; i < tiles.count(); i++) + define_tile(tiles[i]); +} + +void TileSet::define_tile(array& tiles) +{ + for (int i = 0; i < tiles.count(); i++) + define_tile(ibox2(tiles[i].m1, tiles[i].m1 + tiles[i].m2)); +} + int TileSet::GetTileCount() const { return m_tileset_data->m_tiles.count(); diff --git a/src/tileset.h b/src/tileset.h index 5855c5e2..cb5348e7 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -43,6 +43,7 @@ class TileSet : public TextureImage public: TileSet(char const *path); TileSet(char const *path, Image* image); + TileSet(char const *path, Image* image, array& tiles); /* Old style: path to PNG file */ TileSet(char const *path, ivec2 size, ivec2 count); @@ -51,6 +52,7 @@ public: virtual ~TileSet(); protected: + virtual void Init(char const *path, ResourceCodecData* loaded_data); virtual void Init(char const *path, Image* image); public: @@ -58,8 +60,11 @@ public: virtual char const *GetName(); /* New methods */ + void clear_all(); int define_tile(ibox2 rect); void define_tile(ivec2 count); + void define_tile(array& tiles); + void define_tile(array& tiles); int GetTileCount() const; ivec2 GetTileSize(int tileid) const; ibox2 GetTilePixel(int tileid) const; From f533ecffadad8025261e798145087449106aaacc Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 27 Nov 2016 12:32:56 +0100 Subject: [PATCH 15/21] Various compilation fixes. --- src/image/codec/imlib2-image.cpp | 27 +++++++++++++-------- src/image/codec/sdl-image.cpp | 2 +- src/image/resource.cpp | 2 +- src/lolua/baselua.h | 41 ++++++++++++++++---------------- 4 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/image/codec/imlib2-image.cpp b/src/image/codec/imlib2-image.cpp index 674ae622..4896972c 100644 --- a/src/image/codec/imlib2-image.cpp +++ b/src/image/codec/imlib2-image.cpp @@ -22,7 +22,7 @@ static_assert(sizeof(DATA32) == sizeof(uint32_t), "Imlib2 type DATA32 is broken" static_assert(sizeof(DATA16) == sizeof(uint16_t), "Imlib2 type DATA16 is broken"); static_assert(sizeof(DATA8) == sizeof(uint8_t), "Imlib2 type DATA8 is broken"); -#include "../../image/image-private.h" +#include "../../image/resource-private.h" namespace lol { @@ -31,17 +31,18 @@ namespace lol * Imlib2 image codec */ -class Imlib2ImageCodec : public ImageCodec +class Imlib2ImageCodec : public ResourceCodec { public: - virtual bool Load(Image *image, char const *path); - virtual bool Save(Image *image, char const *path); + virtual char const *GetName() { return ""; } + virtual ResourceCodecData* Load(char const *path); + virtual bool Save(char const *path, ResourceCodecData* data); }; /* Set priority higher than SDL because we can save in many formats. */ DECLARE_IMAGE_CODEC(Imlib2ImageCodec, 70) -bool Imlib2ImageCodec::Load(Image *image, char const *path) +ResourceCodecData *Imlib2ImageCodec::Load(char const *path) { Imlib_Image im = nullptr; @@ -57,7 +58,7 @@ bool Imlib2ImageCodec::Load(Image *image, char const *path) #if !LOL_BUILD_RELEASE msg::error("could not load image %s\n", path); #endif - return false; + return nullptr; } imlib_context_set_image(im); @@ -69,11 +70,12 @@ bool Imlib2ImageCodec::Load(Image *image, char const *path) #if !LOL_BUILD_RELEASE msg::error("could not get image data for %s\n", path); #endif - return false; + return nullptr; } ivec2 size(imlib_image_get_width(), imlib_image_get_height()); - image->SetSize(size); + auto data = new ResourceImageData(new Image(size)); + auto image = data->m_image; u8vec4 *dstdata = image->Lock(); @@ -88,11 +90,16 @@ bool Imlib2ImageCodec::Load(Image *image, char const *path) imlib_free_image(); - return true; + return data; } -bool Imlib2ImageCodec::Save(Image *image, char const *path) +bool Imlib2ImageCodec::Save(char const *path, ResourceCodecData *data) { + auto data_image = dynamic_cast(data); + if (data_image == nullptr) + return false; + + auto image = data_image->m_image; ivec2 size = image->GetSize(); Imlib_Image priv = imlib_create_image(size.x, size.y); diff --git a/src/image/codec/sdl-image.cpp b/src/image/codec/sdl-image.cpp index 8b97d18e..416bff2d 100644 --- a/src/image/codec/sdl-image.cpp +++ b/src/image/codec/sdl-image.cpp @@ -62,7 +62,7 @@ ResourceCodecData* SdlImageCodec::Load(char const *path) #if !LOL_BUILD_RELEASE msg::error("could not load image %s\n", path); #endif - return false; + return nullptr; } ivec2 size(surface->w, surface->h); diff --git a/src/image/resource.cpp b/src/image/resource.cpp index 36a1b8ae..7f46c4ed 100644 --- a/src/image/resource.cpp +++ b/src/image/resource.cpp @@ -95,7 +95,7 @@ ResourceCodecData* ResourceLoader::Load(char const *path) //Log error, because we shouldn't be here msg::error("Image::Load: Last codec %s, Error loading resource %s.\n", last_codec->GetName(), path); - return false; + return nullptr; } bool ResourceLoader::Save(char const *path, ResourceCodecData* data) diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index ca30fa35..53c2856f 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -232,17 +232,17 @@ protected: template static int Store(lua_State * l); template static int Del(lua_State * l); //------------------------------------------------------------------------- - template static int ToString(lua_State* l) { ASSERT(false); return 0; } - template static int OpAdd(lua_State* l) { ASSERT(false); return 0; } - template static int OpSubstract(lua_State* l) { ASSERT(false); return 0; } - template static int OpMultiply(lua_State* l) { ASSERT(false); return 0; } - template static int OpDivide(lua_State* l) { ASSERT(false); return 0; } - template static int OpModulo(lua_State* l) { ASSERT(false); return 0; } - template static int OpUnaryNeg(lua_State* l) { ASSERT(false); return 0; } - template static int OpConcat(lua_State* l) { ASSERT(false); return 0; } - template static int CmpEqual(lua_State* l) { ASSERT(false); return 0; } - template static int CmpLessThan(lua_State* l) { ASSERT(false); return 0; } - template static int CmpLessEqual(lua_State* l) { ASSERT(false); return 0; } + template static int ToString(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpAdd(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpSubstract(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpMultiply(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpDivide(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpModulo(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpUnaryNeg(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int OpConcat(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int CmpEqual(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int CmpLessThan(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } + template static int CmpLessEqual(lua_State* l) { UNUSED(l); ASSERT(false); return 0; } }; //----------------------------------------------------------------------------- @@ -334,7 +334,7 @@ private: } public: - //------------------------------------------------------------------------- + //------------------------------------------------------------------------- template T Get() { return Get(InnerDefault(), false); } template T Get(T default_value) { return Get(default_value, true); } template SafeEnum GetEnum() { return GetEnum(InnerDefaultSafeEnum(), false); } @@ -373,7 +373,7 @@ protected: //------------------------------------------------------------------------- #define INNER_ERROR "Your type is not implemented. For pointers, use LuaPtr()" template T InnerDefault() { return T(0); } - template bool InnerIsValid() { ASSERT(false, INNER_ERROR); return false; } + template bool InnerIsValid() { ASSERT(false, INNER_ERROR); return false; } template T InnerGet(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return InnerDefault(); } template int InnerPush(T value) { UNUSED(value); ASSERT(false, INNER_ERROR); return 0; } @@ -398,6 +398,7 @@ protected: { P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); *data = value.m_value; + return 0; } #endif //STACK_STRING @@ -440,7 +441,7 @@ private: #define LOLUA_DECLARE_BEGIN(LUA_FUNC_NAME, INSTANCE_GET) \ static int LUA_FUNC_NAME(lua_State* l) \ { \ - auto s = LuaStack::Begin(l); \ + auto s = Lolua::Stack::Begin(l); \ auto o = s.INSTANCE_GET; #define LOLUA_DECLARE_VARS(...) \ @@ -472,7 +473,7 @@ private: #define LOLUA_DECLARE_RETURN_METHOD_ARGS(LUA_FUNC_NAME, INSTANCE_GET, INSTANCE_CALL, ...) \ static int LUA_FUNC_NAME(lua_State* l) \ { \ - auto s = LuaStack::Begin(l); \ + auto s = Lolua::Stack::Begin(l); \ auto o = s.INSTANCE_GET; \ LOL_CALL(LOL_CAT(LOLUA_VAR_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__)) \ s << o->INSTANCE_CALL(LOL_CALL(LOL_CAT(LOLUA_ARG_, LOL_CALL(LOL_COUNT_TO_12, (__VA_ARGS__))), (__VA_ARGS__))); \ @@ -536,7 +537,7 @@ template<> inline int Stack::InnerPush(uint64_t value) { lua_pushi template<> inline bool Stack::InnerIsValid() { return !!lua_isnumber(m_state, m_index); } template<> inline int32_t Stack::InnerGet(int32_t value) { UNUSED(value); return (int32_t)lua_tointeger(m_state, m_index++); } template<> inline int Stack::InnerPush(int32_t value) { lua_pushinteger(m_state, (lua_Integer)value); return 1; } -#endif STACK_INT32 +#endif // STACK_INT32 //----------------------------------------------------------------------------- #ifndef STACK_UINT32 @@ -564,7 +565,7 @@ template<> inline int Stack::InnerPush(vec3 value) { return (InnerPush inline bool Stack::InnerIsValid() { return InnerIsValid(); } template<> inline vec4 Stack::InnerGet(vec4 value) { return vec4(InnerGet(value.x), Get(value.y, true), Get(value.z, true), Get(value.w, true)); } template<> inline int Stack::InnerPush(vec4 value) { return (InnerPush(value.x) + InnerPush(value.y) + InnerPush(value.z) + InnerPush(value.w)); } -#endif STACK_VEC4 +#endif // STACK_VEC4 #endif //REGION_STACK_VAR @@ -585,7 +586,7 @@ public: T1 GET_NAME(String const &name) \ { \ lua_getglobal(m_lua_state, name.C()); \ - auto stack = LuaStack::Begin(m_lua_state, -1); \ + auto stack = Lolua::Stack::Begin(m_lua_state, -1); \ auto result = stack.GET_NAME(); \ lua_pop(m_lua_state, 1); \ return result; \ @@ -615,7 +616,7 @@ private: template int ObjectHelper::Store(lua_State * l) { - auto stack = LuaStack::Begin(l); + auto stack = Lolua::Stack::Begin(l); TLuaClass* obj = stack.GetPtr(); ASSERT(obj); Loader::StoreObject(l, obj); @@ -625,7 +626,7 @@ int ObjectHelper::Store(lua_State * l) template int ObjectHelper::Del(lua_State * l) { - auto stack = LuaStack::Begin(l); + auto stack = Lolua::Stack::Begin(l); TLuaClass* obj = stack.GetPtr(); ASSERT(obj); delete obj; From ea93e0fecc14bd1a52c58f600429ddba47ff42af Mon Sep 17 00:00:00 2001 From: touky Date: Sun, 27 Nov 2016 11:50:39 -0500 Subject: [PATCH 16/21] baselua fix return 1 added after sam remark --- src/lolua/baselua.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index ca30fa35..7f0aa6a5 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -398,6 +398,7 @@ protected: { P** data = (P**)lua_newuserdata(m_state, sizeof(P*)); *data = value.m_value; + return 1; } #endif //STACK_STRING From a3a802e88f570e26e14a4d4d285a49cd4a37a8da Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 10 Dec 2016 10:50:53 +0100 Subject: [PATCH 17/21] Update PEGTL submodule to latest upstream branch. --- src/3rdparty/pegtl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/3rdparty/pegtl b/src/3rdparty/pegtl index e5f89680..267e992c 160000 --- a/src/3rdparty/pegtl +++ b/src/3rdparty/pegtl @@ -1 +1 @@ -Subproject commit e5f8968013e17a41aeed47ee1904c42ab1f452e2 +Subproject commit 267e992c17074a1afdc35ac361f7dd7525c37939 From 3544e76a4d304c92912ab94b4bf097277351b98b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 12 Dec 2016 15:31:52 +0100 Subject: [PATCH 18/21] input: fix uninitialised variable. --- src/input/input.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/input/input.h b/src/input/input.h index 3a290a6b..7f5a82ab 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -211,7 +211,9 @@ protected: static bool m_capturemouse; - InputDevice(String const &name) : m_name(name) + InputDevice(String const &name) + : m_name(name), + m_input_active(false) { devices.push_unique(this); } From a45a0d45d105d05998e94598f2a9b880fb557ed8 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 12 Dec 2016 15:32:01 +0100 Subject: [PATCH 19/21] audio: do not free buffers that SDL may be using. --- src/audio/audio.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index df2206f5..709ea2b9 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -33,6 +33,7 @@ struct audio_streamer int m_channel; std::function m_callback; #if defined LOL_USE_SDL_MIXER + array m_empty; // SDL keeps a reference to this Mix_Chunk *m_chunk; #endif }; @@ -95,9 +96,8 @@ int audio::start_streaming(std::function const &f) audio_streamer *s = new audio_streamer(); g_streamers.push(s); - array empty; - empty.resize(1024); - s->m_chunk = Mix_QuickLoad_RAW(empty.data(), empty.bytes()); + s->m_empty.resize(1024); + s->m_chunk = Mix_QuickLoad_RAW(s->m_empty.data(), s->m_empty.bytes()); s->m_channel = Mix_PlayChannel(-1, s->m_chunk, -1); s->m_callback = f; Mix_RegisterEffect(s->m_channel, trampoline, nullptr, s); From e3b1011d37dcb1552dce66e04e8cf2baa715951b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 6 Feb 2017 08:50:49 +0100 Subject: [PATCH 20/21] image: rename some stuff. --- src/Makefile.am | 2 +- src/image/combine.cpp | 28 ++++++------- src/image/crop.cpp | 4 +- src/image/image-private.h | 6 +-- src/image/image.cpp | 58 +++++++++++++------------- src/image/kernel.cpp | 12 +++--- src/image/movie.cpp | 2 +- src/image/noise.cpp | 2 +- src/image/pixel.cpp | 4 +- src/image/resample.cpp | 26 ++++++------ src/image/resource.cpp | 8 ++-- src/lol/engine-internal.h | 1 + src/lol/engine.h | 1 + src/lol/gpu/framebuffer.h | 2 +- src/lol/image/image.h | 88 +++++++++++++++++++-------------------- src/lol/image/movie.h | 2 +- src/lol/image/resource.h | 10 ++--- src/lol/legacy.h | 26 ++++++++++++ src/lol/sys/threadtypes.h | 10 ++--- src/textureimage.h | 10 ++--- src/tiler.h | 8 ++-- src/tileset.h | 8 ++-- 22 files changed, 173 insertions(+), 145 deletions(-) create mode 100644 src/lol/legacy.h diff --git a/src/Makefile.am b/src/Makefile.am index 08e25f3c..a8541d94 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,7 +28,7 @@ liblol_core_a_SOURCES = \ liblol_core_a_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/3rdparty/bullet3/src -I$(srcdir)/3rdparty/lua/src liblol_core_headers = \ - lol/engine-internal.h lol/engine.h lol/public.h lol/extras.h \ + lol/engine-internal.h lol/engine.h lol/public.h lol/extras.h lol/legacy.h \ \ lol/base/all.h \ lol/base/avl_tree.h lol/base/features.h lol/base/tuple.h lol/base/types.h \ diff --git a/src/image/combine.cpp b/src/image/combine.cpp index 8fff377c..d52b302f 100644 --- a/src/image/combine.cpp +++ b/src/image/combine.cpp @@ -33,14 +33,14 @@ enum class MergeMode }; template -static Image GenericMerge(Image &src1, Image &src2, float alpha) +static image GenericMerge(image &src1, image &src2, float alpha) { typedef typename PixelType::type pixel_t; ASSERT(src1.GetSize() == src2.GetSize()); int const count = src1.GetSize().x * src2.GetSize().y; - Image dst(src1.GetSize()); + image dst(src1.GetSize()); pixel_t const *src1p = src1.Lock(); pixel_t const *src2p = src2.Lock(); @@ -79,7 +79,7 @@ static Image GenericMerge(Image &src1, Image &src2, float alpha) } template -static Image GenericMerge(Image &src1, Image &src2, float alpha) +static image GenericMerge(image &src1, image &src2, float alpha) { bool gray1 = src1.GetFormat() == PixelFormat::Y_8 || src1.GetFormat() == PixelFormat::Y_F32; @@ -92,57 +92,57 @@ static Image GenericMerge(Image &src1, Image &src2, float alpha) } -Image Image::Merge(Image &src1, Image &src2, float alpha) +image image::Merge(image &src1, image &src2, float alpha) { return GenericMerge(src1, src2, alpha); } -Image Image::Mean(Image &src1, Image &src2) +image image::Mean(image &src1, image &src2) { return GenericMerge(src1, src2, 0.5f); } -Image Image::Min(Image &src1, Image &src2) +image image::Min(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Max(Image &src1, Image &src2) +image image::Max(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Overlay(Image &src1, Image &src2) +image image::Overlay(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Screen(Image &src1, Image &src2) +image image::Screen(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Divide(Image &src1, Image &src2) +image image::Divide(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Multiply(Image &src1, Image &src2) +image image::Multiply(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Add(Image &src1, Image &src2) +image image::Add(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Sub(Image &src1, Image &src2) +image image::Sub(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } -Image Image::Difference(Image &src1, Image &src2) +image image::Difference(image &src1, image &src2) { return GenericMerge(src1, src2, 0.0f); } diff --git a/src/image/crop.cpp b/src/image/crop.cpp index 9709d5b7..9031187f 100644 --- a/src/image/crop.cpp +++ b/src/image/crop.cpp @@ -19,12 +19,12 @@ namespace lol { -Image Image::Crop(ibox2 box) const +image image::Crop(ibox2 box) const { ivec2 const srcsize = GetSize(); ivec2 const dstsize = box.extent(); - Image dst(dstsize); + image dst(dstsize); PixelFormat format = GetFormat(); if (format != PixelFormat::Unknown) diff --git a/src/image/image-private.h b/src/image/image-private.h index a700a857..221f3e66 100644 --- a/src/image/image-private.h +++ b/src/image/image-private.h @@ -43,12 +43,12 @@ public: array2d::type> m_array2d; }; -class ImageData +class image_data { - friend class Image; + friend class image; public: - ImageData() + image_data() : m_size(0, 0), m_wrap_x(WrapMode::Clamp), m_wrap_y(WrapMode::Clamp), diff --git a/src/image/image.cpp b/src/image/image.cpp index c96bc17e..20a99ade 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -23,30 +23,30 @@ namespace lol * Public Image class */ -Image::Image() - : m_data(new ImageData()) +image::image() + : m_data(new image_data()) { } -Image::Image(char const *path) - : m_data(new ImageData()) +image::image(char const *path) + : m_data(new image_data()) { Load(path); } -Image::Image(ivec2 size) - : m_data(new ImageData()) +image::image(ivec2 size) + : m_data(new image_data()) { SetSize(size); } -Image::Image (Image const &other) - : m_data(new ImageData()) +image::image (image const &other) + : m_data(new image_data()) { Copy(other); } -Image & Image::operator =(Image other) +image & image::operator =(image other) { /* Since the argument is passed by value, we’re assured it’s a new * object and we can safely swap our m_data pointers. */ @@ -54,7 +54,7 @@ Image & Image::operator =(Image other) return *this; } -Image::~Image() +image::~image() { for (int k : m_data->m_pixels.keys()) delete m_data->m_pixels[k]; @@ -62,7 +62,7 @@ Image::~Image() delete m_data; } -void Image::Copy(uint8_t* src_pixels, ivec2 const& size, PixelFormat fmt) +void image::Copy(uint8_t* src_pixels, ivec2 const& size, PixelFormat fmt) { ASSERT(fmt != PixelFormat::Unknown); SetSize(size); @@ -71,7 +71,7 @@ void Image::Copy(uint8_t* src_pixels, ivec2 const& size, PixelFormat fmt) size.x * size.y * BytesPerPixel(fmt)); } -void Image::Copy(Image const &src) +void image::Copy(image const &src) { ivec2 size = src.GetSize(); PixelFormat fmt = src.GetFormat(); @@ -86,12 +86,12 @@ void Image::Copy(Image const &src) } } -void Image::DummyFill() +void image::DummyFill() { Load("DUMMY"); } -bool Image::Load(char const *path) +bool image::Load(char const *path) { auto resource = ResourceLoader::Load(path); if (resource == nullptr) @@ -109,20 +109,20 @@ bool Image::Load(char const *path) return true; } -bool Image::Save(char const *path) +bool image::Save(char const *path) { - auto data = new ResourceImageData(new Image(*this)); + auto data = new ResourceImageData(new image(*this)); auto result = ResourceLoader::Save(path, data); delete data; return result; } -ivec2 Image::GetSize() const +ivec2 image::GetSize() const { return m_data->m_size; } -void Image::SetSize(ivec2 size) +void image::SetSize(ivec2 size) { ASSERT(size.x > 0); ASSERT(size.y > 0); @@ -142,24 +142,24 @@ void Image::SetSize(ivec2 size) } /* Wrap-around mode for some operations */ -WrapMode Image::GetWrapX() const +WrapMode image::GetWrapX() const { return m_data->m_wrap_x; } -WrapMode Image::GetWrapY() const +WrapMode image::GetWrapY() const { return m_data->m_wrap_y; } -void Image::SetWrap(WrapMode wrap_x, WrapMode wrap_y) +void image::SetWrap(WrapMode wrap_x, WrapMode wrap_y) { m_data->m_wrap_x = wrap_x; m_data->m_wrap_y = wrap_y; } /* The Lock() method */ -template typename PixelType::type *Image::Lock() +template typename PixelType::type *image::Lock() { SetFormat(T); @@ -167,7 +167,7 @@ template typename PixelType::type *Image::Lock() } /* The Lock2D() method */ -void *Image::Lock2DHelper(PixelFormat T) +void *image::Lock2DHelper(PixelFormat T) { SetFormat(T); @@ -175,7 +175,7 @@ void *Image::Lock2DHelper(PixelFormat T) } template -void Image::Unlock2D(array2d const &array) +void image::Unlock2D(array2d const &array) { ASSERT(m_data->m_pixels.has_key((int)m_data->m_format)); ASSERT(array.data() == m_data->m_pixels[(int)m_data->m_format]->data()); @@ -183,9 +183,9 @@ void Image::Unlock2D(array2d const &array) /* Explicit specialisations for the above templates */ #define _T(T) \ - template PixelType::type *Image::Lock(); \ - template array2d::type> &Image::Lock2D(); \ - template void Image::Unlock2D(array2d::type> const &array); + template PixelType::type *image::Lock(); \ + template array2d::type> &image::Lock2D(); \ + template void image::Unlock2D(array2d::type> const &array); _T(PixelFormat::Y_8) _T(PixelFormat::RGB_8) _T(PixelFormat::RGBA_8) @@ -195,14 +195,14 @@ _T(PixelFormat::RGBA_F32) #undef _T /* Special case for the "any" format: return the last active buffer */ -void *Image::Lock() +void *image::Lock() { ASSERT(m_data->m_format != PixelFormat::Unknown); return m_data->m_pixels[(int)m_data->m_format]->data(); } -void Image::Unlock(void const *pixels) +void image::Unlock(void const *pixels) { ASSERT(m_data->m_pixels.has_key((int)m_data->m_format)); ASSERT(pixels == m_data->m_pixels[(int)m_data->m_format]->data()); diff --git a/src/image/kernel.cpp b/src/image/kernel.cpp index d385f6ac..5b8ac465 100644 --- a/src/image/kernel.cpp +++ b/src/image/kernel.cpp @@ -17,7 +17,7 @@ namespace lol { -array2d Image::BayerKernel(ivec2 size) +array2d image::BayerKernel(ivec2 size) { array2d ret(size); @@ -46,7 +46,7 @@ array2d Image::BayerKernel(ivec2 size) return ret; } -array2d Image::HalftoneKernel(ivec2 size) +array2d image::HalftoneKernel(ivec2 size) { array2d ret(size); @@ -75,7 +75,7 @@ array2d Image::HalftoneKernel(ivec2 size) return NormalizeKernel(ret); } -array2d Image::BlueNoiseKernel(ivec2 size, ivec2 gsize) +array2d image::BlueNoiseKernel(ivec2 size, ivec2 gsize) { float const epsilon = 1.f / (size.x * size.y + 1); gsize = lol::min(size, gsize); @@ -180,7 +180,7 @@ static int cmpdot(const void *p1, const void *p2) return ((Dot const *)p1)->val > ((Dot const *)p2)->val; } -array2d Image::NormalizeKernel(array2d const &kernel) +array2d image::NormalizeKernel(array2d const &kernel) { ivec2 size = kernel.size(); @@ -209,7 +209,7 @@ array2d Image::NormalizeKernel(array2d const &kernel) return dst; } -array2d Image::EdiffKernel(EdiffAlgorithm algorithm) +array2d image::EdiffKernel(EdiffAlgorithm algorithm) { switch (algorithm) { @@ -270,7 +270,7 @@ array2d Image::EdiffKernel(EdiffAlgorithm algorithm) * there is little chance that any value below 0.2 will be useful. */ #define BLUR_EPSILON 0.2f -array2d Image::GaussianKernel(vec2 radius, float angle, vec2 delta) +array2d image::GaussianKernel(vec2 radius, float angle, vec2 delta) { array2d kernel; diff --git a/src/image/movie.cpp b/src/image/movie.cpp index 6a3bc6fc..8824de16 100644 --- a/src/image/movie.cpp +++ b/src/image/movie.cpp @@ -60,7 +60,7 @@ Movie::~Movie() delete m_data; } -void Movie::Feed(Image const &image) +void Movie::Feed(image const &image) { #if LOL_USE_FFMPEG diff --git a/src/image/noise.cpp b/src/image/noise.cpp index 0f6dc8b4..2da3b314 100644 --- a/src/image/noise.cpp +++ b/src/image/noise.cpp @@ -17,7 +17,7 @@ namespace lol { -bool Image::RenderRandom(ivec2 size) +bool image::RenderRandom(ivec2 size) { SetSize(size); vec4 *pixels = Lock(); diff --git a/src/image/pixel.cpp b/src/image/pixel.cpp index 591ade7b..f6d3b2ef 100644 --- a/src/image/pixel.cpp +++ b/src/image/pixel.cpp @@ -60,7 +60,7 @@ static u8vec4 f32tou8(vec4 pixel) * Pixel-level image manipulation */ -PixelFormat Image::GetFormat() const +PixelFormat image::GetFormat() const { return m_data->m_format; } @@ -81,7 +81,7 @@ PixelFormat Image::GetFormat() const * x lossless conversion (u8 to float) * # lossy conversion (dithering and/or convert color→gray) */ -void Image::SetFormat(PixelFormat fmt) +void image::SetFormat(PixelFormat fmt) { PixelFormat old_fmt = m_data->m_format; diff --git a/src/image/resample.cpp b/src/image/resample.cpp index 4c990463..55cad2d6 100644 --- a/src/image/resample.cpp +++ b/src/image/resample.cpp @@ -17,10 +17,10 @@ namespace lol { -static Image ResizeBicubic(Image &image, ivec2 size); -static Image ResizeBresenham(Image &image, ivec2 size); +static image ResizeBicubic(image &src, ivec2 size); +static image ResizeBresenham(image &src, ivec2 size); -Image Image::Resize(ivec2 size, ResampleAlgorithm algorithm) +image image::Resize(ivec2 size, ResampleAlgorithm algorithm) { switch (algorithm) { @@ -32,12 +32,12 @@ Image Image::Resize(ivec2 size, ResampleAlgorithm algorithm) } } -static Image ResizeBicubic(Image &image, ivec2 size) +static image ResizeBicubic(image &src, ivec2 size) { - Image dst(size); - ivec2 const oldsize = image.GetSize(); + image dst(size); + ivec2 const oldsize = src.GetSize(); - vec4 const *srcp = image.Lock(); + vec4 const *srcp = src.Lock(); vec4 *dstp = dst.Lock(); float scalex = size.x > 1 ? (oldsize.x - 1.f) / (size.x - 1) : 1.f; @@ -122,7 +122,7 @@ static Image ResizeBicubic(Image &image, ivec2 size) } dst.Unlock(dstp); - image.Unlock(srcp); + src.Unlock(srcp); return dst; } @@ -134,13 +134,13 @@ static Image ResizeBicubic(Image &image, ivec2 size) /* FIXME: the algorithm does not handle alpha components properly. Resulting * alpha should be the mean alpha value of the neightbouring pixels, but * the colour components should be weighted with the alpha value. */ -static Image ResizeBresenham(Image &image, ivec2 size) +static image ResizeBresenham(image &src, ivec2 size) { - Image dst(size); - ivec2 const oldsize = image.GetSize(); + image dst(size); + ivec2 const oldsize = src.GetSize(); float const invswsh = 1.0f / (oldsize.x * oldsize.y); - vec4 const *srcp = image.Lock(); + vec4 const *srcp = src.Lock(); vec4 *dstp = dst.Lock(); array aline, line; @@ -199,7 +199,7 @@ static Image ResizeBresenham(Image &image, ivec2 size) } dst.Unlock(dstp); - image.Unlock(srcp); + src.Unlock(srcp); return dst; } diff --git a/src/image/resource.cpp b/src/image/resource.cpp index 7f46c4ed..18fa4734 100644 --- a/src/image/resource.cpp +++ b/src/image/resource.cpp @@ -88,13 +88,13 @@ ResourceCodecData* ResourceLoader::Load(char const *path) auto data = codec->Load(path); if (data != nullptr) { - msg::info("Image::Load: Codec %s succesfully loaded %s.\n", codec->GetName(), path); + msg::info("image::Load: Codec %s succesfully loaded %s.\n", codec->GetName(), path); return data; } } //Log error, because we shouldn't be here - msg::error("Image::Load: Last codec %s, Error loading resource %s.\n", last_codec->GetName(), path); + msg::error("image::Load: Last codec %s, Error loading resource %s.\n", last_codec->GetName(), path); return nullptr; } @@ -106,13 +106,13 @@ bool ResourceLoader::Save(char const *path, ResourceCodecData* data) last_codec = codec; if (codec->Save(path, data)) { - msg::info("Image::Save: Codec %s succesfully saved %s.\n", codec->GetName(), path); + msg::info("image::Save: Codec %s succesfully saved %s.\n", codec->GetName(), path); return true; } } //Log error, because we shouldn't be here - msg::error("Image::Save: Last codec %s, Error saving resource %s.\n", last_codec->GetName(), path); + msg::error("image::Save: Last codec %s, Error saving resource %s.\n", last_codec->GetName(), path); return false; } diff --git a/src/lol/engine-internal.h b/src/lol/engine-internal.h index 89b33602..7c37478d 100644 --- a/src/lol/engine-internal.h +++ b/src/lol/engine-internal.h @@ -36,4 +36,5 @@ #include #include +#include diff --git a/src/lol/engine.h b/src/lol/engine.h index 4ce972bd..8ccaf884 100644 --- a/src/lol/engine.h +++ b/src/lol/engine.h @@ -31,4 +31,5 @@ #include #include +#include diff --git a/src/lol/gpu/framebuffer.h b/src/lol/gpu/framebuffer.h index 35e333f2..1038e0f4 100644 --- a/src/lol/gpu/framebuffer.h +++ b/src/lol/gpu/framebuffer.h @@ -103,7 +103,7 @@ public: TextureUniform GetTextureUniform() const; ivec2 GetSize() const; - class Image GetImage() const; + class image GetImage() const; void Bind(); void Unbind(); diff --git a/src/lol/image/image.h b/src/lol/image/image.h index 42fffa15..1ec2ffed 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -13,7 +13,7 @@ #pragma once // -// The Image class +// The image class // --------------- // @@ -58,24 +58,24 @@ enum class EdiffAlgorithm : uint8_t Lite, }; -//Image ----------------------------------------------------------------------- -class Image +// image ----------------------------------------------------------------------- +class image { public: - Image(); - Image(ivec2 size); + image(); + image(ivec2 size); /* XXX: use of this ctor should be discouraged, as it will not * return information about a possible error. */ - Image(char const *path); + image(char const *path); /* Rule of three */ - Image(Image const &other); - Image & operator =(Image other); - ~Image(); + image(image const &other); + image & operator =(image other); + ~image(); void DummyFill(); void Copy(uint8_t* pixels, ivec2 const& size, PixelFormat fmt); - void Copy(Image const &other); + void Copy(image const &other); bool Load(char const *path); bool Save(char const *path); @@ -121,51 +121,51 @@ public: bool RenderRandom(ivec2 size); /* Resize and crop */ - Image Resize(ivec2 size, ResampleAlgorithm algorithm); - Image Crop(ibox2 box) const; + image Resize(ivec2 size, ResampleAlgorithm algorithm); + image Crop(ibox2 box) const; /* Image processing */ - Image AutoContrast() const; - Image Brightness(float val) const; - Image Contrast(float val) const; - Image Convolution(array2d const &kernel); - Image Dilate(); - Image Erode(); - Image Invert() const; - Image Median(ivec2 radii) const; - Image Median(array2d const &kernel) const; - Image Sharpen(array2d const &kernel); - Image Threshold(float val) const; - Image Threshold(vec3 val) const; - Image RGBToYUV() const; - Image YUVToRGB() const; + image AutoContrast() const; + image Brightness(float val) const; + image Contrast(float val) const; + image Convolution(array2d const &kernel); + image Dilate(); + image Erode(); + image Invert() const; + image Median(ivec2 radii) const; + image Median(array2d const &kernel) const; + image Sharpen(array2d const &kernel); + image Threshold(float val) const; + image Threshold(vec3 val) const; + image RGBToYUV() const; + image YUVToRGB() const; /* Dithering */ - Image DitherRandom() const; - Image DitherEdiff(array2d const &kernel, + image DitherRandom() const; + image DitherEdiff(array2d const &kernel, ScanMode scan = ScanMode::Raster) const; - Image DitherOstromoukhov(ScanMode scan = ScanMode::Raster) const; - Image DitherOrdered(array2d const &kernel) const; - Image DitherHalftone(float radius, float angle) const; - Image DitherDbs() const; + image DitherOstromoukhov(ScanMode scan = ScanMode::Raster) const; + image DitherOrdered(array2d const &kernel) const; + image DitherHalftone(float radius, float angle) const; + image DitherDbs() const; /* Combine images */ - static Image Merge(Image &src1, Image &src2, float alpha); - static Image Mean(Image &src1, Image &src2); - static Image Min(Image &src1, Image &src2); - static Image Max(Image &src1, Image &src2); - static Image Overlay(Image &src1, Image &src2); - static Image Screen(Image &src1, Image &src2); - static Image Multiply(Image &src1, Image &src2); - static Image Divide(Image &src1, Image &src2); - static Image Add(Image &src1, Image &src2); - static Image Sub(Image &src1, Image &src2); - static Image Difference(Image &src1, Image &src2); + static image Merge(image &src1, image &src2, float alpha); + static image Mean(image &src1, image &src2); + static image Min(image &src1, image &src2); + static image Max(image &src1, image &src2); + static image Overlay(image &src1, image &src2); + static image Screen(image &src1, image &src2); + static image Multiply(image &src1, image &src2); + static image Divide(image &src1, image &src2); + static image Add(image &src1, image &src2); + static image Sub(image &src1, image &src2); + static image Difference(image &src1, image &src2); private: void *Lock2DHelper(PixelFormat T); - class ImageData *m_data; + class image_data *m_data; }; } /* namespace lol */ diff --git a/src/lol/image/movie.h b/src/lol/image/movie.h index b0b948b2..b7e0609a 100644 --- a/src/lol/image/movie.h +++ b/src/lol/image/movie.h @@ -34,7 +34,7 @@ public: #endif ~Movie(); - void Feed(Image const &image); + void Feed(image const &image); private: class MovieData *m_data; diff --git a/src/lol/image/resource.h b/src/lol/image/resource.h index f8be8bf5..1fe05689 100644 --- a/src/lol/image/resource.h +++ b/src/lol/image/resource.h @@ -37,9 +37,9 @@ namespace lol class ResourceImageData : public ResourceCodecData { public: - ResourceImageData(Image* image) + ResourceImageData(image* img) { - m_image = image; + m_image = img; } virtual ~ResourceImageData() @@ -48,15 +48,15 @@ namespace lol delete m_image; } - Image* m_image = nullptr; + image* m_image = nullptr; }; //ResourceImageData ----------------------------------------------------------- class ResourceTilesetData : public ResourceImageData { public: - ResourceTilesetData(Image* image) - : ResourceImageData(image) + ResourceTilesetData(image* img) + : ResourceImageData(img) { } array m_tiles; diff --git a/src/lol/legacy.h b/src/lol/legacy.h new file mode 100644 index 00000000..aae63039 --- /dev/null +++ b/src/lol/legacy.h @@ -0,0 +1,26 @@ +// +// Lol Engine +// +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// Legacy types that will disappear one day +// ---------------------------------------- +// + +namespace lol +{ + +typedef image Image; + +} /* namespace lol */ + diff --git a/src/lol/sys/threadtypes.h b/src/lol/sys/threadtypes.h index b7ebba71..ec575aac 100644 --- a/src/lol/sys/threadtypes.h +++ b/src/lol/sys/threadtypes.h @@ -124,16 +124,16 @@ public: { } //Returns a dummy image, and start a job to load the image on a thread - Image* Load(const lol::String& path); - bool CheckStatus(Image* image); + image* Load(const lol::String& path); + bool CheckStatus(image* img); protected: virtual void TreatResult(ThreadJob* result); private: - Image m_dummy_image; - map m_images; - array m_loaded_images; + image m_dummy_image; + map m_images; + array m_loaded_images; }; } /* namespace lol */ diff --git a/src/textureimage.h b/src/textureimage.h index b798bbef..2ef7500f 100644 --- a/src/textureimage.h +++ b/src/textureimage.h @@ -39,13 +39,13 @@ protected: public: TextureImage(char const *path); - TextureImage(char const *path, Image* image); + TextureImage(char const *path, image* img); virtual ~TextureImage(); protected: void Init(char const *path); virtual void Init(char const *path, ResourceCodecData* loaded_data); - virtual void Init(char const *path, Image* image); + virtual void Init(char const *path, image* img); protected: virtual void TickDraw(float seconds, Scene &scene); @@ -54,11 +54,11 @@ public: /* Inherited from Entity */ virtual char const *GetName(); - void UpdateTexture(Image* image); + void UpdateTexture(image* img); Texture * GetTexture(); Texture const * GetTexture() const; - Image * GetImage(); - Image const * GetImage() const; + image * GetImage(); + image const * GetImage() const; ivec2 GetImageSize() const; ivec2 GetTextureSize() const; void Bind(); diff --git a/src/tiler.h b/src/tiler.h index f21ed26f..7ec6f91b 100644 --- a/src/tiler.h +++ b/src/tiler.h @@ -30,10 +30,10 @@ public: static TileSet *Register(char const *path, ivec2 size, ivec2 count); static TileSet *Register(String const &path); static TileSet *Register(char const *path); - static TileSet *Register(String const &path, Image* image, ivec2 size, ivec2 count); - static TileSet *Register(char const *path, Image* image, ivec2 size, ivec2 count); - static TileSet *Register(String const &path, Image* image); - static TileSet *Register(char const *path, Image* image); + static TileSet *Register(String const &path, image* img, ivec2 size, ivec2 count); + static TileSet *Register(char const *path, image* img, ivec2 size, ivec2 count); + static TileSet *Register(String const &path, image* img); + static TileSet *Register(char const *path, image* img); static void Deregister(TileSet *); private: diff --git a/src/tileset.h b/src/tileset.h index cb5348e7..cea01d10 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -42,18 +42,18 @@ class TileSet : public TextureImage public: TileSet(char const *path); - TileSet(char const *path, Image* image); - TileSet(char const *path, Image* image, array& tiles); + TileSet(char const *path, image* img); + TileSet(char const *path, image* img, array& tiles); /* Old style: path to PNG file */ TileSet(char const *path, ivec2 size, ivec2 count); - TileSet(char const *path, Image* image, ivec2 size, ivec2 count); + TileSet(char const *path, image* img, ivec2 size, ivec2 count); virtual ~TileSet(); protected: virtual void Init(char const *path, ResourceCodecData* loaded_data); - virtual void Init(char const *path, Image* image); + virtual void Init(char const *path, image* img); public: /* Inherited from Entity */ From 7558d0664aa4e87696b9520888d209ec8cfbbf59 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 13 Jun 2017 09:28:18 +0200 Subject: [PATCH 21/21] image: more method renaming for consistency. --- doc/samples/simplex.cpp | 10 +- doc/tutorial/14_lol_lua.cpp | 8 +- src/gpu/framebuffer.cpp | 22 ++-- src/image/codec/dummy-image.cpp | 20 ++-- src/image/codec/gdiplus-image.cpp | 14 +-- src/image/codec/imlib2-image.cpp | 14 +-- src/image/codec/oric-image.cpp | 46 ++++---- src/image/codec/sdl-image.cpp | 14 +-- src/image/codec/zed-image.cpp | 28 ++--- src/image/codec/zed-palette-image.cpp | 27 ++--- src/image/combine.cpp | 70 ++++++------ src/image/crop.cpp | 28 ++--- src/image/dither/dbs.cpp | 72 +++++++------ src/image/dither/ediff.cpp | 46 ++++---- src/image/dither/ordered.cpp | 54 +++++----- src/image/dither/ostromoukhov.cpp | 26 ++--- src/image/dither/random.cpp | 24 +++-- src/image/filter/colors.cpp | 98 ++++++++--------- src/image/filter/convolution.cpp | 104 +++++++++--------- src/image/filter/dilate.cpp | 150 +++++++++++++------------- src/image/filter/median.cpp | 106 +++++++++--------- src/image/filter/yuv.cpp | 34 +++--- src/image/image.cpp | 54 +++++----- src/image/kernel.cpp | 28 ++--- src/image/noise.cpp | 20 ++-- src/image/pixel.cpp | 66 ++++++------ src/image/resample.cpp | 34 +++--- src/lol/image/image.h | 66 ++++++------ src/lol/sys/file.h | 16 +-- src/sys/file.cpp | 20 ++-- src/sys/threadtypes.cpp | 4 +- src/t/image/image.cpp | 14 +-- src/textureimage.cpp | 43 ++++---- 33 files changed, 717 insertions(+), 663 deletions(-) diff --git a/doc/samples/simplex.cpp b/doc/samples/simplex.cpp index 1b7404fe..71c82c5f 100644 --- a/doc/samples/simplex.cpp +++ b/doc/samples/simplex.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Simplex Noise tutorial // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // © 2013-2014 Guillaume Bittoun // // Lol Engine is free software. It comes without any warranty, to @@ -31,8 +31,8 @@ int main(int argc, char **argv) srand(time(nullptr)); /* Create an image */ - Image img(size); - array2d &data = img.Lock2D(); + image img(size); + array2d &data = img.lock2d(); /* Declare plenty of allocators */ simplex_noise<2> s2; @@ -141,7 +141,7 @@ int main(int argc, char **argv) #endif /* Save image */ - img.Unlock2D(data); - img.Save("simplex.png"); + img.unlock2d(data); + img.save("simplex.png"); } diff --git a/doc/tutorial/14_lol_lua.cpp b/doc/tutorial/14_lol_lua.cpp index 412795e5..adbc9a12 100644 --- a/doc/tutorial/14_lol_lua.cpp +++ b/doc/tutorial/14_lol_lua.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Lua tutorial // -// Copyright © 2014—2015 Benjamin “Touky” Huet +// Copyright © 2014—2017 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -36,14 +36,14 @@ public: //------------------------------------------------------------------------- static int AddFive(lua_State* l) { - auto stack = LuaStack::Begin(l); - int32_t i = stack.Get(); + auto stack = LuaStack::Begin(l); + int32_t i = stack.Get(); i += 5; return (stack << i).End(); } - + LOLUA_DECLARE_RETURN_METHOD_ARGS(AddTenInstance, GetPtr(), AddTenMethod, Get(), Get(), Get()); static int _AddTenInstance(lua_State* l) { diff --git a/src/gpu/framebuffer.cpp b/src/gpu/framebuffer.cpp index ab261ea2..e3094281 100644 --- a/src/gpu/framebuffer.cpp +++ b/src/gpu/framebuffer.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -343,14 +345,14 @@ ivec2 Framebuffer::GetSize() const return m_data->m_size; } -Image Framebuffer::GetImage() const +image Framebuffer::GetImage() const { - Image ret(m_data->m_size); + image ret(m_data->m_size); - u8vec4 *buffer = ret.Lock(); + u8vec4 *buffer = ret.lock(); glReadPixels(0, 0, m_data->m_size.x, m_data->m_size.y, GL_RGBA, GL_UNSIGNED_BYTE, buffer); - ret.Unlock(buffer); + ret.unlock(buffer); return ret; } diff --git a/src/image/codec/dummy-image.cpp b/src/image/codec/dummy-image.cpp index 12319dc0..bf1f6271 100644 --- a/src/image/codec/dummy-image.cpp +++ b/src/image/codec/dummy-image.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -39,9 +41,9 @@ ResourceCodecData* DummyImageCodec::Load(char const *path) if (strcmp("DUMMY", path)) return nullptr; - auto data = new ResourceImageData(new Image(ivec2(256))); + auto data = new ResourceImageData(new image(ivec2(256))); auto image = data->m_image; - u8vec4 *pixels = image->Lock(), *tmp = pixels; + u8vec4 *pixels = image->lock(), *tmp = pixels; for (int j = 0; j < 256; j++) for (int i = 0; i < 256; i++) { @@ -51,7 +53,7 @@ ResourceCodecData* DummyImageCodec::Load(char const *path) tmp->a = (((i >> 4) ^ (j >> 4)) & 1) * 0xff; ++tmp; } - image->Unlock(pixels); + image->unlock(pixels); return data; } diff --git a/src/image/codec/gdiplus-image.cpp b/src/image/codec/gdiplus-image.cpp index f8b2ae33..a4d94878 100644 --- a/src/image/codec/gdiplus-image.cpp +++ b/src/image/codec/gdiplus-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -116,13 +116,13 @@ ResourceCodecData* GdiPlusImageCodec::Load(char const *path) /* FIXME: GDI+ doesn't know about RGBA, only ARGB. And OpenGL doesn't * know about ARGB, only RGBA. So we swap bytes. We could also fix * this in the shader. */ - auto data = new ResourceImageData(new Image(ivec2(size))); + auto data = new ResourceImageData(new image(ivec2(size))); auto image = data->m_image; - u8vec4 *pdst = image->Lock(); + u8vec4 *pdst = image->lock(); u8vec4 *psrc = static_cast(bdata.Scan0); for (int n = 0; n < size.x * size.y; n++) pdst[n] = psrc[n].bgra; - image->Unlock(pdst); + image->unlock(pdst); bitmap->UnlockBits(&bdata); delete bitmap; @@ -184,7 +184,7 @@ bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) } auto image = data_image->m_image; - ivec2 size = image->GetSize(); + ivec2 size = image->size(); Gdiplus::Bitmap *b = new Gdiplus::Bitmap(size.x, size.y, PixelFormat32bppARGB); @@ -201,13 +201,13 @@ bool GdiPlusImageCodec::Save(char const *path, ResourceCodecData* data) return false; } - u8vec4 *psrc = image->Lock(); + u8vec4 *psrc = image->lock(); u8vec4 *psrc0 = psrc; u8vec4 *pdst = static_cast(bdata.Scan0); for (int y = 0; y < size.y; y++) for (int x = 0; x < size.x; x++) *pdst++ = (*psrc++).bgra; - image->Unlock(psrc0); + image->unlock(psrc0); b->UnlockBits(&bdata); if (b->Save(wpath, &clsid, nullptr) != Gdiplus::Ok) diff --git a/src/image/codec/imlib2-image.cpp b/src/image/codec/imlib2-image.cpp index 4896972c..d83f145c 100644 --- a/src/image/codec/imlib2-image.cpp +++ b/src/image/codec/imlib2-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -74,10 +74,10 @@ ResourceCodecData *Imlib2ImageCodec::Load(char const *path) } ivec2 size(imlib_image_get_width(), imlib_image_get_height()); - auto data = new ResourceImageData(new Image(size)); + auto data = new ResourceImageData(new image(size)); auto image = data->m_image; - u8vec4 *dstdata = image->Lock(); + u8vec4 *dstdata = image->lock(); for (int i = 0; i < size.x * size.y; i++) { @@ -86,7 +86,7 @@ ResourceCodecData *Imlib2ImageCodec::Load(char const *path) else dstdata[i] = srcdata[i].bgra; } - image->Unlock(dstdata); + image->unlock(dstdata); imlib_free_image(); @@ -100,13 +100,13 @@ bool Imlib2ImageCodec::Save(char const *path, ResourceCodecData *data) return false; auto image = data_image->m_image; - ivec2 size = image->GetSize(); + ivec2 size = image->size(); Imlib_Image priv = imlib_create_image(size.x, size.y); imlib_context_set_image(priv); imlib_image_set_has_alpha(1); - u8vec4 const *srcdata = image->Lock(); + u8vec4 const *srcdata = image->lock(); u8vec4 *dstdata = (u8vec4 *)imlib_image_get_data(); for (int i = 0; i < size.x * size.y; i++) @@ -118,7 +118,7 @@ bool Imlib2ImageCodec::Save(char const *path, ResourceCodecData *data) } imlib_image_put_back_data((DATA32 *)dstdata); - image->Unlock(srcdata); + image->unlock(srcdata); imlib_save_image(path); imlib_free_image(); diff --git a/src/image/codec/oric-image.cpp b/src/image/codec/oric-image.cpp index 348b59fb..3cf79210 100644 --- a/src/image/codec/oric-image.cpp +++ b/src/image/codec/oric-image.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -36,7 +38,7 @@ public: private: static String ReadScreen(char const *name); - static void WriteScreen(Image &image, array &result); + static void WriteScreen(image &image, array &result); }; DECLARE_IMAGE_CODEC(OricImageCodec, 100) @@ -63,12 +65,12 @@ ResourceCodecData* OricImageCodec::Load(char const *path) if (screen.count() == 0) return nullptr; - auto data = new ResourceImageData(new Image(ivec2(WIDTH, screen.count() * 6 / WIDTH))); - auto image = data->m_image; + auto data = new ResourceImageData(new image(ivec2(WIDTH, screen.count() * 6 / WIDTH))); + auto img = data->m_image; - u8vec4 *pixels = image->Lock(); + u8vec4 *pixels = img->lock(); - for (int y = 0; y < image->GetSize().y; y++) + for (int y = 0; y < img->size().y; y++) { u8vec2 bgfg(0, 7); @@ -101,7 +103,7 @@ ResourceCodecData* OricImageCodec::Load(char const *path) } } - image->Unlock(pixels); + img->unlock(pixels); return data; } @@ -129,18 +131,18 @@ bool OricImageCodec::Save(char const *path, ResourceCodecData* data) result << (uint8_t)name[0]; result << 0; - auto image = data_image->m_image; - Image tmp; - ivec2 size = image->GetSize(); + auto img = data_image->m_image; + image tmp; + ivec2 size = img->size(); if (size.x != WIDTH) { size.y = (int)((float)size.y * WIDTH / size.x); size.x = WIDTH; - tmp = image->Resize(size, ResampleAlgorithm::Bresenham); - image = &tmp; + tmp = img->Resize(size, ResampleAlgorithm::Bresenham); + img = &tmp; } - WriteScreen(*image, result); + WriteScreen(*img, result); File f; f.Open(path, FileAccess::Write); @@ -469,10 +471,10 @@ static uint8_t bestmove(ivec3 const *in, u8vec2 bgfg, return bestcommand; } -void OricImageCodec::WriteScreen(Image &image, array &result) +void OricImageCodec::WriteScreen(image &img, array &result) { - ivec2 size = image.GetSize(); - vec4 *pixels = image.Lock(); + ivec2 size = img.size(); + vec4 *pixels = img.lock(); int stride = (size.x + 1); @@ -526,7 +528,7 @@ void OricImageCodec::WriteScreen(Image &image, array &result) } } - image.Unlock(pixels); + img.unlock(pixels); //fprintf(stderr, " done.\n"); } diff --git a/src/image/codec/sdl-image.cpp b/src/image/codec/sdl-image.cpp index 416bff2d..e24e6dcb 100644 --- a/src/image/codec/sdl-image.cpp +++ b/src/image/codec/sdl-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -75,11 +75,11 @@ ResourceCodecData* SdlImageCodec::Load(char const *path) surface = tmp; } - auto data = new ResourceImageData(new Image(size)); + auto data = new ResourceImageData(new image(size)); auto image = data->m_image; - u8vec4 *pixel_data = image->Lock(); + u8vec4 *pixel_data = image->lock(); memcpy(pixel_data, surface->pixels, 4 * size.x * size.y); - image->Unlock(pixel_data); + image->unlock(pixel_data); SDL_FreeSurface(surface); @@ -93,12 +93,12 @@ bool SdlImageCodec::Save(char const *path, ResourceCodecData* data) return false; auto image = data_image->m_image; - ivec2 size = image->GetSize(); + ivec2 size = image->size(); SDL_Surface *surface = Create32BppSurface(size); - u8vec4 *pixel_data = image->Lock(); + u8vec4 *pixel_data = image->lock(); memcpy(surface->pixels, pixel_data, 4 * size.x * size.y); - image->Unlock(pixel_data); + image->unlock(pixel_data); int ret = SDL_SaveBMP(surface, path); SDL_FreeSurface(surface); diff --git a/src/image/codec/zed-image.cpp b/src/image/codec/zed-image.cpp index 91ee8c6b..0d8e00d7 100644 --- a/src/image/codec/zed-image.cpp +++ b/src/image/codec/zed-image.cpp @@ -1,12 +1,14 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2014 Benjamin Huet -// 2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// 2014 Benjamin Huet +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -43,8 +45,8 @@ ResourceCodecData* ZedImageCodec::Load(char const *path) struct CompactSecondary { CompactSecondary(int32_t size) { m_size = size; } - int32_t m_size; - array m_tiles; + int32_t m_size; + array m_tiles; }; struct CompactMain { @@ -108,7 +110,7 @@ ResourceCodecData* ZedImageCodec::Load(char const *path) file.Open(path, FileAccess::Read, true); //Put file in memory - long file_size = file.GetSize(); + long file_size = file.size(); array file_buffer; file_buffer.resize(file_size); file.Read((uint8_t*)&file_buffer[0], file_size); @@ -227,9 +229,9 @@ ResourceCodecData* ZedImageCodec::Load(char const *path) tex_size <<= 1; //Prepare final image - auto data = new ResourceTilesetData(new Image(ivec2(tex_size))); + auto data = new ResourceTilesetData(new image(ivec2(tex_size))); auto image = data->m_image; - uint8_t *pixels = image->Lock(); + uint8_t *pixels = image->lock(); //Data refactor stage ivec2 pos = ivec2(0); @@ -284,7 +286,7 @@ ResourceCodecData* ZedImageCodec::Load(char const *path) j++; } } - image->Unlock(pixels); + image->unlock(pixels); data->m_tiles = tiles; diff --git a/src/image/codec/zed-palette-image.cpp b/src/image/codec/zed-palette-image.cpp index a0363962..e67e0788 100644 --- a/src/image/codec/zed-palette-image.cpp +++ b/src/image/codec/zed-palette-image.cpp @@ -1,11 +1,14 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// 2014 Benjamin Huet +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -41,8 +44,8 @@ ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) File file; file.Open(path, FileAccess::Read, true); - //Put file in memory - long file_size = file.GetSize(); + // Put file in memory + long file_size = file.size(); array file_buffer; file_buffer.resize(file_size); file.Read((uint8_t*)&file_buffer[0], file_size); @@ -53,18 +56,18 @@ ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) int32_t tex_size = 2; while (tex_size < tex_sqrt) tex_size <<= 1; - auto data = new ResourceImageData(new Image(ivec2(tex_size))); + auto data = new ResourceImageData(new image(ivec2(tex_size))); auto image = data->m_image; #else int32_t tex_sqrt = file_size / 3; int32_t tex_size = 2; while (tex_size < tex_sqrt) tex_size <<= 1; - auto data = new ResourceImageData(new Image(ivec2(tex_size, 1))); + auto data = new ResourceImageData(new image(ivec2(tex_size, 1))); auto image = data->m_image; #endif - u8vec4 *pixels = image->Lock(); + u8vec4 *pixels = image->lock(); for (int i = 0; i < file_buffer.count();) { pixels->r = file_buffer[i++]; @@ -73,7 +76,7 @@ ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) pixels->a = (i == 0) ? 0 : 255; ++pixels; } - image->Unlock(pixels); + image->unlock(pixels); return data; } diff --git a/src/image/combine.cpp b/src/image/combine.cpp index d52b302f..d37fc832 100644 --- a/src/image/combine.cpp +++ b/src/image/combine.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -33,18 +35,18 @@ enum class MergeMode }; template -static image GenericMerge(image &src1, image &src2, float alpha) +static image generic_merge(image &src1, image &src2, float alpha) { typedef typename PixelType::type pixel_t; - ASSERT(src1.GetSize() == src2.GetSize()); - int const count = src1.GetSize().x * src2.GetSize().y; + ASSERT(src1.size() == src2.size()); + int const count = src1.size().x * src2.size().y; - image dst(src1.GetSize()); + image dst(src1.size()); - pixel_t const *src1p = src1.Lock(); - pixel_t const *src2p = src2.Lock(); - pixel_t *dstp = dst.Lock(); + pixel_t const *src1p = src1.lock(); + pixel_t const *src2p = src2.lock(); + pixel_t *dstp = dst.lock(); for (int n = 0; n < count; ++n) { @@ -71,80 +73,80 @@ static image GenericMerge(image &src1, image &src2, float alpha) dstp[n] = lol::abs(src1p[n] - src2p[n]); } - src1.Unlock(src1p); - src2.Unlock(src2p); - dst.Unlock(dstp); + src1.unlock(src1p); + src2.unlock(src2p); + dst.unlock(dstp); return dst; } template -static image GenericMerge(image &src1, image &src2, float alpha) +static image generic_merge(image &src1, image &src2, float alpha) { - bool gray1 = src1.GetFormat() == PixelFormat::Y_8 - || src1.GetFormat() == PixelFormat::Y_F32; - bool gray2 = src2.GetFormat() == PixelFormat::Y_8 - || src2.GetFormat() == PixelFormat::Y_F32; + bool gray1 = src1.format() == PixelFormat::Y_8 + || src1.format() == PixelFormat::Y_F32; + bool gray2 = src2.format() == PixelFormat::Y_8 + || src2.format() == PixelFormat::Y_F32; if (gray1 && gray2) - return GenericMerge(src1, src2, alpha); + return generic_merge(src1, src2, alpha); else - return GenericMerge(src1, src2, alpha); + return generic_merge(src1, src2, alpha); } image image::Merge(image &src1, image &src2, float alpha) { - return GenericMerge(src1, src2, alpha); + return generic_merge(src1, src2, alpha); } image image::Mean(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.5f); + return generic_merge(src1, src2, 0.5f); } image image::Min(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Max(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Overlay(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Screen(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Divide(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Multiply(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Add(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Sub(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } image image::Difference(image &src1, image &src2) { - return GenericMerge(src1, src2, 0.0f); + return generic_merge(src1, src2, 0.0f); } } /* namespace lol */ diff --git a/src/image/crop.cpp b/src/image/crop.cpp index 9031187f..707ea85f 100644 --- a/src/image/crop.cpp +++ b/src/image/crop.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -21,18 +23,18 @@ namespace lol image image::Crop(ibox2 box) const { - ivec2 const srcsize = GetSize(); + ivec2 const srcsize = size(); ivec2 const dstsize = box.extent(); image dst(dstsize); - PixelFormat format = GetFormat(); + PixelFormat fmt = format(); - if (format != PixelFormat::Unknown) + if (fmt != PixelFormat::Unknown) { - dst.SetFormat(format); - uint8_t const *srcp = (uint8_t const *)m_data->m_pixels[(int)format]; - uint8_t *dstp = (uint8_t *)dst.m_data->m_pixels[(int)format]; - uint8_t bpp = BytesPerPixel(format); + dst.set_format(fmt); + uint8_t const *srcp = (uint8_t const *)m_data->m_pixels[(int)fmt]; + uint8_t *dstp = (uint8_t *)dst.m_data->m_pixels[(int)fmt]; + uint8_t bpp = BytesPerPixel(fmt); int len = dstsize.x; diff --git a/src/image/dither/dbs.cpp b/src/image/dither/dbs.cpp index 496ec023..b0fd5ea2 100644 --- a/src/image/dither/dbs.cpp +++ b/src/image/dither/dbs.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2015 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -24,44 +26,44 @@ namespace lol /* FIXME: though the algorithm is supposed to stop, we do not have a real, * guaranteed stop condition here. */ -Image Image::DitherDbs() const +image image::dither_dbs() const { - ivec2 size = GetSize(); + ivec2 isize = size(); /* Build our human visual system kernel. */ - array2d kernel; - kernel.resize(ivec2(NN, NN)); + array2d ker; + ker.resize(ivec2(NN, NN)); float t = 0.f; for (int j = 0; j < NN; j++) for (int i = 0; i < NN; i++) { vec2 v = vec2(i - N, j - N); - kernel[i][j] = exp(-sqlength(v / 1.6f) / 2.f) - + exp(-sqlength(v / 0.6f) / 2.f); - t += kernel[i][j]; + ker[i][j] = exp(-sqlength(v / 1.6f) / 2.f) + + exp(-sqlength(v / 0.6f) / 2.f); + t += ker[i][j]; } for (int j = 0; j < NN; j++) for (int i = 0; i < NN; i++) - kernel[i][j] /= t; + ker[i][j] /= t; /* A list of cells in our picture. If no change is done to a cell * for two iterations, we stop considering changes to it. */ - ivec2 const csize = (size + ivec2(CELL - 1)) / CELL; + ivec2 const csize = (isize + ivec2(CELL - 1)) / CELL; array2d changelist(csize); memset(changelist.data(), 0, changelist.bytes()); - Image dst = *this; - dst.SetFormat(PixelFormat::Y_F32); + image dst = *this; + dst.set_format(PixelFormat::Y_F32); - Image tmp1 = dst.Convolution(kernel); - array2d &tmp1data = tmp1.Lock2D(); + image tmp1 = dst.Convolution(ker); + array2d &tmp1data = tmp1.lock2d(); - dst = dst.DitherRandom(); - array2d &dstdata = dst.Lock2D(); + dst = dst.dither_random(); + array2d &dstdata = dst.lock2d(); - Image tmp2 = dst.Convolution(kernel); - array2d &tmp2data = tmp2.Lock2D(); + image tmp2 = dst.Convolution(ker); + array2d &tmp2data = tmp2.lock2d(); for (int run = 0, last_change = 0; ; ++run) { @@ -84,7 +86,7 @@ Image Image::DitherDbs() const ivec2 const pos(cx * CELL + pixel % CELL, cy * CELL + pixel / CELL); - if (!(pos >= ivec2(0)) || !(pos < size)) + if (!(pos >= ivec2(0)) || !(pos < isize)) continue; /* The best operation we can do */ @@ -103,7 +105,7 @@ Image Image::DitherDbs() const for (ivec2 const op : op_list) { - if (!(pos + op >= ivec2(0)) || !(pos + op < size)) + if (!(pos + op >= ivec2(0)) || !(pos + op < isize)) continue; bool flip = (op == ivec2(0)); @@ -115,9 +117,9 @@ Image Image::DitherDbs() const /* TODO: implement min/max for 3+ arguments */ int imin = max(max(-N, op.x - N), -pos.x); - int imax = min(min(N + 1, op.x + NN - N), size.x - pos.x); + int imax = min(min(N + 1, op.x + NN - N), isize.x - pos.x); int jmin = max(max(-N, op.y - N), -pos.y); - int jmax = min(min(N + 1, op.y + NN - N), size.y - pos.y); + int jmax = min(min(N + 1, op.y + NN - N), isize.y - pos.y); float error = 0.f; for (int j = jmin; j < jmax; j++) @@ -125,9 +127,9 @@ Image Image::DitherDbs() const { ivec2 pos2 = pos + ivec2(i, j); - float m = kernel[i + N][j + N]; + float m = ker[i + N][j + N]; if (!flip) - m -= kernel[i - op.x + N][j - op.y + N]; + m -= ker[i - op.x + N][j - op.y + N]; float p = tmp1data[pos2]; float q1 = tmp2data[pos2]; float q2 = q1 + m * (d2 - d); @@ -154,13 +156,13 @@ Image Image::DitherDbs() const for (int i = -N; i <= N; i++) { ivec2 off(i, j); - float delta = (d2 - d) * kernel[i + N][j + N]; + float delta = (d2 - d) * ker[i + N][j + N]; - if (pos + off >= ivec2(0) && pos + off < size) + if (pos + off >= ivec2(0) && pos + off < isize) tmp2data[pos + off] += delta; if (!flip && pos + off + best_op >= ivec2(0) - && pos + off + best_op < size) + && pos + off + best_op < isize) tmp2data[pos + off + best_op] -= delta; } @@ -173,9 +175,9 @@ Image Image::DitherDbs() const ++changelist[cx][cy]; } - tmp1.Unlock2D(tmp1data); - tmp2.Unlock2D(tmp2data); - dst.Unlock2D(dstdata); + tmp1.unlock2d(tmp1data); + tmp2.unlock2d(tmp2data); + dst.unlock2d(dstdata); return dst; } diff --git a/src/image/dither/ediff.cpp b/src/image/dither/ediff.cpp index 222b1b50..5ff69428 100644 --- a/src/image/dither/ediff.cpp +++ b/src/image/dither/ediff.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -23,49 +25,49 @@ namespace lol * Making the matrix generic is not terribly slower: the performance * hit is around 4% for Floyd-Steinberg and 13% for JaJuNi, with the * benefit of a lot less code. */ -Image Image::DitherEdiff(array2d const &kernel, ScanMode scan) const +image image::dither_ediff(array2d const &ker, ScanMode scan) const { - Image dst = *this; + image dst = *this; - ivec2 size = dst.GetSize(); - ivec2 ksize = kernel.size(); + ivec2 isize = dst.size(); + ivec2 ksize = ker.size(); int kx; for (kx = 0; kx < ksize.x; kx++) - if (kernel[kx][0] > 0.f) + if (ker[kx][0] > 0.f) break; - float *pixels = dst.Lock(); - for (int y = 0; y < size.y; y++) + float *pixels = dst.lock(); + for (int y = 0; y < isize.y; y++) { bool reverse = (y & 1) && (scan == ScanMode::Serpentine); - for (int x = 0; x < size.x; x++) + for (int x = 0; x < isize.x; x++) { - int x2 = reverse ? size.x - 1 - x : x; + int x2 = reverse ? isize.x - 1 - x : x; int s = reverse ? -1 : 1; - float p = pixels[y * size.x + x2]; + float p = pixels[y * isize.x + x2]; float q = p < 0.5f ? 0.f : 1.f; - pixels[y * size.x + x2] = q; + pixels[y * isize.x + x2] = q; float e = (p - q); - for (int j = 0; j < ksize.y && y < size.y - j; j++) + for (int j = 0; j < ksize.y && y < isize.y - j; j++) for (int i = 0; i < ksize.x; i++) { if (j == 0 && i <= kx) continue; - if (x + i - kx < 0 || x + i - kx >= size.x) + if (x + i - kx < 0 || x + i - kx >= isize.x) continue; - pixels[(y + j) * size.x + x2 + (i - kx) * s] - += e * kernel[i][j]; + pixels[(y + j) * isize.x + x2 + (i - kx) * s] + += e * ker[i][j]; } } } - dst.Unlock(pixels); + dst.unlock(pixels); return dst; } diff --git a/src/image/dither/ordered.cpp b/src/image/dither/ordered.cpp index d28cd369..3aeec6b1 100644 --- a/src/image/dither/ordered.cpp +++ b/src/image/dither/ordered.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -17,51 +19,51 @@ namespace lol { -static Image DitherHelper(Image const &image, array2d const &kernel, - float scale, float angle); +static image dither_helper(image const &img, array2d const &ker, + float scale, float angle); -Image Image::DitherOrdered(array2d const &kernel) const +image image::dither_ordered(array2d const &ker) const { - return DitherHelper(*this, kernel, 1.0f, 0.0f); + return dither_helper(*this, ker, 1.0f, 0.0f); } -Image Image::DitherHalftone(float radius, float angle) const +image image::dither_halftone(float radius, float angle) const { /* Increasing the precision is necessary or the rotation will look * like crap. So we create a kernel PRECISION times larger, and ask * the ditherer to scale it by 1/PRECISION. */ float const PRECISION = 4.f; int k = (radius * PRECISION * lol::sqrt(2.f) + 0.5f); - array2d kernel = Image::HalftoneKernel(ivec2(k, k)); + array2d ker = image::kernel::halftone(ivec2(k, k)); - return DitherHelper(*this, kernel, 1.f / PRECISION, angle + F_PI / 4.f); + return dither_helper(*this, ker, 1.f / PRECISION, angle + F_PI / 4.f); } -static Image DitherHelper(Image const &image, array2d const &kernel, - float scale, float angle) +static image dither_helper(image const &img, array2d const &ker, + float scale, float angle) { - ivec2 size = image.GetSize(); - ivec2 ksize = kernel.size(); + ivec2 isize = img.size(); + ivec2 ksize = ker.size(); float cost = lol::cos(angle); float sint = lol::sin(angle); - Image ret = image; - float *dstp = ret.Lock(); + image ret = img; + float *dstp = ret.lock(); - for (int y = 0; y < size.y; y++) + for (int y = 0; y < isize.y; y++) { - for (int x = 0; x < size.x; x++) + for (int x = 0; x < isize.x; x++) { - int kx = (int)((cost * x - sint * y + 2 * size.x * size.y) / scale) % ksize.x; - int ky = (int)((cost * y + sint * x + 2 * size.x * size.y) / scale) % ksize.y; + int kx = (int)((cost * x - sint * y + 2 * isize.x * isize.y) / scale) % ksize.x; + int ky = (int)((cost * y + sint * x + 2 * isize.x * isize.y) / scale) % ksize.y; - float p = dstp[y * size.x + x]; - dstp[y * size.x + x] = (p > kernel[kx][ky]) ? 1.f : 0.f; + float p = dstp[y * isize.x + x]; + dstp[y * isize.x + x] = (p > ker[kx][ky]) ? 1.f : 0.f; } } - ret.Unlock(dstp); + ret.unlock(dstp); return ret; } diff --git a/src/image/dither/ostromoukhov.cpp b/src/image/dither/ostromoukhov.cpp index 613be14e..b392a867 100644 --- a/src/image/dither/ostromoukhov.cpp +++ b/src/image/dither/ostromoukhov.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -69,13 +71,13 @@ static inline vec3 GetDiffusion(float v) return ret; } -Image Image::DitherOstromoukhov(ScanMode scan) const +image image::dither_ostromoukhov(ScanMode scan) const { - Image dst = *this; + image dst = *this; - float *pixels = dst.Lock(); - int w = dst.GetSize().x; - int h = dst.GetSize().y; + float *pixels = dst.lock(); + int w = dst.size().x; + int h = dst.size().y; for (int y = 0; y < h; y++) { @@ -103,7 +105,7 @@ Image Image::DitherOstromoukhov(ScanMode scan) const } } - dst.Unlock(pixels); + dst.unlock(pixels); return dst; } diff --git a/src/image/dither/random.cpp b/src/image/dither/random.cpp index 967d408a..283f6e3b 100644 --- a/src/image/dither/random.cpp +++ b/src/image/dither/random.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -17,19 +19,19 @@ namespace lol { -Image Image::DitherRandom() const +image image::dither_random() const { - Image dst = *this; + image dst = *this; - float *pixels = dst.Lock(); - int count = GetSize().x * GetSize().y; + float *pixels = dst.lock(); + int count = size().x * size().y; for (int n = 0; n < count; ++n) { pixels[n] = (pixels[n] > lol::rand(0.5f)) ? 1.f : 0.f; } - dst.Unlock(pixels); + dst.unlock(pixels); return dst; } diff --git a/src/image/filter/colors.cpp b/src/image/filter/colors.cpp index 78088248..6ab1c4e6 100644 --- a/src/image/filter/colors.cpp +++ b/src/image/filter/colors.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -17,34 +19,34 @@ namespace lol { -Image Image::Brightness(float val) const +image image::Brightness(float val) const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { - float *pixels = ret.Lock(); + float *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = lol::clamp(pixels[n] + val, 0.f, 1.f); - ret.Unlock(pixels); + ret.unlock(pixels); } else { - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = vec4(lol::clamp(pixels[n].rgb + vec3(val), 0.f, 1.f), pixels[n].a); - ret.Unlock(pixels); + ret.unlock(pixels); } return ret; } -Image Image::Contrast(float val) const +image image::Contrast(float val) const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; if (val >= 0.f) { @@ -58,22 +60,22 @@ Image Image::Contrast(float val) const val = lol::clamp(1.f + val, 0.f, 1.f); } - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { float add = -0.5f * val + 0.5f; - float *pixels = ret.Lock(); + float *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = lol::clamp(pixels[n] * val + add, 0.f, 1.f); - ret.Unlock(pixels); + ret.unlock(pixels); } else { vec3 add = vec3(-0.5f * val + 0.5f); - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = vec4(lol::clamp(pixels[n].rgb * val + add, 0.f, 1.f), pixels[n].a); - ret.Unlock(pixels); + ret.unlock(pixels); } return ret; @@ -83,16 +85,16 @@ Image Image::Contrast(float val) const * TODO: the current approach is naive; we should use the histogram in order * to decide how to change the contrast. */ -Image Image::AutoContrast() const +image image::AutoContrast() const { - Image ret = *this; + image ret = *this; float min_val = 1.f, max_val = 0.f; - int count = GetSize().x * GetSize().y; + int count = size().x * size().y; - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { - float *pixels = ret.Lock(); + float *pixels = ret.lock(); for (int n = 0; n < count; ++n) { min_val = lol::min(min_val, pixels[n]); @@ -103,11 +105,11 @@ Image Image::AutoContrast() const for (int n = 0; n < count; ++n) pixels[n] = (pixels[n] - min_val) * t; - ret.Unlock(pixels); + ret.unlock(pixels); } else { - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) { min_val = lol::min(min_val, pixels[n].r); @@ -125,60 +127,60 @@ Image Image::AutoContrast() const (pixels[n].b - min_val) * t, pixels[n].a);; - ret.Unlock(pixels); + ret.unlock(pixels); } return ret; } -Image Image::Invert() const +image image::Invert() const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { - float *pixels = ret.Lock(); + float *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = 1.f - pixels[n]; - ret.Unlock(pixels); + ret.unlock(pixels); } else { - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = vec4(vec3(1.f) -pixels[n].rgb, pixels[n].a); - ret.Unlock(pixels); + ret.unlock(pixels); } return ret; } -Image Image::Threshold(float val) const +image image::Threshold(float val) const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; - float *pixels = ret.Lock(); + float *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = pixels[n] > val ? 1.f : 0.f; - ret.Unlock(pixels); + ret.unlock(pixels); return ret; } -Image Image::Threshold(vec3 val) const +image image::Threshold(vec3 val) const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = vec4(pixels[n].r > val.r ? 1.f : 0.f, pixels[n].g > val.g ? 1.f : 0.f, pixels[n].b > val.b ? 1.f : 0.f, pixels[n].a); - ret.Unlock(pixels); + ret.unlock(pixels); return ret; } diff --git a/src/image/filter/convolution.cpp b/src/image/filter/convolution.cpp index fd300569..08357e2c 100644 --- a/src/image/filter/convolution.cpp +++ b/src/image/filter/convolution.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -17,21 +19,21 @@ namespace lol { -static Image SepConv(Image &src, array const &hvec, +static image SepConv(image &src, array const &hvec, array const &vvec); -static Image NonSepConv(Image &src, array2d const &kernel); +static image NonSepConv(image &src, array2d const &in_kernel); -Image Image::Convolution(array2d const &kernel) +image image::Convolution(array2d const &in_kernel) { /* Find the cell with the largest value */ - ivec2 ksize = kernel.size(); + ivec2 ksize = in_kernel.size(); int bestx = -1, besty = -1; float tmp = 0.f; for (int dy = 0; dy < ksize.y; ++dy) for (int dx = 0; dx < ksize.x; ++dx) - if (lol::sq(kernel[dx][dy]) > tmp) + if (lol::sq(in_kernel[dx][dy]) > tmp) { - tmp = sq(kernel[dx][dy]); + tmp = sq(in_kernel[dx][dy]); bestx = dx; besty = dy; } @@ -52,8 +54,8 @@ Image Image::Convolution(array2d const &kernel) if (dx == bestx) continue; - float p = kernel[dx][dy] * kernel[bestx][besty]; - float q = kernel[dx][besty] * kernel[bestx][dy]; + float p = in_kernel[dx][dy] * in_kernel[bestx][besty]; + float q = in_kernel[dx][besty] * in_kernel[bestx][dy]; if (lol::abs(p - q) > 1.0e-8f) separable = false; @@ -65,29 +67,29 @@ Image Image::Convolution(array2d const &kernel) /* Matrix rank is 1! Separate the filter. */ array hvec, vvec; - float norm = 1.0f / lol::sqrt(lol::abs(kernel[bestx][besty])); + float norm = 1.0f / lol::sqrt(lol::abs(in_kernel[bestx][besty])); for (int dx = 0; dx < ksize.x; dx++) - hvec << norm * kernel[dx][besty]; + hvec << norm * in_kernel[dx][besty]; for (int dy = 0; dy < ksize.y; dy++) - vvec << norm * kernel[bestx][dy]; + vvec << norm * in_kernel[bestx][dy]; return SepConv(*this, hvec, vvec); } else { - return NonSepConv(*this, kernel); + return NonSepConv(*this, in_kernel); } } -Image Image::Sharpen(array2d const &kernel) +image image::Sharpen(array2d const &in_kernel) { - ivec2 ksize = kernel.size(); + ivec2 ksize = in_kernel.size(); array2d newkernel(ksize); for (int dy = 0; dy < ksize.y; ++dy) for (int dx = 0; dx < ksize.x; ++dx) { - newkernel[dx][dy] = - kernel[dx][dy]; + newkernel[dx][dy] = - in_kernel[dx][dy]; if (dx == ksize.x / 2 && dy == ksize.y / 2) newkernel[dx][dy] += 2.f; } @@ -96,16 +98,16 @@ Image Image::Sharpen(array2d const &kernel) } template -static Image NonSepConv(Image &src, array2d const &kernel) +static image NonSepConv(image &src, array2d const &in_kernel) { typedef typename PixelType::type pixel_t; - ivec2 const size = src.GetSize(); - ivec2 const ksize = kernel.size(); - Image dst(size); + ivec2 const size = src.size(); + ivec2 const ksize = in_kernel.size(); + image dst(size); - array2d const &srcp = src.Lock2D(); - array2d &dstp = dst.Lock2D(); + array2d const &srcp = src.lock2d(); + array2d &dstp = dst.lock2d(); for (int y = 0; y < size.y; y++) { @@ -123,7 +125,7 @@ static Image NonSepConv(Image &src, array2d const &kernel) for (int dx = 0; dx < ksize.x; dx++) { - float f = kernel[dx][dy]; + float f = in_kernel[dx][dy]; int x2 = x + dx - ksize.x / 2; if (x2 < 0) @@ -139,33 +141,33 @@ static Image NonSepConv(Image &src, array2d const &kernel) } } - src.Unlock2D(srcp); - dst.Unlock2D(dstp); + src.unlock2d(srcp); + dst.unlock2d(dstp); return dst; } -static Image NonSepConv(Image &src, array2d const &kernel) +static image NonSepConv(image &src, array2d const &in_kernel) { bool const wrap_x = src.GetWrapX() == WrapMode::Repeat; bool const wrap_y = src.GetWrapY() == WrapMode::Repeat; - if (src.GetFormat() == PixelFormat::Y_8 - || src.GetFormat() == PixelFormat::Y_F32) + if (src.format() == PixelFormat::Y_8 + || src.format() == PixelFormat::Y_F32) { if (wrap_x) { if (wrap_y) - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); else - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); } else { if (wrap_y) - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); else - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); } } else @@ -173,32 +175,32 @@ static Image NonSepConv(Image &src, array2d const &kernel) if (wrap_x) { if (wrap_y) - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); else - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); } else { if (wrap_y) - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); else - return NonSepConv(src, kernel); + return NonSepConv(src, in_kernel); } } } template -static Image SepConv(Image &src, array const &hvec, +static image SepConv(image &src, array const &hvec, array const &vvec) { typedef typename PixelType::type pixel_t; - ivec2 const size = src.GetSize(); + ivec2 const size = src.size(); ivec2 const ksize(hvec.count(), vvec.count()); - Image dst(size); + image dst(size); - array2d const &srcp = src.Lock2D(); - array2d &dstp = dst.Lock2D(); + array2d const &srcp = src.lock2d(); + array2d &dstp = dst.lock2d(); array2d tmp(size); @@ -244,20 +246,20 @@ static Image SepConv(Image &src, array const &hvec, } } - src.Unlock2D(srcp); - dst.Unlock2D(dstp); + src.unlock2d(srcp); + dst.unlock2d(dstp); return dst; } -static Image SepConv(Image &src, array const &hvec, +static image SepConv(image &src, array const &hvec, array const &vvec) { bool const wrap_x = src.GetWrapX() == WrapMode::Repeat; bool const wrap_y = src.GetWrapY() == WrapMode::Repeat; - if (src.GetFormat() == PixelFormat::Y_8 - || src.GetFormat() == PixelFormat::Y_F32) + if (src.format() == PixelFormat::Y_8 + || src.format() == PixelFormat::Y_F32) { if (wrap_x) { diff --git a/src/image/filter/dilate.cpp b/src/image/filter/dilate.cpp index 3c4cd040..a92f96b7 100644 --- a/src/image/filter/dilate.cpp +++ b/src/image/filter/dilate.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -22,115 +24,115 @@ namespace lol { -Image Image::Dilate() +image image::Dilate() { - ivec2 const size = GetSize(); - Image ret(size); + ivec2 isize = size(); + image ret(isize); - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { - float const *srcp = Lock(); - float *dstp = ret.Lock(); + float const *srcp = lock(); + float *dstp = ret.lock(); - for (int y = 0; y < size.y; ++y) - for (int x = 0; x < size.x; ++x) + for (int y = 0; y < isize.y; ++y) + for (int x = 0; x < isize.x; ++x) { int y2 = lol::max(y - 1, 0); int x2 = lol::max(x - 1, 0); - int y3 = lol::min(y + 1, size.y - 1); - int x3 = lol::min(x + 1, size.x - 1); - - float t = srcp[y * size.x + x]; - t = lol::max(t, srcp[y * size.x + x2]); - t = lol::max(t, srcp[y * size.x + x3]); - t = lol::max(t, srcp[y2 * size.x + x]); - t = lol::max(t, srcp[y3 * size.x + x]); - dstp[y * size.x + x] = t; + int y3 = lol::min(y + 1, isize.y - 1); + int x3 = lol::min(x + 1, isize.x - 1); + + float t = srcp[y * isize.x + x]; + t = lol::max(t, srcp[y * isize.x + x2]); + t = lol::max(t, srcp[y * isize.x + x3]); + t = lol::max(t, srcp[y2 * isize.x + x]); + t = lol::max(t, srcp[y3 * isize.x + x]); + dstp[y * isize.x + x] = t; } - Unlock(srcp); - ret.Unlock(dstp); + unlock(srcp); + ret.unlock(dstp); } else { - vec4 const *srcp = Lock(); - vec4 *dstp = ret.Lock(); + vec4 const *srcp = lock(); + vec4 *dstp = ret.lock(); - for (int y = 0; y < size.y; ++y) - for (int x = 0; x < size.x; ++x) + for (int y = 0; y < isize.y; ++y) + for (int x = 0; x < isize.x; ++x) { int y2 = lol::max(y - 1, 0); int x2 = lol::max(x - 1, 0); - int y3 = lol::min(y + 1, size.y - 1); - int x3 = lol::min(x + 1, size.x - 1); - - vec3 t = srcp[y * size.x + x].rgb; - t = lol::max(t, srcp[y * size.x + x2].rgb); - t = lol::max(t, srcp[y * size.x + x3].rgb); - t = lol::max(t, srcp[y2 * size.x + x].rgb); - t = lol::max(t, srcp[y3 * size.x + x].rgb); - dstp[y * size.x + x] = vec4(t, srcp[y * size.x + x].a); + int y3 = lol::min(y + 1, isize.y - 1); + int x3 = lol::min(x + 1, isize.x - 1); + + vec3 t = srcp[y * isize.x + x].rgb; + t = lol::max(t, srcp[y * isize.x + x2].rgb); + t = lol::max(t, srcp[y * isize.x + x3].rgb); + t = lol::max(t, srcp[y2 * isize.x + x].rgb); + t = lol::max(t, srcp[y3 * isize.x + x].rgb); + dstp[y * isize.x + x] = vec4(t, srcp[y * isize.x + x].a); } - Unlock(srcp); - ret.Unlock(dstp); + unlock(srcp); + ret.unlock(dstp); } return ret; } -Image Image::Erode() +image image::Erode() { - ivec2 const size = GetSize(); - Image ret(size); + ivec2 isize = size(); + image ret(isize); - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { - float const *srcp = Lock(); - float *dstp = ret.Lock(); + float const *srcp = lock(); + float *dstp = ret.lock(); - for (int y = 0; y < size.y; ++y) - for (int x = 0; x < size.x; ++x) + for (int y = 0; y < isize.y; ++y) + for (int x = 0; x < isize.x; ++x) { int y2 = lol::max(y - 1, 0); int x2 = lol::max(x - 1, 0); - int y3 = lol::min(y + 1, size.y - 1); - int x3 = lol::min(x + 1, size.x - 1); - - float t = srcp[y * size.x + x]; - t = lol::max(t, srcp[y * size.x + x2]); - t = lol::max(t, srcp[y * size.x + x3]); - t = lol::max(t, srcp[y2 * size.x + x]); - t = lol::max(t, srcp[y3 * size.x + x]); - dstp[y * size.x + x] = t; + int y3 = lol::min(y + 1, isize.y - 1); + int x3 = lol::min(x + 1, isize.x - 1); + + float t = srcp[y * isize.x + x]; + t = lol::max(t, srcp[y * isize.x + x2]); + t = lol::max(t, srcp[y * isize.x + x3]); + t = lol::max(t, srcp[y2 * isize.x + x]); + t = lol::max(t, srcp[y3 * isize.x + x]); + dstp[y * isize.x + x] = t; } - Unlock(srcp); - ret.Unlock(dstp); + unlock(srcp); + ret.unlock(dstp); } else { - vec4 const *srcp = Lock(); - vec4 *dstp = ret.Lock(); + vec4 const *srcp = lock(); + vec4 *dstp = ret.lock(); - for (int y = 0; y < size.y; ++y) - for (int x = 0; x < size.x; ++x) + for (int y = 0; y < isize.y; ++y) + for (int x = 0; x < isize.x; ++x) { int y2 = lol::max(y - 1, 0); int x2 = lol::max(x - 1, 0); - int y3 = lol::min(y + 1, size.y - 1); - int x3 = lol::min(x + 1, size.x - 1); - - vec3 t = srcp[y * size.x + x].rgb; - t = lol::min(t, srcp[y * size.x + x2].rgb); - t = lol::min(t, srcp[y * size.x + x3].rgb); - t = lol::min(t, srcp[y2 * size.x + x].rgb); - t = lol::min(t, srcp[y3 * size.x + x].rgb); - dstp[y * size.x + x] = vec4(t, srcp[y * size.x + x].a); + int y3 = lol::min(y + 1, isize.y - 1); + int x3 = lol::min(x + 1, isize.x - 1); + + vec3 t = srcp[y * isize.x + x].rgb; + t = lol::min(t, srcp[y * isize.x + x2].rgb); + t = lol::min(t, srcp[y * isize.x + x3].rgb); + t = lol::min(t, srcp[y2 * isize.x + x].rgb); + t = lol::min(t, srcp[y3 * isize.x + x].rgb); + dstp[y * isize.x + x] = vec4(t, srcp[y * isize.x + x].a); } - Unlock(srcp); - ret.Unlock(dstp); + unlock(srcp); + ret.unlock(dstp); } return ret; diff --git a/src/image/filter/median.cpp b/src/image/filter/median.cpp index 10cc5a9f..5f27109f 100644 --- a/src/image/filter/median.cpp +++ b/src/image/filter/median.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -33,38 +35,38 @@ static int cmpfloat(void const *i1, void const *i2) return (a > b) - (a < b); } -Image Image::Median(ivec2 ksize) const +image image::Median(ivec2 ksize) const { - ivec2 const size = GetSize(); - Image tmp = *this; - Image ret(size); + ivec2 const isize = size(); + image tmp = *this; + image ret(isize); - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { ivec2 const lsize = 2 * ksize + ivec2(1); array2d list(lsize); - float *srcp = tmp.Lock(); - float *dstp = ret.Lock(); + float *srcp = tmp.lock(); + float *dstp = ret.lock(); - for (int y = 0; y < size.y; y++) + for (int y = 0; y < isize.y; y++) { - for (int x = 0; x < size.x; x++) + for (int x = 0; x < isize.x; x++) { /* Make a list of neighbours */ for (int j = -ksize.y; j <= ksize.y; j++) { int y2 = y + j; - if (y2 < 0) y2 = size.y - 1 - ((-y2 - 1) % size.y); - else if (y2 > 0) y2 = y2 % size.y; + if (y2 < 0) y2 = isize.y - 1 - ((-y2 - 1) % isize.y); + else if (y2 > 0) y2 = y2 % isize.y; for (int i = -ksize.x; i <= ksize.x; i++) { int x2 = x + i; - if (x2 < 0) x2 = size.x - 1 - ((-x2 - 1) % size.x); - else if (x2 > 0) x2 = x2 % size.x; + if (x2 < 0) x2 = isize.x - 1 - ((-x2 - 1) % isize.x); + else if (x2 > 0) x2 = x2 % isize.x; - list[i + ksize.x][j + ksize.y] = srcp[y2 * size.x + x2]; + list[i + ksize.x][j + ksize.y] = srcp[y2 * isize.x + x2]; } } @@ -72,39 +74,39 @@ Image Image::Median(ivec2 ksize) const qsort(&list[0][0], lsize.x * lsize.y, sizeof(float), cmpfloat); /* Store the median value */ - dstp[y * size.x + x] = *(&list[0][0] + lsize.x * lsize.y / 2); + dstp[y * isize.x + x] = *(&list[0][0] + lsize.x * lsize.y / 2); } } - tmp.Unlock(srcp); - ret.Unlock(dstp); + tmp.unlock(srcp); + ret.unlock(dstp); } else { ivec2 const lsize = 2 * ksize + ivec2(1); array2d list(lsize); - vec4 *srcp = tmp.Lock(); - vec4 *dstp = ret.Lock(); + vec4 *srcp = tmp.lock(); + vec4 *dstp = ret.lock(); - for (int y = 0; y < size.y; y++) + for (int y = 0; y < isize.y; y++) { - for (int x = 0; x < size.x; x++) + for (int x = 0; x < isize.x; x++) { /* Make a list of neighbours */ for (int j = -ksize.y; j <= ksize.y; j++) { int y2 = y + j; - if (y2 < 0) y2 = size.y - 1 - ((-y2 - 1) % size.y); - else if (y2 > 0) y2 = y2 % size.y; + if (y2 < 0) y2 = isize.y - 1 - ((-y2 - 1) % isize.y); + else if (y2 > 0) y2 = y2 % isize.y; for (int i = -ksize.x; i <= ksize.x; i++) { int x2 = x + i; - if (x2 < 0) x2 = size.x - 1 - ((-x2 - 1) % size.x); - else if (x2 > 0) x2 = x2 % size.x; + if (x2 < 0) x2 = isize.x - 1 - ((-x2 - 1) % isize.x); + else if (x2 > 0) x2 = x2 % isize.x; - list[i + ksize.x][j + ksize.y] = srcp[y2 * size.x + x2].rgb; + list[i + ksize.x][j + ksize.y] = srcp[y2 * isize.x + x2].rgb; } } @@ -139,26 +141,26 @@ Image Image::Median(ivec2 ksize) const } /* Store the median value */ - dstp[y * size.x + x] = vec4(median, srcp[y * size.x + x].a); + dstp[y * isize.x + x] = vec4(median, srcp[y * isize.x + x].a); } } - tmp.Unlock(srcp); - ret.Unlock(dstp); + tmp.unlock(srcp); + ret.unlock(dstp); } return ret; } -Image Image::Median(array2d const &kernel) const +image image::Median(array2d const &kernel) const { - ivec2 const size = GetSize(); - Image tmp = *this; - Image ret(size); + ivec2 const isize = size(); + image tmp = *this; + image ret(isize); /* FIXME: TODO */ #if 0 - if (GetFormat() == PixelFormat::Y_8 || GetFormat() == PixelFormat::Y_F32) + if (format() == PixelFormat::Y_8 || format() == PixelFormat::Y_F32) { } else @@ -167,27 +169,27 @@ Image Image::Median(array2d const &kernel) const ivec2 const ksize = kernel.size(); array2d list(ksize); - vec4 *srcp = tmp.Lock(); - vec4 *dstp = ret.Lock(); + vec4 *srcp = tmp.lock(); + vec4 *dstp = ret.lock(); - for (int y = 0; y < size.y; y++) + for (int y = 0; y < isize.y; y++) { - for (int x = 0; x < size.x; x++) + for (int x = 0; x < isize.x; x++) { /* Make a list of neighbours */ for (int j = 0; j < ksize.y; j++) { int y2 = y + j - ksize.y / 2; - if (y2 < 0) y2 = size.y - 1 - ((-y2 - 1) % size.y); - else if (y2 > 0) y2 = y2 % size.y; + if (y2 < 0) y2 = isize.y - 1 - ((-y2 - 1) % isize.y); + else if (y2 > 0) y2 = y2 % isize.y; for (int i = 0; i < ksize.x; i++) { int x2 = x + i - ksize.x / 2; - if (x2 < 0) x2 = size.x - 1 - ((-x2 - 1) % size.x); - else if (x2 > 0) x2 = x2 % size.x; + if (x2 < 0) x2 = isize.x - 1 - ((-x2 - 1) % isize.x); + else if (x2 > 0) x2 = x2 % isize.x; - list[i][j] = srcp[y2 * size.x + x2].rgb; + list[i][j] = srcp[y2 * isize.x + x2].rgb; } } @@ -222,12 +224,12 @@ Image Image::Median(array2d const &kernel) const } /* Store the median value */ - dstp[y * size.x + x] = vec4(median, srcp[y * size.x + x].a); + dstp[y * isize.x + x] = vec4(median, srcp[y * isize.x + x].a); } } - tmp.Unlock(srcp); - ret.Unlock(dstp); + tmp.unlock(srcp); + ret.unlock(dstp); } return ret; diff --git a/src/image/filter/yuv.cpp b/src/image/filter/yuv.cpp index 063dc8d3..61a8b199 100644 --- a/src/image/filter/yuv.cpp +++ b/src/image/filter/yuv.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -17,28 +19,28 @@ namespace lol { -Image Image::YUVToRGB() const +image image::YUVToRGB() const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = Color::YUVToRGB(pixels[n]); - ret.Unlock(pixels); + ret.unlock(pixels); return ret; } -Image Image::RGBToYUV() const +image image::RGBToYUV() const { - Image ret = *this; - int count = GetSize().x * GetSize().y; + image ret = *this; + int count = size().x * size().y; - vec4 *pixels = ret.Lock(); + vec4 *pixels = ret.lock(); for (int n = 0; n < count; ++n) pixels[n] = Color::RGBToYUV(pixels[n]); - ret.Unlock(pixels); + ret.unlock(pixels); return ret; } diff --git a/src/image/image.cpp b/src/image/image.cpp index 20a99ade..7b283df5 100644 --- a/src/image/image.cpp +++ b/src/image/image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2016 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -20,7 +20,7 @@ namespace lol { /* - * Public Image class + * Public image class */ image::image() @@ -31,13 +31,13 @@ image::image() image::image(char const *path) : m_data(new image_data()) { - Load(path); + load(path); } image::image(ivec2 size) : m_data(new image_data()) { - SetSize(size); + resize(size); } image::image (image const &other) @@ -65,21 +65,21 @@ image::~image() void image::Copy(uint8_t* src_pixels, ivec2 const& size, PixelFormat fmt) { ASSERT(fmt != PixelFormat::Unknown); - SetSize(size); - SetFormat(fmt); + resize(size); + set_format(fmt); memcpy(m_data->m_pixels[(int)fmt]->data(), src_pixels, size.x * size.y * BytesPerPixel(fmt)); } void image::Copy(image const &src) { - ivec2 size = src.GetSize(); - PixelFormat fmt = src.GetFormat(); + ivec2 size = src.size(); + PixelFormat fmt = src.format(); - SetSize(size); + resize(size); if (fmt != PixelFormat::Unknown) { - SetFormat(fmt); + set_format(fmt); memcpy(m_data->m_pixels[(int)fmt]->data(), src.m_data->m_pixels[(int)fmt]->data(), size.x * size.y * BytesPerPixel(fmt)); @@ -88,10 +88,10 @@ void image::Copy(image const &src) void image::DummyFill() { - Load("DUMMY"); + load("DUMMY"); } -bool image::Load(char const *path) +bool image::load(char const *path) { auto resource = ResourceLoader::Load(path); if (resource == nullptr) @@ -109,7 +109,7 @@ bool image::Load(char const *path) return true; } -bool image::Save(char const *path) +bool image::save(char const *path) { auto data = new ResourceImageData(new image(*this)); auto result = ResourceLoader::Save(path, data); @@ -117,12 +117,12 @@ bool image::Save(char const *path) return result; } -ivec2 image::GetSize() const +ivec2 image::size() const { return m_data->m_size; } -void image::SetSize(ivec2 size) +void image::resize(ivec2 size) { ASSERT(size.x > 0); ASSERT(size.y > 0); @@ -158,24 +158,24 @@ void image::SetWrap(WrapMode wrap_x, WrapMode wrap_y) m_data->m_wrap_y = wrap_y; } -/* The Lock() method */ -template typename PixelType::type *image::Lock() +/* The lock() method */ +template typename PixelType::type *image::lock() { - SetFormat(T); + set_format(T); return (typename PixelType::type *)m_data->m_pixels[(int)T]->data(); } -/* The Lock2D() method */ -void *image::Lock2DHelper(PixelFormat T) +/* The lock2d() method */ +void *image::lock2d_helper(PixelFormat T) { - SetFormat(T); + set_format(T); return m_data->m_pixels[(int)T]->data2d(); } template -void image::Unlock2D(array2d const &array) +void image::unlock2d(array2d const &array) { ASSERT(m_data->m_pixels.has_key((int)m_data->m_format)); ASSERT(array.data() == m_data->m_pixels[(int)m_data->m_format]->data()); @@ -183,9 +183,9 @@ void image::Unlock2D(array2d const &array) /* Explicit specialisations for the above templates */ #define _T(T) \ - template PixelType::type *image::Lock(); \ - template array2d::type> &image::Lock2D(); \ - template void image::Unlock2D(array2d::type> const &array); + template PixelType::type *image::lock(); \ + template array2d::type> &image::lock2d(); \ + template void image::unlock2d(array2d::type> const &array); _T(PixelFormat::Y_8) _T(PixelFormat::RGB_8) _T(PixelFormat::RGBA_8) @@ -195,14 +195,14 @@ _T(PixelFormat::RGBA_F32) #undef _T /* Special case for the "any" format: return the last active buffer */ -void *image::Lock() +void *image::lock() { ASSERT(m_data->m_format != PixelFormat::Unknown); return m_data->m_pixels[(int)m_data->m_format]->data(); } -void image::Unlock(void const *pixels) +void image::unlock(void const *pixels) { ASSERT(m_data->m_pixels.has_key((int)m_data->m_format)); ASSERT(pixels == m_data->m_pixels[(int)m_data->m_format]->data()); diff --git a/src/image/kernel.cpp b/src/image/kernel.cpp index 5b8ac465..483ab6cb 100644 --- a/src/image/kernel.cpp +++ b/src/image/kernel.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -17,7 +19,7 @@ namespace lol { -array2d image::BayerKernel(ivec2 size) +array2d image::kernel::bayer(ivec2 size) { array2d ret(size); @@ -46,7 +48,7 @@ array2d image::BayerKernel(ivec2 size) return ret; } -array2d image::HalftoneKernel(ivec2 size) +array2d image::kernel::halftone(ivec2 size) { array2d ret(size); @@ -72,10 +74,10 @@ array2d image::HalftoneKernel(ivec2 size) ret[x][y] = flip ? 10.f - r : r; } - return NormalizeKernel(ret); + return normalize(ret); } -array2d image::BlueNoiseKernel(ivec2 size, ivec2 gsize) +array2d image::kernel::blue_noise(ivec2 size, ivec2 gsize) { float const epsilon = 1.f / (size.x * size.y + 1); gsize = lol::min(size, gsize); @@ -180,7 +182,7 @@ static int cmpdot(const void *p1, const void *p2) return ((Dot const *)p1)->val > ((Dot const *)p2)->val; } -array2d image::NormalizeKernel(array2d const &kernel) +array2d image::kernel::normalize(array2d const &kernel) { ivec2 size = kernel.size(); @@ -209,7 +211,7 @@ array2d image::NormalizeKernel(array2d const &kernel) return dst; } -array2d image::EdiffKernel(EdiffAlgorithm algorithm) +array2d image::kernel::ediff(EdiffAlgorithm algorithm) { switch (algorithm) { @@ -270,7 +272,7 @@ array2d image::EdiffKernel(EdiffAlgorithm algorithm) * there is little chance that any value below 0.2 will be useful. */ #define BLUR_EPSILON 0.2f -array2d image::GaussianKernel(vec2 radius, float angle, vec2 delta) +array2d image::kernel::gaussian(vec2 radius, float angle, vec2 delta) { array2d kernel; diff --git a/src/image/noise.cpp b/src/image/noise.cpp index 2da3b314..408ea524 100644 --- a/src/image/noise.cpp +++ b/src/image/noise.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -19,8 +21,8 @@ namespace lol bool image::RenderRandom(ivec2 size) { - SetSize(size); - vec4 *pixels = Lock(); + resize(size); + vec4 *pixels = lock(); for (int n = 0; n < size.x * size.y; ++n) pixels[n] = vec4(lol::rand(1.f), @@ -28,7 +30,7 @@ bool image::RenderRandom(ivec2 size) lol::rand(1.f), 1.f); - Unlock(pixels); + unlock(pixels); return true; } diff --git a/src/image/pixel.cpp b/src/image/pixel.cpp index f6d3b2ef..db1feae7 100644 --- a/src/image/pixel.cpp +++ b/src/image/pixel.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2004—2015 Sam Hocevar +// Copyright © 2004—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -60,7 +60,7 @@ static u8vec4 f32tou8(vec4 pixel) * Pixel-level image manipulation */ -PixelFormat image::GetFormat() const +PixelFormat image::format() const { return m_data->m_format; } @@ -81,41 +81,41 @@ PixelFormat image::GetFormat() const * x lossless conversion (u8 to float) * # lossy conversion (dithering and/or convert color→gray) */ -void image::SetFormat(PixelFormat fmt) +void image::set_format(PixelFormat fmt) { PixelFormat old_fmt = m_data->m_format; /* Preliminary intermediate conversions */ if (old_fmt == PixelFormat::RGBA_8 && fmt == PixelFormat::Y_F32) - SetFormat(PixelFormat::RGBA_F32); + set_format(PixelFormat::RGBA_F32); else if (old_fmt == PixelFormat::RGB_8 && fmt == PixelFormat::Y_F32) - SetFormat(PixelFormat::RGBA_F32); + set_format(PixelFormat::RGBA_F32); else if (old_fmt == PixelFormat::Y_F32 && fmt == PixelFormat::RGBA_8) - SetFormat(PixelFormat::RGBA_F32); + set_format(PixelFormat::RGBA_F32); else if (old_fmt == PixelFormat::Y_F32 && fmt == PixelFormat::RGB_8) - SetFormat(PixelFormat::RGBA_F32); + set_format(PixelFormat::RGBA_F32); else if (old_fmt == PixelFormat::RGB_F32 && fmt == PixelFormat::RGBA_8) - SetFormat(PixelFormat::RGBA_F32); + set_format(PixelFormat::RGBA_F32); else if (old_fmt == PixelFormat::RGBA_F32 && fmt == PixelFormat::Y_F32) - SetFormat(PixelFormat::RGB_F32); + set_format(PixelFormat::RGB_F32); else if (old_fmt == PixelFormat::RGBA_F32 && fmt == PixelFormat::RGB_8) - SetFormat(PixelFormat::RGB_F32); + set_format(PixelFormat::RGB_F32); else if (old_fmt == PixelFormat::RGB_8 && fmt == PixelFormat::Y_8) - SetFormat(PixelFormat::RGB_F32); + set_format(PixelFormat::RGB_F32); else if (old_fmt == PixelFormat::RGBA_8 && fmt == PixelFormat::Y_8) - SetFormat(PixelFormat::RGB_F32); + set_format(PixelFormat::RGB_F32); else if (old_fmt == PixelFormat::RGB_F32 && fmt == PixelFormat::Y_8) - SetFormat(PixelFormat::Y_F32); + set_format(PixelFormat::Y_F32); else if (old_fmt == PixelFormat::RGBA_F32 && fmt == PixelFormat::Y_8) - SetFormat(PixelFormat::Y_F32); + set_format(PixelFormat::Y_F32); old_fmt = m_data->m_format; /* Set the new active pixel format */ m_data->m_format = fmt; - ivec2 size = GetSize(); - int count = size.x * size.y; + ivec2 isize = size(); + int count = isize.x * isize.y; /* If we never used this format, allocate a new buffer: we will * obviously need it. */ @@ -131,17 +131,17 @@ void image::SetFormat(PixelFormat fmt) case PixelFormat::Unknown: break; case PixelFormat::Y_8: - data = new PixelData(size); break; + data = new PixelData(isize); break; case PixelFormat::RGB_8: - data = new PixelData(size); break; + data = new PixelData(isize); break; case PixelFormat::RGBA_8: - data = new PixelData(size); break; + data = new PixelData(isize); break; case PixelFormat::Y_F32: - data = new PixelData(size); break; + data = new PixelData(isize); break; case PixelFormat::RGB_F32: - data = new PixelData(size); break; + data = new PixelData(isize); break; case PixelFormat::RGBA_F32: - data = new PixelData(size); break; + data = new PixelData(isize); break; } #if __GNUC__ #pragma GCC diagnostic pop @@ -314,31 +314,31 @@ void image::SetFormat(PixelFormat fmt) #if 0 init_tables(); - for (int y = 0; y < size.y; y++) - for (int x = 0; x < size.x; x++) + for (int y = 0; y < isize.y; y++) + for (int x = 0; x < isize.x; x++) for (i = 0; i < 4; i++) { double p, e; uint8_t d; - p = src[4 * (y * size.x + x) + i]; + p = src[4 * (y * isize.x + x) + i]; if (p < 0.) d = 0.; else if (p > 1.) d = 255; else d = (int)(255.999 * pow(p, 1. / global_gamma)); - dest[4 * (y * size.x + x) + i] = d; + dest[4 * (y * isize.x + x) + i] = d; e = (p - u8tof32(d)) / 16; - if (x < size.x - 1) - src[4 * (y * size.x + x + 1) + i] += e * 7; - if (y < size.y - 1) + if (x < isize.x - 1) + src[4 * (y * isize.x + x + 1) + i] += e * 7; + if (y < isize.y - 1) { if (x > 0) - src[4 * ((y + 1) * size.x + x - 1) + i] += e * 3; - src[4 * ((y + 1) * size.x + x) + i] += e * 5; - if (x < size.x - 1) - src[4 * ((y + 1) * size.x + x + 1) + i] += e; + src[4 * ((y + 1) * isize.x + x - 1) + i] += e * 3; + src[4 * ((y + 1) * isize.x + x) + i] += e * 5; + if (x < isize.x - 1) + src[4 * ((y + 1) * isize.x + x + 1) + i] += e; } } #endif diff --git a/src/image/resample.cpp b/src/image/resample.cpp index 55cad2d6..1d4f6437 100644 --- a/src/image/resample.cpp +++ b/src/image/resample.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2004-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2004—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -35,10 +37,10 @@ image image::Resize(ivec2 size, ResampleAlgorithm algorithm) static image ResizeBicubic(image &src, ivec2 size) { image dst(size); - ivec2 const oldsize = src.GetSize(); + ivec2 const oldsize = src.size(); - vec4 const *srcp = src.Lock(); - vec4 *dstp = dst.Lock(); + vec4 const *srcp = src.lock(); + vec4 *dstp = dst.lock(); float scalex = size.x > 1 ? (oldsize.x - 1.f) / (size.x - 1) : 1.f; float scaley = size.y > 1 ? (oldsize.y - 1.f) / (size.y - 1) : 1.f; @@ -121,8 +123,8 @@ static image ResizeBicubic(image &src, ivec2 size) } } - dst.Unlock(dstp); - src.Unlock(srcp); + dst.unlock(dstp); + src.unlock(srcp); return dst; } @@ -137,11 +139,11 @@ static image ResizeBicubic(image &src, ivec2 size) static image ResizeBresenham(image &src, ivec2 size) { image dst(size); - ivec2 const oldsize = src.GetSize(); + ivec2 const oldsize = src.size(); float const invswsh = 1.0f / (oldsize.x * oldsize.y); - vec4 const *srcp = src.Lock(); - vec4 *dstp = dst.Lock(); + vec4 const *srcp = src.lock(); + vec4 *dstp = dst.lock(); array aline, line; aline.resize(size.x); @@ -198,8 +200,8 @@ static image ResizeBresenham(image &src, ivec2 size) dstp[y * size.x + x] = aline[x] * invswsh; } - dst.Unlock(dstp); - src.Unlock(srcp); + dst.unlock(dstp); + src.unlock(srcp); return dst; } diff --git a/src/lol/image/image.h b/src/lol/image/image.h index 1ec2ffed..80391d76 100644 --- a/src/lol/image/image.h +++ b/src/lol/image/image.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -76,46 +76,52 @@ public: void DummyFill(); void Copy(uint8_t* pixels, ivec2 const& size, PixelFormat fmt); void Copy(image const &other); - bool Load(char const *path); - bool Save(char const *path); + bool load(char const *path); + bool save(char const *path); /* Low level access */ - ivec2 GetSize() const; - void SetSize(ivec2); + ivec2 size() const; + void resize(ivec2); - PixelFormat GetFormat() const; - void SetFormat(PixelFormat fmt); + PixelFormat format() const; + void set_format(PixelFormat fmt); WrapMode GetWrapX() const; WrapMode GetWrapY() const; void SetWrap(WrapMode wrap_x, WrapMode wrap_y); /* Lock continuous arrays of pixels for writing */ - template typename PixelType::type *Lock(); - void *Lock(); - void Unlock(void const *pixels); + template typename PixelType::type *lock(); + void *lock(); + void unlock(void const *pixels); /* Lock 2D arrays of pixels for writing */ template - inline array2d::type> &Lock2D() + inline array2d::type> &lock2d() { /* Hack: this indirection is needed because of a Visual Studio ICE */ - return *(array2d::type> *)Lock2DHelper(T); + return *(array2d::type> *)lock2d_helper(T); } template - void Unlock2D(array2d const &); + void unlock2d(array2d const &); /* Image processing kernels */ - static array2d BayerKernel(ivec2 size); - static array2d HalftoneKernel(ivec2 size); - static array2d BlueNoiseKernel(ivec2 size, - ivec2 gsize = ivec2(7, 7)); - static array2d EdiffKernel(EdiffAlgorithm algorithm); - static array2d NormalizeKernel(array2d const &kernel); - static array2d GaussianKernel(vec2 radius, - float angle = 0.f, - vec2 delta = vec2(0.f, 0.f)); + struct kernel + { + kernel() = delete; + + static array2d normalize(array2d const &kernel); + + static array2d bayer(ivec2 size); + static array2d halftone(ivec2 size); + static array2d blue_noise(ivec2 size, + ivec2 gsize = ivec2(7, 7)); + static array2d ediff(EdiffAlgorithm algorithm); + static array2d gaussian(vec2 radius, + float angle = 0.f, + vec2 delta = vec2(0.f, 0.f)); + }; /* Rendering */ bool RenderRandom(ivec2 size); @@ -141,13 +147,13 @@ public: image YUVToRGB() const; /* Dithering */ - image DitherRandom() const; - image DitherEdiff(array2d const &kernel, - ScanMode scan = ScanMode::Raster) const; - image DitherOstromoukhov(ScanMode scan = ScanMode::Raster) const; - image DitherOrdered(array2d const &kernel) const; - image DitherHalftone(float radius, float angle) const; - image DitherDbs() const; + image dither_random() const; + image dither_ediff(array2d const &kernel, + ScanMode scan = ScanMode::Raster) const; + image dither_ostromoukhov(ScanMode scan = ScanMode::Raster) const; + image dither_ordered(array2d const &kernel) const; + image dither_halftone(float radius, float angle) const; + image dither_dbs() const; /* Combine images */ static image Merge(image &src1, image &src2, float alpha); @@ -163,7 +169,7 @@ public: static image Difference(image &src1, image &src2); private: - void *Lock2DHelper(PixelFormat T); + void *lock2d_helper(PixelFormat T); class image_data *m_data; }; diff --git a/src/lol/sys/file.h b/src/lol/sys/file.h index 1d0e2bd6..4800eb66 100644 --- a/src/lol/sys/file.h +++ b/src/lol/sys/file.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -81,7 +83,7 @@ public: int WriteString(const String &buf); long int GetPosFromStart(); void SetPosFromStart(long int pos); - long int GetSize(); + long int size(); long int GetModificationTime(); private: diff --git a/src/sys/file.cpp b/src/sys/file.cpp index 1c97425f..182dd9a4 100644 --- a/src/sys/file.cpp +++ b/src/sys/file.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -182,7 +184,7 @@ class FileData #endif } - long int GetSize() + long int size() { #if __ANDROID__ return 0; @@ -321,9 +323,9 @@ void File::SetPosFromStart(long int pos) } //-- -long int File::GetSize() +long int File::size() { - return m_data->GetSize(); + return m_data->size(); } //-- diff --git a/src/sys/threadtypes.cpp b/src/sys/threadtypes.cpp index fb729fc5..10b37bab 100644 --- a/src/sys/threadtypes.cpp +++ b/src/sys/threadtypes.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // © 2014—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -175,7 +175,7 @@ public: protected: virtual bool DoWork() { - return m_image.Load(m_path.C()); + return m_image.load(m_path.C()); } String m_path; diff --git a/src/t/image/image.cpp b/src/t/image/image.cpp index ee8edbaa..92015075 100644 --- a/src/t/image/image.cpp +++ b/src/t/image/image.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Unit tests // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2017 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -23,13 +23,13 @@ lolunit_declare_fixture(image_test) { lolunit_declare_test(open_image) { - Image image("data/gradient.png"); + image img("data/gradient.png"); - ivec2 size = image.GetSize(); - lolunit_assert_equal(size.x, 256); - lolunit_assert_equal(size.y, 16); + ivec2 isize = img.size(); + lolunit_assert_equal(isize.x, 256); + lolunit_assert_equal(isize.y, 16); - u8vec4 *data = image.Lock(); + u8vec4 *data = img.lock(); lolunit_assert(data); lolunit_assert_equal((int)data[0].r, 0x00); @@ -40,7 +40,7 @@ lolunit_declare_fixture(image_test) lolunit_assert_equal((int)data[255].g, 0xff); lolunit_assert_equal((int)data[255].b, 0xff); - image.Unlock(data); + img.unlock(data); } }; diff --git a/src/textureimage.cpp b/src/textureimage.cpp index 1ef46289..e87beeb0 100644 --- a/src/textureimage.cpp +++ b/src/textureimage.cpp @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2014 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2017 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include @@ -44,10 +46,10 @@ TextureImage::TextureImage(char const *path) Init(path); } -TextureImage::TextureImage(char const *path, Image* image) +TextureImage::TextureImage(char const *path, image* img) : m_data(GetNewData()) { - Init(path, image); + Init(path, img); } TextureImage::~TextureImage() @@ -66,19 +68,19 @@ void TextureImage::Init(char const *path, ResourceCodecData* loaded_data) auto image_data = dynamic_cast(loaded_data); if (image_data != nullptr) { - Init(path, new Image(*image_data->m_image)); + Init(path, new image(*image_data->m_image)); } delete image_data; } -void TextureImage::Init(char const *path, Image* image) +void TextureImage::Init(char const *path, image* img) { m_data->m_name = String(" ") + path; m_data->m_texture = nullptr; - m_data->m_image = image; - m_data->m_image_size = m_data->m_image->GetSize(); + m_data->m_image = img; + m_data->m_image_size = m_data->m_image->size(); m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x), PotUp(m_data->m_image_size.y)); @@ -111,13 +113,13 @@ void TextureImage::TickDraw(float seconds, Scene &scene) m_data->m_texture = nullptr; } - PixelFormat format = m_data->m_image->GetFormat(); + PixelFormat format = m_data->m_image->format(); int planes = BytesPerPixel(format); int w = m_data->m_texture_size.x; int h = m_data->m_texture_size.y; - uint8_t *pixels = (uint8_t *)m_data->m_image->Lock(); + uint8_t *pixels = (uint8_t *)m_data->m_image->lock(); bool resized = false; if (w != m_data->m_image_size.x || h != m_data->m_image_size.y) { @@ -129,6 +131,7 @@ void TextureImage::TickDraw(float seconds, Scene &scene) pixels = tmp; resized = false; } + /* FIXME: no unlock? */ m_data->m_texture = new Texture(ivec2(w, h), format); m_data->m_texture->SetData(pixels); @@ -146,12 +149,12 @@ char const *TextureImage::GetName() return m_data->m_name.C(); } -void TextureImage::UpdateTexture(Image* image) +void TextureImage::UpdateTexture(image* img) { - m_data->m_image = image; - m_data->m_image_size = m_data->m_image->GetSize(); + m_data->m_image = img; + m_data->m_image_size = m_data->m_image->size(); m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x), - PotUp(m_data->m_image_size.y)); + PotUp(m_data->m_image_size.y)); } Texture * TextureImage::GetTexture() @@ -164,12 +167,12 @@ Texture const * TextureImage::GetTexture() const return m_data->m_texture; } -Image * TextureImage::GetImage() +image * TextureImage::GetImage() { return m_data->m_image; } -Image const * TextureImage::GetImage() const +image const * TextureImage::GetImage() const { return m_data->m_image; }