mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-02 10:15:32 +00:00
compile with luagate
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// Created by Louis Auzuret on 10/06/21
|
||||
//
|
||||
|
||||
#include "LuaGate.hpp"
|
||||
|
||||
namespace LuaG
|
||||
{
|
||||
State::State()
|
||||
: _state(luaL_newstate())
|
||||
{
|
||||
luaL_openlibs(_state);
|
||||
}
|
||||
|
||||
State::~State()
|
||||
{
|
||||
lua_close(_state);
|
||||
}
|
||||
|
||||
lua_State *State::getState(void)
|
||||
{
|
||||
return _state;
|
||||
}
|
||||
|
||||
void State::dofile(std::string filepath)
|
||||
{
|
||||
luaL_dofile(_state, filepath.c_str());
|
||||
}
|
||||
|
||||
void State::dostring(std::string str)
|
||||
{
|
||||
luaL_dostring(_state, str.c_str());
|
||||
}
|
||||
|
||||
float State::getReturnNumber(void)
|
||||
{
|
||||
float res = 0;
|
||||
|
||||
if (lua_isnil(_state, -1))
|
||||
return res;
|
||||
if (!lua_isnumber(_state, -1))
|
||||
return res;
|
||||
res = lua_tonumber(_state, -1);
|
||||
lua_pop(_state, 1);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool State::getReturnBool(void)
|
||||
{
|
||||
bool res = false;
|
||||
|
||||
if (lua_isnil(_state, -1))
|
||||
return res;
|
||||
if (!lua_isboolean(_state, -1))
|
||||
return res;
|
||||
res = lua_toboolean(_state, -1);
|
||||
lua_pop(_state, 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
bool State::callFunction(std::string funcName, int nbParams, int nbReturns)
|
||||
{
|
||||
lua_getglobal(_state, funcName.c_str());
|
||||
if (!lua_isfunction(_state, -1))
|
||||
return false;
|
||||
lua_pcall(_state, nbParams, nbReturns, 0);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by Louis Auzuret on 10/06/21
|
||||
//
|
||||
|
||||
#include <string>
|
||||
#include "lua.hpp"
|
||||
|
||||
namespace LuaG
|
||||
{
|
||||
class State
|
||||
{
|
||||
private:
|
||||
//! @”rief Lua state
|
||||
lua_State *_state;
|
||||
public:
|
||||
//! @brief ctor
|
||||
State();
|
||||
|
||||
//! @brief dtor
|
||||
~State();
|
||||
|
||||
//! @brief No copy constrructor
|
||||
State(const State &) = delete;
|
||||
|
||||
//! @brief No assign operator
|
||||
State &operator=(const State &) = delete;
|
||||
|
||||
//! @brief Get Lua state
|
||||
lua_State *getState(void);
|
||||
|
||||
//! @brief Execute a file in this state
|
||||
void dofile(std::string filepath);
|
||||
|
||||
//! @brief Execute a string in this state
|
||||
void dostring(std::string str);
|
||||
|
||||
//! @brief Get return Number
|
||||
float getReturnNumber(void);
|
||||
|
||||
//! @brief Get return Number
|
||||
bool getReturnBool(void);
|
||||
|
||||
//! @brief call a lua function
|
||||
bool callFunction(std::string funcName, int nbParams, int nbReturns);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user