Hi forum!
I'm starting out in c++ after three courses in school, using Code::Blocks and working on a small project, a textbased game in cmd.
So I'm using code randomly found on different forums (this being a most helpful resource!) and I've found a function Sleep() in <windows.h> and combining it with
BlockInput() from <winable.h> to display a short intro of text.
To my question: Is there a less brutal way to simulate BlockInput() while Sleep() is called? My antivirus-program (Avira) is telling me my .exe is a malware of type TR/ATRAPS.Gen2 and similar problems appear on other computers.
Also it could be nice running the program without the mouse blocked if scrolling through some forum while running the game (since it's only occupying half the screen)
My idea was to reset or clear the cin streambuffer after Sleep(), but I haven't figured out how input really works, and how to implement that..
I also tried cin.sync() and cin.ignore(numeric_limits<streamsize>::max(), '\n')
but without the results I hoped for..
For summary I have a short code that I want to implement without using BlockInput() as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <windows.h>
#include <winable.h>
#include <conio.h>
using namespace std;
int get_key ()
{
int ch = getch();
if ( ch == 0 || ch == 224 )
ch = 256 + getch();
return ch;
}
int main()
{
BlockInput(true);
cout << "Showing a line for approx. 3 seconds" << endl;
Sleep(3000);
cout << "Showing another line for approx. 3 seconds" << endl;
Sleep(3000);
cout << "Press any key to continue!" << endl;
BlockInput(false);
get_key();
cout << "Game start!" << endl;
//some more code that asks for a name and some other variables
return 0;
}
| |
If the code above is executed and a key is pressed while displaying the two lines, nothing happens, but the mouse cursor and other input outside the cmd is frozen. If BlockInput() is commented then if a key is pressed during the "intro" the program will exit before you can se the "Press any key to continue!" line...
See my problem?