I writing simple cmd game. My question is how to use timing function while I use _getch() command? Because this getch function stops the timing and the timer continuous after I click the keys.:@

Recommended Answers

All 10 Replies

You're entering the realm of concurrency. For example, you might create one thread to handle user input while another handles the timer.

You're entering the realm of concurrency. For example, you might create one thread to handle user input while another handles the timer.

Ok that's looks simple but how? Can you post an example code?

Ok that's looks simple but how?

I wish concurrency were simple.

Can you post an example code?

Sure. With this example I'm assuming your "timer" is actually a timeout for the input. That seems to be the more common situation relative to an ongoing timer separate from the input loop. Since you mentioned _getch(), I'm also assuming that Win32 threads are supported on your implementation:

#include <windows.h>
 
namespace jsw {
    namespace threading {
        class auto_event {
        public:
            auto_event(): _event(CreateEvent(0, false, false, 0)) {}
 
            BOOL wait(DWORD timeout = 1) const
            {
                return WaitForSingleObject(_event, timeout) == WAIT_OBJECT_0;
            }
 
            BOOL set() { return SetEvent(_event); }
        private:
            HANDLE _event;
        };
 
        class thread {
        public:
            static thread start(
                LPTHREAD_START_ROUTINE fn, LPVOID args = 0, 
                DWORD state = 0, DWORD timeout = 5000)
            {
                return thread(CreateThread(0, 0, fn, args, state, 0), timeout);
            }
 
            static void sleep(DWORD milliseconds) { Sleep(milliseconds); }
            static void exit(DWORD exitCode) { ExitThread(exitCode); }
        public:
            thread(HANDLE thread, DWORD timeout): _thread(thread), _timeout(timeout) {}
            ~thread() { CloseHandle(_thread); }
 
            DWORD exit_code() const
            {
                DWORD exitCode = NO_ERROR;
 
                GetExitCodeThread(_thread, &exitCode);
 
                return exitCode;
            }
 
            HANDLE handle() const { return _thread; }
            BOOL is_alive() const { return exit_code() == STILL_ACTIVE; }
            DWORD join() { return WaitForSingleObject(_thread, _timeout); }
            DWORD suspend() { return SuspendThread(_thread); }
            DWORD resume() { return ResumeThread(_thread); }
            BOOL abort(DWORD exitCode) { return TerminateThread(_thread, exitCode); }
        private:
            HANDLE _thread;
            DWORD _timeout;
        };
    }
}
 
#include <iostream>
#include <conio.h>
 
DWORD WINAPI get_key(LPVOID args)
{
    using namespace jsw::threading;
 
    auto_event *e = (auto_event*)((LPVOID*)args)[0];
    int *key = (int*)((LPVOID*)args)[1];
 
    *key = _getch();
    e->set();
 
    return NO_ERROR;
}
 
int main()
{
    using namespace jsw::threading;
 
    auto_event e;
    int key;
    LPVOID args[2] = {&e, &key};
 
    thread worker = thread::start(get_key, args);
 
    if (e.wait(5000))
        std::cout<<'\''<< (char)key <<"' detected\n";
    else {
        worker.abort(NO_ERROR);
        std::cout<<"Timed out\n";
    }
}

Thanks for sharing this code but for me it still doesn't work! Anyway that game is just an experiment and can be played without moving monsters(the idea for I was wanted the code) too.

but for me it still doesn't work!

That's not a helpful description of the problem.

While multithreading is indeed the way to go in such cases, I believe the OP might be able to get away with just a _kbhit call.

#include <windows.h>
#include <conio.h>

#include <iostream>

int main()
{
    int counter = 0;
    char ch = 0;

    while (true)
    {
        ch = 0;

        if (_kbhit()) ch = _getch();

        // hit esc to quit
        if (ch == 27) break;

        std::cout << counter++;

        if (ch) std::cout << ' ' << ch;

        std::cout << std::endl;

        Sleep(150);
    }

    return 0;
}

By the way, something I've been wanting to ask for a while now, what does the 's' in "jsw" stand for?

commented: much easier then Narue's. Same thing I had in mind! +5

what does the 's' in "jsw" stand for?

Sandra.

Member Avatar for iamthwee

@m4ster_r0shi I believe you are right in that this probably does what the OP wants.

However, nice snippet narue... One day, what you code will make sense to me LOL.

the kbhit() can solve this problem but if someone wants to do jobs in separate ways s/he needs to do it with threads.

Thank you m4ster_r0shi you solution works 100%!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.