General C++ game loop question

Discussion in 'Mac Programming' started by Branskins, Jun 14, 2010.

  1. Branskins macrumors 65816

    Joined:
    Dec 8, 2008
    #1
    I know I asked something about a timer a while ago, but after rewriting something I have been working on (I am writing a game for fun so I can learn more about programming) I have come to the point where I need to figure out how to do something to really make it into a game.

    Currently, my "game" has status bars, "Hunger", "Happiness", "Comfort", and "Cleanliness" (I guess that reveals a little bit!).

    Now, what I need to happen is to make these statuses regenerate so you must interact with the object to make them go back up.

    I understand about using a timer in the loop that then goes and updates the statuses, but the problem is, if I display a menu of options to do in the loop, and the program is waiting for user input, I don't know how to have the timer run and update the statuses when the user is deciding what to do.

    For example:
    Hunger: 25

    "Oh know I better feed it!", user puts in prompt to feed it and cannot decide what to feed it. 5 minutes go buy and the user still hasn't decided.

    During this time, that hunger level should go down 1 (lets say every 1 second to be clear, though this will not be the case), how would I update this when the user is idle or trying to decide something? Do I have to use threading?

    Sorry if this is a newbie question, but I it has stumped me!
     
  2. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #2
    Using a thread is one way to do it. But threads are a bit tricky in c++. insted you could use a time calculation to count down the value.
     
  3. Branskins thread starter macrumors 65816

    Joined:
    Dec 8, 2008
    #3
    The big thing here is that the statuses get updated even when a user is idle or deciding on a choice.

    Right now, the prompts are all text based. Here is an example:
    1. Feed
    2. Play
    3. Clean
    4. etc...

    Let's say those are the choices to pick. When sitting there trying to decide, I need all of the statuses to continue updating and for the statuses to display it.

    If I sit staring at the screen for 20 minutes with the menu choices in front of me still waiting for input, I would like to watch the Hunger level slowly go down (I mean I don't *want* to watch it go down since that really isn't enjoyable!). It is really the major point of the game.

    How would I make this time based calculation to continue updating even when the game hangs waiting for user input? Or am I looking at this the wrong way?
     
  4. SamMiller0 macrumors member

    SamMiller0

    Joined:
    Aug 17, 2004
    Location:
    San Jose, CA
    #4
    you want a "hunger" display to be updated while the user is being prompted for input?
     
  5. Branskins thread starter macrumors 65816

    Joined:
    Dec 8, 2008
    #5
    This objects status bar should still go down. The object is still hungry regardless of having to wait for input from the user.

    Of course when I transfer this to have a graphical interface, etc..., the object will be on the screen and you will press a button that says "Food" or something to feed the object. The object is still on the screen during all of this. It is all time based.

    For instance if it didn't do this, then I could create a loophole: let's say the object is hungry, and you go to the select option and don't enter in a selection, the object is basically frozen until a choice is made. I don't want this to happen because I want it to be realtime for obvious reasons.

    Am I looking at this wrong? Is this an interface thing or something else?
     
  6. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #6
    In this case, you can either start using threads as you considered earlier, or write your own task-switcher.

    Just something that performs one operation on a stack of tasks in a round-robin fashion. So one task would be to process input, another would be to update status. Break down each task so that they don't block the task switcher from switching to another task, and ensure that each task completes within a specific quantum of time.

    The trick is designing the tasks rather than the switcher. As a bonus, you're on your way to understanding how an OS works.
     
  7. SamMiller0 macrumors member

    SamMiller0

    Joined:
    Aug 17, 2004
    Location:
    San Jose, CA
    #7
    use poll/epoll/select to multiplex your I/O. If no input is waiting from the user, update your objects.

    Or use blocking I/O and threads as a previous poster has suggested.
     

Share This Page