|
|
|
|
|
Draw Boxes on the Screen in CProgramming How to Use Conio and the Curses Library in C to Draw a Rectangle on the Screen.
An interesting little project showing how to draw a box on the screen in C, in two approaches using first the conio library under DOS, and then Unix curses.
This interesting little project is a must for anyone working on text interfaces, and is a great learning tool for several key programming concepts. We shall be touching on the following computer programming areas:
In addition, we shall address porting issues, namely porting a standard C program from the DOS environment (Windows command line) to the Unix environment, and getting the same results. This last is important, and not as straightforward a it might at first seem. Statement of The ProblemIn a menu based character only environment, we might like to draw certain geometric objects. A rectangle, for instance, representing a border around the menu screen. There are several ways to achieve this, depending on what the end result should be. We could, for example, simply create a counted loop that prints successive lines on the screen, using the standard C formatted printing function, printf. However, this would mean that while we would be able to draw a box, this would be all we could draw. What we want is a way to draw the box without disturbing the other characters on the screen. To do this, we need to perform two specific steps:
This is accomplished in different ways, depending upon the platform. We shall concern ourselves with command line platforms in this article : namely DOS (MS-DOS, DR-DOS etc.) and Unix variants. For DOS platforms, we need to use the conio.h header file (console input and output), while for Unix and other standard ANSI environments, we use the curses library, available as the ncurses.h or curses.h header file. The Box Program DesignWe shall design a simple program, containing a single user defined function 'DrawBox' which will take as parameters:
The dimensions of the box shall be passed in from the command line: drawbox draw_character top_x top_y width height In the above, 'drawbox' is the name of the executable built when we compile the program. This may differ depending on your compiler setup. We shall draw a rectangle, using the draw_character, proceeding from a screen co-ordinate < top_x, top_y >, of a < width > by < height >. The DrawBox FunctionThere are two approaches to drawing the box. Either use a single loop to loop through each possible location, and draw a character if that location appears on the border, or use two loops to draw first the vertical lines, and then the horizontal ones. The second is far easier. void DrawBox ( char cChar, int nTopX, int nTopY, int nWidth, int nHeight) { for ( y = nTopY; y < nTopY + nHeight; y++) { // First do the left hand line gotoxy ( nTopX, y ); putch( cChar ); // Now do the right hand line gotoxy ( nTopX + nWidth, y ); putch( cChar ); } for ( x = nTopX; x < nTopX + nWidth; x++) { // First do the top line gotoxy ( x, nTopY ); putch( cChar ); // Now do the bottom line gotoxy ( x, nTopy + nHeight ); putch( cChar ); } } In the above function, we have used the gotoxy function from the conio.h library, and the standard putch function from the stdio.h library. In the first loop, y represents the line, and in the second, x represents the column. The rest should be fairly self explanatory. The main FunctionIn the main function, we shall use argv to extract the command line parameters that we are interested in. The last four of these will need to be converted to integers, and we shall use the atoi function. void main ( int argc, char ** argv) { // Check the usage if ( argc 6 ) { printf("Usage : %s char top_x top_y width height", argv[0]); } // Call the function DrawBox ( argv[1][0], // Need the first character atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5])); } We have taken some care in the above to check the passed parameters, but not the validity of each one. This should really be done, by assigning them to temporary values and checking for erroneous numerical values as necessary. Porting to UnixIn order to port this to Unix, and as a matter of cross-platform compatibility, it is necessary to use the curses library. This is sometimes available as a port to a non-Unix platform - pdcurses.sf.net is a good example. Using curses, we can rewrite the DrawBox function to use the curses equivalent to gotoxy, called move. The move function is a little different, as it takes the following form: move ( row, column ); The gotoxy function expects the parameters in the form <column, row>, so care must be taken when porting such functions. Apart from this change, the rest stays the same. There are also two additional pieces of work that need to be done : initializing the screen environment to use curses, and closing down the screen environment. These functions are:
The initscr function is called before the main processing, and endwin() as the last action before the program exits. ConclusionAs can be seen, there is some additional work to be done in the Unix curses environment, but it does represent a much more flexible interface, as those who experiment further will find out. Possible enhancements might include using line drawing characters, or specific characters for the four corners, but for now, the above gives us enough quite enough to be going on with.
The copyright of the article Draw Boxes on the Screen in C in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Draw Boxes on the Screen in C in print or online must be granted by the author in writing.
|
|
|
|