Skip to content

Instantly share code, notes, and snippets.

@pharan
Created April 1, 2016 09:28
Show Gist options
  • Save pharan/77eefc335910d967cbccf2966634c3f7 to your computer and use it in GitHub Desktop.
Save pharan/77eefc335910d967cbccf2966634c3f7 to your computer and use it in GitHub Desktop.
Sample for subclassing cocos2d::Node with basic rendering through a (batchable) TriangleCommand
#include "SimpleCommand.h"
USING_NS_CC;
using namespace std;
Scene* SimpleCommand::scene () {
Scene *scene = Scene::create();
scene->addChild(SimpleCommand::create());
return scene;
}
bool SimpleCommand::init () {
if (!Node::init()) return false;
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));
_texture = _director->getTextureCache()->addImage("sprite.png");
setPosition(100, 100);
return true;
}
void SimpleCommand::draw (Renderer* renderer, const Mat4& transform, uint32_t transformFlags) {
TrianglesCommand::Triangles* triangles = new TrianglesCommand::Triangles();
float x = 0, y = 0;
float w = 80, h = 80;
triangles->vertCount = 4;
triangles->verts = new V3F_C4B_T2F[4];
triangles->verts[0].colors = Color4B::WHITE;
triangles->verts[0].texCoords.u = 0;
triangles->verts[0].texCoords.v = 1;
triangles->verts[0].vertices.x = 0;
triangles->verts[0].vertices.y = 0;
triangles->verts[0].vertices.z = 0;
triangles->verts[1].colors = Color4B::WHITE;
triangles->verts[1].texCoords.u = 0;
triangles->verts[1].texCoords.v = 0;
triangles->verts[1].vertices.x = 0;
triangles->verts[1].vertices.y = h;
triangles->verts[1].vertices.z = 0;
triangles->verts[2].colors = Color4B::WHITE;
triangles->verts[2].texCoords.u = 1;
triangles->verts[2].texCoords.v = 1;
triangles->verts[2].vertices.x = w;
triangles->verts[2].vertices.y = 0;
triangles->verts[2].vertices.z = 0;
triangles->verts[3].colors = Color4B::WHITE;
triangles->verts[3].texCoords.u = 1;
triangles->verts[3].texCoords.v = 0;
triangles->verts[3].vertices.x = w;
triangles->verts[3].vertices.y = h;
triangles->verts[3].vertices.z = 0;
triangles->indexCount = 6;
triangles->indices = new GLushort[6];
triangles->indices[0] = 0;
triangles->indices[1] = 1;
triangles->indices[2] = 2;
triangles->indices[3] = 3;
triangles->indices[4] = 2;
triangles->indices[5] = 1;
TrianglesCommand* trianglesCommand = new TrianglesCommand();
trianglesCommand->init(_globalZOrder, _texture->getName(), getGLProgramState(), BlendFunc::ALPHA_PREMULTIPLIED, *triangles, transform, transformFlags);
renderer->addCommand(trianglesCommand);
}
#ifndef _SIMPLECOMMAND_H_
#define _SIMPLECOMMAND_H_
class SimpleCommand : public cocos2d::Node {
public:
static cocos2d::Scene* scene ();
virtual bool init ();
virtual void draw (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t transformFlags) override;
CREATE_FUNC (SimpleCommand);
protected:
cocos2d::Texture2D* _texture;
};
#endif // _SIMPLECOMMAND_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment