Ncurses window artifacts

  artifacts, c++, pdcurses, windows

I have recently asked a question about a couple of problems (now solved) that I had with the code below. The program itself is simple, there is a character that moves in a boxed window according to the user’s input captured with the wgetch() function inside the game loop.
Going ahead in the coding though I discovered a new issue:
the window that i create (which I called stage) has to be as big as the screen (stdscr) otherwise the movement of the character will leave "trails" recreating the problems mentioned here Problems with curses C++.
Therefore if I execute the createstage() function setting the window to be smaller than the screen (which is the way it’s supposed to work) there will be these graphic artifacts.
Is there a way I can fix this without having to draw/implement the box/window by myself or working with the stdscr only?

#include "curses.h"
#include <windows.h>

void disableselection(){
  HANDLE hInput;
  DWORD prev_mode;
  hInput = GetStdHandle(STD_INPUT_HANDLE);
  GetConsoleMode(hInput, &prev_mode);
  SetConsoleMode(hInput, prev_mode & ENABLE_EXTENDED_FLAGS);
}

void disableresizing(){
  HWND consoleWindow = GetConsoleWindow();
  SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) &   ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
 }

 void initialize(){
  initscr();
  curs_set(0);
  disableselection();
  disableresizing();
  noecho();
 }

 WINDOW* createstage(int starty=0, int startx=0, int height=LINES, int width=COLS){
  WINDOW* win=newwin(height-starty,width-startx,starty,startx);
  nodelay(win,TRUE);
  refresh();
  return win;
}

void end(WINDOW* stage){
  clear();
  refresh();
  nodelay(stdscr,FALSE);
  printw("press something to exit");
  getch();
  endwin();
}

int main(){
  initialize();
  WINDOW* stage= createstage();
  int ywindow,xwindow,heightwindow,widthwindow;
  getbegyx(stage,ywindow,xwindow);
  getmaxyx(stage,heightwindow,widthwindow);
  heightwindow--;
  widthwindow--;
  int x = xwindow+1, y = ywindow+1;
  char c = '@';
  while(TRUE){
      box(stage,0,0);
      wmove(stage,y,x);
      waddch(stage,'@');
      c=wgetch(stage);
      if(c=='w' && y-1>ywindow){
        y--;
      }
      if (c=='s' && y+1<heightwindow){
        y++;
      }
      if(c=='d' && x+1<widthwindow){
        x++;
      }
      if(c=='a' && x-1>xwindow){
        x--;
      }
      wrefresh(stage);
      napms(16);
      wclear(stage);

  }
  end(stage);
}

Using C++ and PDCurses3.8 on Windows

Source: Windows Questions

LEAVE A COMMENT