简体   繁体   English

C ++和Lua与参数的集成(...)(Lua 5.1)

[英]C++ and Lua integration with arguments (…) (Lua 5.1)

I'm currently trying to pass a function from C++ to Lua. 我目前正在尝试将函数从C ++传递给Lua。 Problem is that this function has a parameter of (int, int, char *, ...). 问题是这个函数有一个参数(int,int,char *,...)。 I'm pretty new at using Lua as well. 我也很擅长使用Lua。

So I got all the arguments from the lua stack, and I put the relevant (...) ones into a vector. 所以我得到了lua堆栈中的所有参数,并将相关的(...)放入向量中。 What now? 现在怎么办?

Here is the code in question: 这是有问题的代码:

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);
}

This is the PrintW function: 这是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();

 } 

This is where I pass it into Lua state. 这是我将它传递到Lua州的地方。

m_theModel->theInterface.Pushfunction("PrintOnScreen",L_PrintDebug);

I hope to be able to call it in Lua as so, 我希望能够在Lua中这样称呼它,

PrintOnScreen(10, 100, "Camera 2 Pos: %f %f %f", Camx, Camy, Camz)

or 要么

PrintOnScreen(10,100, "FPS: %f", fps)

The issue appears to be that you need to call a function with a va list, but you are constructing the arguments in a vector and there is no reasonable way to deal with constructing a va_list at run-time. 问题似乎是您需要使用va列表调用函数,但是您正在向量中构造参数,并且没有合理的方法来处理在运行时构造va_list。

I'd recomment making your print function take an arbitrary set of arguments, displaying them in order (a bit more like the chain of C++'s cout ), and forget about using va_list altogether 我建议让你的print函数接受一组任意的参数,按顺序显示它们(有点像C ++的cout链),而忘记完全使用va_list

You can do this by iterating over the arguments and appending to your buffer based on the type of argument. 您可以通过迭代参数并根据参数类型附加到缓冲区来完成此操作。 If you are only dealing with text and floating point this is fairly simple. 如果您只处理文本和浮点,这很简单。 For example (Note: I have not tested this): 例如(注意:我没有测试过这个):

char text[256]; /* Be careful! Buffer overflow potential for not checking lengths of sprintf and not using snprintf */
char *buf = &text[0]; /* start buf at beginning of text */
int x = lua_tointeger(state, 1);
int y = lua_tointeger(state, 2);
for (int i = 3, i < n; i++) {
    if (lua_type(state,i) == LUA_TNUMBER) {
         sprintf(buf, "%f", lua_tonumber(state,i));
    } else if (lua_type(state,i) == LUA_TSTRING) {
         sprintf(buf, "%s", lua_tostring(state,i));
    } else {
         // ERROR SOMEHOW
    }
    buf += strlen(buf); /* Move the end of buf for more appending
}
/* At this point, we've filled in text by walking through it with our buf pointer, and we have a complete string */
glPushAttrib(GL_LIST_BIT);
glListBase(m_base-32);
glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
glPopAttrib();

You could then call in Lua like so: 然后你可以这样打电话给Lua:

PrintOnScreen(10, 100, "Camera 2 Pos: ", Camx, " ", Camy, " ", Camz)

or 要么

PrintOnScreen(10,100,"FPS: ", fps)

This is slightly less convenient with lists of numbers that need to be space separated, but is, I think, easier to code for. 对于需要空格分隔的数字列表,这稍微不方便,但我认为更容易编码。

Again, note that the example code above is untested, and may contain off-by-one errors or other bugs. 再次注意,上面的示例代码未经测试,可能包含一个错误或其他错误。 Especially you should add bounds checking and use snprintf instead of sprintf. 特别是你应该添加边界检查并使用snprintf而不是sprintf。 Or, use the c++ stringstream for the construction. 或者,使用c ++ stringstream进行构造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM