/*****************************************************************************
 * $LastChangedDate: 2011-03-17 19:59:52 -0400 (Thu, 17 Mar 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Interface of C functions for Lua scripts.
 * @remarks @remarks module.txt documents how to export new C++ classes to Lua.
 *
 * ----------------------------
 * -- THIS IS AUTO-GENERATED --
 * ----------------------------
 *
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#include "base/module.hh"
using namespace base;
#include "physics/module.hh"
#include "physics/physics_control.hh" // GetSimulationSpeed()
using namespace physics;
#include "program/module.hh"
using namespace program;
#include "lua_bind/module.hh"
#include "lua_bind/bind.hh"

namespace lua {


namespace bind {

#define BIND_CLASS_NAME      "Base"
#define BIND_METATABLE_NAME  LUABIND_METATABLE_STRING(BIND_CLASS_NAME)

//==============================================================================
// C functions called by Lua that access C++ objects.
//==============================================================================

/*****************************************************************************
 * milliseconds Base:GetElapsedTime()
 *****************************************************************************/
INTERN int 
Base_GetElapsedTime( lua_State* L ) 
{ 
   try { 

        // Since Lua's tick handler cannot be disabled (active while paused),
        // return the elapsed time counted even while paused.
        lua_pushnumber( L, GET_TIMER().GetElapsedTimeTotal().INT() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * int Base:GetMemoryUsed()
 *****************************************************************************/
INTERN int 
Base_GetMemoryUsed( lua_State* L ) 
{ 
   try { 

#if DEBUG
        lua_pushnumber( L, MemoryUsed() );
#else
        lua_pushnumber( L, 0 );  // N/A in non-DEBUG
#endif
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * string Base:GetScriptDir()
 *****************************************************************************/
INTERN int 
Base_GetScriptDir( lua_State* L ) 
{ 
   try { 

        lua_pushstring( L, GET_LUA().GetScriptDir().c_str() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * f Base:GetSimulationSpeed()
 *****************************************************************************/
INTERN int 
Base_GetSimulationSpeed( lua_State* L ) 
{ 
   try { 

        lua_pushnumber( L, PhysicsControl::GetSimulationSpeed() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * milliseconds Base:GetTickFreq()
 * Frequency of program's timer-tick (its pulse).
 *****************************************************************************/
INTERN int 
Base_GetTickFreq( lua_State* L ) 
{ 
   try { 

        lua_pushnumber( L, PROGRAM_CONF.mTimerTickFreq.INT() );
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * bool Base:IfDebug()
 *****************************************************************************/
INTERN int 
Base_IfDebug( lua_State* L ) 
{ 
   try { 

#if DEBUG
        lua_pushboolean( L, true );
#else
        lua_pushboolean( L, false );
#endif
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * bool Base:IfGfxSysIsOSG()  -- if graphics system is OpenSceneGraph
 *****************************************************************************/
INTERN int 
Base_IfGfxSysIsOSG( lua_State* L ) 
{ 
   try { 

#if COMPILE_OSG
        lua_pushboolean( L, true );
#else
        lua_pushboolean( L, false );
#endif
        return 1;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

/*****************************************************************************
 * Base:SetSimulationSpeed(f)
 *****************************************************************************/
INTERN int 
Base_SetSimulationSpeed( lua_State* L ) 
{ 
   try { 

        const fp simulationSpeed = POP_FP( L );
        PhysicsControl::SetSimulationSpeed( simulationSpeed );
        return 0;  // stack items to return
    
   } CATCH_LUA_EXCEPTION 
   return 0; 
} 

//==============================================================================
// Register binding with Lua. 
//==============================================================================

#if ! DOXYGEN 

INTERN const struct luaL_reg sMethodTable[] =
{
  // New(),__gc() omitted as Lua cannot instantiate this C++ class.
  { "GetElapsedTime", Base_GetElapsedTime },
  { "GetMemoryUsed", Base_GetMemoryUsed },
  { "GetScriptDir", Base_GetScriptDir },
  { "GetSimulationSpeed", Base_GetSimulationSpeed },
  { "GetTickFreq", Base_GetTickFreq },
  { "IfDebug", Base_IfDebug },
  { "IfGfxSysIsOSG", Base_IfGfxSysIsOSG },
  { "SetSimulationSpeed", Base_SetSimulationSpeed },
  { NULL, NULL }
};

void
RegisterBase( lua_State* L )
{
    REGISTER_BINDING( L,
                      BIND_METATABLE_NAME,
                      BIND_CLASS_NAME,
                      sMethodTable );
}

#endif // ! DOXYGEN 

} // namespace bind 

} // namespace lua 
