我正在用ncurses製作一個基本的終端應用程序引擎。我已經做了一個基本的窗口課程,它存儲了你正在使用的窗口,並允許你寫它。現在,我正在做一個ColorWindow類,它繼承自BasicWindow,並允許您使用顏色進行書寫。每抽象,我initilizing我需要的類的方法中的一種顏色對,這裏是示例代碼:ncurses中的顏色對在類中無法正常工作
編輯:我這裏寫的所有代碼
#include <ncurses.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
class ColorWindow : public BasicWindow {
private:
int npairs;
bool canColor;
void initializePairs() {
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
npairs = 8;
}
public:
ColorWindow() {
if(has_colors()) {
initializePairs();
this->canColor = true;
}
else {
this->canColor = false;
}
}
ColorWindow(int he, int wi, int stx, int sty) : BasicWindow(he, wi, stx, sty) {
if(has_colors()) {
initializePairs();
this->canColor = true;
}
else {
this->canColor = false;
}
}
int addColorPair(int foreground, int background) {
if(foreground < 0 || foreground > 7
|| background < 0 || background > 7)
return -1;
init_pair(npairs, foreground, background);
npairs++;
return npairs-1;
}
bool ColorWindow::writeStringWithColor(int x, int y,const char* message, int pair) {
if(!isInLimits(x, y))
return false;
if(pair >= npairs)
return false;
if(canColor)
return false;
wattron(getContainer(), COLOR_PAIR(pair));
mvwprintw(getContainer(), x, y, message);
wattroff(getContainer(), COLOR_PAIR(7));
}
bool changeColor(int color, int r, int g, int b) {
if(!can_change_color())
return false;
if(color < 0 || color > 7 || r < 0 || r > 1000
|| g < 0 || g > 1000 || b < 0 || b > 1000)
return false;
init_color(color, r, g, b);
return true;
}
};
class BasicWindow {
private:
WINDOW* container;
int startx, starty, height, width;
public:
BasicWindow() {
this->height = 20;
this->width = 84;
this->starty = (LINES - height)/2; /* Calculating for a center placement */
this->startx = (COLS - width)/2;
}
BasicWindow(int he, int wi, int stx, int sty) {
this->height = he;
this->width = wi;
this->starty = sty;
this->startx = stx;
}
WINDOW* createNewContainer() {
this->container = newwin(height, width, starty, startx);
wrefresh(this->container); /* Show that box */
return getContainer();
}
WINDOW* getContainer() {
return this->container;
}
bool writeString(int x, int y, const char* message) {
if(!isInLimits(x, y))
return false;
mvwprintw(this->container, x, y, message);
}
void refreshContainer() {
refresh();
wrefresh(this->container); /* Show that box */
}
void destroyWindow() {
wborder(this->container, ' ', ' ', ' ',' ',' ',' ',' ',' ');
wrefresh(this->container);
delwin(this->container);
}
bool isInLimits(int x, int y) {
if(x < 0 || x >= this->width-1) {
return false;
}
if(y < 0 || y >= this->height-1) {
return false;
}
return true;
};
這裏是主要:
#include <iostream>
#include <unistd.h>
#include <ncurses.h>
#include "windows.h"
int main() {
ColorWindow cw = ColorWindow(20, 80, 0, 0);
initscr(); /* Start curses mode */
cbreak(); /* Line buffering disabled, Pass on
* everty thing to me */
keypad(stdscr, TRUE);
start_color();
WINDOW* container;
container = cw.createNewContainer();
cw.writeStringWithColor(0, 10, "Hello everyone in color!!", 2);
cw.refreshContainer();
sleep(2);
endwin();
return 0;
}
它初始化顏色模式,但什麼也沒有顯示,就好像選擇的對是BLACK BLACK一樣。
如果任何人都可以給我一個關於我做錯了什麼的暗示,我會對它進行補充。
該示例仍然不能編譯。 –
最後,我已將所有代碼和makefile上傳到此github回購。 https://github.com/Shirkamdev/physic_simulator – Shirkam