*/}}
Browse Source

tns object type conversion

YimingWu 8 months ago
parent
commit
5bc97a008a
6 changed files with 59 additions and 8 deletions
  1. 2 1
      la_data.c
  2. 10 0
      la_tns.h
  3. 17 6
      la_tns_kernel.c
  4. 1 1
      la_util.c
  5. 1 0
      resources/la_lualibs.cpp
  6. 28 0
      resources/la_tnslibs.cpp

+ 2 - 1
la_data.c

@@ -2825,7 +2825,8 @@ int la_WriteProp(laUDF *udf, laPropPack *pp, int FromThis, int UseInstanceList){
                 if (((laSubProp*)p)->GetType){ pc=((laSubProp*)p)->GetType(inst); need_type=1; }
 
                 if (pc->Hyper == 2){
-                    if((!p->UDFIsSingle) && UseInstanceList&&inst!=udf->CurrentH2Instance->Instance){
+                    void* compare=udf->CurrentH2Instance?udf->CurrentH2Instance->Instance:0;
+                    if((!p->UDFIsSingle) && UseInstanceList && inst!=compare){
                         inst = laGetNextInstance(p, inst, &pi); pp->EndInstance = inst; continue;}
                     if(need_type){  la_WriteString(udf, pc->Identifier); }
                     hi = inst;

+ 10 - 0
la_tns.h

@@ -1321,3 +1321,13 @@ void tnsHCY2RGBLinear(real *hcy, real *rgb);
 void tnsRGB2HCYLinear(real *rgb, real *hcy);
 void tnsHCY2RGB(real *hcy, real *rgb);
 void tnsRGB2HCY(real *rgb, real *hcy);
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+extern const char* LA_TNS_LIB_COMMON;
+
+#ifdef __cplusplus
+}
+#endif

+ 17 - 6
la_tns_kernel.c

@@ -3518,7 +3518,8 @@ tnsLight *tnsCreateLight(tnsObject *under, char *Name, real AtX, real AtY, real
     l->Strength = Strength;
     l->UniDirectional = UniDirectional;
 
-    if (root && UniDirectional && !root->ParentObject) memAssignRef(root, &root->ParentObject,l);
+    // ?????????? WTF
+    //if (root && UniDirectional && !root->ParentObject) memAssignRef(root, &root->ParentObject,l);
     return l;
 }
 
@@ -3742,7 +3743,7 @@ void tnsEvaluateThisObject(tnsObject *o, tnsEvaluateData* ed){
 }
 
 void tnsClearPlayDuplicate(tnsObject* o){
-    if(!o->PlayDuplicate) return;
+    if(!o || !o->PlayDuplicate) return;
     while(lstPopPointer(&o->PlayDuplicate->ChildObjects));
     for(laListItemPointer* lip=o->ChildObjects.pFirst;lip;lip=lip->pNext){
         tnsObject*co=lip->p; tnsClearPlayDuplicate(co);
@@ -3787,7 +3788,13 @@ void tnsFreePlayDuplicate(tnsObject* o){
     o->PlayDuplicate=0;
 }
 
-void tnsLuaInit(lua_State* L){ lua_settop(L,0);
+void tnsLuaSetObject(lua_State* L, tnsObject*o){
+    lua_getglobal(L,"ffi"); lua_getfield(L,-1,"cast"); lua_pushstring(L,"struct tnsObject *"); lua_pushlightuserdata(L,o);
+    lua_call(L,2,1); lua_remove(L,-2); lua_setfield(L,-2,"object");
+}
+void tnsLuaInit(lua_State* L){
+    if(luaL_loadstring(L, LA_TNS_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint("    Error loading tns lua libs\n"); };
+    lua_settop(L,0);
     lua_newtable(L); lua_setglobal(L,"scene");
     lua_getglobal(L,"scene"); lua_pushstring(L,"nodes"); lua_newtable(L); lua_settable(L,1);
     lua_pushnil(L); lua_setfield(L,1,"node"); lua_pop(L,1);
@@ -3795,8 +3802,11 @@ void tnsLuaInit(lua_State* L){ lua_settop(L,0);
 void tnsLuaEnsureNode(lua_State* L,int index){ lua_settop(L,0);
     lua_getglobal(L,"scene"); lua_getfield(L,1,"nodes");
     lua_pushinteger(L,index); lua_gettable(L,2); if(lua_isnil(L,3) || (!lua_istable(L,3))){
-        lua_pushinteger(L,index); lua_newtable(L); lua_settable(L,2); lua_pop(L,2);
+        lua_pushinteger(L,index); lua_newtable(L); lua_settable(L,2); lua_pop(L,1);
+        lua_pushinteger(L,index); lua_gettable(L,2);
+        lua_pushlightuserdata(L,0); lua_setfield(L,3,"object");
     }
+    lua_pop(L,3);
 }
 void tnsLuaRemoveNode(lua_State* L,int index){ lua_settop(L,0);
     lua_getglobal(L,"scene"); lua_pushstring(L,"nodes"); lua_gettable(L,1);
@@ -3805,7 +3815,8 @@ void tnsLuaRemoveNode(lua_State* L,int index){ lua_settop(L,0);
 void tnsLuaRunNode(lua_State* L, tnsEvaluatedNode* en, char* func, tnsObject* root){ lua_settop(L,0); int index=en->LuaID;
     lua_getglobal(L,"scene"); lua_getfield(L,1,"nodes");
     lua_pushinteger(L,index); lua_gettable(L,2); lua_setfield(L,1,"node"); lua_pop(L,1);
-    lua_getfield(L,1,"node"); lua_getfield(L,2,"scripts");
+    lua_getfield(L,1,"node"); tnsLuaSetObject(L,root);
+    lua_getfield(L,2,"scripts");
     if(lua_isnil(L,3) || (!lua_istable(L,3))){
         lua_pop(L,1); lua_newtable(L); lua_setfield(L,2,"scripts"); lua_getfield(L,2,"scripts");
     }
@@ -3962,7 +3973,7 @@ void tnsEvaluateObjectTree(tnsObject* from, tnsEvaluateData* UseED, int Evaluate
 
     if(ed->SceneEvaluateMode){ tnsEvaluateSyncNode(ed, 0); ed->Scene->CurrentParent=CP; ed->Scene->CurrentChild=CC; }
     if(!UseED){ ed->Done=1;
-        if(ed->Scene){ tnsPrintEvaluatedNode(ed->Scene->Root,0);}
+        //if(ed->Scene){ tnsPrintEvaluatedNode(ed->Scene->Root,0);}
     }
 }
 void tnsDrawLayer(tnsEvaluateData* ed,int Layer,void* CustomData){

+ 1 - 1
la_util.c

@@ -2338,7 +2338,7 @@ void la_luaLoadLibs(lua_State *L){
     
     luaL_openlibs(L);
     lua_register(L,"log",lalua_Log);
-    if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint("    Error loading lagui lua libs"); };
+    if(luaL_loadstring(L, LA_LUA_LIB_COMMON) || lua_pcall(L, 0, 0, 0)){ logPrint("    Error loading lagui lua libs\n"); };
 
     lua_gc(L, LUA_GCRESTART, -1);
 }

+ 1 - 0
resources/la_lualibs.cpp

@@ -20,6 +20,7 @@
 
 extern "C" const char* LA_LUA_LIB_COMMON=R"(
 function ddump(tbl, ...)
+    log("[dump]")
     if(type(tbl)~='table') then log("Not a table."); return end
     local levels=100
     local dtableonly=0

+ 28 - 0
resources/la_tnslibs.cpp

@@ -0,0 +1,28 @@
+/*
+* LaGUI: A graphical application framework.
+* Copyright (C) 2022-2023 Wu Yiming
+*
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "la_5.h"
+
+extern "C" const char* LA_TNS_LIB_COMMON=R"(
+ffi.cdef[[
+typedef struct tnsObject tnsObject;
+int tnsSizeOfObject(tnsObject* o);
+void tnsMoveObjectLocal(tnsObject *o, double x, double y, double z);
+tnsObject *tnsFindObject(const char *Name, tnsObject *FromObj);
+]]
+)";