0

I have three different classes. Each class is responsible for drawing a specific thing using ncurses.

I must draw all the three things at once. One of the classes is responsible for a board, and the other two classes draws something inside the board.

I got it to work, but the problem is that whenever I use clear, it clears the board and the other two things; I want the board to stay and never get erased. I want to clear the drawing that only the specific class is responsible for.

For example, let's say I have a board, and I have a person class and a dog class. When I call the draw method in the person class, it draws me the person inside the board, but whenever I move it to a different point, it draws a new person, but it never clears the old person.

Same thing with the dog unless I use the clear method from the curses.h but it erases and clears everything including the board and the dog.

However, I only want to erase the person and not everything. Is there any built-in method to use from ncurses other than clear or erase, or anything that clears the whole screen?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
mySelf
  • 3
  • 2

2 Answers2

0

As Ivan Rubinson suggested in the comments, the usual approach is to clear everything then redraw everything every frame/repaint.

According to the documentation, clear() should clear the whole screen:

The clear(), erase(), wclear() and werase() functions clear every position in the current or specified window.

The clear() and wclear() functions also achieve the same effect as calling clearok(), so that the window is cleared completely on the next call to wrefresh() for the window and is redrawn in its entirety.


Your main loop / drawing code should look something like this (pseudocode):

clear(); // clear everything

// redraw everything
for(auto& widget : drawables) 
{
    widget.draw();
}

// display
refresh();
Community
  • 1
  • 1
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
0

You can do this by creating a window for each of the objects, and removing them when they're done. That way, you need only clear the window that you're removing, refresh it and then delete it.

That is, use newwin, waddstr, wrefresh, wclear and delwin rather than addstr, refresh and clear.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105