Cocos2d-x glClearColor

24 Apr 2016

I love cocos2d-x. But sometimes the cocos2d-x team changes something which breaks previously working code. Not that they break cocos2d-x features, but in this case they added stuff that wil override default openGL behaviour. I spent at least two hours to figure this out, so that's why I am writing a blog post about it. Perhaps it will save you some time.

I wanted to change the default background color of openGL. In older versions of cocos2d-x, I could do this:

bool AppDelegate::applicationDidFinishLaunching() {
//default setup code for creating the opengl view
.....
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

And it would use a white background. So I thought, let's use that again. But somehow, it didn't work anmyore. I tried a lot of things, glClear, setting the clear color of the renderer, but all to no avail. Everything I tried ended up in having a black background. So I started searching for all the places where the cocos2d-x team uses the glClearColor and found out that they called it in the new FrameBuffer class. So I tried with the following piece of code:

bool AppDelegate::applicationDidFinishLaunching() {
//default setup code for creating the opengl view
.....
auto buffer = experimental::FrameBuffer::getOrCreateDefaultFBO(glview);
buffer->setClearColor(Color4F(1.0f, 1.0f, 1.0f, 1.0f));

And this was the code that did the trick. The buffer is some sort of singleton, and you will need the Director's glview to get it. Perhaps it should be better if the team refactored this so there is a getter in the Director class, but for now, this will do the trick.

back to blog
comments powered by Disqus