Running a loop paralel with the other

closed account (42hU7k9E)
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.

http://img231.imageshack.us/my.php?image=ss322009ik3.jpg

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!

Desperate Kajzec. :)
Last edited on
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <windows.h>
#include <time.h>

using std::cout;

int main()
{
    int i=0;
    while (true)
    {
        i++;
        clock_t endPause=clock()+100;
        while (clock()<endPause)
        {
            if (GetAsyncKeyState(VK_SPACE))
            {
                system("cls");
                cout<<i;
            }
        }
    }
    return 0;
}


This program increases a variable i with 1, ten times a second. If space is pressed, it displays the current value of i.

For the clock() function: http://www.cplusplus.com/reference/clibrary/ctime/clock.html

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]
Last edited on
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:

http://www.devarticles.com/c/a/Cplusplus/Multithreading-in-C/

It really is an exciting topic.

~psault


Actually, Multi-threading is not typically used to achieve what is being asked by the OP.

Scipio's approach is better.
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.

~psault
This has become popular only very recently.

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.

Topic archived. No new replies allowed.