running in real time

This is a discussion on running in real time within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm working on a program wherein the main function of my program is a tautological while loop which ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    8

    running in real time

    Hey guys,

    I'm working on a program wherein the main function of my program is a tautological while loop which repeatedly gets input from the user and displays the appropriate output... and I'm interested in getting it to run in real time. I was wondering if anybody had any tips on how to do this?

    I had thought about possibly trying a loop which would run every .25 seconds, but I don't think that would work with typing input (right now I'm using fgets). I'm completely stumped. Any thoughts?

    Thanks in advance.
    Last edited by do_kev; 03-17-2008 at 01:07 AM. Reason: left out an important sentence without which this post made no sense

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    what does the user input to do with running in real-time? No user is able to provide an input with the speed of the CPU.

    could you explain better what you want to achieve?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Yeah I'm with vart on this one. Huh??

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    real time as opposed to what? CPU time? Do you perhaps mean you just want the loop/program to 'tick over' once per second rather than at full CPU speed?

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    or do you mean realtime as opposed to imaginarytime? are you possibly a Cosmologist? ;->

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm going to borrow the "Hat of Guessing" and guess that you are trying to make "non-waiting input", by which I mean that I think you want to read input from the user without waiting for enter.

    If so, there are a few ways to do that, and which one is the "right" one depends on what OS you are using.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    My apologies for unclarity!

    I'm going to borrow the "Hat of Guessing" and guess that you are trying to make "non-waiting input", by which I mean that I think you want to read input from the user without waiting for enter
    I have to admit, I'm very curious! At the moment, I think this goes beyond what I need to do, though.

    Basically, as I understand it, my program right now stops running when it hits fgets and waits for the users input to continue. I'm wondering if it's possible, while still allowing for the user to give input, to have the program continue executing (and to do things at regular time intervals.)

    Essentially, I'm trying to make a game (text only), and the world needs to continue running even when waiting for the users input. The specific problem that I'm trying to solve is that I'm trying to write a system for fighting monsters, wherein the monsters should attack at regular intervals, whether the user is providing input or not!

    I hope this makes more sense!

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Appears that the "Hat of Guessing" was working today. Wohoo!

    In either case, there are two solutions:
    1. You use some sort of "non-waiting input", which is what I originally suggested.
    2. Use multiple threads. Have one thread deal with user input, and another thread (or two) deal with monsters and players attacking each other, obviously using the user input when available, but only the "wait for user input" will be waiting for the input from the user.

    Both methods gets rather complicated rather quickly, as you probably still want to output the players and monsters fight, whilst waiting for the input, which in turn means that you need to "repaint" the question for the user.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Ahh its so much clearer now. Non-blocking IO is pretty common in games I suppose.

    I've done this before using functions in conio.h, and low and behold, a quick google search for "non-blocking console io" turned up references to conio.h and specifically the kbhit() function.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by arpsmack View Post
    Ahh its so much clearer now. Non-blocking IO is pretty common in games I suppose.

    I've done this before using functions in conio.h, and low and behold, a quick google search for "non-blocking console io" turned up references to conio.h and specifically the kbhit() function.
    conio.h is a "Borland special" [although some others have implemented "compatibility" libraries]. However, if you are going to use unportable functions, it's probably better to use a more modern solution.

    I'm pretty sure the FAQ has an entry for "How do I read input without waiting for Enter" or something very close to that title.

    There are certainly also several threads on the same subject.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    i think you could approach this in a more basic way, depending....if as i think.. your aim is
    to give a more dynamic feel to the text based play
    like..if a user is not coming back sharpish with inputting their move when being attacked or under certain circumstances (like in a potential ambush location or rounding a corner in monster's lair or something)

    maybe you could just give the user a reponse timelimit, if it is exceeded the program is free to go and do something (like you have missed a turn i suppose) then it is the users chance again to respond, you could mix up these response timelimits to give the effect of suddenly being attacked or suddenly spotting something then having to deal with it pronto. (the user is not informed of these time limits unless you wanted to do that to create 'pressure')

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rogster001 View Post
    maybe you could just give the user a reponse timelimit, if it is exceeded the program is free to go and do something (like you have missed a turn i suppose) then it is the users chance again to respond, you could mix up these response timelimits to give the effect of suddenly being attacked or suddenly spotting something then having to deal with it pronto. (the user is not informed of these time limits unless you wanted to do that to create 'pressure')
    But this still assumes that you have a way to "continue" from a fgets() or similar function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    Quote Originally Posted by matsp View Post
    But this still assumes that you have a way to "continue" from a fgets() or similar function.

    --
    Mats
    's true 'strue....

  14. #14
    Registered User
    Join Date
    Mar 2008
    Posts
    8
    Thanks for the help everybody!

    I'm still not entirely sure how I'm going to do this, but I have some good starting points, and I'm sure I'll come up with something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. having a real tough time with functions..
    By simstim99 in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2007, 12:48 AM
  2. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 09:10 PM
  3. Real time clock
    By caduardo21 in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2005, 11:22 AM
  4. Help !!! Time is running out !!!
    By khpuce in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-14-2005, 10:57 AM
  5. Killing someones grandparents
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 09-07-2003, 07:56 AM

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