English
Načítání konfiguračních souborů pomocí skriptovacího jazyka Lua.
09.05.2006
Tutorial na import programovacího jazyka Lua do C++ projektu najdete
zde.
Příklad načte konfigurační soubor
K3dEngine.lua
-- Graphics
Graphics=
{
FullScreen=false ,
ScreenWidth=800 ,
ScreenHeight=600 ,
ColorBits=32 ,
DepthBits=32 ,
DisplayFreq=70 ,
Fovy=60 ,
Near=2 ,
Far=32000 ,
Flags=18
}
--Texture
Texture=
{
Texture=true ,
Multitexture=false ,
Lightmap=true ,
Gamma=10.00000 ,
Mipmap=true ,
Linear=true
}
--FontBmp
FontBmp=
{
Font="/textures/bmpfont/font.bmp" ,
Italic=false ,
DrawFps=true
}
Napsal jsem vlastní třídu
CLua
která obsahuje všechny potřebné funkce pro načítání konfiguračního souboru
class CLua
{
lua_State *m_pLua; ///< Pointer to Lua state
public :
CLua ()
{
// Open Lua
m_pLua = lua_open();
luaopen_base(m_pLua);
luaopen_io(m_pLua);
luaopen_string(m_pLua);
luaopen_math(m_pLua);
}
~CLua()
{
// Close Lua state
lua_close(m_pLua);
}
/// \brief Load lua script file
/// \param _strFilename Filename string
/// \retval int Positive value, if file correctly loaded
int LuaLoadFile(const char *_strFilename)
{
return luaL_loadfile(m_pLua, _strFilename);
}
/// \brief Call lua script
/// \param _iNumArgs Number of arguments
/// \param _iNumResults Number of results
/// \param _iErrFunc Error function index
int LuaPCall(const int _iNumArgs,const int _iNumResults,const int _iErrFunc)
{
return lua_pcall(m_pLua, _iNumArgs, _iNumResults, _iErrFunc);
}
/// \brief Draw error message to the konsole and close Lua state
/// \param _strFmt Input message string
void LuaError (const char *_strFmt, ...)
{
va_list argp;
va_start(argp, _strFmt);
vfprintf(stderr, _strFmt, argp);
cerr << endl;
va_end(argp);
}
/// \brief Convert parameter from stack to the string
/// \param _id Index of stack position
/// \retval string
const char *LuaToString(const int _id)
{
return lua_tostring(m_pLua, _id);
}
/// \brief Convert parameter from stack to the number
/// \param _id Index of stack position
/// \retval double
double LuaToNumber(const int _id)
{
return lua_tonumber (m_pLua, _id);
}
/// \brief Convert parameter from stack to the boolean
/// \param _id Index of stack position
/// \retval double
int LuaToBoolean(const int _id)
{
return lua_toboolean (m_pLua, _id);
}
/// \brief Read the value of any global Lua variable
/// \param _strVarName Variable name
void LuaGetGlobal(const char *_strVarName)
{
lua_getglobal (m_pLua, _strVarName);
}
/// \brief Check if type of a lua_Object is number
/// \param _id of stack position
int LuaIsNumber(const int _id)
{
return lua_isnumber (m_pLua, _id);
}
/// \brief Check if type of a lua_Object is boolean
/// \param _id of stack position
int LuaIsBoolean(const int _id)
{
return lua_isboolean(m_pLua, _id);
}
/// \brief Check if type of a lua_Object is string
/// \param _id of stack position
int LuaIsString(const int _id)
{
return lua_isstring(m_pLua, _id);
}
/// \brief Push string key to stack
/// \param strKey String key
void LuaPushString(const char *strKey)
{
lua_pushstring(m_pLua, strKey);
}
/// \brief Pop value from stack
/// \param _id of stack position
void LuaPop(const int _id)
{
lua_pop(m_pLua, _id);
}
/// \brief Read a value from a table that resides somewhere in the stack
void LuaGetTable(const int _id)
{
lua_gettable(m_pLua, _id);
}
/// \brief Get double value from table field
/// \param _strKey Field string key
/// \retval double Double value
double LuaGetFieldNumber(const char * _strKey)
{
double dResult;
lua_pushstring(m_pLua, _strKey);
// get table[key]
lua_gettable(m_pLua, -2 );
if (!lua_isnumber(m_pLua, -1 ))
{
LuaError("should be a number" );
}
dResult = lua_tonumber(m_pLua, -1 );
// remove number
lua_pop(m_pLua, 1 );
return dResult;
}
/// \brief Get bool value from table field
/// \param _strKey Field string key
/// \retval bool Bool value
bool LuaGetFieldBool(const char * _strKey)
{
bool bResult;
lua_pushstring(m_pLua, _strKey);
// get table[key]
lua_gettable(m_pLua, -2 );
if (!lua_isboolean(m_pLua, -1 ))
{
LuaError("should be a boolean" );
}
bResult = (bool )lua_toboolean(m_pLua, -1 );
// remove number
lua_pop(m_pLua, 1 );
return bResult;
}
/// \brief Get bool value from table field
/// \param _strKey Field string key
/// \retval bool Bool value
const char *LuaGetFieldString(const char * _strKey)
{
const char * strResult;
lua_pushstring(m_pLua, _strKey);
// get table[key]
lua_gettable(m_pLua, -2 );
if (!lua_isstring(m_pLua, -1 ))
{
LuaError("should be a string" );
}
strResult = lua_tostring(m_pLua, -1 );
// remove number
lua_pop(m_pLua, 1 );
return strResult;
}
/// \brief Get positive if is valid table
/// \param _id of stack position
int LuaIsTable(const int _id)
{
return lua_istable(m_pLua, _id);
}
};
V konstruktoru třídy vytvoříme Lua objekt a zinicializujem. V destruktoru smažeme Lua objekt
CLua ()
{
// Open Lua
m_pLua = lua_open();
luaopen_base(m_pLua);
luaopen_io(m_pLua);
luaopen_string(m_pLua);
luaopen_math(m_pLua);
}
~CLua()
{
// Close Lua state
lua_close(m_pLua);
}
Z třídy
COptions
budeme volat Lua funkce třídy
CLua
Otevřeme soubor
K3dEngine.lua
/// \brief Load file by Lua
/// \param _strFileName Filename
/// \retval bool True if file correctly loaded
inline bool COptions::LoadFile(CString& _strFileName)
{
if (LuaLoadFile(_strFileName.GetString().c_str()) || LuaPCall(0 ,0 ,0 ))
{
LuaError("cannot run configuration file: %s" , LuaToString(-1 ));
return false ;
}
return true ;
}
Otevřeme tabulku
Graphics
Ze které načteme například bool a int hodnoty
LuaGetGlobal("Graphics" );
if (!LuaIsTable(-1 ))
{
LuaError(" is not a valid graphics table" );
}
bool bFullscreen = LuaGetFieldBool("FullScreen" );
int iWidth = (int ) LuaGetFieldNumber("ScreenWidth" );
Otevřeme tabulku
Texture
Ze které načteme například float hodnotu
LuaGetGlobal("Texture" );
if (!LuaIsTable(-1 ))
{
LuaError(" is not a valid texture table" );
}
float fGamma = (float )LuaGetFieldNumber("Gamma" );
Otevřeme tabulku
FontBmp
Ze které načteme například string hodnotu
LuaGetGlobal("FontBmp" );
if (!LuaIsTable(-1 ))
{
LuaError(" is not a valid bmp font table" );
}
const char * strFont = LuaGetFieldString("Font" );
home
/
opengl game