Search code examples
cterminalncurses

ncurses can't display element on x y position from structure


I'm trying to display an robot with ncurses. When x & y are printed with printf all is ok, but with mvwprintw(window, x, y, "a"); nothing is displaying. What is the problem?

File: view.

#include <ncurses.h>
#include "config.c"
#include "arena.c"

void create_window(){
    robot robots[NUMBER_ROBOTS];

    create_robots(robots);
    initscr();
    raw();
    noecho();

    WINDOW * window = newwin(TERM_HEIGHT, TERM_WIDTH, 0, 0);

    refresh();

    box(window, 0,0); 

    for (size_t i = 0; i < NUMBER_ROBOTS; i++)
    {   
        //to_string(robots[i]);
        int x = get_posx(robots[i]);
        int y = get_posy(robots[i]);

        printf("X%d Y%d\n", x, y);

        wrefresh(window);
        mvwprintw(window, x, y, "a");

    }
    wrefresh(window);
    getch(); 
    endwin();
}

void launch(int argc, char const *argv[])
{   

    create_window(); 

}

File robot.h


typedef struct robot{
  int state;
  char id[1];
  double posX,posY;
  double posXo,posYo;
  int speed;
  int life;
  missile missiles[2];  
} robot ;


File arena.c


int get_posx(robot r){
    return (r.posX*TERM_WIDTH)/WIDTH;
}

int get_posy(robot r){
    return (r.posY*TERM_HEIGHT)/HEIGHT;
}

Screen of the execution: enter image description here There are any "a" displayed.


Solution

  • There's a few possibilities:

    • the call to mvwprintw interchanges the x/y coordinates (so it will take that rather large x value and make that a row-number, losing any text written when it goes out of range)
    • the screenshot doesn't show the bottom of the box (perhaps that TERM_HEIGHT is incorrect, aggravating the problem with the coordinates)
    • the printf gets in the way, confusing curses about where the cursor really is
    • the getch should be a wgetch(window), just in case (I don't see any) there were pending updates to stdscr.