void GamePlay::updateCar(float dt) { int move = CCEGLView::sharedOpenGLView()->getDesignResolutionSize().width * mCarSpeed / 100 * dt; for(int i = 0; i < mCars->count(); i++) { CCSprite *car = (CCSprite*)mCars->objectAtIndex(i); // regenerate car if moved out of screen if(car->getPositionY() + car->getContentSize().height/2 < 0) { this->removeChild(car); generateCar(i); continue; } car->setPositionY(car->getPositionY() - move); } }
// on "init" you need to initialize your instance bool GamePlay::init() { ////////////////////////////// // 1. super init first if( !CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)) ) //RGBA { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(GamePlay::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + visibleSize.height - pCloseItem->getContentSize().height/2)); // create menu, it's an autorelease object CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // load game sprites CCSpriteBatchNode* spritebatch = CCSpriteBatchNode::create("game_play.png"); CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache(); cache->addSpriteFramesWithFile("game_play.plist"); mNumLane = 3; mLanePosX = new int[mNumLane]; for(int i = 0; i < mNumLane; i++) { int laneWidth = CCEGLView::sharedOpenGLView()->getDesignResolutionSize().width / mNumLane; mLanePosX[i] = laneWidth*i + laneWidth/2; } mIsHardMode = true; initUserCar(); // init other car mMinDistance = 120; mMaxDistance = 150; mCarSpeed = 150; mNumCar = CCEGLView::sharedOpenGLView()->getDesignResolutionSize().height / (mUserCar->getContentSize().height * (mMinDistance + 100) / 100); mNumCar *= 2; mNumCar +=2;// add 2 extra cars srand(time(NULL)); mCars = new CCArray(mNumCar); for(int i = 0; i < mNumCar; i++) { generateCar(i); } this->schedule(schedule_selector(GamePlay::update)); this->setTouchEnabled(true); return true; }
void init() { int vboIndex = 0; // Used to auto-increment the vbo index /*select clearing (background) color*/ glClearColor(0.0, 0.0, 0.0, 0.0); //populate our arrays generateCar(); generateStage(); generateWheel(); generateHead(); generateEye(); generatePylon(); // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader-transform.glsl", "fshader-transform.glsl" ); glUseProgram( program ); // Create all vertex array object glGenVertexArrays( VAO_COUNT, &vao[0] ); glGenBuffers(VAO_COUNT*2, &vbo[0]); // Create and initialize any buffer objects glBindVertexArray( vao[CAR] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(carVerts), carVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(carColors), carColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[STAGE] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(stageVerts), stageVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(stageColors), stageColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[WHEEL] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(wheelVerts), wheelVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(wheelColors), wheelColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[HEAD] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(headVerts), headVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(headColors), headColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[EYE] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(eyeVerts), eyeVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(eyeColors), eyeColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[WHEEL_STRIPE] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(wheelStripeVerts), wheelStripeVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(wheelStripeColors), wheelStripeColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[WHEEL_CONNECTORS] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(wheelConVerts), wheelConVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(wheelConColors), wheelConColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindVertexArray( vao[PYLON] ); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(pylonVerts), pylonVerts, GL_STATIC_DRAW); vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer( GL_ARRAY_BUFFER, vbo[vboIndex++] ); glBufferData( GL_ARRAY_BUFFER, sizeof(pylonColors), pylonColors, GL_STATIC_DRAW ); vColor = glGetAttribLocation(program, "vColor"); glEnableVertexAttribArray(vColor); glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, 0); //grab pointers for our modelview and perspecive uniform matrices model_view = glGetUniformLocation(program, "model_view"); projection = glGetUniformLocation(program, "projection"); //Only draw the things in the front layer glEnable(GL_DEPTH_TEST); }