home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / OMEGA2 / OMSCREEN.CPP < prev    next >
C/C++ Source or Header  |  1992-04-25  |  12KB  |  564 lines

  1. //
  2. // *************************************************************************
  3. // *                                                                       *
  4. // *    OMEGA C++ Windowing Class Library                                  *
  5. // *    =================================                                  *
  6. // *                                                                       *
  7. // *    Copyright 1991,92 Tom Clancy                                       *
  8. // *    Submitted to the public domain, April 1992                         *
  9. // *                                                                       *
  10. // *************************************************************************
  11. // *                                                                       *
  12. // *    Screen Class Methods (low level screen routines)                   *
  13. // *                                                                       *
  14. // *************************************************************************
  15. //
  16.  
  17.  
  18. #include <dos.h>
  19. #include <conio.h>
  20. #include <mem.h>
  21. #include <string.h>
  22. #include "fastwrit.hpp"
  23. #include "omscreen.hpp"
  24.  
  25.  
  26. //
  27. // oScreen Static Data Members
  28. //
  29.  
  30. int sLen=25;
  31. int sWid=80;
  32. int Oldvmode=0;
  33. int oScreen::vActive=FALSE;
  34. unsigned vseg;
  35. unsigned vofs;
  36. unsigned vvseg;
  37. unsigned vvofs;
  38.  
  39.  
  40. extern int FWIDTH;
  41.  
  42.  
  43. static int vidmodes[8][3] = {
  44.   0x3,  25, 80,
  45.   0x50, 30, 80,
  46.   0x51, 43, 80,
  47.   0x52, 60, 80,
  48.   0x53, 25,132,
  49.   0x58, 30,132,
  50.   0x59, 43,132,
  51.   0x5A, 60,132,
  52. };
  53.  
  54.  
  55. unsigned char boxstyles[2][6] = {
  56.   0xC4,0xB3,0xDA,0xBF,0xC0,0xD9,
  57.   0xCD,0xBA,0xC9,0xBB,0xC8,0xBC,
  58. };
  59.  
  60.  
  61. #define VIDEO 0x10
  62.  
  63. union REGS regs;
  64.  
  65. int video_mode(void) {
  66.  
  67.   regs.h.ah=0x0f;
  68.   int86(VIDEO,®s,®s);
  69.   return regs.h.al;
  70. }
  71.  
  72. void OMEGA_SETUP() {
  73.   //initmouse();
  74.   Oldvmode=video_mode();
  75.   if(Oldvmode==7) {
  76.     vseg=vvseg=0xB000;
  77.     vofs=vvofs=0;
  78.   }
  79.   else {
  80.     vseg=vvseg=0xB800;
  81.     vofs=vvofs=0;
  82.   }
  83.   FWIDTH=sWid*2;
  84. }
  85.  
  86.  
  87. oScreen::oScreen() {
  88.   FGcolor=WHITE;
  89.   BKcolor=BLACK;
  90.   Fillchar=32;
  91. }
  92.  
  93.  
  94. int  oScreen::getscreenwid() {
  95.   return sWid;
  96. }
  97.  
  98. int  oScreen::getscreenlen() {
  99.   return sLen;
  100. }
  101.  
  102. unsigned oScreen::getvvseg() {
  103.   return vvseg;
  104. }
  105.  
  106. unsigned oScreen::getvvofs() {
  107.   return vvofs;
  108. }
  109.  
  110. void oScreen::writeat(int x, int y, int fg, int bk, char *str) {
  111.  
  112.   hidemouse();
  113.   fastwritestr(x,y,(char)fg,(char)bk,(char far *)str,vvseg,vvofs);
  114. }
  115.  
  116. void oScreen::plainwriteat(int x, int y, char *str) {
  117.  
  118.   hidemouse();
  119.   plainwritestr(x,y,(char far *)str,vvseg,vvofs);
  120. }
  121.  
  122. void oScreen::chwriteat(int x, int y, int fg, int bk, unsigned char c) {
  123.  
  124.   hidemouse();
  125.   fastwritech(x,y,(char)fg,(char)bk,c,vvseg,vvofs);
  126. }
  127.  
  128. void oScreen::plainchwriteat(int x, int y, unsigned char c) {
  129.  
  130.   hidemouse();
  131.   plainwritech(x,y,c,vvseg,vvofs);
  132. }
  133.  
  134. void oScreen::writecenter(int y, int fg, int bk, char *str) {
  135.  
  136.   writeat((sWid/2)-(strlen(str)/2),y,fg,bk,str);
  137. }
  138.  
  139. void oScreen::writeshadow(int x, int y) {
  140.  
  141.   hidemouse();
  142.   shadowchar(x,y,vvseg,vvofs);
  143. }
  144.  
  145. void oScreen::writebetween(int y, int x1, int x2, int fg, int bk, char *str) {
  146.  
  147.   int start, len;
  148.   start=x1;
  149.   len=strlen(str);
  150.   len=(x2-start-len) /2;
  151.   start=start+len+1;
  152.   writeat(start,y,fg,bk,str);
  153. }
  154.  
  155. void oScreen::attribln(int y, int x1, int x2, int fg, int bk) {
  156.  
  157.   hidemouse();
  158.   fastattrib(y,x1,x2,(char)fg,(char)bk,vvseg,vvofs);
  159. }
  160.  
  161. void oScreen::fillarea(int x, int y, int x2, int y2, int fg, int bk, char c) {
  162.  
  163.   register char i;
  164.   char *str;
  165.  
  166.   str=new char[sWid+1];
  167.   setmem(str,sWid,c);
  168.   str[(x2-x)+1]='\0';
  169.   for(i=y; i<=y2; i++)
  170.     writeat(x,i,fg,bk,str);
  171.   delete str;
  172.  
  173. }
  174.  
  175. void oScreen::fillscreen(int fg, int bk, char c) {
  176.  
  177.   fillarea(1,1,sWid,sLen,(char)fg,(char)bk,c);
  178. }
  179.  
  180. void oScreen::clearline(int y, int fg, int bk) {
  181.  
  182.   fillarea(1,y,getscreenwid(),y,fg,bk,32);
  183. }
  184.  
  185. void oScreen::clearline(int y, int x1, int x2, int fg, int bk) {
  186.  
  187.   fillarea(x1,y,x2,y,fg,bk,32);
  188. }
  189.  
  190. void oScreen::attribarea(int x, int y, int x2, int y2, int fg, int bk) {
  191.  
  192.   register int i;
  193.   for(i=y; i<=y2; i++)
  194.     attribln(i,x,x2,(char)fg,(char)bk);
  195.  
  196. }
  197.  
  198. void oScreen::attribscreen(int fg, int bk) {
  199.  
  200.   attribarea(1,1,sWid,sLen,(char)fg,(char)bk);
  201. }
  202.  
  203. void oScreen::clrscr() {
  204.  
  205.   fillarea(1,1,sWid,sLen,FGcolor,BKcolor,Fillchar);
  206. }
  207.  
  208. void oScreen::drawline(int y, int x1, int x2, int fg, int bk, int style) {
  209.   if(style<0 || style>1) style=1;
  210.   fillarea(x1,y,x2,y,fg,bk,boxstyles[style][vertbar]);
  211. }
  212.  
  213. void oScreen::drawbox(int x, int y, int x2, int y2, int fg, int bk, int style) {
  214.  
  215.   char *line = new char[(x2-x)+2];
  216.  
  217.   memset(line,32,(x2-x)+2);
  218.   line[(x2-x)+1]='\0';
  219.   line[0]=boxstyles[style][horizbar];
  220.   line[x2-x]=boxstyles[style][horizbar];
  221.  
  222.   drawline(y,x,x2,fg,bk,style);
  223.   chwriteat(x,y,fg,bk,boxstyles[style][upperleft]);
  224.   chwriteat(x2,y,fg,bk,boxstyles[style][upperright]);
  225.   register int i;
  226.   for(i=y+1; i<y2; i++)
  227.     writeat(x,i,fg,bk,line);
  228.   drawline(y2,x,x2,fg,bk,style);
  229.   chwriteat(x,y2,fg,bk,boxstyles[style][bottomleft]);
  230.   chwriteat(x2,y2,fg,bk,boxstyles[style][bottomright]);
  231.  
  232.   delete line;
  233. }
  234.  
  235. void oScreen::drawhollowbox(int x, int y, int x2, int y2, int fg, int bk, int style) {
  236.   char *line = new char[(x2-x)+2];
  237.  
  238.   memset(line,32,(x2-x)+2);
  239.   line[(x2-x)+1]='\0';
  240.   line[0]=boxstyles[style][horizbar];
  241.   line[x2-x]=boxstyles[style][horizbar];
  242.  
  243.   fillarea(x,y+1,x,y2-1,fg,bk,boxstyles[style][horizbar]);
  244.   fillarea(x2,y+1,x2,y2-1,fg,bk,boxstyles[style][horizbar]);
  245.   drawline(y,x,x2,fg,bk,style);
  246.   drawline(y2,x,x2,fg,bk,style);
  247.   chwriteat(x,y,fg,bk,boxstyles[style][upperleft]);
  248.   chwriteat(x2,y,fg,bk,boxstyles[style][upperright]);
  249.   chwriteat(x,y2,fg,bk,boxstyles[style][bottomleft]);
  250.   chwriteat(x2,y2,fg,bk,boxstyles[style][bottomright]);
  251.  
  252.   delete line;
  253. }
  254.  
  255. // Absolute screen writing functions
  256.  
  257. void oScreen::abswritecenter(int y, int fg, int bk, char *str) {
  258.  
  259.   hidemouse();
  260.   fastwritestr((sWid/2)-(strlen(str)/2),y,(char)fg,(char)bk,str,vvseg,vvofs);
  261. }
  262.  
  263. void oScreen::abswritebetween(int y, int x1, int x2, int fg, int bk, char *str) {
  264.  
  265.   hidemouse();
  266.   int start, len;
  267.   start=x1;
  268.   len=strlen(str);
  269.   len=(x2-start-len) /2;
  270.   start=start+len+1;
  271.   fastwritestr(start,y,(char)fg,(char)bk,str,vvseg,vvofs);
  272. }
  273.  
  274.  
  275. void oScreen::absfillarea(int x, int y, int x2, int y2, int fg, int bk, char c) {
  276.  
  277.   hidemouse();
  278.   register char i;
  279.   char *str;
  280.  
  281.   str=new char[sWid+1];
  282.   setmem(str,sWid,c);
  283.   str[(x2-x)+1]='\0';
  284.   for(i=y; i<=y2; i++)
  285.     fastwritestr(x,i,(char)fg,(char)bk,str,vvseg,vvofs);
  286.   delete str;
  287.  
  288. }
  289.  
  290. void oScreen::absfillscreen(int fg, int bk, char c) {
  291.  
  292.   absfillarea(1,1,sWid,sLen,(char)fg,(char)bk,c);
  293. }
  294.  
  295. void oScreen::absclearline(int y, int fg, int bk) {
  296.  
  297.   absfillarea(1,y,getscreenwid(),y,fg,bk,32);
  298. }
  299.  
  300. void oScreen::absclearline(int y, int x1, int x2, int fg, int bk) {
  301.  
  302.   absfillarea(x1,y,x2,y,fg,bk,32);
  303. }
  304.  
  305. void oScreen::absattribarea(int x, int y, int x2, int y2, int fg, int bk) {
  306.  
  307.   hidemouse();
  308.   register int i;
  309.   for(i=y; i<=y2; i++)
  310.     fastattrib(i,x,x2,(char)fg,(char)bk,vvseg,vvofs);
  311.  
  312. }
  313.  
  314. void oScreen::absattribscreen(int fg, int bk) {
  315.  
  316.   absattribarea(1,1,sWid,sLen,(char)fg,(char)bk);
  317. }
  318.  
  319. void oScreen::absclrscr() {
  320.  
  321.   absfillarea(1,1,sWid,sLen,FGcolor,BKcolor,Fillchar);
  322. }
  323.  
  324.  
  325. void oScreen::absdrawline(int y, int x1, int x2, int fg, int bk, int style) {
  326.  
  327.   hidemouse();
  328.   if(style<0 || style>1) style=1;
  329.   register char i;
  330.   char *str;
  331.  
  332.   str=new char[sWid+1];
  333.   setmem(str,sWid,boxstyles[style][vertbar]);
  334.   str[(x2-x1)+1]='\0';
  335.   fastwritestr(x1,y,(char)fg,(char)bk,str,vvseg,vvofs);
  336.   delete str;
  337. }
  338.  
  339.  
  340. void oScreen::absdrawbox(int x, int y, int x2, int y2, int fg, int bk, int style) {
  341.  
  342.   hidemouse();
  343.   char *line = new char[(x2-x)+2];
  344.  
  345.   memset(line,32,(x2-x)+2);
  346.   line[(x2-x)+1]='\0';
  347.   line[0]=boxstyles[style][horizbar];
  348.   line[x2-x]=boxstyles[style][horizbar];
  349.  
  350.   absdrawline(y,x,x2,fg,bk,style);
  351.   fastwritech(x,y,(char)fg,(char)bk,boxstyles[style][upperleft],vvseg,vvofs);
  352.   fastwritech(x2,y,(char)fg,(char)bk,boxstyles[style][upperright],vvseg,vvofs);
  353.   register int i;
  354.   for(i=y+1; i<y2; i++)
  355.     fastwritestr(x,i,(char)fg,(char)bk,line,vvseg,vvofs);
  356.   absdrawline(y2,x,x2,fg,bk,style);
  357.   fastwritech(x,y2,(char)fg,(char)bk,boxstyles[style][bottomleft],vvseg,vvofs);
  358.   fastwritech(x2,y2,(char)fg,(char)bk,boxstyles[style][bottomright],vvseg,vvofs);
  359.  
  360.   delete line;
  361. }
  362.  
  363.  
  364. void oScreen::setvidmode(int m) {
  365.  
  366.  if(m<0 || m>7) return;
  367.  regs.h.ah=0;
  368.  regs.h.al=vidmodes[m][0];
  369.  int86(VIDEO,®s,®s);
  370.  if(video_mode()==vidmodes[m][0]) {
  371.    sLen=vidmodes[m][1];
  372.    sWid=vidmodes[m][2];
  373.    FWIDTH=sWid*2;
  374.  }
  375.  else
  376.    restoreoldvmode();
  377.  
  378. }
  379.  
  380. int oScreen::setvga43() {
  381.   regs.h.ah=0x12;
  382.   regs.h.al=1;
  383.   regs.h.bl=0x30;
  384.   int86(VIDEO,®s,®s);
  385.   if(regs.x.ax==0x12) {
  386.     regs.h.ah=0;
  387.     regs.h.al=3;
  388.     int86(VIDEO,®s,®s);
  389.     sLen=43;
  390.     sWid=80;
  391.     FWIDTH=sWid*2;
  392.     return 1;
  393.   }
  394.   return 0;
  395. }
  396.  
  397. int oScreen::setvga50() {
  398.  
  399.   regs.h.ah=0x1A;
  400.   regs.h.al=0;
  401.   int86(VIDEO,®s,®s);
  402.   unsigned char look=regs.h.bl;
  403.   if(look==8) {
  404.     regs.x.ax=0x1112;
  405.     regs.h.bl=0x00;
  406.     int86(VIDEO,®s,®s);
  407.     sLen=50;
  408.     sWid=80;
  409.     FWIDTH=sWid*2;
  410.     return 1;
  411.   }
  412.   return 0;
  413. }
  414.  
  415.  
  416. void oScreen::restoreoldvmode() {
  417.  regs.h.ah=0;
  418.  regs.h.al=Oldvmode;
  419.  int86(VIDEO,®s,®s);
  420.  sLen=25;
  421.  sWid=80;
  422.  FWIDTH=sWid*2;
  423. }
  424.  
  425. void oScreen::cursoron(void) {
  426.  
  427.   regs.x.ax=0x100;
  428.   regs.x.cx=0x607;
  429.   int86(VIDEO,®s,®s);
  430. }
  431.  
  432. void oScreen::cursoroff(void) {
  433.  
  434.   regs.x.ax=0x100;
  435.   regs.x.cx=0x2000;
  436.   int86(VIDEO,®s,®s);
  437. }
  438.  
  439. void oScreen::blockcursor(void) {
  440.  
  441.   regs.h.ah=1;
  442.   regs.h.ch=0;
  443.   regs.h.cl=15;
  444.   int86(VIDEO,®s,®s);
  445. }
  446.  
  447. void oScreen::linecursor(void) {
  448.  
  449.   regs.h.ah=1;
  450.   regs.h.ch=6;
  451.   regs.h.cl=7;
  452.   int86(VIDEO,®s,®s);
  453. }
  454.  
  455. void oScreen::movexy(int x, int y) {
  456.  
  457.   regs.h.ah=2;
  458.   regs.h.bh=0;
  459.   regs.h.dh=y-1;
  460.   regs.h.dl=x-1;
  461.   int86(VIDEO,®s,®s);
  462. }
  463.  
  464. void oScreen::wherexy(int &x, int &y) {
  465.  
  466.   regs.h.ah=3;
  467.   regs.h.bh=0;
  468.   int86(VIDEO,®s,®s);
  469.   x=regs.h.dl;
  470.   y=regs.h.dh;
  471. }
  472.  
  473.  
  474. void oScreen::save_screen() {
  475.  
  476.   hidemouse();
  477.   screenbuff=new char[sWid*sLen*2];
  478.   movedata(vvseg,vvofs,FP_SEG(screenbuff),FP_OFF(screenbuff),sWid*sLen*2);
  479. }
  480.  
  481. void oScreen::show_screen() {
  482.  
  483.   hidemouse();
  484.   if(screenbuff!=NULL)
  485.     movedata(FP_SEG(screenbuff),FP_OFF(screenbuff),vvseg,vvofs,sWid*sLen*2);
  486. }
  487.  
  488. void oScreen::restore_screen() {
  489.  
  490.   if(screenbuff!=NULL) {
  491.     show_screen();
  492.     delete screenbuff;
  493.   }
  494. }
  495.  
  496. char *oScreen::save_image(int x, int y, int x2, int y2, image &im) {
  497.  
  498.   register int i;
  499.   char *c;
  500.  
  501.   im.len=y2-y+1;
  502.   im.wid=x2-x+1;
  503.  
  504.   im.i=new char[im.len*im.wid*2];
  505.   c=im.i;
  506.   hidemouse();
  507.   for(i=y; i<=y2; i++) {
  508.     movedata(vvseg,vvofs+(((i-1)*FWIDTH)+(x-1)*2),FP_SEG(c),FP_OFF(c),im.wid*2);
  509.     c+=im.wid*2;
  510.   }
  511.   return im.i;
  512. }
  513.  
  514. void oScreen::show_image(int x, int y, image im) {
  515.  
  516.   register int i;
  517.   char *c;
  518.  
  519.   c=im.i;
  520.   hidemouse();
  521.   for(i=y; i<y+im.len; i++) {
  522.     movedata(FP_SEG(c),FP_OFF(c),vvseg,vvofs+(((i-1)*FWIDTH)+(x-1)*2),im.wid*2);
  523.     c+=im.wid*2;
  524.   }
  525. }
  526.  
  527. void oScreen::restore_image(int x, int y, image &im) {
  528.  
  529.   if(im.i!=NULL) {
  530.     show_image(x,y,im);
  531.     delete im.i;
  532.   }
  533. }
  534.  
  535.  
  536. void oScreen::activate_virtual() {
  537.  
  538.   hidemouse();
  539.   vscreen=new char[sWid*sLen*2];
  540.   if(vscreen) {
  541.     vActive=TRUE;
  542.     vvseg=FP_SEG(vscreen);
  543.     vvofs=FP_OFF(vscreen);
  544.     movedata(vseg,0,vvseg,vvofs,sWid*sLen*2);
  545.   }
  546. }
  547.  
  548. void oScreen::deactivate_virtual() {
  549.   if(vActive) {
  550.     display_virtual();
  551.     delete vscreen;
  552.     vvseg=vseg;
  553.     vvofs=vofs;
  554.     vActive=FALSE;
  555.   }
  556. }
  557.  
  558. void oScreen::display_virtual() {
  559.  
  560.   hidemouse();
  561.   movedata(vvseg,vvofs,vseg,0,sWid*sLen*2);
  562. }
  563.  
  564.