/* Bounce by kecebongsoft (http://kecebongsoft.wordpress.com) */ #define WINDOW_WIDTH 800 #define WINDOW_HEIGHT 600 #define WINDOW_MODE GLFW_WINDOW #define GAME_STATE_INIT_DEVICE 1 #define GAME_STATE_INIT_LEVEL 2 #define GAME_STATE_GAMEPLAY 3 #define GAME_STATE_EXIT 4 #define DISPLAY_LIST_CUBE 1 #include #include #include //#include #include #include // Header file for standard file i/o. #include #include #include struct Ball{ GLfloat pos[3]; GLfloat angle[3]; GLfloat max[3]; GLfloat min[3]; float speed; float maxspeed; float yspeed; float gravity; GLUquadric* quad; }; int gameState = GAME_STATE_INIT_DEVICE; std::vector > levelmap; // Holds array of map (tileset) GLuint cubetexture[3]; GLuint balltexture[3]; Ball ball; GLfloat vcamera[3]; GLfloat LightAmbient[]= { 0.2f, 0.2f, 0.2f, 0.0f }; GLfloat LightDiffuse[]= { 1.5f,1.5f, 1.5f, 0.0f }; GLfloat LightPosition[]= { 50.0f, 5.0f, -5.0f, 1.0f }; // tmp vars : float angle,x,y,z; float max(float x, float y){ return x>y?x:y; } float min(float x, float y){ return x()); int row,col, startcol, startrow; row=col=0; while (fgets(line, 2, txtlevel)) { if (line[0]!='\n') { levelmap[levelmap.size()-1].push_back(atoi(line)); if(atoi(line)==9){ startrow = row; startcol = col; } col++; } else { levelmap.push_back(std::vector()); col=0; row++; } } fclose(txtlevel); std::reverse(levelmap.begin(), levelmap.end()); for(int i=0;i0){ ball.speed-=0.00025; } if(ball.speed<0){ ball.speed+=0.00025; } ball.pos[0]+=ball.speed; ball.pos[1]+=ball.yspeed; ball.yspeed-=ball.gravity; ball.angle[2]+=(ball.speed * -45); row = (int)floorf((ball.pos[1] + 1) / 2); col= (int)floorf((ball.pos[0] + 1) / 2); if(row=0){ i--; } ball.min[0] = ((GLfloat)i * 2) + 2; i = col+1; while(levelmap[row][i]!=1 && (int)i=0){ i--; } ball.min[1] = ((GLfloat)i * 2) + 1.8; i = row+1; while(levelmap[i][col]!=1 && (int)iball.max[0]){ ball.pos[0] = ball.max[0]; ball.speed*=-1; ball.speed+=0.015; ball.speed=0; } if(ball.pos[0]ball.max[1]){ ball.pos[1] = ball.max[1]; ball.yspeed*=-1; } if(ball.pos[1]0){ ball.yspeed-=0.08; //} } } void update() { x += (ball.pos[0] - x) / 50; y += (ball.pos[1] - y) / 30; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); gluPerspective(45.0f,(GLfloat)WINDOW_WIDTH/(GLfloat)WINDOW_HEIGHT,0.1f,100.0f); gluLookAt(x,y+6,z,x,y+1,0,0,1,0); //gluLookAt(x,y,z,x,y,0,0,1,0); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glMatrixMode(GL_MODELVIEW); updateBall(); } void drawMap(){ for(int i=0;i0.0006){ // Wait for ticks input(); update(); lasttick = glfwGetTime(); } // Don't wait for ticks draw(); } bool gameLoop() { switch (gameState) { case GAME_STATE_INIT_DEVICE: if (initGraphic()) { gameState = GAME_STATE_INIT_LEVEL; } else { gameState = GAME_STATE_EXIT; } break; case GAME_STATE_INIT_LEVEL: if (initLevel()) { gameState = GAME_STATE_GAMEPLAY; } else { gameState = GAME_STATE_EXIT; } break; case GAME_STATE_GAMEPLAY: gameplay(); break; case GAME_STATE_EXIT: return false; break; } return true; } int main() { initWindow(); while (gameLoop()) { } terminate(0); return 0; }