-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsole.cpp
More file actions
66 lines (56 loc) · 1.26 KB
/
Console.cpp
File metadata and controls
66 lines (56 loc) · 1.26 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include "Console.h"
#include <io.h>
#include <fcntl.h>
void ConsoleLoop(std::atomic<bool>* loopEnabled, HWND hwnd)
{
using namespace std;
string str;
str.resize(256);
LPDWORD read = new DWORD;
while (*loopEnabled)
{
if (WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), 1) != WAIT_OBJECT_0)
{
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), (char*)&str[0], 255 * sizeof(TCHAR), read, NULL);
if (str == "quit")
{
PostMessage(hwnd, WM_QUIT, 0, 0);
return;
}
}
}
delete read;
}
Console::Console()
{
hfIn = 0;
hfOut = 0;
loopEnabled = false;
}
void Console::enable()
{
AllocConsole();
handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
int hCrt = _open_osfhandle((long)handleOut, _O_TEXT);
hfOut = _fdopen(hCrt, "w");
setvbuf(hfOut, NULL, _IONBF, 0); //Can assign custom buffer here! Currently no buffering (_IONBF)
*stdout = *hfOut;
handleIn = GetStdHandle(STD_INPUT_HANDLE);
hCrt = _open_osfhandle((long)handleIn, _O_TEXT);
hfIn = _fdopen(hCrt, "r");
setvbuf(hfIn, NULL, _IOLBF, 256);
*stdin = *hfIn;
loopEnabled = true;
//loopThread = new std::thread(ConsoleLoop, &loopEnabled);
}
void Console::disable()
{
loopEnabled = false;
if (loopThread)
{
// loopThread->join();
// delete loopThread;
}
FreeConsole();
}