3

Well, I can pause, the problem is that all my "pauses" are accumulating instead of being sequentially done. I tried many clocks stuff, sleep,.. with each time the same result. Am I missing something here? I just want to do this to help me program with a visual feedback, it won't stay in the code, so anything, even dirty, is welcome!

void Dockable::resize_cells() {
    for (int i = 0; i < cells.size(); i++) {
        for (int j = 0; j < cells[i].size(); j++) {
            if (cells[i][j] != layout) {
                if (cells[i][j]->is.docked) {
                    int found_col = 0;
                    bool found = false;
                    int pos = 0;
                    for (int k = 0; k < cells.size(); k++) {
                        int col = 0;
                        for (int l = 0; l < cells[k].size(); l++) {
                            if (!found) {
                                if (cells[i][j] == cells[k][l]) {
                                    found = true;
                                    pos = col;
                                }
                            }
                            if (!(l - 1 < 0) &&
                                cells[k][l] != cells[k][l - 1]) {
                                col++;
                            }
                            else {
                                col++;
                            }
                            if (found) {
                                if (col > found_col) {
                                    found_col = col;
                                }
                            }
                        }
                    }       
                    cells[i][j]->x_size(layout->x_size() / found_col);
                    cells[i][j]->x((layout->x_size() / found_col) * pos);
                    cells[i][j]->refresh_shape_apply();
                    wait();
                }
            }
        }
    }
}
void Dockable::wait()
{
    clock_t counter;
    clock_t target;
    counter = clock();
    target = clock() + 100;
    while (counter < target) {
        counter = clock();
        std::cout << counter << std::endl;
    }
}

refresh_shape_apply, applies the transformations, since it's executed before the wait, I had hopes that it would work.

Nick Mack
  • 75
  • 6
  • What do you mean by accumulating? Can you write out a descrption of what you want? Something like, *Do stuff. Wait. Do stuff. Wait. Do stuff. Wait. Do stuff.* or *Do stuff. Do stuff. wait. Do stuff. Do stuff.* – user4581301 Dec 24 '19 at 07:00
  • yeah, it's basically wait then do stuff like you said. Right now, whatever I try, the waiting time just adds up instead of doing something, then waiting, then doing something, and waiting again. So right now, instead of watching every cells being resized, I just wait a long time, then they appear resized. But now that I think about it, isn't the renderer also waiting to do it's thing ? Gosh, that might be it :s – Nick Mack Dec 24 '19 at 07:06
  • `Dockable::` let me think that you use some kind of GUI framework (e.g. [tag:qt]). This is something you should urgently [edit] in because this would be an essential fact of your problem. – Scheff's Cat Dec 24 '19 at 07:07
  • 1
    well, Dockable is my thing, but I use sfml, so yeah, I think I found out what was my problem, the render loop is probably freezing aswell during that waiting time – Nick Mack Dec 24 '19 at 07:08
  • 1
    Most GUIs (actually all I know) are driven by some kind of event loop. In this case, any (re-)action of application should return ASAP to this event loop (or run in a separate thread). Waiting in code prevents updating of screen output because the update is performed via call from event loop as well. – Scheff's Cat Dec 24 '19 at 07:08
  • 1
    The solution is to break up your loop and make the loop iteration variables to some kind of "global" state. The code has to be reorganized so that some iterations are done until e.g. some time has been consumed, then leave loop to update output, then re-enter loop to compute further. – Scheff's Cat Dec 24 '19 at 07:11
  • 1
    The other solution could be to embed a nested call of event loop processing in one of the inner loops. That does work in Qt and GTK+ and probably in other GUIs as well. However, it's probably not applicable to your case. Beside of this, (IMHO) it is not as clean design as the above approach. – Scheff's Cat Dec 24 '19 at 07:14
  • Thanks a lot Scheff ! I'm going to do exactly that "then leave loop to update output, then re-enter loop to compute further. " – Nick Mack Dec 24 '19 at 07:42
  • I would upvote you Scheff, but I'm low on rep :/ Sorry! – Nick Mack Dec 24 '19 at 07:48

1 Answers1

-1

You could use this code

system("pause");

This could help pause the code until you press any letter to continue.

mohsen
  • 65
  • 6
  • The asker's problem is not with making the program sleep, wait, pause, or what have you.They already have that figured out. It's with making the GUI show the results in time-delayed steps. If you pause the whole program you also pause the rendering of the output, and this won't give the asker what they want. – user4581301 Dec 24 '19 at 22:39
  • Sidenote: this is a brutal way to pause a program. It literally starts another program that waits for a keypress and blocks the original process until the program exits. – user4581301 Dec 24 '19 at 22:42