I am writing a simple ascii game, where you move a char around the window and shoot a target. The game is inside a do while loop, which waits for you to press a key, and do an action depending on the key value(shoot, move left, right etc.). I have static targets, which I display before the loop. A simple screenshot to get a view of what I'm talking about.
Now the problem is, when I try to put in another loop, which I want it to do itself indipendently, but still in the main loop.
This indipendent loop has an algorithm that defines how should the target's move.
Now I'm asking you to help me, how to make that loop work indipendently inside the main loop, which wait's for you to press a key.
Thank you verry much, this would help me a LOT!
I don't know or this is exactly what you're looking for, and there is a way to really let things run paralel, completely independendly (I don't know it and I believe its pretty complex), but I think the following construction will do:
The function GetAsyncKeyState() from windows.h returns a bool value - true if the key is pressed and false if it isn't. You can acces the arrows and space by VK_SPACE, VK_RIGHT etc. The normal keys can be entered by there ASCII codes of the capital letters. For example, if you want to happen someting when 'a' is pressend: GetAsyncKeyState( (int) 'A')
I hope this helps. Good luck.
[edit]
Ps. I strongly recommend you to make the things inside the while (clock()<endPause) loop not to complex: try to use classes, call there memberfunctions and do the complex things there.
[/edit]
What you are describing is called multithreading and it is fairly complex, as Scipio said. I have only made a small demonstration program of two loops running concurrently using this method. There is a lot to look into on it, but this site is what got me far enough to see what it's about:
I read somewhere that multithreading is often used in game programming for controlling different entities on the screen. It might not be appropriate for an ASCII game, but it couldn't hurt to be exposed to such a topic.
The general concept for a game is a centralised loop. This "game-loop" will handle calling your rendering system and getting all of your assets updated etc.
It's also used to poll the keyboard for values (using ASync function calls).
Scipio's method is in line with this.