Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在D中返回opengl显示回调_Opengl_D - Fatal编程技术网

在D中返回opengl显示回调

在D中返回opengl显示回调,opengl,d,Opengl,D,我已经用D编写了一个简单的hello world opengl程序,使用转换的gl头 到目前为止,我的代码是: import std.string; import c.gl.glut; Display_callback display() { return Display_callback // line 7 { return; // just display a blank window }; } // line 10 void main(strin

我已经用D编写了一个简单的hello world opengl程序,使用转换的gl头

到目前为止,我的代码是:

import std.string;
import c.gl.glut;

Display_callback display()
{
    return Display_callback // line 7
    {
        return; // just display a blank window
    };
} // line 10

void main(string[] args)
{
    glutInit(args.length, args);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(800,600);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);
    glutMainLoop();
}
我的问题是
display()
函数
glutDisplayFunc()
需要一个返回
Display\u回调的函数,该函数的类型定义为
typedef GLvoid function()Display\u回调。当我试图编译时,dmd说

line 7: found '{' when expecting ';' following return statement
line 10: unrecognized declaration

如何在此处正确返回
Display\u回调
?另外,如何将D字符串和字符串文本更改为
char*
?我对
glutInit
glutCreateWindow
的调用不喜欢它们得到的D字符串。谢谢您的帮助。

您不能将嵌套函数或方法用作函数类型,因为它们依赖于可用的上下文信息(分别是堆栈或对象)。您必须使用静态或文件作用域函数:

void displayEmptyWindow () {
    return;
}

Display_callback display() {
    return &displayEmptyWindow;
}
编辑:如果使用D2,可以使用以下代码将字符串转换为C字符串:

string str = "test string";

// add one for the required NUL terminator for C
char[] mutableString = new char[str.length + 1];
mutableString[] = str[];
mutableString[str.length] = '\0';

// and, finally, get a pointer to the contents of the array
char* cString = mutableString.ptr;
如果您确信正在调用的函数不会修改字符串,则可以将其简化一点:

someCFunction(cast(char*)toStringz(str));
glutDisplayFunc()需要一个不带参数的函数返回GLvoid(即nothing)。您提到的typedef正在创建一个名为Display_callback的typedef,这是一种不接受任何参数且不返回任何内容的函数类型,例如:

GLvoid myGLCallback()
{
    return; // do nothing
}

谢谢你的回复。但是,尝试返回
displayEmptyWindow
会导致
无法隐式转换void*类型的表达式(&displayWindow())以显示回调
错误(基本上就是我尝试将
Display
定义为简单返回void时得到的结果)
toStringz()
还返回
immutable(char)*
,这是opengl仍然不喜欢的。@Max我发布的代码在逐字复制时对我有效。您是否意外地将
()
添加到返回语句中?至于字符串问题,你是对的。我将用正确的信息更新我的答案。我不确定昨晚我做错了什么,但再次尝试你的代码是有效的。谢谢您的帮助。为什么不在
std.string
中使用
toStringz
?@Bernard
toStringz
返回
不可变(char)*
,因此它不能传递给任何需要修改字符串的函数。仍然不起作用<代码>错误:无法隐式转换类型为void的表达式(myGLCallback())以显示\u callback
GLvoid myGLCallback(){}/*。。。等等…*/glutDisplayFunc(myGLCallBack);//这不管用?如果没有,请发布带有这些更改的新代码