GreenLights
GreenLights

Reputation: 41

NCurses "mvwaddch" Clarification Needed

I have been wrestling with the nuances of ncurses windows all day and I've finally been stumped. I have an array map[x][y] filled with random integers. The idea is to print the array into the window using the for loop. I've changed the location of everything in the while loop many times.

To test whether mvwaddch was working I added a test line of code: mvwaddch(main_window,10,10,'B');

This causes 'B' to display in the window at (10,10). But only after pressing a key. Why is this?

Since this works, I have become completely baffled as to why my map isn't printing. How can I print my map?

EDIT: FULL CODE

#include <ncurses.h>
#include <stdlib.h>
#include <math.h>

int main() {

    // Curses Initialization
    initscr();
    cbreak();
    noecho();
    keypad(stdscr,TRUE);

    // Main Variables
    int inpt;
    int playing = true;

    // Map Variables
    srand(1);
    int mapWidth = 80;
    int mapHeight = 20;
    int map[mapWidth][mapHeight];

    // Window Initialization
    WINDOW *main_window;
    int wheight = LINES;
    int wwidth = COLS;
    main_window = newwin(wheight,wwidth,0,0);
    wborder(main_window, '|', '|', '-', '-', '*', '*', '*', '*');

    // Build Map
    for(int x=0; x>=mapWidth; x++){
        for(int y=0; y>=mapHeight; y++){
            map[mapWidth][mapHeight] = floor(rand() % 11 + 1);
        }
    }

**END EDIT**


    while(playing) {
        inpt = wgetch(main_window);
        wrefresh(main_window);
    
        mvwaddch(main_window,10,10,'B');    
    
        // Draw Map
        for(int y=1; y>=mapHeight-1; y++){
            for(int x=1; x>=mapWidth-1; x++){
                mvwaddch(main_window,x,y,map[x][y]);
            }   
        }   

       if (inpt == 'q'){
        endwin();
        playing = false;
       }   
       if (inpt == KEY_RESIZE){
        wclear(main_window);
        wborder(main_window, '|', '|', '-', '-', '*', '*', '*', '*');    
        wrefresh(main_window);
       }

    }

As a sidenote, the window border is working well, with the border resizing to the screen properly. This was another tricky method to iron out if anyone ever is searching.

Upvotes: 1

Views: 228

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54525

Ignoring the rest of the loop, this part

inpt = wgetch(main_window);
wrefresh(main_window);

mvwaddch(main_window,10,10,'B'); 

does a refresh as part of wgetch, then an unnecessary refresh, and then adds a character — which will be painted on the next refresh (i.e., when it loops back to wgetch).

Upvotes: 2

Related Questions