为什么颜色在ncurses中不起作用?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么颜色在ncurses中不起作用?相关的知识,希望对你有一定的参考价值。
我几乎完成了我的项目,但我不能改变任何颜色的窗户或文字。
我尝试了attron
和attroff
,但它不起作用。全部编译,但没有任何反应,仍然是默认颜色。 Wattron也不起作用。
#include "stdio.h"
#include "ncurses.h"
#include "math.h"
#include "unistd.h"
#include "stdlib.h"
double PI = 3.15927;
bool drawObject(WINDOW *win, int **obj, int size, int y, int x);
bool clearObject(WINDOW *win, int **obj, int size, int y, int x);
bool drawPoint(WINDOW *win, int y, int x, char c);
bool drawPoint(WINDOW *win, int y, int x, char c)
{
bool r = mvwinch(win, y, x) != 0x20;
mvwaddch(win, y, x, c);
return r;
}
bool drawObject(WINDOW *win, int **obj, int size, int y, int x)
{
int k;
bool r = false;
for (k = 0; k < size; ++k)
{
r |= drawPoint(win, y + obj[k][0], x + obj[k][1], (char)obj[k][2]);
}
wrefresh(win);
return r;
}
bool clearObject(WINDOW *win, int **obj, int size, int y, int x)
{
int k;
for (k = 0; k < size; ++k)
{
mvwaddch(win, y + obj[k][0], x + obj[k][1], ' ');
}
wrefresh(win);
}
int main(int argc, char **argv)
{
int k = 0;
WINDOW *win, *status;
int ox = 40, oy = 20;
bool isCrash = false;
int obj1_size = 6;
int **obj1_def = (int**)malloc(sizeof(int*) * obj1_size);
for (k = 0; k < obj1_size; ++k)
{
obj1_def[k] = (int*)malloc(sizeof(int) * 3);
}
obj1_def[0][0] = 0;
obj1_def[0][1] = 1;
obj1_def[0][2] = (int)'\';
obj1_def[1][0] = 0;
obj1_def[1][1] = -1;
obj1_def[1][2] = (int)'/';
obj1_def[2][0] = 1;
obj1_def[2][1] = 0;
obj1_def[2][2] = (int)'W';
obj1_def[3][0] = 0;
obj1_def[3][1] = 0;
obj1_def[3][2] = (int)'H';
obj1_def[4][0] = -1;
obj1_def[4][1] = 0;
obj1_def[4][2] = (int)'H';
obj1_def[5][0] = -2;
obj1_def[5][1] = 0;
obj1_def[5][2] = (int)'A';
double obj1_step = PI / 50;
double obj1_t = 1.5;
int obj1_a = 4;
int obj1_x = 0, obj1_y = 0;
double obj2_step = PI / 20;
double obj2_t = 0;
int obj2_a = 3;
int obj2_b = 10;
int obj2_x = 0, obj2_y = 0;
initscr();
refresh();
startcolor();
init_pair(1, COLOR_RED, COLOR_BLACK);
win = newwin(40, 80, 0, 0);
status = newwin(2, 80, 40, 0);
wrefresh(win);
wrefresh(status);
while (true)
{
//1 object
clearObject(win, obj1_def, obj1_size, obj1_y, obj1_x);
obj1_x =(int)(3 * obj1_t * sin(obj1_t * 0.5) * cos(obj1_t));
obj1_y =(int)(2 * obj1_t * cos(obj1_t * 1.5) * cos(obj1_t));
obj1_x += 50;
obj1_y += 20;
if (obj1_x < 1)
{
obj1_x = 1;
}
if (obj1_x > 78)
{
obj1_x = 78;
}
if (obj1_y < 2)
{
obj1_y = 2;
}
if (obj1_y > 38)
{
obj1_y = 38;
}
isCrash = drawObject(win, obj1_def, obj1_size, obj1_y, obj1_x);
if (isCrash == true)
{
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
mvwprintw(status, 1, 0, "Crash !!!");
break;
}
obj1_t += obj1_step;
if (obj1_t > 17.215)
{
obj1_t = 1.5;
}
//2nd object
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
mvwaddch(win, obj2_y, obj2_x+1, ' ');
mvwaddch(win, obj2_y, obj2_x-1, ' ');
mvwaddch(win, obj2_y-1, obj2_x, ' ');
mvwaddch(win, obj2_y-1, obj2_x-1, ' ');
attroff(COLOR_PAIR(1));
wrefresh(win);
obj2_x =(int)(obj2_a * obj2_t - obj2_b * sin(obj1_t));
obj2_y =(int)(obj2_a - obj2_b * cos(obj1_t));
obj2_y += 20;
if (obj2_x < 1)
{
obj2_x = 1;
}
if (obj2_x > 78)
{
obj2_x = 78;
}
if (obj2_y < 2)
{
obj2_y = 2;
}
if (obj2_y > 38)
{
obj2_y = 38;
}
isCrash = mvwinch(win, obj2_y, obj2_x+1) != 0x20 ||
mvwinch(win, obj2_y, obj2_x-1) != 0x20 ||
mvwinch(win, obj2_y, obj2_x) != 0x20 ||
mvwinch(win, obj2_y-1, obj2_x) != 0x20 ||
mvwinch(win, obj2_y-1, obj2_x-1) != 0x20;
mvwaddch(win, obj2_y, obj2_x+1, '-');
mvwaddch(win, obj2_y, obj2_x-1, '-');
mvwaddch(win, obj2_y, obj2_x, '-');
mvwaddch(win, obj2_y-1, obj2_x, '\');
mvwaddch(win, obj2_y-1, obj2_x-1, '\');
wrefresh(win);
if (isCrash == true)
{
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
mvwprintw(status, 1, 0, "Crash !!!");
break;
}
obj2_t += obj2_step;
if (obj2_t > 30)
{
obj2_t = 0;
}
wmove(win,0,0);
mvwprintw(status, 0, 0, "[%d, %d] [%d, %d]", obj1_x, obj1_y, obj2_x, obj2_y);
wrefresh(win);
wrefresh(status);
usleep(100000);
}
wrefresh(status);
getch();
delwin(win);
delwin(status);
endwin();
for (k = 0; k < obj1_size; ++k)
{
free(obj1_def[k]);
}
free(obj1_def);
return 0;
}
答案
在这块
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
你在stdscr
中打开了color-pair-1,但是在win
(一个不同的窗口)中添加了字符。 attron
仅影响您正在设置属性的窗口。这可能是你看到的问题。
在另一个地方,你正在做getch
(对于stdscr
),它可以干扰win
的更新,但似乎你已经添加了wrefresh
来弥补这一点。
以上是关于为什么颜色在ncurses中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
onRequestPermissionsResult 在片段中不起作用