I almost finished my project, but I can't change any colours of windows or text.
I tried attron
and attroff
but it doesn't work. All is compiled, but nothing happened, still default colours. Wattron doesn't work either.
#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;
}
In this chunk
attron(COLOR_PAIR(1));
mvwaddch(win, obj2_y, obj2_x, ' ');
you've turned color-pair-1 on in stdscr
, but are adding characters to win
(a different window). The attron
only affects the window you're setting the attributes on. That's likely the problem you're seeing.
In another place, you're doing getch
(for stdscr
), which can interfere with refresh's for win
, but it seems that you've added wrefresh
's to compensate for that.