Multithreading while in main loop C++

86 Views Asked by At

I'm trying to write a simple console game, space shooter -ish. The game runs on main loop. I want to use multithreading to manage shooting projectiles and another for movement of the enemy. How should I set it up? The shooting function works on a loop and while projectile is flying you can't do anything else. I can't find similiar problem anywhere, any answer much appraciated.

I tried defining thread where you can see async and it would crush. I tried setting thread objects in main for game func. and shooting func. but didn't work either.

while(1) {
        if (_kbhit) {
            char ch = _getch();
            switch (ch) {
            case 'a':
                if (shipx > 3) {
                    shipx--;
                    drawShip();
                    break;
                }
            case 'd':
                if (shipx < screenw / 2 - 9) {
                    shipx++;
                    drawShip();
                    break;
                }
            case 's': {async(launch::async, fire); break;}
            }
-------------------------------------------------------------
void fire() {

        gotoxy(shipx + 4, screenh - 4);
        cout << "^";
        for (int i = screenh - 5;i > 0;i--) {
            gotoxy(shipx + 4, i);
            cout << "^";
            gotoxy(shipx + 4, i + 1);
            cout << " ";
            this_thread::sleep_for(40ms);
        }   text```


0

There are 0 best solutions below