Search code examples
cncurses

using ncurses to print ascii art from file acts strangely


I am trying to print ascii art, which is stored in a separate file, to the terminal using ncurses.

Here is my C code:

#include <ncurses.h>
#include <string.h>

int main() {
  initscr();
  raw();

  WINDOW * startScreen = newwin(20, 70, 0, 0);
  curs_set(FALSE);
  start_color();
  init_pair(1, COLOR_WHITE, COLOR_BLACK);
  attron(COLOR_PAIR(1));
  /** (READS AND PRINTS THE FILE HERE, DESCRIPTION BELOW) **/
  wrefresh(startScreen);
  attroff(COLOR_PAIR(1));
  wgetch(startScreen);
  endwin();
}

The above code reads the content of a file and print a block character if the character is an # in the file. (sort of printing the file character by character) Everything is printed in startScreen, an independent window.

Here's the content of the the file:

##     ##  ######   ######   ######  ######  ##        ########
###   ###    ##    ##    ## ##    ##   ##    ##        ##
#########    ##    ##       ##         ##    ##        ##
## ### ##    ##     ######   ######    ##    ##        ######
##  #  ##    ##          ##       ##   ##    ##        ##
##     ##    ##    ##    ## ##    ##   ##    ##        ##
##     ##  ######   ######   ######  ######  ########  ########


  #####   ######  ##     ## ##     ##   ###    ##   ## ######
 ##   ## ##    ## ###   ### ###   ###  ## ##   ###  ## ##   ##
##       ##    ## #### #### #### #### ##   ##  #### ## ##    ##
##       ##    ## ## ### ## ## ### ## ##    ## ####### ##    ##
##       ##    ## ##  #  ## ##  #  ## ######## ## #### ##    ##
 ##   ## ##    ## ##     ## ##     ## ##    ## ##  ### ##   ##
  #####   ######  ##     ## ##     ## ##    ## ##   ## ######

However, when I run the C code, it gives this:

▒▒     ▒▒  ▒▒▒▒▒▒   ▒▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒▒▒▒▒  ▒▒        ▒
▒▒▒   ▒▒▒    ▒▒    ▒▒    ▒▒ ▒▒    ▒▒   ▒▒    ▒▒        ▒▒
▒    ▒▒    ▒▒               ▒▒         ▒▒    ▒▒        ▒▒
▒▒ ▒▒▒ ▒▒    ▒▒     ▒▒▒▒▒▒   ▒▒▒▒▒▒    ▒▒    ▒▒        ▒▒▒▒▒▒
▒▒  ▒  ▒▒    ▒▒          ▒▒       ▒▒   ▒▒    ▒▒        ▒▒
▒▒     ▒▒    ▒▒    ▒▒    ▒▒ ▒▒    ▒▒   ▒▒    ▒▒        ▒▒
▒▒     ▒▒  ▒▒▒▒▒▒   ▒▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒▒▒▒▒  ▒  ▒


  ▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒     ▒▒ ▒▒     ▒▒   ▒▒▒    ▒▒   ▒▒ ▒▒▒▒▒▒
 ▒▒   ▒▒ ▒▒    ▒▒ ▒▒▒   ▒▒▒ ▒▒▒   ▒▒▒  ▒▒ ▒▒   ▒▒▒  ▒▒ ▒▒   ▒▒
▒▒       ▒▒    ▒▒ ▒▒▒▒ ▒▒▒▒ ▒▒▒▒ ▒▒▒▒ ▒▒   ▒▒  ▒▒▒▒ ▒▒ ▒▒    ▒▒
▒▒       ▒▒    ▒▒ ▒▒ ▒▒▒ ▒▒ ▒▒ ▒▒▒ ▒▒ ▒▒    ▒▒ ▒ ▒▒    ▒▒
▒▒       ▒▒    ▒▒ ▒▒  ▒  ▒▒ ▒▒  ▒  ▒▒ ▒ ▒▒ ▒▒▒▒ ▒▒    ▒▒
 ▒▒   ▒▒ ▒▒    ▒▒ ▒▒     ▒▒ ▒▒     ▒▒ ▒▒    ▒▒ ▒▒  ▒▒▒ ▒▒   ▒▒
  ▒▒▒▒▒   ▒▒▒▒▒▒  ▒▒     ▒▒ ▒▒     ▒▒ ▒▒    ▒▒ ▒▒   ▒▒ ▒▒▒▒▒▒

I have completely no idea why are there blocks missing. Why is it behaving like this?

There's another question with a similar cause. However, the characteristic of the problem is different, thus a separate question.


Solution

  • It seems that this problem is unique in Windows Subsystem for Linux, where I first came across this problem.

    The same script works 100% fine in Linux, but not in Windows Subsystem for Linux.

    A workaround to make it work in WSL is to refresh the window after printing each character, wrefresh(startScreen). However, the root cause is still unknown.