-1

So I'm trying to program a server for my game. I want the server to look beautiful and organised and cross-platfrom too so I decided to you ncurses..

I'm trying to show at the top a loading the classical { '-', '\\', '|', '/' } thing

but the problem when I try to print player connected or data it write on the loading bar and its annoying.

Some picture here gonna explain the situation

What I want

What I want

What it's actually doing

What it's actually doing

and this is my code:

//INCLUDEs
int clients = 0;
char chars[] = { '-', '\\', '|', '/' };
unsigned int i;
void showStatus(char a)
{
    attron(COLOR_PAIR(1));
    mvprintw(2, 101 - 25, "%c\r", a);
    mvprintw(2, 59, "%d\r", clients);
    refresh();
}


    void caller()
    {   
        for (i = 0; ; ++i) {
            showStatus(chars[i % sizeof(chars)]);
            //fflush(stdout);
            std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        }
    }

int main(int argc, char ** argv){
//init everything..
printw("-------------- ~~ BS SERVER PRE-ALPHA (v0.0.1) | Clients: (%d/32) | Running (%c) ~~ ---------------\n\r", 0, '|');
//more code....
std::thread thr(caller);
    while (1) {
//check for connections...
}
thr.join();

    endwin();
}

PS: I tried removing the thread repacing it with for, I want the thread so the char can rotate and the server can keep receiving data in the same time.

Thanks

Community
  • 1
  • 1
Omarito
  • 577
  • 6
  • 22
  • Think about where `showStatus()` leaves the cursor. Since the program is multi-threaded, you need someway to keep the 2 threads from printing to the console at the same and a way to ensure the cursor is where it should be before printing. Also, what's going to happen when you need to start scrolling the window? – 001 Feb 14 '18 at 22:37
  • I was thinking about that too, I tried setxy(); and bunch of other things they all fail I don't know how can i keep it at the end of the text which is in the terminal everytime.. My next idea is to use subwindows what do you think about that (like a window for the status and the other for data output). – Omarito Feb 15 '18 at 07:10
  • no - curses won't handle multithreads. you'll have to rethink how your program is organized. – Thomas Dickey Feb 15 '18 at 09:45

1 Answers1

0

Well, it actually works, my idea, was to draw a window for the status and update it whenver we need to, and output logs in the main window and its working like charm.

Omarito
  • 577
  • 6
  • 22