home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / VIDEO.CPP < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  247 lines

  1. // +++Date last modified: 05-Jul-1997
  2.  
  3. // Implements a simple text video control class
  4. // public domain
  5. // by David L. Nugent <davidn@unique.blaze.net.au>
  6. // 3:632/348@fidonet
  7. // Implementation for 16 & 32-bit - MSDOS & OS/2
  8.  
  9. #include "video.hpp"
  10. #include <string.h>
  11.  
  12. #if defined(__EMX__)
  13. #define __OS2__
  14. #endif
  15.  
  16. #if defined(__OS2__)
  17. #define INCL_VIO
  18. #define INCL_KBD
  19. #include <os2.h>
  20. #else
  21. # include <dos.h>
  22. # include <bios.h>
  23. # if defined(__386__)   // Note: assumes DOS4GW
  24. #  define vbios(r) int386(0x10,(r),(r))
  25. #  undef far
  26. #  define far
  27. static unsigned short * screenPtr = (unsigned short *)0xb8000;
  28. # else
  29. #  define vbios(r) int86(0x10,(r),(r))
  30. static unsigned short far * screenPtr = (unsigned short far *)MK_FP(0xb800,0);
  31. # endif
  32. #endif
  33.  
  34. #define NO_VAL  cell_t(0xffffu)
  35.  
  36. video::video()
  37.   : maxx(NO_VAL), maxy(NO_VAL),
  38.     curx(NO_VAL), cury(NO_VAL),
  39.     attr(7), fill(' ')
  40. {
  41.   maxxy(maxx, maxy);
  42.   wherexy(curx, cury);
  43. }
  44.  
  45. video::~video()
  46. {
  47.   putcursor();
  48. }
  49.  
  50. void
  51. video::cls()
  52. {
  53.   scroll(0, 0, NO_VAL, NO_VAL, NO_VAL, 1);
  54. }
  55.  
  56. void
  57. video::scroll(cell_t left, cell_t top, cell_t right, cell_t bottom, cell_t lines, char dir)
  58. {
  59. #if defined(__OS2__)
  60.   BYTE cell[2] = { (BYTE)fill, (BYTE)attr };
  61.   if (dir)
  62.     VioScrollUp(top, left, bottom, right, lines, cell, 0);
  63.   else
  64.     VioScrollDn(top, left, bottom, right, lines, cell, 0);
  65. #else
  66.   union REGS r;
  67.   r.h.ah = (unsigned char)(dir ? 6 : 7);
  68.   r.h.al = (unsigned char)((lines == NO_VAL) ? 0 : lines);
  69.   r.h.bh = (unsigned char)attr;
  70.   r.h.bl = (unsigned char)fill;
  71.   r.h.ch = (unsigned char)top;
  72.   r.h.cl = (unsigned char)left;
  73.   r.h.dh = (unsigned char)((bottom == NO_VAL) ? (maxy-1) : bottom);
  74.   r.h.dl = (unsigned char)((right == NO_VAL) ? (maxx-1) : right);
  75.   vbios(&r);
  76. #endif
  77. }
  78.  
  79. void
  80. video::gotoxy(cell_t x, cell_t y)
  81. {
  82.   cury = y;
  83.   curx = x;
  84. }
  85.  
  86. void
  87. video::putcursor()
  88. {
  89.   if (cury > maxy)
  90.   {
  91.     cell_t toscroll = cell_t(cury - maxy + 1);
  92.     scroll(0, 0, maxx, maxy, toscroll);
  93.     cury -= toscroll;
  94.   }
  95. #if defined(__OS2__)
  96.   VioSetCurPos(cury, curx, 0);
  97. #else
  98.   union REGS r;
  99.   r.h.ah = 2;
  100.   r.h.bh = 0;
  101.   r.h.dh = (unsigned char)cury;
  102.   r.h.dl = (unsigned char)curx;
  103.   vbios(&r);
  104. #endif
  105. }
  106.  
  107.  
  108. void
  109. video::maxxy(cell_t & cols, cell_t & rows)
  110. {
  111.   if (maxx == NO_VAL && maxy)
  112.   {
  113. #if defined(__OS2__)
  114.     VIOMODEINFO vm;
  115.     vm.cb = sizeof(vm);
  116.     VioGetMode(&vm, 0);
  117.     maxx = vm.col;
  118.     maxy = vm.row;
  119. #else
  120.     union REGS r;
  121.     r.x.ax = 0x0f00;
  122.     vbios(&r);
  123.     maxx = r.h.ah;
  124.     if (r.h.al == 7 || r.h.al == 15)
  125. # if defined(__386__)
  126.       screenPtr = (unsigned short *)(0xB000 << 4);
  127. # else
  128.       screenPtr = (unsigned short far *)MK_FP(0xB000,0);
  129. # endif
  130.     unsigned char far * p = (unsigned char far *)0x484;
  131.     maxy = *p;
  132.     if (!maxy)
  133.       maxy = 25;
  134.     else
  135.       ++maxy;
  136. #endif
  137.   }
  138.   cols = maxx;
  139.   rows = maxy;
  140. }
  141.  
  142. void
  143. video::wherexy(cell_t & x, cell_t & y)
  144. {
  145.   if (curx == NO_VAL && cury == NO_VAL)
  146.   { // Only need to check this the once
  147. #if defined(__OS2__)
  148.     VioGetCurPos(&cury, &curx, 0);
  149. #else
  150.     union REGS r;
  151.  
  152.     r.h.ah = 3;
  153.     r.h.bh = 0;
  154.     vbios(&r);
  155.     cury = r.h.dh;
  156.     curx = r.h.dl;
  157. #endif
  158.   }
  159.   x = curx;
  160.   y = cury;
  161. }
  162.  
  163. void
  164. video::put(register int ch)
  165. {
  166.   putcursor();
  167. #if defined(__OS2__)
  168.   BYTE tmp[2];
  169.   tmp[0] = BYTE(ch);
  170.   tmp[1] = BYTE(attr);
  171.   VioWrtCellStr((PCH)tmp, 2, cury, curx, 0);
  172. #else
  173.   unsigned short far * ptr = screenPtr + (cury * maxx) + curx;
  174.   *ptr++ = (unsigned short)((ch & 0xff) | (attr << 8));
  175. #endif
  176.   adjustcursor(1);
  177. }
  178.  
  179. void
  180. video::put(char const * s)
  181. {
  182.   putcursor();
  183. #if defined(__OS2__)
  184.   USHORT l = USHORT(strlen(s) * 2);
  185.   BYTE * tmp = new BYTE[l];
  186.   for (USHORT i = 0 ; i < l ;)
  187.   {
  188.     tmp[i++] = *s++;
  189.     tmp[i++] = BYTE(attr);
  190.   }
  191.   VioWrtCellStr((PCH)tmp, l, cury, curx, 0);
  192.   delete[] tmp;
  193.   l /= 2;
  194. #else
  195.   unsigned short far * ptr = screenPtr + (cury * maxx) + curx;
  196.   unsigned short l = (unsigned short) strlen(s);
  197.   for (unsigned short i = 0 ; i < l ; ++i)
  198.     *ptr++ = (unsigned short)((*s++ & 0xff) | (attr << 8));
  199. #endif
  200.   adjustcursor(l);
  201. }
  202.  
  203. void
  204. video::adjustcursor(cell_t cols, cell_t rows)
  205. {
  206.   curx += cols;
  207.   rows += (curx / maxx);
  208.   curx  = cell_t(curx % maxx);
  209.   cury += rows;
  210.   gotoxy(curx, cury);
  211. }
  212.  
  213. void
  214. video::repchr(int ch, int n)
  215. {
  216. #if defined(__OS2__)
  217.   USHORT l = USHORT(n * 2);
  218.   BYTE * tmp = new BYTE[l];
  219.   for (USHORT i = 0 ; i < l ;)
  220.   {
  221.     tmp[i++] = char(ch);
  222.     tmp[i++] = BYTE(attr);
  223.   }
  224.   VioWrtCellStr((PCH)tmp, l, cury, curx, 0);
  225.   delete[] tmp;
  226. #else
  227.   unsigned short far * ptr = screenPtr + (cury * maxx) + curx;
  228.   for (unsigned short i = 0 ; i < (unsigned short)n ; ++i)
  229.     *ptr++ = (unsigned short)((ch & 0xff) | (attr << 8));
  230. #endif
  231.   adjustcursor(cell_t(n));
  232. }
  233.  
  234. unsigned short
  235. video::getkey()
  236. {
  237.   putcursor();
  238. #if defined(__OS2__)
  239.   KBDKEYINFO K;
  240.   KbdCharIn(&K, IO_WAIT, 0);
  241.   return (unsigned short)((K.chScan << 8) | K.chChar);
  242. #else
  243.   return _bios_keybrd(_KEYBRD_READ);
  244. #endif
  245. }
  246.  
  247.