OpenGL View
Overview
The cocos2d-x engine use GLView
and GLViewImpl
to creating window with OpenGL/OpenGL ES context and receiving input and events. GLView
is an abstract class and GLViewImpl
is its subclass which really do the things. In different platforms GLViewImpl
are not the same.
Steps of Hook
If you want to have you own GLViewImpl
you can do according to the following steps (The example is on windows platform, plain Win32 is used for window and input management, and WGL (with extensions) is used to create contexts).
1.Write a new GLViewImpl
Note:In order to avoid the conflict with the default GLViewImpl provided by engine, your GLView subclass name should not be the same with GLViewImpl, so in the example we names it GLViewNew.You can download the file to get the details and there are some important things you should pay attention to.
You need at least to achieve the following functions:
1 2 3 4 5 6 7 | static GLViewNew* create (const std:: string& viewName); //a static method, create window and OpenGL context and also set event callbacks void pollEvents() override; // event processing (window and input) bool isOpenGLReady() override; void swapBuffers() override; //usually we use double buffer void end() override; //called by Director::end(),in this function we should make windowShouldClose() return true, so that the loop can exit bool windowShouldClose() override; //very important,the key whether mainloop could exits normally, and we need to make sure that when we close the window manually the function can return true to exit mainloop HWND getWin32Window() override; //return the window handle |
2.Add the files to your projects and include the head file in appDelegate.cpp
3.Modefy the code in appDelegate.cpp
1 2 3 4 5 6 7 8 | bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLViewNew::create("My Game");//use GLViewNew instead of GLViewImpl director->setOpenGLView(glview); } |
Now you have your own GLView implement,you can create any type of window and context you want,enjoy it.And we are going to provide an interface to make context attributions setting easyly.