現在、C++ から Lua に関数を渡そうとしています。問題は、この関数に (int、int、char *、...) のパラメーターがあることです。私もLuaを使用するのはかなり新しいです。
そのため、lua スタックからすべての引数を取得し、関連する (...) 引数をベクトルに入れました。今何?
問題のコードは次のとおりです。
static int L_PrintDebug(lua_State *state)
{
MVC_View * theView = MVC_View::GetInstance(MVC_Model::GetInstace());
int n = lua_gettop(state);
// check if number of arguments is less than 3
// then we stop as not enough parameters
if(n < 3)
{
std::cout << "Error: printDebug(number,number, char *, ... )" << std::endl;
lua_error(state);
}
// gets parameter
int x = lua_tointeger(state, 1);
int y = lua_tointeger(state, 2);
char * words = (char*) lua_tostring(state, 3);
//Get the rest of the arguments.
std::vector<float> Numbers;
for(int i = 4;i != n; i++)
{
if(lua_type(state,i) == LUA_TNUMBER)
{
float theNumber = lua_tonumber(state,i);
Numbers.push_back(theNumber);
}
}
// call C++ function
//This is where I'm stuck.
theView->Printw(x,y,words);
}
これは PrintW 関数です。
void MVC_View::Printw (float x, float y, const char* format, ...)
{
glRasterPos2f(x,y);
char text[256];
va_list ap;
if(format==NULL)
return;
va_start(ap,format);
vsprintf_s(text,format,ap);
va_end(ap);
glPushAttrib(GL_LIST_BIT);
glListBase(m_base-32);
glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
glPopAttrib();
}
ここで Lua 状態に渡します。
m_theModel->theInterface.Pushfunction("PrintOnScreen",L_PrintDebug);
そのままLuaで呼び出せるようになればいいのですが、
PrintOnScreen(10, 100, "Camera 2 Pos: %f %f %f", Camx, Camy, Camz)
また
PrintOnScreen(10,100, "FPS: %f", fps)