Jump to content
  • Advertisement
Sign in to follow this  
gskywalkerx

Newb Question

This topic is 3963 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, first of all I would like to say Hi, im new to the forums. Now, i started working on a C++ project a few days in which im going to train my OO skills a bit so i'm working on a simple email simulator. But i want to know how to output text slowly.. you know like in the hacker games thingies ;)

Share this post


Link to post
Share on other sites
Advertisement
The easiet way if you are using Windows and are happy with platform-specific code is to include windows.h (not a problem in console projects) and use Sleep():


#include <windows.h>
#include <iostream>
#include <string>

int main()
{
std::string s="Hello from a slow string...\n";

for(size_t i=0;i<s.size();++i)
{
std::cout << s;
Sleep(1000); // the argument is in miliseconds
}
}


Hope this helps.

Share this post


Link to post
Share on other sites
You could create a timer, store the text in an array, and print it off using a loop that checks for time elapsed.

Edit: Or you can do what EasilyConfused said, but I don't like Sleep() :)

Share this post


Link to post
Share on other sites
thanks, im using sleep() command for my Loading thing. It's a fake but it gives the feel that it's real. Anyway thanks. I hope everyone is nice like you ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by phear-
You could create a timer, store the text in an array, and print it off using a loop that checks for time elapsed.

Edit: Or you can do what EasilyConfused said, but I don't like Sleep() :)


Sleep is more process friendly - it allows your program to run without hogging the CPU. If accurate timing is important you can "under-sleep" a bit, because (IIRC) the value you pass to sleep is the minimum value, not the maximum. But for a simple delay between print statements, you don't need to worry about that.

Share this post


Link to post
Share on other sites
If you need a bit more accuracy than Sleep() provides, the next thing to look at is timeGetTime(). You need to link against winmm.lib to use this, which few compilers seem to do by default, but how you do this will depend on what compiler and IDE you are using:


#include <windows.h>
#include <iostream>
#include <string>

void Delay(DWORD milisecs)
{
DWORD start=timeGetTime();
DWORD current=start;

while(current-start<milisecs) current=timeGetTime();
}

int main()
{
std::string s="Hello from a slow string...\n";

for(size_t i=0;i<s.size();++i)
{
std::cout << s;
Delay(1000);
}
}


timeGetTime() is not as accurate as QueryPerformanceCounter(), but is quite a lot simpler to use.

I have personally had great success with vastly improving the accuracy of timeGetTime() by calling:


timeBeginPeriod(1);


at program startup, and have been using this to frame-time my games without any reported problems for a couple of years. However, the MSDN docs do state that if you do this, you should also call:


timeEndPeriod(1);


at program termination, or your computer will explode and cause satellites to drop out of the sky (or something).

Quote:
Original post by gskywalkerx
thanks, im using sleep() command for my Loading thing. It's a fake but it gives the feel that it's real.


I firmly agree with rip-off that anything other than Sleep() is overkill for delaying text printing to a console, and stand by my original suggestion in this context. Sleep() is not a fake in any way - it is just not as accurate as other approaches.

Share this post


Link to post
Share on other sites
To make it even more 1337h4x0r, randomize the delays ;)

#include <windows.h>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
srand( time(0) );
string s = "Hello from a slow string...\n";
int min = 50;
int max = 350;

for( int i=0; i < s.size(); i++ ) {
cout << s;
Sleep( rand() %(max-min) +min );
}
}



I didn't test it, and it's late, but I think it's kosher.

Edit:
I tested it, and it works :) Try and play around with the min and max values for better effects. These values are alright though

[Edited by - c4c0d3m0n on February 4, 2008 5:24:07 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!