Wednesday, January 2, 2013

Printing text in different ways in the screen using ncurses programs

Since I have published a more detailed post about ncurses programs, installations and also a simple program in the previous program, I'll be explaining more examples increasing their complexity as well.

printing plain text on screen with known positions

I think you already have an idea on how to print a text on screen. Now my target is to print the same text  on screen but thrice one down the other.

the concept is very simple. All the rest of the code to start and end the ncurses program is same but we have to change the positions to print both the lines. if it is one down the other, we should not change the x coordinate but have to change the y coordinate as shown below.

#include<ncurses.h>
int main()
{
        initscr();

        move(12,28);
        printw("FRIEND\n");

move(13,28);
        printw("FRIEND\n");
move(14,28);
        printw("FRIEND\n");

        getch();
        endwin();
        return 0;
}

now compile and run the program to obtain the below output.



















Now you may have understood that every action  in ncurses needs to mention the positions. you have something to print/draw , so you tell the program to move to that position and print them on screen. It's simple, isn't it?

Printing colored text on screen

Now our task is to print some text on two positions of the screen but with different foreground and background colors. observe the following code and try to understand how it is done.

#include<ncurses.h>
int main()
{
initscr();                                                 //initialize the screen
start_color();                                         //start color mode in ncurses

init_pair(1,6,5);                                     //initializing color pairs
init_pair(2,4,2); 

attrset(COLOR_PAIR(1));                     //declaring attribute sets which uses color pair 1
move(15,20);                                            //move the curser to this location
printw("My first coloured text\n");       //print this message

attrset(COLOR_PAIR(2));
move(16,20);
printw("My second coloured text \n");

getch();
endwin();

return 0;
}

observe the output as shown below


















So, basically to use foreground and background colors, we need to,
  • call start_color() method, 
  • call init_pair(<color pair number> , <foreground color number> , <background color number> ) method and initialize the color pairs that you are planning to use in the program. this should contian a unique color pair number, number of the foreground color and the number of the background color respectively.
  • if you want some text to print in the colors that you mentioned in  those color pairs, before moving to the location, mention the appropriate attribute set using attrset(COLOR_PAIR(<color pair number>))
check out the colors and their appropriate numbers in the following chart.
COLOR_BLACK 0 
COLOR_RED 1
COLOR_GREEN 2
COLOR_YELLOW 3 
COLOR_BLUE 4 
COLOR_MAGENTA 5 
COLOR_CYAN 6
COLOR_WHITE 7


Printing three colored box frames on screen using an ncurses program






















I think this code need no much explanations since, if you went through all the previous ncurses programs accordingly, this has nothing complex to be explained  just observe in the source code how I have set the locations and made the boxes as frames not as color filled boxes.


#include<ncurses.h>
int main()
{
int x,y;                                   //initializing x and y coordinate variables

initscr();     
start_color();                        //starting the color mode

init_pair(1,3,6);                    //initializing the color pairs
init_pair(2,7,5);
init_pair(3,2,3);

 //running the for loop to print the horizontal lines of the frame colored cyan
for(x=5;x<10;x=x+4) 

{
for(y=20;y<26;y++)
{
attrset(COLOR_PAIR(1));
move(x,y);
printw("*");
}

}

//running the for loop to print the vertical lines of the frame colored cyan
for(y=20;y<26;y=y+5)
{
for(x=6;x<9;x=x+1)
{
  attrset(COLOR_PAIR(1));
move(x,y);
printw("*");
printw("\n");
}

}
//----------------------end of printing the cyan colored frame --------------------------------
//running the for loop to print the horizontal lines of the frame colored magenta
        for(x=5;x<10;x=x+4)
                {
                for(y=60;y<66;y++)
                        {
                        attrset(COLOR_PAIR(2));
                        move(x,y);
                        printw("*");
                        }

                }
//running the for loop to print the vertical lines of the frame colored magenta
        for(y=60;y<66;y=y+5)
        {
                for(x=6;x<9;x=x+1)
                {
                attrset(COLOR_PAIR(2));
                move(x,y);
                printw("*");
                printw("\n");
                }

        }
//----------------------end of printing the magenta colored frame --------------------------------
//running the for loop to print the horizontal lines of the frame colored yellow
        for(x=15;x<20;x=x+4)
                {
                for(y=40;y<46;y++)
                        {
                        attrset(COLOR_PAIR(3));
                        move(x,y);
                        printw("*");
                        }

                }
//running the for loop to print the vertical lines of the frame colored yellow
        for(y=40;y<46;y=y+5)
        {
                for(x=16;x<19;x=x+1)
                {
                attrset(COLOR_PAIR(3));
                move(x,y);
                printw("*");
                printw("\n");
                }

        }
//----------------------end of printing the yellow colored frame--------------------------------
getch();
endwin();
return 0;
}

I have commented most of the code in the program. All what we are doing is, printing a line by keeping the x value constant and increasing the y values one by one ( so, we can print a horizontal line) . then, i jump four x coordinates (down) and do the same to print an identical parallel line to the first line. That is what we did in the first nested for loop.

Second nested for loop does the same but here the x and y tasks changes. this prints two vertical lines parallel to each other.

Can you modify this code a bit to print three colored boxes(without just the frames) . If you got this right, my task should be simple for you. The output I expect is shown below.




















This is really simple. All what you need is just three nested for loops not six like the above code. you are not going to print the lines separately here but print the entire box at once. OK, I'll do a small help for you. observe the piece of code below.


for(x=5;x<10;x++

{
for(y=20;y<26;y++)
{
attrset(COLOR_PAIR(1));
move(x,y);
printw("*");
}

}

this will simply print you the cyan colored box with the little modification that i have highlighted. I guess you could observe the reason for the change we made for the increment. We don't need to keep space between lines to make it a frame thus we don't need to let x coordinate increment by 4 (jump four positions in x axis) but increase it one by one to fill the box with colored stars.

No comments:

Post a Comment