Sign in to follow this  

Timer of ten seconds

This topic is 4302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey im trying to make a countdown of ten seconds using the following code: g_iCountDown = .01; if (g_ChipMenu == FALSE) { g_iTimer = 10, g_iTimer >= 0 g_iTimer - g_iCountDown = g_iTimer2; g_pCustBarSprite->SetPosition( x coordinate, y coordinate); g_pCustBarSprite->SetHidden(FALSE); if (g_iTimer2 == 0) { g_ChipMenu = TRUE; g_iTimer = 10; g_CustBarSprite->SetHidden(TRUE); PlaySound((LPCSTR)IDW_CUSTBAR, g_hInstance, SND_ASYNC | SND_RESOURCE); } } I defined that stuff in a header file, and I need to know how to make it go down in 10 seconds. When it reaches 0 seconds then the chipmenu should show up and this should start again when the menu is false. I will apreciate any help at all. Also, I keep getting error messages when I try compiling it. I dont know what is wrong. Can you guys see anything wrong with the code here?

Share this post


Link to post
Share on other sites
The trouble with the approach you're using is that ... well, is

g_iTimer = 10, g_iTimer >= 0 g_iTimer - g_iCountDown = g_iTimer2;

this supposed to be part of a for loop?

Anyway, the trouble is that the CPU probably runs through these calculations in a fraction of a second - maybe 200 ticks where hundreds of millions of ticks are executed per second.

On Windows, check out SetTimer, see also, Using Timers. Also check out CreateWaitableTimer. See also, Using Waitable Timer Objects.

Share this post


Link to post
Share on other sites
Hey, I messed up on the first code I pasted. I'm going to show you the header and cpp this time. Also, I can't use WM_TIMER messages in this program because they do not run async. Since they cause the program to stop to process the messages, this would not work in this program. Here's the header file:

//-------------------------------------------------
// Include Files
//-------------------------------------------------
#include <windows.h>
#include "Resource.h"
#include "GameEngine.cpp"
#include "GameEngine.h" //include the main game files and stuff
#include "Bitmap.cpp"
#include "Bitmap.h"
#include "Sprite.cpp"
#include "Sprite.h"


//-------------------------------------------------
// Global Variables
//-------------------------------------------------

HINSTANCE g_hInstance;
GameEngine* g_pGame;
HDC g_hOffscreenDC;
HBITMAP g_hOffscreenBitmap;
Sprite* g_pCustBarSprite;
int g_iTimer;
float g_iTimer2;
float g_iCountdown;
bool g_ChipMenu;

//------------------------------------------------
// Function Declarations
//------------------------------------------------
void CustBar();

Here is the cpp code:

void CustBar() {
g_iCountDown = .01;
g_iTimer = 10;

if (g_ChipMenu == FALSE) {
g_pCustBarSprite->SetPosition( 12, 16);
g_pCustBarSprite->SetHidden(FALSE);
g_iTimer - g_iCountDown = g_iTimer2;
g_iTimer2 >= 0;

if (g_iTimer2 == 0) {
g_ChipMenu = TRUE;
g_CustBarSprite->SetHidden(TRUE);
}
}
}


The code is not fully done as far as timing. Everything else should work. I can't compile it because I don't have the files i need. The person I am helping has compiled the older one. This code has not been tested yet. The main thing i need is how to make a timer of ten seconds, but it can't be a WM_TIMER message.

Share this post


Link to post
Share on other sites
What is g_iTimer2 >= 0; supposed to do?

If you need an async timer that doesn't involve WM_TIMER messages, check out the links I dropped above regarding CreateWaitableTimer.

Share this post


Link to post
Share on other sites
It is supposed to make sure that g_iTimer2 never gets below 0. When it gets to 0 then the other if statement takes place. All the time chipmenu is false this process goes on.

Share this post


Link to post
Share on other sites
Quote:
Original post by SonicD007
It is supposed to make sure that g_iTimer2 never gets below 0. When it gets to 0 then the other if statement takes place. All the time chipmenu is false this process goes on.


Ok. So shouldn't it be wrapped in a conditional statement? As it stands, it looks to me that the statement doesn't do anything, much less ensure that g_iTimer2 never gets below 0.

And this

g_iTimer - g_iCountDown = g_iTimer2;

looks strange to me too. Shouldn't the left hand and right hand sides be flipped?

g_iTimer2 = g_iTimer - g_iCountDown;

Share this post


Link to post
Share on other sites
Yea, that should be flipped. What's the difference though anyway?

The code does do something though. Well not right now maybe not, but once i figure out how to use a timer of 10 seconds, it is supposed to test if the chipmenu is inactive, if so, then it does its ten seconds while displaying a custbar sprite. Once the ten seconds are up, it checks if the timer is 0, if it is, then the chipmenu goes up again and when the chipmenu goes off, then the process start over.

The whole flipping part, I think that was an error. Good looking into that code :D Thanks!

Share this post


Link to post
Share on other sites
Yeah, I think I can see what you're doing.

Firstly the difference is that the variable on the left is the variable you wish to assign a new value to. The way you have it will only generate an error.

Secondly, g_iCountdown will need to be set to a value representing the number of milliseconds passed since the last call. This will need to be done using the system clock. Check out the links in LessBread's post for that. The way you have it, the delay will be in microseconds, as the CPU will run this code VERY fast, as LessBread said.

Thirdly, the logic seems wrong. Instead of:


g_iTimer2 >= 0;

if (g_iTimer2 == 0) {
g_ChipMenu = TRUE;
g_CustBarSprite->SetHidden(TRUE);
}



That won't work. Instead try:


if (g_iTimer2 <= 0) {
g_ChipMenu = TRUE;
g_CustBarSprite->SetHidden(TRUE);
}



Conditions like that need to be in some kind of statement, such as if-statements used above.

Share this post


Link to post
Share on other sites
LOL I can't believe i made that mistake! I meant to put <= 0. Thanks! Also, I'm gonna look into those sites right now. I'll post back if I get this to work. Thanks for clearing up the difference between the left side and right.

Good catch there.

Share this post


Link to post
Share on other sites
I know you said you didn't want to use WM_TIMER but have you considered using DWORD timeGetTime(void)? It would be more accurate then counting function cycles because it reports in milliseconds.

int numberToCountTo = 10;
DWORD startTime = timeGetTime() / 1000; //1000 milliseconds in a second
while((timeGetTime() / 1000) - (startTime) < numberToCountTo);

Hope this helps to time your sprites.

Share this post


Link to post
Share on other sites
Gary, I tried the code that you posted, but it doesnt run async. It causes the whole program to stop to do that, then it continues with the other code. We need something like what you posted, but it must allow the program to continue running while it processes the timer.

Share this post


Link to post
Share on other sites

This topic is 4302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this