@@ -64,6 +64,7 @@ Game::Game(int initial_pos_x, int initial_pos_y) :
6464 maxY = height / 2 ;
6565
6666 InitializeGlew ();
67+ CreateHeartBuffers ();
6768 CreateBackgroundBuffers ();
6869 CreateRocketBuffers ();
6970 CreateAsteroidBuffers ();
@@ -165,11 +166,15 @@ void Game::FireAnimation() {
165166
166167void Game::RenderFunction (void ) {
167168 Rocket* rocket = Rocket::getInstance ();
169+
170+ glClearColor (0 .0f , 0 .0f , 0 .0f , 1 .0f );
171+ glClear (GL_COLOR_BUFFER_BIT);
172+
168173 if (rocket->getIsDead ()) {
174+ // TODO: render a png with the text GAME OVER
175+ // glutSwapBuffers();
169176 return ;
170177 }
171- glClearColor (0 .0f , 0 .0f , 0 .0f , 1 .0f );
172- glClear (GL_COLOR_BUFFER_BIT);
173178
174179 if (rotationAngle > 360 .f ) {
175180 rotationAngle = 0 .0f ;
@@ -243,16 +248,26 @@ void Game::RenderFunction(void) {
243248 glBindVertexArray (bulletVao);
244249 glDrawArrays (GL_POLYGON, 0 , Constants::nrOfVerticesPerCircle);
245250 }
251+
246252 rocket->RocketAsteroidsCollision (asteroids);
253+
254+ glm::mat4 heartMatrix = glm::scale (glm::mat4 (1 .0f ), glm::vec3 (4 .7f , 4 .7f , 1 .0f ));
255+ heartMatrix = backgroundMatrix * heartMatrix;
256+
257+ glm::mat4 heartTranslateMatrix = glm::translate (glm::mat4 (1 .0f ), glm::vec3 (10 .0f , 0 .0f , 0 .0f ));
258+ for (int i = 0 ; i < rocket->getRemainingLives (); i++) {
259+ if (i != 0 ) {
260+ heartMatrix = heartMatrix * heartTranslateMatrix;
261+ }
262+ glUniformMatrix4fv (myMatrixLocation, 1 , GL_FALSE, &heartMatrix[0 ][0 ]);
263+ glBindVertexArray (heartVao);
264+ glDrawArrays (GL_POLYGON, 0 , 14 );
265+ }
247266
248267 glutPostRedisplay ();
249268 glFlush ();
250269}
251270
252- bool colliding (Rocket rocket, Asteroid asteroid)
253- {
254- return false ;
255- }
256271void Game::CreateBackgroundBuffers () {
257272
258273 const int nrOfVertices = 1000 * 5 ;
@@ -391,21 +406,18 @@ void Game::CreateAsteroidBuffers() {
391406 GLfloat Vertices[1000 ];
392407 for (int k = 0 ; k < Constants::nrOfVerticesPerCircle; k++) {
393408 float theta = Constants::TWO_PI * k / Constants::nrOfVerticesPerCircle;
394- // cout << "angle " << float(Constants::TWO_PI * float(k)) / float(Constants::nrOfVerticesPerCircle) << "\n";
395409 float x = cos (theta);
396410 float y = sin (theta);
397411 // varfurile corespunzatoare cercului
398412 Vertices[9 * k] = x;
399413 Vertices[9 * k + 1 ] = y;
400414 Vertices[9 * k + 2 ] = 0 .0f ;
401415 Vertices[9 * k + 3 ] = 1 .0f ;
402- // cout << 4*k << " " << Vertices[4 * k] << " " << Vertices[4 * k + 1] << " " << Vertices[4 * k + 2] << " " << Vertices[4 * k + 3] << "\n";
403416
404417 Vertices[9 * k + 4 ] = 1 .0f ;
405418 Vertices[9 * k + 5 ] = 0 .0f ;
406419 Vertices[9 * k + 6 ] = 0 .0f ;
407-
408- // cout << (float)k / 18 << " ";
420+
409421 Vertices[9 * k + 7 ] = (sin (theta) + 1 ) / 2 ;
410422 Vertices[9 * k + 8 ] = (cos (theta) + 1 )/ 2 ;
411423 }
@@ -435,6 +447,7 @@ void Game::CreateAsteroidBuffers() {
435447 glEnableVertexAttribArray (2 );
436448 glVertexAttribPointer (2 , 2 , GL_FLOAT, GL_FALSE, 9 * sizeof (GLfloat), (GLvoid*)(7 * sizeof (GLfloat)));
437449}
450+
438451void Game::UpdateAsteroids () {
439452 auto end = std::remove_if (asteroids.begin (),
440453 asteroids.end (),
@@ -481,6 +494,7 @@ void Game::GenerateAsteroids(int nrOfAsteroids) {
481494 this ->asteroids .push_back (Game::GenerateSingleAsteroid ());
482495 }
483496}
497+
484498void Game::LoadTexture (GLuint &texture, const char * imageName)
485499{
486500 glGenTextures (1 , &texture);
@@ -499,6 +513,7 @@ void Game::LoadTexture(GLuint &texture, const char* imageName)
499513 SOIL_free_image_data (image);
500514 glBindTexture (GL_TEXTURE_2D, 0 );
501515}
516+
502517void Game::loadTextures () {
503518 for (auto & imageName : Constants::textureImages) {
504519 GLuint texture;
@@ -572,3 +587,53 @@ void Game::UpdateBullets() {
572587
573588 bullets.erase (end, bullets.end ());
574589}
590+
591+ void Game::CreateHeartBuffers () {
592+
593+ GLfloat Vertices[] = {
594+ 7 .0f , 3 .7f , 0 .0f , 1 .0f ,
595+
596+ 8 .0f , 4 .5f , 0 .0f , 1 .0f ,
597+ 9 .6f , 6 .0f , 0 .0f , 1 .0f ,
598+ 10 .4f , 7 .5f , 0 .0f , 1 .0f ,
599+ 10 .0f , 9 .0f , 0 .0f , 1 .0f ,
600+ 8 .7f , 9 .8f , 0 .0f , 1 .0f ,
601+ 7 .8f , 9 .7f , 0 .0f , 1 .0f ,
602+
603+ 7 .0f , 8 .9f , 0 .0f , 1 .0f ,
604+
605+ 6 .2f , 9 .7f , 0 .0f , 1 .0f ,
606+ 5 .3f , 9 .8f , 0 .0f , 1 .0f ,
607+ 4 .0f , 9 .0f , 0 .0f , 1 .0f ,
608+ 3 .6f , 7 .5f , 0 .0f , 1 .0f ,
609+ 4 .4f , 6 .0f , 0 .0f , 1 .0f ,
610+ 6 .0f , 4 .5f , 0 .0f , 1 .0f ,
611+ };
612+
613+ const int verticesCount = sizeof (Vertices) / sizeof (GLfloat);
614+
615+ GLfloat Colors[verticesCount];
616+
617+ for (int i = 0 ; i < verticesCount; i += 4 ) {
618+ Colors[i] = 1 .0f ;
619+ Colors[i + 1 ] = 0 .0f ;
620+ Colors[i + 2 ] = 0 .0f ;
621+ Colors[i + 3 ] = 1 .0f ;
622+ }
623+
624+ glGenBuffers (1 , &heartVbo);
625+ glBindBuffer (GL_ARRAY_BUFFER, heartVbo);
626+ glBufferData (GL_ARRAY_BUFFER, sizeof (Vertices), Vertices, GL_STATIC_DRAW);
627+
628+ glGenVertexArrays (1 , &heartVao);
629+ glBindVertexArray (heartVao);
630+
631+ glVertexAttribPointer (0 , 4 , GL_FLOAT, GL_FALSE, 0 , 0 );
632+ glEnableVertexAttribArray (0 );
633+
634+ glGenBuffers (1 , &heartColorBufferId);
635+ glBindBuffer (GL_ARRAY_BUFFER, heartColorBufferId);
636+ glBufferData (GL_ARRAY_BUFFER, sizeof (Colors), Colors, GL_STATIC_DRAW);
637+ glVertexAttribPointer (1 , 4 , GL_FLOAT, GL_FALSE, 0 , 0 );
638+ glEnableVertexAttribArray (1 );
639+ }
0 commit comments