K3dLua.cpp

Go to the documentation of this file.
00001 
00013 /***************************************************************************
00014  *   Copyright (C) 2007 by Jan Koci   *
00015  *   honza.koci@email.cz   *
00016  *   http://kengine.sourceforge.net/tutorial/
00017  *                                                                         *
00018  *   This program is free software; you can redistribute it and/or modify  *
00019  *   it under the terms of the GNU General Public License as published by  *
00020  *   the Free Software Foundation; either version 2 of the License, or     *
00021  *   (at your option) any later version.                                   *
00022  *                                                                         *
00023  *   This program is distributed in the hope that it will be useful,       *
00024  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00025  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00026  *   GNU General Public License for more details.                          *
00027  *                                                                         *
00028  *   You should have received a copy of the GNU General Public License     *
00029  *   along with this program; if not, write to the                         *
00030  *   Free Software Foundation, Inc.,                                       *
00031  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00032  ***************************************************************************/
00033 
00034 // Load all files from a given directory
00035 // http://www.lua.org/pil/29.1.html
00036 
00037 
00038 #include "K3dLua.h"
00039 
00040 K3dLua::K3dLua(K3dGameData *_pGameData)
00041 {
00042         m_pGameData = _pGameData;
00043         // Set this class to the global data
00044         m_pGameData->SetLua(this);
00045         m_pLua = NULL;
00046         InitLua();
00047         // Open Lua
00048         //m_pLua = lua_open();
00049 /*
00050         m_pLua = luaL_newstate();
00051         if(m_pLua)
00052         {
00053                 luaL_openlibs(m_pLua);
00054                 //luaopen_base(m_pLua);
00055                 //luaopen_table(m_pLua);
00056                 //luaopen_string(m_pLua);
00057                 //luaopen_math(m_pLua);
00058                 //m_pLuaThread = lua_newthread(m_pLua);
00059                 //luaopen_base(m_pLuaThread);
00060                 //luaopen_table(m_pLuaThread);
00061                 //luaopen_string(m_pLuaThread);
00062                 //luaopen_math(m_pLuaThread);
00063                 //m_pLuaDebug = new lua_Debug;
00064         }
00065         else
00066         {
00067                 cerr << "Error in K3dLua::K3dLua() -- Lua is not opened!!!" << endl;
00068         }
00069 */
00070 }
00071 
00072 K3dLua::~K3dLua()
00073 {
00074         std::cout << "Destructor ~K3dLua()" << endl;
00075         DeleteLua();
00076         // Close Lua state
00077 /*
00078         if(m_pLua)
00079         {
00080                 if(m_pLuaDebug)
00081                 {
00082                         delete m_pLuaDebug;
00083                         m_pLuaDebug = NULL;
00084                 }
00085                 //lua_close(m_pLua);
00086                 std::cout << "Destructor ~K3dLua() OK" << endl;
00087         }
00088 */
00089 }
00090 
00092 void K3dLua::InitLua()
00093 {
00094         if(m_pLua == NULL)
00095         {
00096                 m_pLua = luaL_newstate();
00097                 if(m_pLua)
00098                 {
00099                         luaL_openlibs(m_pLua);
00100                 }
00101                 else
00102                 {
00103                         cerr << "Error -- K3dLua::InitLua() -- Lua is not allocated!!!" << endl;
00104                 }
00105         }
00106 }
00107 
00109 void K3dLua::DeleteLua()
00110 {
00111         if(m_pLua)
00112         {
00113                 lua_close(m_pLua);
00114                 m_pLua = NULL;
00115         }
00116 }
00117 
00119 void K3dLua::ResetLua()
00120 {
00121         DeleteLua();
00122         InitLua();
00123 }
00124 
00128 bool K3dLua::LuaLoadFile(const char *_strFilename)
00129 {
00130         std::cout << "bool K3dLua::LuaLoadFile(const char *_strFilename)" << endl;
00131         std::cout << "_strFilename = " << _strFilename << endl;
00132         if(luaL_loadfile(m_pLua ,_strFilename) || LuaPCall(0,0,0))
00133         {
00134                 LuaError("Error -- K3dLua::LuaLoadFile() -- cannot run file: %s", LuaToString(-1));
00135                 return false;
00136         }
00137         return true;
00138 }
00139 
00144 int K3dLua::LuaPCall(const int _iNumArgs,const int _iNumResults,const int _iErrFunc)
00145 {
00146         return lua_pcall(m_pLua, _iNumArgs, _iNumResults, _iErrFunc);
00147         
00148 }
00149 
00152 void K3dLua::LuaError (const char *_strFmt, ...)
00153 {
00154         va_list argp;
00155         va_start(argp, _strFmt);
00156         vfprintf(stderr, _strFmt, argp);
00157         cerr << endl;
00158         va_end(argp);
00159 }
00160 
00164 void K3dLua::LuaRegister( const char *_strName, lua_CFunction _f)
00165 {
00166         lua_register(m_pLua, _strName, _f);
00167 }
00168 
00172 const char *K3dLua::LuaToString(const int _id)
00173 {
00174         return lua_tostring(m_pLua, _id);
00175 }
00176 
00180 double K3dLua::LuaToNumber(const int _id)
00181 {
00182         return lua_tonumber (m_pLua, _id);
00183 }
00184 
00188 int K3dLua::LuaToBoolean(const int _id)
00189 {
00190         return lua_toboolean (m_pLua, _id);
00191 }
00192 
00195 void K3dLua::LuaGetGlobal(const char *_strVarName)
00196 {
00197         if(LuaCheckStack(1))
00198         {
00199                 lua_getglobal(m_pLua, _strVarName);
00200         }
00201         else
00202         {
00203                 cerr << "Error :: void K3dLua::LuaGetGlobal() -- Lua stack overflow !!!" << endl;
00204         }
00205         
00206 }
00207 
00210 int K3dLua::LuaIsNumber(const int _id)
00211 {
00212         return lua_isnumber (m_pLua, _id);
00213 }
00214 
00217 int K3dLua::LuaIsBoolean(const int _id)
00218 {
00219         return lua_isboolean(m_pLua, _id);
00220 }
00221 
00224 int K3dLua::LuaIsString(const int _id)
00225 {
00226         return lua_isstring(m_pLua, _id);
00227 }
00228 
00231 void K3dLua::LuaPushString(const char *strKey)
00232 {
00233         if(LuaCheckStack(1))
00234         {
00235                 lua_pushstring(m_pLua, strKey);
00236         }
00237         else
00238         {
00239                 cerr << "Error :: void K3dLua::LuaPushString() -- Lua stack overflow !!!" << endl;
00240         }
00241 }
00242 
00245 void K3dLua::LuaPop(const int _id)
00246 {       
00247         lua_pop(m_pLua, _id);
00248 }
00249 
00251 void K3dLua::LuaGetTable(const int _id)
00252 {
00253         lua_gettable(m_pLua, _id);
00254 }
00255 
00259 double K3dLua::LuaGetFieldNumber(const char * _strKey)
00260 {
00261         if(LuaCheckStack(1))
00262         {
00263                 double dResult;
00264                 lua_pushstring(m_pLua, _strKey);
00265                 // get table[key] 
00266                 lua_gettable(m_pLua, -2);
00267                 if (!lua_isnumber(m_pLua, -1))
00268                 {
00269                         K3dString strErr = "Error, not found in map file a number `";
00270                         strErr += _strKey;
00271                         strErr += "`";
00272                         LuaError(strErr.GetString().c_str());
00273                 }
00274                 dResult = lua_tonumber(m_pLua, -1);
00275                 // remove number
00276                 lua_pop(m_pLua, 1);
00277                 return dResult;
00278         }
00279         else
00280         {
00281                 cerr << "Error :: void K3dLua::LuaGetFieldNumber() -- Lua stack overflow !!!" << endl;
00282                 return 0;
00283         }
00284         
00285 }
00286 
00290 bool K3dLua::LuaGetFieldBool(const char * _strKey)
00291 {
00292         if(LuaCheckStack(1))
00293         {
00294                 bool bResult;
00295                 lua_pushstring(m_pLua, _strKey);
00296                 // get table[key] 
00297                 lua_gettable(m_pLua, -2);
00298                 if (!lua_isboolean(m_pLua, -1))
00299                 {
00300                         K3dString strErr = "Error, not found in map file a boolean `";
00301                         strErr += _strKey;
00302                         strErr += "`";
00303                         LuaError(strErr.GetString().c_str());
00304                 }
00305                 bResult = (bool)lua_toboolean(m_pLua, -1);
00306                 // remove number
00307                 lua_pop(m_pLua, 1);
00308                 return bResult;
00309         }
00310         else
00311         {
00312                 cerr << "Error :: bool K3dLua::LuaGetFieldBool() -- Lua stack overflow !!!" << endl;
00313                 return 0;
00314         }
00315         
00316 }
00317 
00321 const char *K3dLua::LuaGetFieldString(const char * _strKey)
00322 {
00323         if(LuaCheckStack(1))
00324         {
00325                 const char * strResult=NULL;
00326                 lua_pushstring(m_pLua, _strKey);
00327                 // get table[key] 
00328                 lua_gettable(m_pLua, -2);
00329                 if (!lua_isstring(m_pLua, -1))
00330                 {
00331                         K3dString strErr = "Error, not found in map file a string `";
00332                         strErr += _strKey;
00333                         strErr += "`";
00334                         LuaError(strErr.GetString().c_str());
00335                         return strResult;
00336                 }
00337                 strResult = lua_tostring(m_pLua, -1);
00338                 // remove number
00339                 lua_pop(m_pLua, 1);
00340                 return strResult;
00341         }
00342         else
00343         {
00344                 cerr << "Error :: bool K3dLua::LuaGetFieldString() -- Lua stack overflow !!!" << endl;
00345                 return 0;
00346         }
00347         
00348 }
00349 
00352 int K3dLua::LuaIsTable(const int _id)
00353 {
00354         return lua_istable(m_pLua, _id);
00355 }
00356 
00358 void K3dLua::StackDump()
00359 {
00360         int i;
00361         int top = lua_gettop(m_pLua);
00362         for (i = 1; i <= top; i++) 
00363         {  /* repeat for each level */
00364                 int t = lua_type(m_pLua, i);
00365                 switch (t) 
00366                 {
00367                         case LUA_TSTRING:  /* strings */
00368                         printf("`%s'", lua_tostring(m_pLua, i));
00369                         break;
00370                 
00371                         case LUA_TBOOLEAN:  /* booleans */
00372                         printf(lua_toboolean(m_pLua, i) ? "true" : "false");
00373                         break;
00374                 
00375                         case LUA_TNUMBER:  /* numbers */
00376                         printf("%g", lua_tonumber(m_pLua, i));
00377                         break;
00378                 
00379                         default:  /* other values */
00380                         printf("%s", lua_typename(m_pLua, t));
00381                         break;
00382                 }
00383                 printf("  ");  /* put a separator */
00384         }
00385         printf("\n");  /* end the listing */
00386 }
00387 
00391 bool K3dLua::LuaCheckStack(const int _iNum)
00392 {
00393         return LuaCheckStack(m_pLua, _iNum);
00394 }
00395 
00400 bool K3dLua::LuaCheckStack(lua_State *_pState, const int _iNum)
00401 {
00402         //int iSize = lua_gettop (_pState);
00403         if(lua_checkstack(_pState, 1)>0)
00404         {
00405                 return true;
00406         }
00407         return false;
00408 }
00409 

Generated on Thu Aug 16 23:53:27 2007 for K3dEngine by  doxygen 1.5.0