How do I print a matrix in a window using ncurses?

69 views Asked by At

My program reads through a named pipe a two-dimensional matrix that contains a maze and my objective is for it to be printed in a top window using ncurses, but it is not working.

Note: The maze is printed in the console but when I use ncurses it doesn't seem to work.

Code:

// (janelaTopo -> TOP WINDOW)
// (janelaBaixo -> BOTTOM WINDOW)

void writeMap(WINDOW *janela, int tipo)
{

    // quando temos o scroll ativo, não deveremos ter a borda desenhada na janela para não termos o problema escrever em cima das bordas
    if (tipo == 1)
    {
        // scrollok(janela, TRUE); // liga o scroll na "janela".
        // wprintw(janela, "\n #> ");
    }
    else
    {
        keypad(janela, TRUE);
        werase(janela);
        wclear(janela);
        wborder(janela, '|', '|', '-', '-', '+', '+', '+', '+');
    }
    refresh();
    wrefresh(janela);
}

int main(int argc, char *argv[])
{

    if (argc != 2)
    {
        fprintf(stderr, "USo: %s <Nome do Jogador>\n", argv[0]); // argv[0] = ./JOGOUI
        exit(EXIT_FAILURE);
    }

    if (access("cliente_servidor", F_OK) != 0)
    {
        printf("[ERRO] O 'motor' nao esta a funcionar!\n");
        exit(1);
    }

    initscr();
    raw();
    noecho();
    keypad(stdscr, TRUE);
    attrset(A_DIM);

    // mvprintw(1, 1, "Bem vindo");
    // mvprintw(2, 10, "[ Up,Down,Right e Left comandos janela de cima ]");
    // mvprintw(3, 10, "[ space - muda para o foco da janela de baixo / q - sair ]");

    WINDOW *janelaTopo = newwin(30, 82, 1, 1);
    WINDOW *janelaBaixo = newwin(15, 82, 33, 1);

    desenhaMapa(janelaTopo, 2);
    wprintw(janelaBaixo, "Linha de Comandos\n");
    desenhaMapa(janelaBaixo, 1);
    trataTeclado(janelaTopo, janelaBaixo);
    wrefresh(janelaTopo);
    delwin(janelaTopo);
    wclear(janelaBaixo);
    wrefresh(janelaBaixo);
    delwin(janelaBaixo);

    // ABRIR NAMED PIPES
    //--------------------------ENVIA INFORMACAO AO MOTOR--------------------------//

    // Abertura do File descriptor para escrita do nome do jogador
    int fd_cliente = open("cliente_servidor", O_WRONLY);

    char nome_jogador[TAMANHO_NOME];
    strncpy(nome_jogador, argv[1], sizeof(nome_jogador));

    // Tente abrir o FIFO para leitura várias vezes até ter sucesso

    if (write(fd_cliente, &nome_jogador, sizeof(nome_jogador)) == -1)
    {
        perror("Erro ao escrever no FIFO no JOGOUI");
        return 1; // Ou tome a ação apropriada
    }
    close(fd_cliente);

    // Abertura do File descriptor para a leitura de mensagens
    int fd_servidor = open("servidor_cliente", O_RDONLY);

    if (fd_servidor == -1)
    {
        perror("Erro ao abrir o FIFO servidor_cliente no JOGOUI");
        return 2;
    }

    // Configuração do timeout
    fd_set set;
    struct timeval timeout;
    FD_ZERO(&set);
    FD_SET(fd_servidor, &set);
    timeout.tv_sec = 5; // 5 segundos de timeout
    timeout.tv_usec = 0;

    // Espera por dados ou timeout
    int result = select(fd_servidor + 1, &set, NULL, NULL, &timeout);

    if (result == -1)
    {
        perror("Erro durante a chamada do select");
        close(fd_servidor);
        return 3;
    }
    else if (result == 0)
    {
        // Timeout ocorreu
        printf("Timeout - Não foram recebidos dados do servidor\n");
    }
    else
    {
        // Receber a matriz do labirinto do servidor
        char linhaRecebida[NCOL];

        // Lê cada linha do labirinto do FIFO no JOGOUI
        while (read(fd_servidor, linhaRecebida, sizeof(linhaRecebida)) > 0)
        {
            // Imprime a linha na janela
            // desenhaMapa(janelaTopo, 2);
            wprintw(janelaTopo, "%s", linhaRecebida);
            wrefresh(janelaTopo);

            //printf("%s", linhaRecebida);
        }
    }

    close(fd_servidor);
    endwin();

    return 0;
}

I expected the maze to be printed in the upper window.

0

There are 0 answers