Well it wasn’t as easy as I thought it was going to be, but I managed to get a version of my scrolling texture program working under cocos2d-x V3 Beta 2.
I set up cocos2d-x v3 as in my previous post. Then I edited the HelloWorldScene.cpp, pretty much re-writing it, to replicate what I did previously in cocos2d after using the great PRKit.
The idea is exactly the same – an array of points is created, representing the triangle strip to be drawn to create the surface and sub-terrain of our landscape. so,first point is the surface, second the same X coordinate but at the bottom of the screen (y = 0). Third point is the next surface point to the right, fourth is the same X coordinate but at the bottom of the screen. And so on.
Every time the cocos layer is drawn, we decrease all the x coordinates – so the effect of our ‘landscape’ scrolling to the left is achieved.
Scrolling textures with cocos2d-x 3
It’s not that big, so I will paste the whole code from HelloWorldScene.cpp here
auto scene = Scene::create();
auto layer = HelloWorld::create();
if ( !Layer::init() )
_textureOffset = cocos2d::Point(0,0);
_numberOfPoints = 200;
_texture = Director::getInstance()->getTextureCache()->addImage("somerock.png");
_areaTrianglePoints = (cocos2d::Point*) malloc(sizeof(cocos2d::Point) * _numberOfPoints);
_texturePoints = (cocos2d::Point*) malloc(sizeof(cocos2d::Point) * _numberOfPoints);
float maxDx = 50;
float maxDy = 240;
for (int i=0;i<_numberOfPoints;i+=2)
cocos2d::Point p = cocos2d::Point(x,y);
_areaTrianglePoints[i] = p;
_areaTrianglePoints[i+1] = cocos2d::Point(x, 0);
x+= rand() % (int)maxDx;
float dy = (rand() % (int)(maxDy * 2)) – maxDy;
if (y>maxy) y=maxy;
if (y<0) y=20;
_glProgram = cocos2d::ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE) ;
_scrollSpeed = cocos2d::Point(-0.5,0);
_textureOffset -= _scrollSpeed;
for (int i = 0; i < _numberOfPoints; i++)
_areaTrianglePoints[i] += _scrollSpeed;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(cocos2d::Point), _areaTrianglePoints);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, sizeof(cocos2d::Point), _texturePoints);
glDrawArrays(GL_TRIANGLE_STRIP, 0, _numberOfPoints);
for (int i = 0; i < _numberOfPoints; i++)
cocos2d::Point p = cocos2d::Point(_areaTrianglePoints[i].x + _textureOffset.x, _areaTrianglePoints[i].y + _textureOffset.y);
_texturePoints[i] = p * (1.0f / _texture->getPixelsWide());
_texturePoints[i].y = 1 – _texturePoints[i].y;
I’m off now to build this into PooperPig for cocos2d-x!
Software Developer (Senior)
Software developer par excellence,sometime artist, teacher, musician, husband, father and half-life 2 player (in no particular order either of preference or ability)
Started programming aged about 16 on a Commodore Pet.
Self-taught 6500 assembler - wrote Missile Command on the Pet (impressive, if I say so myself, on a text-only screen!)
Progressed to BBC Micro - wrote a number of prize-winning programs - including the best graphics application in one line of basic (it drew 6 multicoloured spheres viewed in perspective)
Trained with the MET Police as a COBOL programmer
Wrote platform game PooperPig which was top of the Ceefax Charts for a while in the UK
Did a number of software dev roles in COBOL
Progressed to Atari ST - learned 68000 assembler & write masked sprite engine.
Worked at Atari ST User magazine as Technical Editor - and was editor of Atari ST World for a while.
Moved on to IBM Mid range for work - working as team leader then project manager
Emigrated to Aus.
Learned RPG programming on the job (by having frequent coffee breaks with the wife!!)
Moved around a few RPG sites
Wrote for PC User magazine - was Shareware Magazine editor for a while.
Organised the first large-scale usage of the Internet in Australia through PC User magazine.
Moved from RPG to Delphi 1
Developed large applications in Delphi before moving on to VB .Net and C#
Became I.T. Manager - realised how boring paper pushing can be
And now I pretty much do .Net development in the daytime, while redeveloping PooperPig for the mobile market at night.