首页 > 解决方案 > ncurses库我想知道为什么不能绘制颜色

问题描述

[我是使用谷歌翻译的日本人]

演示代码是void Renderer ();函数部分,为什么我不能绘制颜色?我不知道原因。

此外,该addch ();功能可以正常绘制颜色。

操作系统: Ubuntu

参考站点A: https ://www.linuxjournal.com/content/programming-color-ncurses

参考站点B: https ://www.linuxjournal.com/content/about-ncurses-colors-0

屏幕.cpp

#include "../header/Screen.hpp"


#include "../header/Character.hpp"
#include "../header/Color.hpp"
#include "../header/Vector.hpp"
#include "../header/Screen.hpp"



// ######################## コンストラクタ ######################## 
Screen::Screen()
{

      //ウインドウ初期化
    getmaxyx(stdscr,windowSize.y,windowSize.x);
    window = newpad(windowSize.y,windowSize.x);
    start_color();              //カラーを有効化

    prefresh(window,0,0,0,0,windowSize.y,windowSize.x);


      size.x = windowSize.x;
      size.y = windowSize.y;
      maxSize = size.x * size.y;

      stage = std::make_unique<std::vector<Character>>(size.x * size.y);
      
      for(std::vector<Character>::iterator itr = stage->begin(); itr != stage->end(); itr++)
      {
            itr->chr = ' ';
            itr->color = Color::NONE;
            itr->type = 0;
      }
}



// ######################## 画面サイズ更新 ########################
void Screen::UpdateScreen()
{
      //画面サイズを取得
      getmaxyx(stdscr,windowSize.y,windowSize.x);
      
      //前のウインドウサイズをより大きければ要素を代入
      if((windowSize.x * windowSize.y) > maxSize) 
      {
            maxSize = windowSize.x * windowSize.y;
            size.x = windowSize.x;
            size.y = windowSize.y;

            for(int i = 0; i< (windowSize.x * windowSize.y); i++)
            {
                  stage->emplace_back(Character{Color::NONE,' ',0});
            }
      }
}



// ######################## Update ########################
void Screen::Update()
{
      UpdateScreen();   //画面サイズを更新
}

// ######################## 文字設定 ######################## 
void Screen::Input(int x,int y,Character c)
{
      stage->at((y * size.x) + x) = c;
}

// ######################## 文字削除 ######################## 
void Screen::Delete(int x,int y)
{
      stage->at((y * size.x) + x).chr = ' ';
      stage->at((y * size.x) + x).color = Color::NONE;
      stage->at((y * size.x) + x).type = 0;
}

// ######################## Renderer ######################## 
void Screen::Renderer()const
{

      for(int y = 0; y < size.y; y++)
      {
            for(int x = 0; x < size.x; x++)
            {
//              attron(COLOR_PAIR(stage->at((y * size.x) + x).color));
                attron(COLOR_PAIR(16));

                  mvwaddch(window,y,x,stage->at((y * size.x) + x).chr);
                  //addch(stage->at((y * size.x) + x).chr);
                //attrset(COLOR_PAIR(16));

                //attroff(stage->at((y * size.x) + x).type);
//              attroff(COLOR_PAIR(stage->at((y * size.x) + x).color));
            }
      }

    prefresh(window,0,0,0,0,windowSize.y,windowSize.x);      
}

// ######################## デストラクタ ######################## 
Screen::~Screen()
{


}

标签: c++

解决方案


wattron(window,COLOR_PAIR(stage->at((y * size.x) + 
wattroff(window,COLOR_PAIR(stage->at((y * size.x) + 

在使用 windows 时,必须使用wattron()andwattroff()函数而不是attron()andattroff()函数。

对于for语句,疑问句的注释部分内的代码需要进行如下修改。

    for(int y = 0; y < size.y; y++)
      {
            for(int x = 0; x < size.x; x++)
            {
                  wattron(window,COLOR_PAIR(stage->at((y * size.x) + x).color));

                  mvwaddch(window,y,x,stage->at((y * size.x) + x).chr);
                  
                  wattroff(window,COLOR_PAIR(stage->at((y * size.x) + x).color));

            }
      }


推荐阅读