Advertisement
Delfigamer

lua config example

Jan 20th, 2016
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <lua/lua.hpp>
  2. #include <string>
  3. #include <cstdio>
  4.  
  5. void config_loadfile( lua_State* L, char const* path )
  6. {
  7.     if( luaL_loadfile( L, "conf.lua" ) || lua_pcall( L, 0, 0, 0 ) )
  8.     {
  9.         fprintf( stderr, "error in config file \"%s\": %s\n",
  10.             path, lua_tostring( L, -1 ) );
  11.         std::terminate();
  12.     }
  13. }
  14.  
  15. void lua_getvalue( lua_State* L, int index, bool* result )
  16. {
  17.     *result = lua_toboolean( L, index );
  18. }
  19.  
  20. void lua_getvalue( lua_State* L, int index, int* result )
  21. {
  22.     *result = lua_tointeger( L, index );
  23. }
  24.  
  25. void lua_getvalue( lua_State* L, int index, double* result )
  26. {
  27.     *result = lua_tonumber( L, index );
  28. }
  29.  
  30. void lua_getvalue( lua_State* L, int index, std::string* result )
  31. {
  32.     size_t len;
  33.     char const* ch = lua_tolstring( L, index, &len );
  34.     *result = std::string( ch, ch + len );
  35. }
  36.  
  37. template< typename T >
  38. T config_getvalue(
  39.     lua_State* L, std::string const& expression, T const& defval )
  40. {
  41.     T result;
  42.     std::string chunk = std::string( "return " ) + expression;
  43.     if( luaL_loadstring( L, chunk.c_str() ) || lua_pcall( L, 0, 1, 0 ) ||
  44.         lua_isnil( L, -1 ) )
  45.     {
  46.         result = defval;
  47.     }
  48.     else
  49.     {
  50.         lua_getvalue( L, -1, &result );
  51.     }
  52.     lua_pop( L, -1 );
  53.     return result;
  54. }
  55.  
  56. int main( int argc, char const** argv )
  57. {
  58.     char const* platform = argc >= 2 ? argv[ 1 ] : "default";
  59.     lua_State* conf = luaL_newstate();
  60.     lua_pushstring( conf, platform );
  61.     lua_setglobal( conf, "platform" );
  62.     config_loadfile( conf, "conf.lua" );
  63.     int windowwidth = config_getvalue( conf, "display.width", 640 );
  64.     int windowheight = config_getvalue( conf, "display.height", 480 );
  65.     float gamma = config_getvalue( conf, "display.gamma", 2.2 );
  66.     bool fullscreen = config_getvalue( conf, "display.fullscreen", false );
  67.     std::string renderername = config_getvalue(
  68.         conf, "display.renderer", std::string( "d3d9renderer" ) );
  69.     printf( "window size = %i*%i\n", windowwidth, windowheight );
  70.     printf( "gamma = %.5f\n", gamma );
  71.     printf( "fullscreen = %s\n", fullscreen ? "true" : "false" );
  72.     printf( "renderer library = %s.dll\n", renderername.c_str() );
  73.     return 0;
  74. }
  75.  
  76. //////////////////////////////////////////////////////////////////
  77.  
  78. display = {
  79.     gamma = 1 / 2.2,
  80. }
  81. if platform == 'win' then
  82.     display.renderer = 'd3drenderer'
  83.     display.fullscreen = true
  84. elseif platform == 'linux' then
  85.     display.renderer = 'glrenderer'
  86.     display.fullscreen = false
  87. elseif platform == 'android' then
  88.     display.renderer = 'glesrenderer'
  89.     display.fullscreen = true
  90. else
  91.     display.renderer = 'softwarerenderer'
  92.     display.fullscreen = false
  93. end
  94.  
  95. if display.fullscreen then
  96.     display.width, display.height = 1024, 768
  97. else
  98.     display.width, display.height = 640, 480
  99. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement