Skip to content

Instantly share code, notes, and snippets.

@yadurajiv
Last active May 31, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yadurajiv/7daa81df43a2cc872269135b7f4d154a to your computer and use it in GitHub Desktop.
Save yadurajiv/7daa81df43a2cc872269135b7f4d154a to your computer and use it in GitHub Desktop.
Strange flicker cocos2d-x - integrated gfx card vs dedicated gfx card - https://www.youtube.com/watch?v=LrOEYtUKeh8 (works fine on the dedicated card)
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
// 1. super init first
if ( !Layer::init() )
{
return false;
}
visibleSize = cocos2d::Director::getInstance()->getVisibleSize();
origin = cocos2d::Director::getInstance()->getVisibleOrigin();
renderTexFinal = cocos2d::RenderTexture::create(visibleSize.width, visibleSize.height);
renderTexFinal->setPosition(cocos2d::Vec2(origin.x, origin.y));
renderTexFinal->setAutoDraw(false);
renderTexFinal->setVisible(false);
this->addChild(renderTexFinal);
auto antialias = cocos2d::Sprite::createWithTexture(renderTexFinal->getSprite()->getTexture());
antialias->setPosition(cocos2d::Vec2(visibleSize.width / 2, visibleSize.height / 2));
antialias->setAnchorPoint(cocos2d::Vec2::ANCHOR_MIDDLE);
antialias->setFlippedY(true);
antialias->getTexture()->setAntiAliasTexParameters();
//Initializing and binding
auto listener = cocos2d::EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
backgroundGroup = cocos2d::Node::create();
this->addChild(backgroundGroup);
playerGroup = cocos2d::Node::create();
this->addChild(playerGroup);
for (int i = 0; i < 10; i++) {
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width * i, 0));
// add the sprite as a child to this layer
backgroundGroup->addChild(sprite, 0);
}
auto draw = cocos2d::DrawNode::create();
draw->drawSolidCircle(cocos2d::Vec2(32,32), 32, 360, 24, cocos2d::Color4F::RED);
auto tex = cocos2d::RenderTexture::create(64, 64);
tex->begin();
draw->visit();
tex->end();
player = cocos2d::Sprite::createWithTexture(tex->getSprite()->getTexture());
playerGroup->addChild(player);
scheduleUpdate();
return true;
}
void HelloWorld::update(float dt) {
if (leftKeyPress) {
movement.x -= dt * 180;
}
if (rightKeyPress) {
movement.x += dt * 180;
}
if (upKeyPress) {
movement.y += dt * 180;
}
if (downKeyPress) {
movement.y -= dt * 180;
}
player->setPosition(player->getPosition() + movement);
cocos2d::Vec2 pos = player->getPosition();
auto per = dt;
if (dt != 1)
per = dt * 3;
auto center = Vec2(visibleSize.width / 2, visibleSize.height / 2);
auto val = Vec2(center.x - pos.x, center.y - pos.y);
auto lerped = lastPosition.lerp(val, per);
lastPosition = lerped;
backgroundGroup->setPosition(lastPosition);
playerGroup->setPosition(lastPosition);
movement.set(cocos2d::Vec2::ZERO);
}
// Implementation of the keyboard event callback function prototype
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW) {
leftKeyPress = false;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW) {
upKeyPress = false;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW) {
downKeyPress = false;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW) {
rightKeyPress = false;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_ENTER) {
cocos2d::GLViewImpl* view = (cocos2d::GLViewImpl*)cocos2d::Director::getInstance()->getOpenGLView();
if (view->isFullscreen()) {
view->setWindowed(960, 640);
} else {
view->setFullscreen();
}
}
}
void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) {
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_LEFT_ARROW) {
leftKeyPress = true;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_UP_ARROW) {
upKeyPress = true;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_DOWN_ARROW) {
downKeyPress = true;
}
if (keyCode == cocos2d::EventKeyboard::KeyCode::KEY_RIGHT_ARROW) {
rightKeyPress = true;
}
}
void HelloWorld::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) {
renderTexFinal->beginWithClear(0, 0, 0, 0);
backgroundGroup->visit();
playerGroup->visit();
renderTexFinal->end();
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
void update(float dt);
virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags)override;
void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event);
void onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event);
cocos2d::Node* backgroundGroup;
cocos2d::Node* playerGroup;
cocos2d::Sprite* player;
cocos2d::Vec2 movement;
cocos2d::Vec2 lastPosition;
cocos2d::Size visibleSize;
cocos2d::Vec2 origin;
bool leftKeyPress;
bool upKeyPress;
bool downKeyPress;
bool rightKeyPress;
cocos2d::RenderTexture* renderTexFinal;
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment