Jump to content
  • Advertisement
Sign in to follow this  
Evil Booger

Need help using time.h functions (C++)

This topic is 4074 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

I need a system that sets a variable to "inactive" for 0.5 seconds and then sets it "active" until the user presses spacebar. Now, I’m not completely helpless and I did a little googling and found that the time.h might help me with this particularly the diff function. At the reference site I found(http://www.cplusplus.com) there was a clock example that made the program wait for one second: (http://www.cplusplus.com/reference/clibrary/ctime/clock.html) I need a function similar to that but without pausing the entire program. Any help will be appreciated be it code or theory. Thank you in advance.

Share this post


Link to post
Share on other sites
Advertisement
You will have to rework your algorithm in terms of the X seconds have elapsed since Y primitive. This primitive is pretty simple to obtain:
class Wait
{
std::clock_t end;
public:
Wait(unsigned milliseconds) :
end(std::clock() + milliseconds * CLOCKS_PER_SEC / 1000) {}
bool Done() const { return std::clock() > end; }
};


For instance, your example would probably be:

Wait wait(500);
variable = Inactive;
while (somecondition) {
if (variable == Inactive && wait.Done())
variable = Active;

if (variable == Active && spacebar_pressed()) {
variable = Inactive;
wait = Wait(500);
}
}

Share this post


Link to post
Share on other sites
Edit 2: clock(), clock_t and the like are not in the std namespace, but you can reference them either with or without std::, just FYI.

Share this post


Link to post
Share on other sites
Wow thanks ToolhrVyk but there's one part of your code that I don't understand, the ": end..." part. And I recently learned from personal experience that you should never ever use code you don't completely understand.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
Wow thanks ToolhrVyk but there's one part of your code that I don't understand, the ": end..." part. And I recently learned from personal experience that you should never ever use code you don't completely understand.


Very good philosophy. As for the : end part, it's called an initialization list.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
Wow thanks ToolhrVyk but there's one part of your code that I don't understand, the ": end..." part. And I recently learned from personal experience that you should never ever use code you don't completely understand.


In C++, the idiomatic way to initialise members in a constructor is in the initialiser list.

A small example, instead of using r = 0; b = 0; ... below, we use initialiser lists to the same effect.

class Colour {
byte r,g,b,a; // byte being some kind of typedef

public:
Colour()
: r(0),g(0),b(0),a(0)
{ /* nothing else to do =) */ }

// note here we don't need to qualify with this->a
// even though our parameters have the same name as the members,
// the compiler knows which is which...
Colour( byte r, byte g, byte b, byte a )
: r(r),g(g),b(b),a(a)
{ /* nothing else to do =) */ }
};




A better example. Say we wanted to have a "person" object with a "name" field of type std::string.

If we use this code:

class Person {
std::string name;
public:
Person( const std::string &startName ) {
name = startName;
}
};




What actually happens is name is default constructed (using std::string::string() ), then has std::string::operator=( const std::string & ) called on it.

The following code only calls one std::sting function, std::string::string( const std::string & ), the copy constructor:

class Person {
std::string name;
public:
Person( const std::string &name )
: name(name)
{}
};


Share this post


Link to post
Share on other sites
Ok thanks guys I understand initializer lists now. And I noticed another thing:
wait = Wait(500);

I had no idea that was possible, maybe I need to study c++ a little more before any more game attempts.

Anyways, what would the "somecondition" variable be? The main game loop ( while(quit==false) perhaps?

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Booger
Ok thanks guys I understand initializer lists now. And I noticed another thing:
wait = Wait(500);

I had no idea that was possible, maybe I need to study c++ a little more before any more game attempts.


All you need to know is that Wait(500) constructs a temporary object of type Wait using its Wait(unsigned) constructor. You may then use that temporary object in any way you wish, including assigning it to a variable.

Quote:
Anyways, what would the "somecondition" variable be? The main game loop ( while(quit==false) perhaps?


Yes, or whatever loop you need to run in addition to this active-inactive variable business. Although it would probably be something more like while(!quit) [wink]

Share this post


Link to post
Share on other sites
While quit is an admirable name that serves most people well, I dislike even having the simplest logic in a bool condition when it isn't required. So instead of while(!quit) loops, I prefer to name the condition variable "running" (or a variant thereof), so while(running).

I find its easier to follow than the slightly "double negative" of !quit.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!