1

Heres a picture of the output (left is .exe, right is Visual Studio.] enter image description here I'm trying to make a tic-tac-toe program, and when I run it in Visual Studio it displays fine and there are no bugs. However when I try to run the .exe file that was made from the VS compiler, rather than colored X's and O's it just displays the ansi color code that it would be. Does anyone know how I would fix this, or what the cause could be?

Here's how I'm defining the colors:

#define ANSI_COLOR_CYAN    "\x1b[36m"
#define ANSI_COLOR_RESET   "\x1b[0m"

Here's an example of one of my printf calls:

printf(ANSI_COLOR_RED"\nYOU LOST. BETTER LUCK NEXT TIME...\n\n");
printf(ANSI_COLOR_RESET);
k.g
  • 13
  • 3
  • Howzit, How are you setting the color at the moment in your app? – Gordon Beeming Feb 07 '21 at 18:36
  • 1
    Provide some code so that you get helped better and faster. – Rohan Bari Feb 07 '21 at 18:39
  • Ok, ill add code of my color definitions and my calls for printf. – k.g Feb 07 '21 at 18:39
  • 1
    Your Windows console does not have terminal emulation. – Weather Vane Feb 07 '21 at 18:40
  • @GordonBeeming I copy and pasted some samples of my code into my question. – k.g Feb 07 '21 at 18:43
  • @WeatherVane Is there any way to enable terminal emulation on my console? – k.g Feb 07 '21 at 18:46
  • [The live demo](https://godbolt.org/z/rxraTf) clearly indicates that the code is fine. There's something awry with your terminal (i.e. Command Prompt). – Rohan Bari Feb 07 '21 at 18:48
  • I *think* Windows 10 introduced proper terminal emulation, but Windows 7 does not have it. – Weather Vane Feb 07 '21 at 18:49
  • @RohanBari I'm not running the program through command prompt I'm just clicking on the .exe file to run it, not sure if that would make a difference though. – k.g Feb 07 '21 at 18:50
  • Are you using Windows I'm not sure if it does now, but when I used to use Windows CMD (about 1 and a half years back), it didn't support ANSI escape sequences (or at least colors; I had the exact same issue as you). Maybe it still doesn't – mediocrevegetable1 Feb 07 '21 at 18:50
  • 1
    Related: [How to enable VT100 terminal emulation in windows 10?](https://stackoverflow.com/questions/64474568/how-to-enable-vt100-terminal-emulation-in-windows-10) – Weather Vane Feb 07 '21 at 18:52
  • @mediocrevegetable1 Yeah I'm using Windows 10. – k.g Feb 07 '21 at 18:55

2 Answers2

1

The color escape codes are interpreted by your terminal. That's the place to look for fixes to this problem. Here is an overview of ansi color code usage in Windows, and it seems like it should work, but needs some fixes on older versions:

Enabling ANSI colors in older versions of Windows To use ANSI colours in the Windows terminal requires setting VirtualTerminalLevel. VirtualTerminalLevel = 1 is now set by default for the terminal and in ConPTY. In Windows versions 1511 through to 1903 this had to be enabled in the registry at:

[HKEY_CURRENT_USER\Console] "VirtualTerminalLevel"=dword:00000001

Alternatively it can be enabled by calling the SetConsoleMode API with the ENABLE_VIRTUAL_TERMINAL_PROCESSING flag.

Gnoom
  • 183
  • 6
  • 2
    It doesn't say 'old', it says 'older'. All versions mentioned (1511 through to 1903) are Windows 10 versions. – Gnoom Feb 07 '21 at 19:18
0

I was able to get it working by adding the ansi_escapes.h and ansi_escapes.c files from this website into my visual studio project and then rebuilding the program. here's the website with the files

k.g
  • 13
  • 3