C++、Ncurses - 难以理解菜单窗口如何工作

问题描述 投票:0回答:0

我的程序有两个版本。一种使用 ncurses,另一种则不使用。不使用ncurses的那个工作得很好,没有什么问题。我想升级我的程序,所以我决定使用 ncurses。问题是我不明白窗口系统是如何工作的。没有像样的文档,教程也不是很有帮助。我要求的是有关 ncurses 中窗口的一般帮助,以及使用我的代码运行此类功能的示例。

我尝试过的:

#include <string>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "ncurses.h"

void start() {
  initscr();
  noecho();
  std::cout << "Hello, word!" << '\n';
  getch();
}

int main() {
  initscr();
  noecho();
  cbreak();

  int yMax;
  int xMax;
  getmaxyx(stdscr, yMax, xMax);

  std::string line;
  std::ifstream file("resources/graphics/logo.txt");
  if(file.is_open()) {
    while(getline(file, line)) {
      std::cout << line << '\n';
    }
  } else {
    std::cout << "logo.txt could not be found! \n";
  }
  
  WINDOW * menuwin = newwin(6, xMax / 2, 10, xMax / 4);
  keypad(menuwin, true);
  box(menuwin, 0, 0);
  refresh();
  wrefresh(menuwin);

  std::string choices[4] = {"Start", "Continue", "Settings", "Controls"};
  int choice;
  int highlight = 0;
  while(1) {
    for(int i = 0; i < 4; ++i) {
      if(i == highlight) {
    wattron(menuwin, A_REVERSE);
      }
      mvwprintw(menuwin, i + 1, 1, choices[i].c_str());
      wattroff(menuwin, A_REVERSE);
    }
    choice = wgetch(menuwin);
    switch(choice) {
    case KEY_UP:
      highlight--;
      if(highlight == -1) {
    highlight = 0;
      }
      break;
    case KEY_DOWN:
      highlight++;
      if(highlight == 4) {
    highlight = 3 ;
      }
      break;
    default:
      break;
    }
    //keycode 10 = enter;
    if(choice == 10) {
      break;
    }
  }

  if(choices[highlight].c_str() == 0 | 1 | 2 | 3) {
    start();
  }
  //printw("%s", choices[highlight].c_str());
  getch();
  endwin();
}

我的期望(图形表示):



        ####
       #     #   ####   #####   #    #  #   ####   #    #  #  #####   ######
       #        #    #  #    #  ##   #  #  #       #    #  #  #    #  #
       #  ####  #    #  #    #  # #  #  #   ####   ######  #  #    #  #####
       #     #  #    #  #####   #  # #  #       #  #    #  #  #####   #
       #     #  #    #  #   #   #   ##  #  #    #  #    #  #  #   #   #
        #####    ####   #    #  #    #  #   ####   #    #  #  #    #  ######
                                                               #    
       #          ##    #####   #   #  #####   #  #    #    #    #    #
       #         #  #   #    #  #  #   #    #  #  ##   #    #    #    #
       #        #    #  #####    ##    #    #  #  # #  #    #    ######
       #        ######  #    #   #     #####   #  #  # #    #    #    #
       #        #    #  #    #  #      #   #   #  #   ##    #    #    #
       #######  #    #  #####  #       #    #  #  #    #  #####  #    #
                  #             #              #             #
                                                #
                                  Start
                                  Continue
                                  Settings
                                  Controls

我得到了什么(图形表示):

Start
Continue
Settings
Controls[]Hello, world!

顺便说一句,[] 代表图形故障。

我有一个 main2.cpp,我尝试在其中添加多个窗口,但发生的情况是它们彼此独立加载。因此,windows1 加载、等待输入并关闭。然后窗口 2 打开。我希望它们都打开,或者最好让这两个功能都在同一个窗口中。

c++ linux terminal ncurses roguelike
© www.soinside.com 2019 - 2024. All rights reserved.