Turbo / Borland C specific: console snippit

Discussion in 'C' started by hobbyist, Mar 24, 2012.

  1. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    In case someone is interested; here's a bit of code to move about a cursor within a viewport. Dunno how many bugs there are, but feel free to point any out.

    Code:
    /* Compiled using BC 5; MSDOS app
    
       dos based apps are dodgy in
       windows; not recommended on
       XP, Vista, Win7.  Try dosbox
       instead
    */
    
    #include <conio.h>
    
    #define UP 72  /* up arrow    */
    #define LF 75  /* left arrow  */
    #define RT 77  /* right arrow */
    #define DN 80  /* down arrow  */
    
    #define VP_UL  201  /* upper left      */
    #define VP_LL  200  /* lower left      */
    #define VP_UR  187  /* upper right     */
    #define VP_LR  188  /* lower right     */
    #define VP_VL  186  /* vertical line   */
    #define VP_HL  205  /* horizontal line */
    
    
    /* to change the viewport size, modify below */
    
    #define VP_X_MIN 13 /* minimum value of cx */
    #define VP_X_MAX 65 /* maximum value of cx */
    #define VP_Y_MIN  5 /* minimum value of cy */
    #define VP_Y_MAX 20 /* maximum value of cy */
    
    /*
       cprintf is needed if changing of
       foreground and background colors
       is desired
    
       textbackground(0-7)
       textcolor(0-14)
    */
    
    void drawViewport(void);
    
    int main(void) {
    
       int cx = 39, cy = 12, scan1, scan2;
       unsigned char mc = 234;
    
       _setcursortype(_NOCURSOR);
       drawViewport();
       gotoxy(cx, cy);
       cprintf("%c", mc);
    
       for(; (scan1 = getch()) != 13; ) {
          if(scan1 == 0) {
    
             scan2 = getch();
             gotoxy(cx, cy);
             putch(' ');
    
             cx = (scan2 == LF && cx == VP_X_MIN ? VP_X_MAX :
                   scan2 == LF && cx - 1 >= VP_X_MIN ? --cx  :
                   scan2 == RT && cx == VP_X_MAX ? VP_X_MIN :
                   scan2 == RT && cx + 1 <= VP_X_MAX ? ++cx  : cx);
    
             cy = (scan2 == UP && cy == VP_Y_MIN ? VP_Y_MAX :
                   scan2 == UP && cy - 1 >= VP_Y_MIN ? --cy  :
                   scan2 == DN && cy == VP_Y_MAX ? VP_Y_MIN :
                   scan2 == DN && cy + 1 <= VP_Y_MAX ? ++cy  : cy);
    
             gotoxy(cx, cy);
             cprintf("%c", mc);
          }
       }
    
       _setcursortype(_NORMALCURSOR);
    
       return 0;
    }
    
    void drawViewport(void) {
    
       int x1 = VP_X_MIN - 1,
           x2 = VP_X_MAX + 1,
           y1 = VP_Y_MIN - 1,
           y2 = VP_Y_MAX + 1;
    
       /* draw top and two end points of viewport */
    
       gotoxy(x1, y1); cprintf("%c", VP_UL);
    
       for(x1 = VP_X_MIN; x1 < x2; ++x1) {
          gotoxy(x1, y1); cprintf("%c", VP_HL);
       }
    
       gotoxy(x2, y1); cprintf("%c", VP_UR);
    
       /* draw left and right sides */
    
       x1 = VP_X_MIN - 1;
       y1 += 1;
    
       for(; y1 < y2; ++y1) {
          gotoxy(x1, y1); cprintf("%c", VP_VL);
          gotoxy(x2, y1); cprintf("%c", VP_VL);
       }
    
       /* draw bottom and two end points */
    
       gotoxy(x1, y2); cprintf("%c", VP_LL);
    
       for(x1 = VP_X_MIN; x1 < x2; ++x1) {
          gotoxy(x1, y2); cprintf("%c", VP_HL);
       }
    
       gotoxy(x2, y2); cprintf("%c", VP_LR);
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice