I'm trying to display a game in the console window using a screen buffer but cannot compile it.
I have this simple code:
#include <Windows.h>
#include <iostream>
#include <thread>
int main() {
const int width = 120, height = 40;
wchar_t* screen = new wchar_t[width * height];
HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
SetConsoleActiveScreenBuffer(console);
DWORD bytesWritten = 0;
while (true) {
for (int i = 0; i < width * height; i++) screen[i] = L' ';
wsprintf(&screen[width + 5], L"Testing");
WriteConsoleOutputCharacter(console, screen, width * height, {0, 0}, &bytesWritten);
Sleep(1000 / 30);
}
return 0;
}
It won't compile, getting two errors:
test.cpp:16:44: error: cannot convert 'wchar_t*' to 'LPSTR {aka char*}' for argument '1' to 'int wsprintfA(LPSTR, LPCSTR, ...)'
and
test.cpp:18:87: error: cannot convert 'wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'BOOL WriteConsoleOutputCharacterA(HANDLE, LPCSTR, DWORD, COORD, PDWORD)'
How can I get this working and how to do it even better?
I'm using g++ 6.3.0 to compile.
Thanks!
wsprintf()
andWriteConsoleOutputCharacterA()
takechar*
notwchar_t*
arguments. What happens when you change the type ofscreen
tochar*
(and change the call tonew
to usechar
)? \$\endgroup\$argument of type "char *" is incompatible with parameter of type "LPWSTR" (15, 14)
andargument of type "const char *" is incompatible with parameter of type "LPCWSTR" (15, 34)
for wsprintf() \$\endgroup\$wsprintfA()
say not to use it. In any event, you need to make yourscreen
variable be the same type as the parameter of the function you're calling. If it takes awchar_t*
, thenscreen
should bewchar_t*
. If the function takes achar*
thenscreen
should be achar*
. \$\endgroup\$