【发布时间】:2020-07-01 04:43:25
【问题描述】:
我正在尝试使用 Boost::Python 从 Python 中的 Application C++ 基类派生,并且正在努力包装 GLFW 回调,特别是由于 GLFWwindow* window 参数。
这是 MCVE:
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <boost/python.hpp>
class Application
{
public:
Application();
GLFWwindow* window;
virtual int onKeyPressed(GLFWwindow*, int, int, int , int);
};
Application *app;
Application::Application()
{
glfwInit();
window = glfwCreateWindow(800.0, 600.0, "example", NULL, NULL);
glfwMakeContextCurrent(window);
}
int Application::onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
return 1;
}
struct ApplicationWrap : Application, boost::python::wrapper<Application>
{
int onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
return this->get_override("onKeyPressed")(window, key, scancode, action, mods);
}
};
static void _onKeyPressed(GLFWwindow *window, int key, int scancode, int action, int mods)
{
app->onKeyPressed(window, key, scancode, action, mods);
}
int main() {
// app = new Application();
// glfwSetKeyCallback(app->window, _onKeyPressed);
// delete app;
return 0;
}
BOOST_PYTHON_MODULE(example)
{
namespace python = boost::python;
python::class_<ApplicationWrap, boost::noncopyable>("Application")
//.def("onKeyPressed", _onKeyPressed).staticmethod("_onKeyPressed")
;
}
// import example
// class DerivedApplication(example.Application):
// def __init__(self):
// example.Application.__init__(self)
// def onKeyPressed(window):
// print("successfully overrides example.Application.onKeyPressed.")
// DerivedApplication()
编译:
g++ -I/usr/include/python3.7 main.cpp -lglfw -lpython3.7 -lboost_python3
错误:不完整类型“struct GLFWwindow”的使用无效 类型ID(T) ^~~~~~~~~
注意:'struct GLFWwindow' typedef struct 的前向声明 GLFW窗口 GLFW窗口;
感谢您提供有关如何解决此问题的任何反馈。
【问题讨论】:
-
错误指向哪一行代码?
-
嗨,它发生在
int onKeyPressed(...)内ApplicationWrap。我正在尝试编写一个更详尽的示例,因为我刚刚意识到onKeyPressed需要是静态的才能传递给glfwSetKeyCallback。 -
尝试
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(GLFWwindow)将其注册为opaque pointer。 -
@dxiv 解决了它:) 我将不得不阅读它是如何工作的。如果你想要代表,你把它变成一个答案。再次感谢!
-
很高兴它有帮助。而且,是的,请查看并仔细检查,因为我上次玩这些已经有一段时间了,我可能还需要学习更新的技巧。
标签: c++ boost-python