Advertisement

drawing sprites

Started by June 22, 2017 04:18 AM
87 comments, last by Tom Sloper 7 years, 4 months ago


	void TimerFunction(int value)
{
 glutPostRedisplay();
 glutTimerFunc(1000, TimerFunction, 1);
}
	void drawScene() {
 glClear(GL_COLOR_BUFFER_BIT);
     drawScene_bug();
 TimerFunction(1);
 eraseScene_bug();
// drawScene_bug_two();
// eraseScene_bug_two();
	drawScene_ship();
	drawScene_bullet();
 glutSwapBuffers();
}
	

Is there a question coming with this?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Advertisement

Nope. This is bat country.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

sorry but the question is how do I draw a sprite and then wait for  a period of time and then draw another sprite over it.

also I want to erase the sprite after I draw it.

What it looks like you're trying to do is draw the first sprite, and then "wait" (is this why you were asking about Sleep()?) and then draw the second. This won't work. The "wait" call you are attempting to make here will just halt everything for the duration of the call, giving the program the appearance of freezing for a second or however long you are waiting.

The correct way to handle this kind of thing is, unfortunately, more complicated.

What you can do is keep track of how much time has elapsed between the last time you called drawScene() and this current call to drawScene(). There are various ways to do this, using various platform-specific or platform-agnostic timing APIs that can give you the "current time" in some fashion. std::chrono::high_resolution_clock is probably a reasonable place for you to look at (specifically, the now() function).


void drawScene() {
   auto timeStampNow = std::chrono::high_resolution_clock::now();
   auto elapsed = timeStampNow - timeStampPrevious;

   // ...

   timeStampPrevious = timeStampNow;
}

Here, you get the current time, subtract it from the previous time (which you are storing elsewhere, in a member variable perhaps) and compute the elapsed time since the last call. At the end of the function, you set the "previous time" to the current time, so it's correctly set up for the next time drawScene() is called.

Once you have this elapsed time, you can use it to "count down" until the next time you are supposed to do something. If you have another member variable that is, say, "timeUntilSecondSprite" initially set to some value corresponding to how many seconds until the next sprite show up, you might do


timeUntilSecondSprite -= elapsed;
if (timeUntilSecondSprite <= 0.0f) {
  // Counter has run out, draw the sprite now.
}

You can implement as many time-based effects as you want with this approach, optionally resetting the counters when they hit zero if you want them to occur (for example) every N seconds.

Advertisement
10 minutes ago, phil67rpg said:

sorry but the question is how do I draw a sprite and then wait for  a period of time and then draw another sprite over it.

Which part(s) of what you said are you having problems with?

1) Drawing the sprite.

2) Waiting for a period of time.

3) Drawing another sprite on top of it?

5 minutes ago, phil67rpg said:

also I want to erase the sprite after I draw it.

This was explained to you in a previous thread you made. Was the explanation not clear? If so, what was unclear about it?

Hello to all my stalkers.

lactose I am having a problem with the wait period

1 minute ago, phil67rpg said:

lactose I am having a problem with the wait period

Josh's reply covers that part.

Hello to all my stalkers.

27 minutes ago, jpetrie said:

auto elapsed = timeStampNow - timeStampPrevious;

I get an error with the minus sign(-) no operand matches these operators

This topic is closed to new replies.

Advertisement