Skip to content

Instantly share code, notes, and snippets.

@johngirvin
Created January 8, 2018 00:25
Show Gist options
  • Save johngirvin/dd19e2058de516d3de78022312aab316 to your computer and use it in GitHub Desktop.
Save johngirvin/dd19e2058de516d3de78022312aab316 to your computer and use it in GitHub Desktop.
Cocos2D-X 2D Camera Test
#include "LowResScene.h"
NewCamera* NewCamera::createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane)
{
auto ret = new (std::nothrow) NewCamera();
if (ret)
{
ret->initOrthographic(zoomX, zoomY, nearPlane, farPlane);
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
NewCamera::NewCamera() : Camera()
{
}
NewCamera::~NewCamera()
{
}
// ====================================================================================================
void LowResScene::addLowResChild(Node *child)
{
assert(lowResObjects != nullptr);
lowResObjects->addChild(child);
}
void LowResScene::removeLowResChild(Node *child, bool cleanup)
{
assert(lowResObjects != nullptr);
lowResObjects->removeChild(child, cleanup);
}
// ====================================================================================================
bool LowResScene::init()
{
if (!Scene::init()) { return false; }
return true;
}
void LowResScene::onEnter()
{
Scene::onEnter();
ccdirector->setClearColor(Color4F::ORANGE);
// initialise low-res render target
// calculate scale to fit the render target into the window
auto windowSize = ccdirector->getWinSizeInPixels();
auto renderScale = std::floor(std::min(
windowSize.width / ((float) Game::DW),
windowSize.height / ((float) Game::DH)
));
DLOG("window: %4d x %4d", (int) windowSize.width, (int) windowSize.height);
DLOG("render: %4d x %4d", Game::DW, Game::DH);
DLOG("scale : %.2f", renderScale);
lowResCamera = NewCamera::createOrthographic(Game::DW, Game::DH, -1024, 1024);
lowResCamera->setCameraFlag(CameraFlag::USER8);
addChild(lowResCamera);
lowResObjects = Layer::create();
addChild(lowResObjects);
lowResTarget = RenderTexture::create(Game::DW, Game::DH, Texture2D::PixelFormat::RGBA4444);
lowResTarget->getSprite()->getTexture()->setAliasTexParameters();
lowResTarget->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
lowResTarget->setPositionNormalized(Vec2::ANCHOR_MIDDLE);
lowResTarget->setScale(renderScale);
lowResTarget->setKeepMatrix(true);
addChild(lowResTarget);
for (int y = -Game::SZ; y < Game::DH+Game::SZ; y += Game::SZ)
{
for (int x = -Game::SZ; x < Game::DW+Game::SZ; x += Game::SZ)
{
auto sprite = Sprite::create("bub.png");
sprite->setAnchorPoint(Vec2::ANCHOR_BOTTOM_LEFT);
sprite->setPosition(x,y);
if (x == 0 && y == 0) {
sprite->setColor(Color3B::MAGENTA);
} else if (x == 0) {
sprite->setColor(Color3B::RED);
} else if (y == 0) {
sprite->setColor(Color3B::BLUE);
} else if (Game::DW-x == Game::SZ) {
sprite->setColor(Color3B::YELLOW);
} else if (Game::DH-y == Game::SZ) {
sprite->setColor(Color3B::ORANGE);
} else {
sprite->setColor(Color3B::GRAY);
sprite->setOpacity(64);
}
sprite->getTexture()->setAliasTexParameters();
addLowResChild(sprite);
}
}
scheduleUpdate();
}
void LowResScene::onExit()
{
Scene::onExit();
}
void LowResScene::update(float delta)
{
lowResCamera->setRotation(lowResCamera->getRotation()+1);
lowResCamera->setScale(lowResCamera->getScale()*0.99f);
if (lowResCamera->getScale() < 0.01) lowResCamera->setScale(2);
Scene::update(delta);
}
void LowResScene::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
bool isLowResCamera = (Camera::getVisitingCamera()->getCameraFlag() == lowResCamera->getCameraFlag());
if (isLowResCamera) {
lowResObjects->setCameraMask(static_cast<unsigned short>(lowResCamera->getCameraFlag()), true);
lowResTarget->beginWithClear(0, 0, 0.4f, 1);
}
Scene::visit(renderer, parentTransform, parentFlags);
if (isLowResCamera) {
lowResTarget->end();
}
}
#ifndef _LOWRES_SCENE_H__
#define _LOWRES_SCENE_H__
#include "GameShared.h"
class NewCamera : public Camera
{
public:
static NewCamera* createOrthographic(float zoomX, float zoomY, float nearPlane, float farPlane);
NewCamera();
~NewCamera();
const Mat4& getViewMatrix() const override {
Mat4 t1;
Mat4::createTranslation(-getPositionX(), -getPositionY(), 0, &t1);
Vec3 origin(Game::DW2, Game::DH2, 0);
Mat4 o1;
Mat4::createTranslation(origin, &o1);
Mat4 s1;
Mat4::createScale(getScale(), getScale(), 1, &s1);
Mat4 r1;
Mat4::createRotationZ(DEG2RAD(getRotation()), &r1);
Mat4 o2;
Mat4::createTranslation(-origin, &o2);
_view = t1 * o1 * s1 * r1 * o2;
_viewInv = _view.getInversed();
_viewProjectionDirty = true;
_frustumDirty = true;
return _view;
}
};
class LowResScene : public Scene
{
public:
NVG_CCREF(LowResScene)
void onEnter() override;
void onExit() override;
void update(float delta) override;
void visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
void addLowResChild(Node *child);
void removeLowResChild(Node *child, bool cleanup = true);
protected:
RenderTexture *lowResTarget;
NewCamera *lowResCamera;
Layer *lowResObjects;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment