Outdated Docs > OpenGL View

OpenGL View

Overview

The cocos2d-x engine uses GLView and GLViewImpl to create window with OpenGL/OpenGL ES context and receive input and events. GLView is an abstract class and GLViewImpl is its subclass which really do the things. In different platforms, the GLViewImpl is not the same.

The Steps to Hook

If you want to have you own GLViewImpl, you can follow the following steps (The example is based 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 conflicting with the default GLViewImpl provided by the engine, your GLView subclass should not have the name as "GLViewImpl", so in the example we name it GLViewNew.You can download the file to get more details and there are some important things you should pay attention to.

You need at least to implement 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 setting context attributions easily later.

GLViewNew.h (1.2 kB) huangsw, 2014-08-08 10:14

GLViewNew.cpp (14.2 kB) huangsw, 2014-08-08 10:14

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.