Skip to content

Instantly share code, notes, and snippets.

@yadurajiv
Last active May 31, 2017 07:04
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/86432f64b448b96880fbeb015673042a to your computer and use it in GitHub Desktop.
Save yadurajiv/86432f64b448b96880fbeb015673042a to your computer and use it in GitHub Desktop.
cocos2d-x full screen jitter test
#include "HelloWorldScene.h"
#include "editor-support/cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
USING_NS_CC;
using namespace cocostudio::timeline;
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;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
//Initializing and binding
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
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
this->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());
//player = cocos2d::Sprite::create("cell.png");
addChild(player);
this->runAction(cocos2d::Follow::create(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);
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) {
GLViewImpl* view = (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;
}
}
#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);
void onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event);
void onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event* event);
cocos2d::Sprite* player;
cocos2d::Vec2 movement;
bool leftKeyPress;
bool upKeyPress;
bool downKeyPress;
bool rightKeyPress;
// 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