Display text slowly

This is a discussion on Display text slowly within the C Programming forums, part of the General Programming Boards category; I tried one of the games in the game section of the cboard. It was a text adventure game and ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    33

    Question Display text slowly

    I tried one of the games in the game section of the cboard. It was a text adventure game and it displayed the text slowly(a little too slowly it was slower than reading speed). I looked at the code and I couldn't figure out how this is done. I also looked in the faq and I couldn't find anything.

    How can I print text slowly onto the screen?

    I'm making a text adventure game and this technique would really add some shine to my game!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can use a delay such as sleep(). While sleep() is not standard, I think it will be available in one form or another most places. On *nix, it uses seconds, which one letter a second is too slow, but you can instead use something with finer granularity such as nanosleep().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    Quote Originally Posted by MK27 View Post
    You can use a delay such as sleep(). While sleep() is not standard, I think it will be available in one form or another most places. On *nix, it uses seconds, which one letter a second is too slow, but you can instead use something with finer granularity such as nanosleep().
    In the above mentioned game there was sleep() but my compiler gave the error message of "undefined rederence to 'sleep' ". So I looked up sleep in the faq and it said to use unistd.h, dos.h or windows.h , so i did and it still gave the same error message. In the faq it also said that sleep() is non-standard. Also covered in the faq was a sleep function :

    Code:
     
    Code:
    #include <time.h> 
    
    
    void sleep_seconds ( long seconds ) {
      clock_t limit, now = clock();
      limit = now + seconds * CLOCKS_PER_SEC;
      while ( limit > now )
        now = clock();
    }
    
    
    void sleep_ticks ( long ticks ) {
      clock_t limit, now = clock();
      limit = now + ticks;
      while ( limit > now )
        now = clock();
    }
    
    
    void wait(long milliseconds)
    {
      long timeout = clock() + milliseconds;
      while( clock() < timeout ) continue;
    }
    Is this the same as sleep() but portable?

    Also is there a way I can use sleep() with my compiler? I use code::blocks minGW.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Quote Originally Posted by sampleandhold View Post
    Is this the same as sleep() but portable?
    It will produce the desired result, but it's nothing like any system's sleep. For instance, using sleep would minimize CPU usage, while yours will actually maximize it. Nice try though.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    33
    Quote Originally Posted by GReaper View Post
    It will produce the desired result, but it's nothing like any system's sleep. For instance, using sleep would minimize CPU usage, while yours will actually maximize it. Nice try though.
    Actually I didn't make it. I found it in the faq section.

    Is there a way I can use sleep() with my compiler code::blocks mingGW ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-25-2009, 12:22 AM
  2. Type text = Press button = Display text in Google?
    By Raze88 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2008, 08:39 AM
  3. My text doesn't display
    By joeprogrammer in forum Game Programming
    Replies: 11
    Last Post: 02-23-2006, 09:01 PM
  4. Can't display text from file
    By newbie543 in forum C Programming
    Replies: 1
    Last Post: 09-03-2005, 08:05 PM
  5. Case restarting (slowly)
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-07-2002, 07:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21