I'm writing a game where I create a map using ncurses. What I want to try and do(if possible) is make the map the size of the terminal, in other words in the main program I have:

// Create a game on an 80x30 map
  Game gameInstance( 80, 30 ) ;
  //run game
  gameInstance.run() ;

But what I want to try and do is detect the size of the terminal window the user already has. Then pass those x and y values to the gameInstance() function.

Since this is a non-native c++ thing, I would recommend using system() to call the appropriate system command to get the terminal size (I don't know what it is, but I'm sure one exists). Include in the command writing this result to a file. Then read the file in your c++ program.

In bash it is:

system("echo $COLUMNS $LINES > file.txt");

Hope this helps.


I got it to work using this code which basically does what you said.

#include <iostream>
#include <stdlib.h>
#include <ncurses.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include "game.h"

using namespace std;

int main() {

  struct winsize ws;

  if( ioctl( 0, TIOCGWINSZ, &ws ) != 0 ){
    fprintf(stderr, "TIOCGWINSZ:%s\n", strerror(errno));
  // Create a game on a map with  current terminal size
  Game gameInstance( ws.ws_col, ws.ws_row ) ;

  gameInstance.run() ;

  return 0 ;

This question has already been answered. Start a new discussion instead.