Advertisement

Spaceolopy

Started by July 25, 2022 01:09 AM
86 comments, last by pbivens67 1Β year, 8Β months ago

drawBug(float POSx, float POSy, int bugType, int bugFrame);

Then somewhere in code you would have bugFrame variable cycling between 2 values. Or more frames later.

All the bug draw code is in drawBug. It does one thing, draw bugs. All the bugs. Anywhere.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

so I should refactor the following code.

void drawBug_one(int i)
{
	Bug bug;

	bug.bug_x = -10.0f;
	bug.bug_y = 90.0f;
	bug.bugWidth = 10.0f;
	bug.bugHeight = 10.0f;

	bugs.push_back(bug);

	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[12]);
	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);

	glVertex3f(bugs[i].bug_x, bugs[i].bug_y, 0.0f);
	glTexCoord3f(1.0f, 0.0f, 0.0f);

	glVertex3f(bugs[i].bug_x, bugs[i].bug_y + bugs[i].bugHeight, 0.0f);
	glTexCoord3f(1.0f, 1.0f, 0.0f);

	glVertex3f(bugs[i].bug_x + bugs[i].bugWidth, bugs[i].bug_y + bugs[i].bugHeight, 0.0f);
	glTexCoord3f(0.0f, 1.0f, 0.0f);

	glVertex3f(bugs[i].bug_x + bugs[i].bugWidth, bugs[i].bug_y, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}

void drawBug_two(int i)
{
	Bug bug;

	bug.bug_x = -10.0f;
	bug.bug_y = 90.0f;
	bug.bugWidth = 10.0f;
	bug.bugHeight = 10.0f;

	bugs.push_back(bug);

	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[8]);
	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);

	glVertex3f(bugs[i].bug_x, bugs[i].bug_y, 0.0f);
	glTexCoord3f(1.0f, 0.0f, 0.0f);

	glVertex3f(bugs[i].bug_x, bugs[i].bug_y + bugs[i].bugHeight, 0.0f);
	glTexCoord3f(1.0f, 1.0f, 0.0f);

	glVertex3f(bugs[i].bug_x + bugs[i].bugWidth, bugs[i].bug_y + bugs[i].bugHeight, 0.0f);
	glTexCoord3f(0.0f, 1.0f, 0.0f);

	glVertex3f(bugs[i].bug_x + bugs[i].bugWidth, bugs[i].bug_y, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}
Advertisement
drawBug(1);
drawBug(2);

is a thousand times better than

drawBug1();
drawBug2();

Again, the dice code from the blog is mostly all you need to go by.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

here is my refactored code

void drawBug(float POSx, float POSy, int bugType, int bugFrame)
{
	glEnable(GL_TEXTURE_2D);

	glBindTexture(GL_TEXTURE_2D, texture[bugType]);
	glBegin(GL_POLYGON);
	glTexCoord3f(0.0f, 0.0f, 0.0f);

	glVertex3f(POSx, POSy, 0.0f);
	glTexCoord3f(1.0f, 0.0f, 0.0f);

	glVertex3f(POSx + 10.0f, POSy, 0.0f);
	glTexCoord3f(1.0f, 1.0f, 0.0f);

	glVertex3f(POSx + 10.0f, POSy + 10.0f, 0.0f);
	glTexCoord3f(0.0f, 1.0f, 0.0f);

	glVertex3f(POSx, POSy + 10.0f, 0.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
}
texture[bugType]

If you store frame1 and frame2 consecutively, you can get frames working with

texture[bugType + bugFrame]

where bugFrame would be 0 or 1.

The only issue I see is you are going to have to keep track of the bugTypes somehow. They would be something like 6, 8, 10, 12. With only a few bugs this isn't a problem, except for the so-called β€œmagic numbers”.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

I know what bugtype is don't know what bugFrame is.

Advertisement

pbivens67 said:
I already have 2 bug textures loaded in, I just want one bug to draw to the screen and then another bug to draw in the same spot, basically I want the bugs to be drawn alternately one on the top of another.

I assumed you were trying to get the animation working for Bug Invaders. No? Why else would you want to draw bugs in the same spot?

2 frames of an animated space invader.

πŸ™‚πŸ™‚πŸ™‚πŸ™‚πŸ™‚<←The tone posse, ready for action.

I want to animate the bugs like the animation above

I want to animate the bugs like the animation above

how do I animate the bugs like in space invaders

This topic is closed to new replies.

Advertisement