Deleting a sprite

I have a very simple question, how do I delete a sprite using the deletetextures function. I am able to erase the sprite but glColor3f turns all the sprites off as well.

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		glPushMatrix();
		coll(0);
		drawEnemyPlaneCollision();
		glDeleteTextures(1, &texture[2]);
		glColor3f(0.0f, 0.0f, 0.0f);
		glPopMatrix();
	}

You should not delete a texture unless you’re certain you’re finished with it. Presumably, whatever texture[2] represents is going to be used in the not-too-distant future, and if you deleted, you’ll have to recreate it.

You should instead just not render the object. Or more to the point, you should only render the stuff that’s there, and if something is destroyed, it isn’t there anymore.

1 Like

how does one not render an object, can you please explain further?

You know how to render an object. So not rendering one means… not doing that.

I am able to draw a sprite and then when a bullet hits it a collision sprite is drawn and then a white square is drawn which I want to be black. let me know if you need more code.

Uhm, draw a black square then?
Slightly more serious though, I think you may not realize that almost all OpenGL applications constantly render images starting from a cleared color buffer. That means if you want something to not be shown any longer you just don’t issue the OpenGL commands that draw it, but do draw everything that you want to keep visible.
So at the core of the application is a loop that near the beginning clears buffers (glClear), then draws everything that should still be visible and near the end calls glSwapBuffers.

I used the glColor3f(0.0f,0.0f,0.0f) function to draw a black square but it blacks out everything.

Yes, isn’t that what you asked for? I think at this point you need to more accurately describe what you want to do, otherwise I’m not sure how to help.

ok I have a plane sprite that moves across the bottom of the screen. I also have a enemy plane sprite that moves from the top of the screen to the bottom of the screen. the plane can also shoot bullet sprites from the plane to the top of the screen. when the bullet hits the enemy plane sprite it draws an animated collision sprite but then it blacks out the enemy plane which is what I want but it also blacks out the plane sprite at the bottom of the screen as well, which is what I don’t want. let me know if you need code.

Depending on whether you make use of the depth buffer (and assuming I understand what you are after :wink: ), I believe these are your options:

  • with depth buffer:
    • change the z coordinate of the quad (or pair of triangles) that you render for your player plane to be smaller (i.e. closer to the camera) than those for the collision sprite.
  • without depth buffer:
    • what ends up drawn on top depends on the order you draw things in. To have the player plane on top of any collision sprites you draw the player plane sprite after the collision sprite.

I am using without depth buffer and have tried drawing things in different order but it still does not solve my problem.

Hmm, “it doesn’t work” without more details is usually not a problem description that allows for an answer more helpful than “that means you have a bug in your code” :wink: Forum Posting Guidelines have more suggestions on how to ask questions in a way that increases your chances of getting a helpful answer.

For your specific problem: does changing the draw order change anything (just perhaps not in the way you want) or is everything unchanged? In the latter case I would think you actually have a depth buffer and depth testing enabled - I cannot think of why it would not change the resulting image otherwise (maybe someone else here can think of something).
In the former case, maybe you can post a screenshot of what you are getting and either a second image that you’ve manipulated to show what you want or use the screenshot to describe how it differs from the desired outcome?

here is some code maybe this will help

void coll_ship_one()
{
	//draw bullet
	float x = -2.0f+move_sprite;
	float y = -80.0f + shoot_up;
	float oWidth = 4.0f;
	float oHeight = 4.0f;
	//draw ship
	float xTwo = -12.5f;
	float yTwo = 80.0f+move_down;
	float oTwoWidth = 25.0f;
	float oTwoHeight = 20.0f;

	if (checkCollide(x, y, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == 1)
	{
		glPushMatrix();
		coll(0);
		drawEnemyPlaneCollision();
		glDeleteTextures(1, &texture[2]);
		glColor3f(0.0f, 1.0f, 1.0f);
		glPopMatrix();
	}
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glPushMatrix();
	drawPlane();
	drawEnemyPlane();
	drawBulletSprite();
	coll_ship_one();
	glPopMatrix();
	glutSwapBuffers();
}

Not me, sorry. I asked some questions and made a suggestion on how to proceed in the second paragraph of my previous reply.

can I get some help on how to not draw an sprite

It’s hard to help someone with a negative problem. If you ask someone to help you not walk to your car, they’d look at you oddly. That’s basically what you’re asking here.

Your actual problem seems to be that you’ve cobbled together some code that does kinda what you want, but you only have a limited understanding of how and why it currently behaves as it does. I say this because, if you understood it, how not to render a sprite would be as obvious as how not to walk to your car. If you have a ball of code that you don’t really understand, trying to change it often leads to asking questions like this one.

Imparting that understanding would essentially require starting over from page one and teaching you the basics of computer graphics (among other things). That’s not really something that one can reasonably expect from people on a forum.

well I have watched many videos on c++ programming, are there any good videos on opengl?

well I finally solved my problem, thanks for all the help, what should I add to my game?