我正在尝试为我的未来游戏创建一个关卡编辑器,为此,我需要在wxWidgets库的wxGLCanvas中显示cocos2dx场景。
如何将wxGLCanvas与cocos2d :: GLView链接?
我做了什么:
创建了一个继承自GLView的类ccwxGLView:
class ccwxGLView : public cocos2d::GLView
{
ccwxGLCanvas* mGLCanvas;
public:
ccwxGLView(ccwxGLCanvas* canvas) {
setGLCanvas(canvas);
}
virtual ~ccwxGLView(void) {}
static ccwxGLView* create(ccwxGLCanvas* canvas) {
auto ret = new (std::nothrow) ccwxGLView(canvas);
if(ret) {
ret->autorelease();
return ret;
}
return nullptr;
}
void setGLCanvas(ccwxGLCanvas* canvas) {
mGLCanvas = canvas;
}
ccwxGLCanvas* getGLCanvas() {
return mGLCanvas;
}
HWND getWin32Window() {
return mGLCanvas->GetParent()->GetHWND();
}
/** Force destroying EGL view, subclass must implement this method. */
virtual void end() {
mGLCanvas->Close();
wxDELETE(mGLCanvas);
mGLCanvas = nullptr;
}
/** Get whether opengl render system is ready, subclass must implement this method. */
virtual bool isOpenGLReady() {
return (mGLCanvas && mGLCanvas->IsShown());
}
/** Exchanges the front and back buffers, subclass must implement this method. */
virtual void swapBuffers() {
mGLCanvas->SwapBuffers();
}
virtual void setIMEKeyboardState(bool open) {}
virtual bool windowShouldClose() {
return (mGLCanvas == nullptr);
}
};
创建了一个继承自wxGLCanvas的类:
class ccwxGLCanvas : public wxGLCanvas
{
wxGLContext* m_context;
public:
ccwxGLCanvas(wxFrame* parent, int* args)
: wxGLCanvas(parent, wxID_ANY, args, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
m_context = new wxGLContext(this);
}
virtual ~ccwxGLCanvas() { delete m_context; }
void resized(wxSizeEvent& evt) {
Refresh();
}
void render(wxPaintEvent& evt) {
cocos2d::Director* director = cocos2d::Director::getInstance();
if(director != nullptr && director->getRunningScene() != nullptr)
director->drawScene();
}
};
在AppDelegate中,我修改了glView初始化:
if(!glview) {
glview = ccwxGLView::create(mGLCanvas);
director->setOpenGLView(glview);
}
在app init函数上,我写了这样的东西:
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
frame = new wxFrame((wxFrame *)NULL, -1, wxT("Hello GL World"), wxPoint(50,50), wxSize(400,200));
int args[] = {WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0};
glPane = new ccwxGLCanvas( (wxFrame*) frame, args);
sizer->Add(glPane, 1, wxEXPAND);
frame->SetSizer(sizer);
frame->SetAutoLayout(true);
frame->Show();
ccApp = new AppDelegate();
ccApp->setGLCanvas(glPane);
ccApp->applicationDidFinishLaunching();
当我跑步时,会显示一个wxFrame(窗口),应用程序会立即崩溃。 调用堆栈:
libcocos2d.dll!cocos2d :: Renderer :: setupVBO()第352行C ++ libcocos2d.dll!cocos2d :: Renderer :: setupBuffer()第285行C ++ libcocos2d.dll!cocos2d :: Renderer :: initGLView()第272行C ++ libcocos2d.dll!cocos2d :: Director :: setOpenGLView(cocos2d :: GLView * openGLView)第394行C ++ editor.exe!AppDelegate :: applicationDidFinishLaunching()第44行C ++ editor.exe!EditorApp :: OnInit()第25行C ++ wxbase30ud_vc_custom.dll!wxAppConsoleBase :: CallOnInit()第93行C ++ wxbase30ud_vc_custom.dll!wxEntryReal(int& argc,wchar_t * * argv)第479行C ++ wxbase30ud_vc_custom.dll!wxEntry(int& argc,wchar_t * * argv)第188行C ++