home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / window / tcwin / screen.c next >
Encoding:
C/C++ Source or Header  |  1987-06-22  |  7.9 KB  |  304 lines

  1. /*
  2.                          TURBO C WINDOWS 1.0
  3.                                  by
  4.                             Craig Arnush
  5.  
  6.      These are TURBO C WINDOWS 1.0 by Craig Arnush.  It is not
  7. necessary, although highly useful, that you make use of the
  8. SCREEN.H file included here.  It has all sorts of helpful
  9. definitions.
  10.      This file is by no means yet complete, I am planning on
  11. writing scanning utilities to stay within the windows.  I will
  12. also be coming up with some documentation to make this whole
  13. thing a little more understandable.  Be patient.
  14.      If you find this useful, I would greatly appreciate your
  15. comments and/or donations to help me in writing all of these
  16. utilities.  Suggestions are especially helpful as they tell me
  17. what you're looking for.  Please address all correspondence to:
  18.  
  19.                          Craig Arnush
  20.                          6018 Via Sonoma
  21.                          RPV, CA 90274
  22.  
  23. */
  24.  
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <dos.h>
  28. #include <mem.h>
  29. #include <string.h>
  30. #include <alloc.h>
  31. #include "screen.h"
  32.  
  33. char colorcard, snow;
  34. char far *displayptr;
  35.  
  36. struct scrptr
  37.    {
  38.    char x1, y1, x2, y2, oldx, oldy;
  39.    char *s;
  40.    } windows[16];
  41. char totwins;
  42.  
  43. struct screenelement
  44.    {
  45.    char character;
  46.    char color;
  47.    };
  48.  
  49. int egainstalled(void)                      /* Tests for EGA */
  50. {
  51.    union REGS reg;
  52.  
  53.    reg.x.ax = 0X1200;
  54.    reg.x.bx = 0X0010;
  55.    reg.x.cx = 0XFFFF;
  56.    int86(0X10, ®, ®);
  57.    return((reg.x.cx == 0XFFFF) ? 0 : 1);
  58. }                                           /* egainstalled */
  59.  
  60. int shiftstat(void)                         /* get the shift status          */
  61. {                                           /* returns 1st status byte in    */
  62.    return(*(int far *)0x00000417L);         /* lo byte of int, and 2nd in hi */
  63. }
  64.  
  65. char wherex(void)
  66. {
  67.    return(*(char far *)(colorcard ? 0x00000450L : 0x00000452L) + 1);
  68. }
  69.  
  70. char wherey(void)
  71. {
  72.    return(*(char far *)(colorcard ? 0x00000451L : 0x00000453L) + 1);
  73. }
  74.  
  75. int getkey(void)                            /* get char & scan code from kb */
  76. {
  77.    int key, lo, hi;
  78.  
  79.    key = bioskey(0);
  80.    lo = key & 0x00FF;
  81.    hi = (key & 0xFF00) >> 8;
  82.    return((lo == 0) ? hi + 1000 : lo);
  83. }                                           /* getkey */
  84.  
  85. void clrscr(char color)                     /* Clears the screen */
  86. {
  87.    char i, j;
  88.    int offset;
  89.  
  90.    for (i = (windows[totwins].x1 + 1); i <= (windows[totwins].x2 - 1); ++i)
  91.       for (j = (windows[totwins].y1 + 1); j <= (windows[totwins].y2 - 1); ++j)
  92.          {
  93.          offset = ((j-1) * 160) + ((i-1) << 1);
  94.          *(displayptr + offset) = 0x20;                  /* " " */
  95.          *(displayptr + ++offset) = color;
  96.          }
  97. }                                           /* clrscr */
  98.  
  99. void gotoxyabs(int col, int row)
  100. {
  101.    union REGS reg;
  102.  
  103.    reg.h.ah = 2;
  104.    reg.h.bh = 0;
  105.    reg.h.dh = --row;
  106.    reg.h.dl = --col;
  107.    int86(0X10, ®, ®);
  108. }
  109.  
  110. void scroll(char color, char direction)
  111. {
  112.    union REGS reg;
  113.  
  114.    if (direction == UP)
  115.     reg.x.ax = 0x0601;
  116.    else if (direction == DN)
  117.     reg.x.ax = 0x0701;
  118.    reg.h.ch = windows[totwins].y1;
  119.    reg.h.cl = windows[totwins].x1;
  120.    reg.h.dh = windows[totwins].y2 - 2;
  121.    reg.h.dl = windows[totwins].x2 - 2;
  122.    reg.h.bh = color;
  123.    int86(0x10, ®, ®);
  124. }
  125.  
  126. void gotoxy(int col, int row)               /* Move to column, row */
  127. {
  128.    col = windows[totwins].x1 + col - 1;
  129.    if (col >= windows[totwins].x2)
  130.       return;
  131.    row = (int) windows[totwins].y1 + row - 1;
  132.    if (row >= windows[totwins].y2)
  133.       return;
  134.    gotoxyabs(col, row);
  135. }                                           /* gotoxy */
  136.  
  137. void putonscreen(char c, char color, char x, char y)
  138. {
  139.    *(int far *)(displayptr+((y-1)*160)+((x-1) << 1)) = ((color << 8) + c);
  140. }
  141.  
  142. void border(void)
  143. {
  144.    char i, x1, y1, x2, y2;
  145.  
  146.    x1 = windows[totwins].x1;
  147.    y1 = windows[totwins].y1;
  148.    x2 = windows[totwins].x2;
  149.    y2 = windows[totwins].y2;
  150.    putonscreen('┌', 0x1F, x1, y1);
  151.    putonscreen('┐', 0x1F, x2, y1);
  152.    putonscreen('└', 0x1F, x1, y2);
  153.    putonscreen('┘', 0x1F, x2, y2);
  154.    for (i = (x1 + 1); i < x2; ++i)
  155.       {
  156.       putonscreen('─', 0x1F, i, y1);
  157.       putonscreen('─', 0x1F, i, y2);
  158.       }
  159.    for (i = (y1 + 1); i < y2; ++i)
  160.       {
  161.       putonscreen('│', 0x1F, x1, i);
  162.       putonscreen('│', 0x1F, x2, i);
  163.       }
  164. }
  165.  
  166. void openwindow(char x1, char y1, char x2, char y2)
  167. {
  168.    char i, j;
  169.    int scrofs, savofs;
  170.  
  171.    if (totwins < 16)
  172.       {
  173.       ++totwins;
  174.       windows[totwins].x1 = x1;
  175.       windows[totwins].y1 = y1;
  176.       windows[totwins].x2 = x2;
  177.       windows[totwins].y2 = y2;
  178.       windows[totwins].oldx = wherex();
  179.       windows[totwins].oldy = wherey();
  180.       windows[totwins].s = (char *) malloc((x2-x1+1) * (y2-y1+1) * 2);
  181.       savofs = 0;
  182.       for (i = x1; i <= x2; ++i)
  183.          for (j = y1; j <= y2; ++j)
  184.             {
  185.             scrofs = ((j-1) * 160) + ((i-1) << 1);
  186.             *(windows[totwins].s + savofs++) = *(displayptr + scrofs++);
  187.             *(windows[totwins].s + savofs++) = *(displayptr + scrofs);
  188.             }
  189.       border();
  190.       clrscr(0x07);
  191.       gotoxy(1, 1);
  192.       }
  193. }
  194.  
  195. void closewindow(void)
  196. {
  197.    char i, j;
  198.    int scrofs, savofs;
  199.  
  200.    if (totwins > 0)
  201.       {
  202.       savofs = 0;
  203.       for (i = windows[totwins].x1; i <= windows[totwins].x2; ++i)
  204.          for (j = windows[totwins].y1; j <= windows[totwins].y2; ++j)
  205.             {
  206.             scrofs = ((j-1) * 160) + ((i-1) << 1);
  207.             *(displayptr + scrofs++) = *(windows[totwins].s + savofs++);
  208.             *(displayptr + scrofs) = *(windows[totwins].s + savofs++);
  209.             }
  210.       gotoxyabs(windows[totwins].oldx, windows[totwins].oldy);
  211.       free(windows[totwins].s);
  212.       --totwins;
  213.       }
  214. }
  215.  
  216. void setcursor(int startline, int endline)  /* Sets the shape of the cursor */
  217. {
  218.    union REGS reg;
  219.  
  220.    reg.h.ah = 1;
  221.    reg.x.cx = (startline << 8) + endline;
  222.    int86(0X10, ®, ®);
  223. }                                           /* setcursor */
  224.  
  225. void colorscr(char color)                   /* colors the screen */
  226. {
  227.    char i, j;
  228.  
  229.    for (i = (windows[totwins].x1 + 1); i <= (windows[totwins].x2 - 1); ++i)
  230.       for (j = (windows[totwins].y1 + 1); j <= (windows[totwins].y2 - 1); ++j)
  231.          *(displayptr + ((j-1) * 160) + ((i-1) << 1) + 1) = color;
  232. }                                           /* colorscr */
  233.  
  234. void writef(char color, char *msg, ...)
  235. {
  236.  char *output;
  237.  va_list argptr;
  238.  char i, tx, ty;
  239.  
  240.  output = "";
  241.  va_start(argptr, msg);
  242.  vsprintf(output, msg, argptr);
  243.  va_end(argptr);
  244.  tx = wherex();
  245.  ty = wherey();
  246.  i = 0;
  247.  while (output[i] != '\0')
  248.   {
  249.   if (output[i] == '\n')
  250.    {
  251.    tx = windows[totwins].x1 + 1;
  252.    ++ty;
  253.    }
  254.   else
  255.    putonscreen(output[i], color, tx++, ty);
  256.   if (tx >= windows[totwins].x2)
  257.    {
  258.    tx = windows[totwins].x1 + 1;
  259.    ++ty;
  260.    }
  261.   if (ty >= windows[totwins].y2)
  262.    {
  263.    scroll(color, UP);
  264.    ty = windows[totwins].y2 - 1;
  265.    }
  266.   ++i;
  267.   }
  268.  gotoxyabs(tx, ty);
  269. }
  270.  
  271. void initdisplay(void)
  272. /* Initializes various global variables - must be called before using the
  273.    above procedures and functions.
  274. */
  275. {
  276.    union REGS reg;
  277.  
  278.    reg.h.ah = 15;
  279.    int86(0X10, ®, ®);
  280.    colorcard = (reg.h.al != 7);
  281.    snow = (!egainstalled() && colorcard);
  282.    displayptr = (char far *)(colorcard ? 0xB8000000L : 0xB0000000L);
  283.    windows[0].x1 = 0;
  284.    windows[0].y1 = 0;
  285.    windows[0].x2 = 81;
  286.    windows[0].y2 = 26;
  287.    windows[0].s  = (char *) displayptr;
  288.    totwins = 0;
  289. } /* initdisplay */
  290.  
  291. /*----------------------*/
  292. main()
  293. {
  294. initdisplay();
  295. openwindow(10, 5, 50, 15);
  296. gotoxyabs(11, 10);
  297. getkey();
  298. writef(0x17, "Now is the time for all good men to come to the aid of their countries!");
  299. gotoxyabs(11, 14);
  300. getkey();
  301. writef(0x17, "This sentence is written for the sole purpose of testing the scroll.\n");
  302. getkey();
  303. closewindow();
  304. }