1
\$\begingroup\$

I was trying to build a game like this

enter image description here

and i want to create and destroy these ships at runtime , I have used GL_QUADS and used an image to plot over the QUAD, how can I dynamically create multiple Quads,paste (ship) image over them and destroy them when they collide with the canon ball, thanks

here is a chunk of code

ship = SOIL_load_OGL_texture(
        "ship2.png",
        SOIL_LOAD_RGBA,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
        );
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glColor3f(1, 1, 1);
    glBindTexture(GL_TEXTURE_2D, ship);
    glBegin(GL_QUADS);
    glTexCoord2d(0, 0);
    glVertex2d(450 - x, 0);
    glTexCoord2d(1, 0);
    glVertex2d(500 - x, 0);
    glTexCoord2d(1, 1);
    glVertex2d(500 - x, 100);
    glTexCoord2d(0, 1);
    glVertex2d(450 - x, 100);
    glEnd();
\$\endgroup\$
1
  • 1
    \$\begingroup\$ "how can I dynamically create multiple Quads,paste (ship) image over them and destroy them when they collide with the canon ball, thanks" -- I don't understand. Are you asking how to un-draw ships? Because the normal approach here is to just redraw the whole screen every frame, which will naturally and implicitly reflect any changes to your scene. \$\endgroup\$ Dec 15, 2016 at 19:15

1 Answer 1

1
\$\begingroup\$

It's difficult to tell what's going on based on your minimal code sample, but:

  • Don't reload the texture for each ship. Load it once at startup and reuse it for each ship.
  • It looks like you're loading the texture every time you draw the ship. If so, don't do that.
  • Since all of your ships look the same, just keep a vector of model transforms (matrices). When you destroy a ship, remove its transform from the vector. Voila, it won't be drawn on the next refresh.

In general, it's best to avoid dynamically creating and destroying objects in your update/render loop whenever possible. Allocation, asset loading and glCreate* are expensive operations and can significantly affect performance. On a mobile device, it also impacts battery life.

Good luck!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .