cpp-terminal icon indicating copy to clipboard operation
cpp-terminal copied to clipboard

Flickering

Open iganinja opened this issue 1 year ago • 20 comments

Hi

I'm using cpp-terminal to program a small text editor where I want to perform some programming experiments, and also to contribute to the library as a user. I'm glad the cpp-terminal exists, it's easy to "paint" things in a multiplatform way, and I have some stuff already running in Windows, Linux and RPI3. So thanks to all contributors!

My question is, and sorry if this is not the correct place to ask it, what's the supposed behavior of the library related to flickering? I'm using the Window class in order to render the final string, which I print with std::cout and std::flush. I draw the whole terminal, by the way. That is, I get terminal size at the beginning and use it to configure the Window instance.

I'm updating the library frequently (I mean, pulling it from its repository), and yesterday it was flickering like crazy, specially in Windows. Some minutes ago I updated the library again and now it seems it has no flickering at all.

So, I understand that it should not flicker if I use the Window class as I mentioned. Is this correct?

Thanks in advance.

iganinja avatar Apr 12 '23 15:04 iganinja

@iganinja Hello, thanks for your nice words. Some changes has been done related to resizing and maybe this create new issues. Would you mind give some codes to test ? What do you call flickering, something like redrawing (sorry my english is not so good)?

Does this happen when you change the size of your terminal? have you tried to use the new way provided by #234 (in https://github.com/jupyter-xeus/cpp-terminal/blob/master/examples/menu.cpp). The API has changed quite a lot and it is still not fixed; pulling gives you the new features but would need to change your code too, that's why there is not official new version. I'm just proposing some change and @MCWertGaming is the maintainer of the package

flagarde avatar Apr 13 '23 08:04 flagarde

Hi @flagarde, thanks for your answer.

I have this currently:

image

(Yeah, colors are horrible, but they will be configurable 😂 )

I'm waiting to input with Term::Platform::read_raw(), then update things accordingly, create the string with Window::render() and show it with std::cout + std::flush . Wait 100ms and start again.

With flickering I mean that whole screen was blinking, some frames were drawn black, or the screen filling was noticeable. That was happening until I pulled cpp-terminal code from master yesterday, now it does not blink at all, even if I don't do the 100ms waiting. So I guess the problem is solved, but my question was that cpp-terminal being used the way I use it, should not blink/flicker at all, shouldn't it?

When I change the size of the terminal strange artifacts occur and also some blinking, but this doesn't bother me, it's normal even in graphical applications. After I stop resizing everything works perfectly. I didn't try the new way provided in the link you mention, I use the Window class for the moment.

iganinja avatar Apr 13 '23 10:04 iganinja

@iganinja I know they were some bug before the last merge in windows. Please note that key can be NO_KEY so you should add a test case for this. The reason is that read_raw return Event that can be other things than Key. But it is allowed to cast Event to Key and in the case the event is not a key, the key is then NO_KEY.

I recommend you to use the same trick than in menu.cpp in the example. Maybe you don't have to do the wait by yourself. You could use read_event that return only when it received event (key, change screen size ...). read_raw is more an internal function

flagarde avatar Apr 13 '23 10:04 flagarde

@iganinja This is fixed ?

flagarde avatar May 26 '23 11:05 flagarde

Hi! I tested it with my program in Windows: it flickers a little bit in a line. In Linux it seems rock solid, no flickering at all.

iganinja avatar May 26 '23 17:05 iganinja

@iganinja thx for the feedback. We can let this open then.. Not sure if it's a problem from the library of from Window$. Maybe there is some hack we could use I remember some discussion about this.

Are you using cout or Term::terminal<< ?

flagarde avatar May 26 '23 17:05 flagarde

std::cout. Perhaps I have somewhat older version? I didn't touch it in weeks.

iganinja avatar May 26 '23 20:05 iganinja

@iganinja nice Turbo Pascal style menus. :) It would be nice to have them as widgets in cpp-terminal eventually.

certik avatar May 28 '23 04:05 certik

I noticed flickering too in my application as you described on Windows but not on Linux. I figured it was probably an issue with the frame rate of the console or when the console triggers a new redraw of the screen. I guessed that the Windows console maybe has a higher frame rate so it is more likely to trigger a redraw just after the ClearScreen command. So I got rid of ClearScreen and rewrote the rendering of my screen elements so that they do not need a clear screen before printing.

Maybe that helps

TobiasWallner avatar Jul 03 '23 09:07 TobiasWallner

I wonder if it's not the contrary the console in windows is poorly optimized. Are you using Term::terminal or cout. I heard somewhere cout is not buffered or things like this in windows or a very small buffer

flagarde avatar Jul 03 '23 15:07 flagarde

I notized it with both. but part of my change from not using ClearScreen anymore also involves of just rendering and printing the few characters that actually change instead of re-rendering and drawing the whole screen. So maybe because my newer solution needs less memory or buffer space as you suggest fixed it.

TobiasWallner avatar Jul 04 '23 16:07 TobiasWallner

@TobiasWallner Your solution is general or a bit hacky? If it's a general one I think this could be incorporated into this library as it would be a very nice addition 😀

flagarde avatar Jul 04 '23 16:07 flagarde

it is general to my application but probably specific in terms of your library.

It is only applies to the design elemts of my projet like grids, labels, editable text fields, comman lines and the like.

The idea is that each element has functions that change something, like inserting a character or moveing the cursor. They will do that + give you a render of the changes which is just a move to the one character and the new value.

TobiasWallner avatar Jul 04 '23 17:07 TobiasWallner

I see, so it seems the problem is the clearscreen ?

flagarde avatar Jul 04 '23 17:07 flagarde

yes I think so

TobiasWallner avatar Jul 05 '23 12:07 TobiasWallner

@TobiasWallner I'm trying to implement a more system base API to wrtite to console. I have been told the C C++ Windows is quite buggy and not so efficient sometimes. I already did a first step with file.hpp but need to improve it. Maybe this will improve performance. I have seen an other way to improve performance I will do a PR for this

I didn't benchmark as i'm doind on virtualbox so the results it's maybe not what one should expect but I can see it by eyes so lets implement and check on other system

flagarde avatar Jul 07 '23 13:07 flagarde

I could run a benchmark natively if you want me too.

TobiasWallner avatar Jul 08 '23 21:07 TobiasWallner

@TobiasWallner Would be great or to have a program with benching on this repo :)

flagarde avatar Jul 08 '23 21:07 flagarde

Your solution is general or a bit hacky? If it's a general one I think this could be incorporated into this library as it would be a very nice addition

I was wondering, you do have a class that stores one frame, the window class, right? Could we calculate the difference of of two windows? - which is then a map of all things that changed? and then just print that to reduce the ammount of data that gets printed every time?

could look like this:

newFrame = render();
diff = newFrame - prevFrame;
Term::cout << diff;
prevFrame = newFrame;

TobiasWallner avatar Jul 22 '23 18:07 TobiasWallner

We already do the diff, although the diff is quite "primitive": https://github.com/jupyter-xeus/cpp-terminal/blob/a40326cfdacd59971a9b59163868bd456f2f8c81/cpp-terminal/window.cpp#L199, we need to minimize the change needed to output, so that it flickers less.

However, the demos that we have don't seem to flicker for me.

certik avatar Jul 22 '23 18:07 certik