home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / asm4qc / demo4qc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-30  |  15.0 KB  |  563 lines

  1. /*********************************************************************
  2.  *  ASM4QC, Assembly routines for QuickC                             *
  3.  *  Version 2.02, Copyright (c) 1989-90 SoftCircuits (tm)            *
  4.  *                                                                   *
  5.  *  DEMO4QC.C, Demonstration C program for ASM4QC library            *
  6.  *  C source code                                                    *
  7.  *********************************************************************/
  8.  
  9. #include <ctype.h>                  /* for toupper() prototype */
  10. #include <string.h>                 /* for strlen() prototype */
  11. #include "asm4qc.h"                 /* include ASM4QC header file */
  12.  
  13. /* attribute variables (default to monochrome values) */
  14. int menu_text = foreback(WHITE,BLACK);
  15. int menu_frame = foreback(WHITE,BLACK);
  16. int menu_title = foreback(BLACK,WHITE);
  17. int menu_hilite = foreback(BLACK,WHITE);
  18. int info_text = foreback(WHITE,BLACK);
  19. int info_frame = foreback(WHITE,BLACK);
  20. int input_text = foreback(WHITE,BLACK);
  21. int input_frame = foreback(WHITE,BLACK);
  22. int input_title = foreback(BLACK,WHITE);
  23. int status_bar = foreback(BLACK,WHITE);
  24.  
  25. #define  TOP      7           /* define text window coordinates */
  26. #define  BOTTOM   17
  27. #define  LEFT     7
  28. #define  RIGHT    73
  29. #define  WINDOWHEIGHTH  ((BOTTOM - TOP) - 2)
  30. #define  NUMOFLINES  22       /* number of lines of text to be displayed */
  31.  
  32. #define  HEADER_BAR  "ASM4QC, assembly routines for QuickC"
  33. #define  FOOTER_BAR  "Version 2.02, Copyright (c) 1989-90 SoftCircuits (tm)"
  34.  
  35. /* declare screen window buffers */
  36. int windowbuffer[((BOTTOM - TOP) + 1) * ((RIGHT - LEFT) + 1)];
  37. int menubuffer[299], menubuffer2[299], headerbuffer[264];
  38. int footerbuffer[264], footerbuffer2[264], editor_buffer[366];
  39.  
  40. /* declare miscellaneous global varaibles */
  41. char input_buffer[60];           /* kbded() input buffer */
  42. int  row,column;                 /* initial cursor location */
  43. int  scroll_position = 0;        /* position within demoinfo text */
  44. int  selection = 1;              /* current menu selection */
  45. int  mtop = 6, mbottom = 18;     /* menu coordinates */
  46. int  mleft = 29, mright = 51;
  47.  
  48. char *menu_data[] = {            /* define menu text */
  49.     "Read About ASM4QC",
  50.     "Line Input Editor",
  51.     "Move This Menu",
  52.     "Beep",
  53.     "Exit to DOS"
  54. };
  55.  
  56. char *window_data[] = {          /* define demo info text */
  57.     "",
  58.     "            DEMO4QC -- Demo program for ASM4QC",
  59.     "   Version 2.02, Copyright (c) 1989-90 SoftCircuits (tm)",
  60.     "",
  61.     "",
  62.     "   Welcome to ASM4QC (Assembly routines for QuickC). These",
  63.     "routines give your C programs high performance video",
  64.     "functions plus much more. Note: If you're using a CGA",
  65.     "monitor that doesn't snow, restart this program with the /q",
  66.     "option for even faster video performance.",
  67.     "",
  68.     "   Because ASM4QC was written in assembly language, it was",
  69.     "possible to optimize these routines for both speed and",
  70.     "power. And it naturally follows that they are compact and",
  71.     "will add hardly anything to overall program size.",
  72.     "",
  73.     "   With ASM4QC, you'll be able to cut the time it takes to",
  74.     "code colorful video routines down to seconds. And you'll be",
  75.     "able to code professional windowing routines in minutes.",
  76.     "And the resulting routines will run at the fastest speed",
  77.     "possible on your computer. Period!",
  78.     "",
  79.     ""
  80. };
  81.  
  82. struct vconfig vdata;            /* declare structure for getvconfig() */
  83.  
  84. /*
  85. ** removes menu and restores original screen
  86. */
  87. void restore_screen(void)
  88. {
  89.     /* restore text under menu */
  90.     restorewin(menubuffer,mtop,mbottom,mleft,mright);
  91.  
  92.     /* restore text under header bar */
  93.     restorewin(headerbuffer,1,1,1,vdata.textcols);
  94.  
  95.     /* restore text under footer bar */
  96.     restorewin(footerbuffer,vdata.textrows,vdata.textrows,1,vdata.textcols);
  97.  
  98.     setcurs(row,column);     /* set cursor to original position */
  99.     restorecurs();           /* and un-hide it */
  100.  
  101. } /* restore_screen */
  102.  
  103. /*
  104. ** moves the menu around the screen
  105. */
  106. void move_menu(void)
  107. {
  108.     int done = FALSE;
  109.  
  110.     /* change footer bar information */
  111.     _color(status_bar);
  112.     savewin(footerbuffer2,vdata.textrows,vdata.textrows,1,vdata.textcols);
  113.     setcurs(vdata.textrows,1);
  114.     repch(' ',vdata.textcols);
  115.     setvoff(vdata.textrows,10);
  116.     prints("Move menu: \x18  \x19  \x1B  \x1A");
  117.     setvoff(vdata.textrows,45);
  118.     prints("Return to normal mode: Esc");
  119.  
  120.     /* fill a buffer with an image of the menu */
  121.     savewin(menubuffer2,mtop,mbottom,mleft,mright);
  122.  
  123.     while(!done) {
  124.         switch(getkey()) {
  125.             case UP_KEY :
  126.                 if(mtop > 2) {
  127.                     restorewin(menubuffer,mtop,mbottom,mleft,mright);
  128.                     mtop--; mbottom--;
  129.                     savewin(menubuffer,mtop,mbottom,mleft,mright);
  130.                     restorewin(menubuffer2,mtop,mbottom,mleft,mright);
  131.                 }
  132.                 break;
  133.             case DOWN_KEY :
  134.                 if(mbottom < (int)(vdata.textrows - 1)) {
  135.                     restorewin(menubuffer,mtop,mbottom,mleft,mright);
  136.                     mtop++; mbottom++;
  137.                     savewin(menubuffer,mtop,mbottom,mleft,mright);
  138.                     restorewin(menubuffer2,mtop,mbottom,mleft,mright);
  139.                 }
  140.                 break;
  141.             case LEFT_KEY :
  142.                 if(mleft > 1) {
  143.                     restorewin(menubuffer,mtop,mbottom,mleft,mright);
  144.                     mleft--; mright--;
  145.                     savewin(menubuffer,mtop,mbottom,mleft,mright);
  146.                     restorewin(menubuffer2,mtop,mbottom,mleft,mright);
  147.                 }
  148.                 break;
  149.             case RIGHT_KEY :
  150.                 if(mright < (int)vdata.textcols) {
  151.                     restorewin(menubuffer,mtop,mbottom,mleft,mright);
  152.                     mleft++; mright++;
  153.                     savewin(menubuffer,mtop,mbottom,mleft,mright);
  154.                     restorewin(menubuffer2,mtop,mbottom,mleft,mright);
  155.                 }
  156.                 break;
  157.             case ESCAPE_KEY :
  158.                 done = TRUE;
  159.                 break;
  160.         } /* switch */
  161.         kbdflush();      /* clear any keystrokes waiting in keyboard buffer */
  162.     }
  163.  
  164.     /* restore footer bar */
  165.     restorewin(footerbuffer2,vdata.textrows,vdata.textrows,1,vdata.textcols);
  166.  
  167. } /* move_menu */
  168.  
  169. /*
  170. ** sounds a series of tones
  171. */
  172. void test_beep(void)
  173. {
  174.     beep(55,8);
  175.     beep(110,2);
  176.     beep(220,2);
  177.     beep(440,2);
  178.     beep(880,2);
  179.     kbdflush();              /* flush keyboard buffer */
  180.  
  181. } /* test_beep */
  182.  
  183. /*
  184. ** test input line-editor
  185. */
  186. void input_editor(void)
  187. {
  188.     /* save information under input window */
  189.     savewin(editor_buffer,16,21,10,70);
  190.  
  191.     /* clear window area */
  192.     _color(input_text);
  193.     scroll(0,16,21,10,70);
  194.  
  195.     /* create window frame */
  196.     _color(input_frame);
  197.     frame(16,21,10,70);
  198.  
  199.     /* create help bar */
  200.     setvoff(21,26); _color(input_title);
  201.     printsa(" Press Escape to close window ");
  202.  
  203.     _color(input_text); setcurs(17,11); biosprintsa(">");
  204.     kbdflush();              /* clear keyboard buffer */
  205.  
  206.     /* allow user input until escape is pressed */
  207.     while(kbded(input_buffer,57) != -1) {
  208.         /* advance cursor to next line */
  209.         if (cursrow() > 19) {
  210.             scroll(1,17,20,11,69);
  211.             setcurs(cursrow(),11);
  212.         }
  213.         else
  214.             setcurs(cursrow() + 1,11);
  215.         biosprintsa(">");
  216.     }
  217.  
  218.     /* close input window */
  219.     restorewin(editor_buffer,16,21,10,70);
  220.  
  221. } /* input_editor */
  222.  
  223. /*
  224. ** fills info window
  225. */
  226. void fill_window(void)
  227. {
  228.     int element, line;
  229.     /* clear the window */
  230.     scroll(0,TOP + 1, BOTTOM - 1, LEFT + 1, RIGHT - 1);
  231.  
  232.     /* write text to window */
  233.     for(element = scroll_position, line = 0;line <= WINDOWHEIGHTH;line++) {
  234.         setvoff(line + TOP + 1,11);
  235.         prints(window_data[element++]);
  236.     }
  237. } /* fill_window */
  238.  
  239. /*
  240. ** moves to bottom of demo info text
  241. */
  242. void goto_end(void)
  243. {
  244.     if(scroll_position != NUMOFLINES - WINDOWHEIGHTH) {
  245.         scroll_position = NUMOFLINES - WINDOWHEIGHTH;
  246.         fill_window();
  247.     }
  248. } /* goto_end */
  249.  
  250. /*
  251. ** moves to top of demo info text
  252. */
  253. void goto_home(void)
  254. {
  255.     if(scroll_position != 0) {
  256.         scroll_position = 0;
  257.         fill_window();
  258.     }
  259. } /* goto_home */
  260.  
  261. /*
  262. ** moves demo info text one page down
  263. */
  264. void page_down(void)
  265. {
  266.     if(scroll_position < (NUMOFLINES - WINDOWHEIGHTH)) {
  267.         if((scroll_position+= (WINDOWHEIGHTH + 1)) >= NUMOFLINES - WINDOWHEIGHTH) {
  268.             scroll_position = NUMOFLINES - WINDOWHEIGHTH;
  269.         }
  270.         fill_window();
  271.     }
  272. } /* page_down */
  273.  
  274. /*
  275. ** moves demo video text one page up
  276. */
  277. void page_up(void)
  278. {
  279.     if(scroll_position > 0) {
  280.         if((scroll_position-= (WINDOWHEIGHTH + 1)) < 0) {
  281.             scroll_position = 0;
  282.             }
  283.         fill_window();
  284.     }
  285. } /* page_up */
  286.  
  287. /*
  288. ** moves demo video text one line down
  289. */
  290. void scroll_down(void)
  291. {
  292.     if(scroll_position > 0) {
  293.         scroll(-1, TOP + 1, BOTTOM - 1, LEFT + 1, RIGHT -1);
  294.         setvoff(TOP + 1,11);
  295.         prints(window_data[--scroll_position]);
  296.     }
  297. } /* scroll_down */
  298.  
  299. /*
  300. ** moves demo video text one line up
  301. */
  302. void scroll_up(void)
  303. {
  304.     if(scroll_position < (NUMOFLINES - WINDOWHEIGHTH)) {
  305.         scroll(1, TOP + 1, BOTTOM - 1, LEFT + 1, RIGHT - 1);
  306.         setvoff(BOTTOM - 1,11);
  307.         prints(window_data[++scroll_position + WINDOWHEIGHTH]);
  308.     }
  309. } /* scroll_up */
  310.  
  311. /*
  312. ** displays scrollable information about asm4qc
  313. */
  314. void demoinfo(void)
  315. {
  316.     int done = FALSE;
  317.  
  318.     /* save information under window */
  319.     savewin(windowbuffer,TOP,BOTTOM,LEFT,RIGHT);
  320.  
  321.     /* create window border */
  322.     _color(info_frame);
  323.     frame(TOP,BOTTOM,LEFT,RIGHT);
  324.  
  325.     /* write text to window */
  326.     _color(info_text);
  327.     fill_window();
  328.  
  329.     /* save old footer bar */
  330.     savewin(footerbuffer2,vdata.textrows,vdata.textrows,1,vdata.textcols);
  331.  
  332.     /* create new footer bar */
  333.     _color(status_bar);
  334.     setcurs(vdata.textrows,1);
  335.     repch(' ',vdata.textcols);
  336.     setvoff(vdata.textrows,6);
  337.     prints("Move: \30  \31  PgUp  PgDn  Home  End");
  338.     setvoff(vdata.textrows,59);
  339.     prints("Close window: Esc");
  340.  
  341.     _color(info_text);
  342.     while(!done) {
  343.         switch(getkey()) {                 /* get a key from user */
  344.             case HOME_KEY:                  /* home key */
  345.                 goto_home();
  346.                 break;
  347.             case UP_KEY:                    /* cursor up */
  348.                 scroll_down();
  349.                 break;
  350.             case PGUP_KEY:                  /* page up */
  351.                 page_up();
  352.                 break;
  353.             case END_KEY:                   /* end key */
  354.                 goto_end();
  355.                 break;
  356.             case DOWN_KEY:                  /* cursor down */
  357.                 scroll_up();
  358.                 break;
  359.             case PGDN_KEY:                  /* page down */
  360.                 page_down();
  361.                 break;
  362.             case ESCAPE_KEY:                /* escape key */
  363.                 done = TRUE;
  364.                 break;
  365.         }
  366.     }
  367.  
  368.     /* close window and restore footer bar */
  369.     restorewin(windowbuffer,TOP,BOTTOM,LEFT,RIGHT);
  370.     restorewin(footerbuffer2,vdata.textrows,vdata.textrows,1,vdata.textcols);
  371.  
  372. } /* demoinfo */
  373.  
  374. /*
  375. ** changes the current menu selection and moves the hilight bar
  376. */
  377. void move_selection(int change)
  378. {
  379.     setvoff(selection+mtop+3,mleft+2);  /* unhighilight current selection */
  380.     printa(menu_text,19);
  381.     selection+=change;                  /* calculate new selection */
  382.     if(selection > 5)
  383.         selection = 1;
  384.     else if(selection < 1)
  385.         selection = 5;
  386.     setvoff(selection+mtop+3,mleft+2);  /* hilight new selection */
  387.     printa(menu_hilite,19);
  388.  
  389. } /* move_selection */
  390.  
  391. /*
  392. ** this function makes up the main program loop
  393. */
  394. void run(void)
  395. {
  396.     int done = FALSE;                 /* initialize boolean variable */
  397.  
  398.     while(!done) {
  399.         switch(getkey()) {               /* get key from user */
  400.             case RETURN_KEY :            /* if it's a carriage return */
  401.                 switch(selection) {        /*  carry out command based on */
  402.                     case 1:                 /*  current selection */
  403.                         demoinfo();
  404.                         break;
  405.                     case 2:
  406.                         input_editor();
  407.                         break;
  408.                     case 3:
  409.                         move_menu();
  410.                         break;
  411.                     case 4:
  412.                         test_beep();
  413.                         break;
  414.                     case 5:
  415.                         done = TRUE;
  416.                         break;
  417.                 }
  418.                 break;
  419.             case UP_KEY:                 /* cursor up */
  420.                 move_selection(-1);
  421.                 break;
  422.             case DOWN_KEY:               /* cursor down */
  423.                 move_selection(1);
  424.                 break;
  425.             case ESCAPE_KEY:             /* escape key */
  426.                 done = TRUE;
  427.                 break;
  428.         }
  429.     }
  430. } /* run */
  431.  
  432. /*
  433. ** creates the main menu
  434. */
  435. void draw_menu(void)
  436. {
  437.     int line;
  438.  
  439.     /* save text under the menu */
  440.     savewin(menubuffer,mtop,mbottom,mleft,mright);
  441.  
  442.     /* clear menu area */
  443.     _color(menu_text); scroll(0,mtop,mbottom,mleft,mright);
  444.  
  445.     /* create menu border */
  446.     _color(menu_frame); frame(mtop,mbottom,mleft,mright);
  447.  
  448.     /* create menu title */
  449.     _color(menu_title); setvoff(6,33); printsa("   Main Menu   ");
  450.  
  451.     /* create help bar at the bottom of the menu */
  452.     _color(status_bar);
  453.     setvoff(17,30); printsa("  \x18-\x19-\x11\xC4\xD9 to select  ");
  454.     setvoff(17,32); printa(foreback(BLACK,WHITE),1);
  455.     setvoff(17,34); printa(foreback(BLACK,WHITE),1);
  456.     setvoff(17,36); printa(foreback(BLACK,WHITE),3);
  457.  
  458.     /* write menu text */
  459.     for (line = 10; line < 15; line++) {
  460.         setvoff(line,32);
  461.         prints(menu_data[line - 10]);
  462.     }
  463.  
  464.     /* create menu hilite bar */
  465.     setvoff(10,31); printa(menu_hilite,19);
  466.  
  467. } /* draw_menu */
  468.  
  469. /*
  470. ** creates the initial screen
  471. */
  472. void draw_screen(void)
  473. {
  474.     row = cursrow(); column = curscol(); /* save cursor position */
  475.     hidecurs();                          /* hide the text cursor */
  476.  
  477.     /* save text under header bar */
  478.     savewin(headerbuffer,1,1,1,vdata.textcols);
  479.  
  480.     /* save text under footer bar */
  481.     savewin(footerbuffer,vdata.textrows,vdata.textrows,1,vdata.textcols);
  482.  
  483.     /* create header bar */
  484.     _color(status_bar);
  485.     setcurs(1,1);            /* clear header area */
  486.     repch(' ',vdata.textcols);
  487.     setvoff(1,(vdata.textcols - strlen(HEADER_BAR)) / 2);
  488.     prints(HEADER_BAR);      /* print header */
  489.  
  490.     /* create footer bar */
  491.     setcurs(vdata.textrows,1); /* clear header area */
  492.     repch(' ',vdata.textcols);
  493.     setvoff(vdata.textrows,(vdata.textcols - strlen(FOOTER_BAR)) / 2);
  494.     prints(FOOTER_BAR);      /* print footer */
  495.  
  496.     /* cause frame() to use double-line frame characters */
  497.     setdoubleframe();
  498.  
  499.     /* create the menu */
  500.     draw_menu();
  501. } /* draw_screen */
  502.  
  503. /*
  504. ** initializes video variables
  505. */
  506. void initialize_video(int argc, char *argv[])
  507. {
  508.     int count, force_blackwhite = FALSE, force_nosnowcheck = FALSE;
  509.  
  510.     /* check command line options */
  511.     for(count = 1; count < argc; count++) {
  512.         switch(argv[count][0]) {
  513.             case '/' :
  514.             case '-' :
  515.                 switch(toupper(argv[count][1])) {
  516.                     case 'Q':
  517.                         force_nosnowcheck = TRUE;
  518.                         break;
  519.                     case 'B':
  520.                         force_blackwhite = TRUE;
  521.                         break;
  522.                     }
  523.                 break;
  524.             }
  525.         }
  526.  
  527.     /* if initvideo() returned a non-zero value and the /b option */
  528.     /* was not specified, assign attribute variables with colors  */
  529.     /* values that work well on color systems                     */
  530.     if(initvideo() && !force_blackwhite) {
  531.         menu_text = foreback(GREEN,BLACK);
  532.         menu_frame = foreback(CYAN,BLACK);
  533.         menu_title = foreback(BOLD+WHITE,RED);
  534.         menu_hilite = foreback(BOLD+WHITE,YELLOW);
  535.         info_text = foreback(BOLD+CYAN,BLUE);
  536.         info_frame = foreback(BOLD+WHITE,BLUE);
  537.         input_text = foreback(BOLD+WHITE,RED);
  538.         input_frame = foreback(BOLD+YELLOW,RED);
  539.         input_title = foreback(RED,WHITE);
  540.         status_bar = foreback(BOLD+WHITE,BLUE);
  541.     }
  542.  
  543.     /* turn off snow checking if specified */
  544.     if(force_nosnowcheck)
  545.         setsnowcheck(OFF);
  546.  
  547.     /* retrieve display information */
  548.     getvconfig(&vdata);
  549.  
  550. } /* initialize_video */
  551.  
  552. /*
  553. ** here is main
  554. */
  555. void main(int argc,char *argv[])
  556. {
  557.     initialize_video(argc,argv);  /* initialize video variables */
  558.     draw_screen();                /* create initial screen */
  559.     run();                        /* run main program loop */
  560.     restore_screen();             /* restore the screen */
  561.  
  562. } /* main */
  563.