home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / GRAFDEMO.ZIP / SETPXL.C < prev   
Text File  |  1989-05-23  |  10KB  |  371 lines

  1. #include <dos.h>
  2. #include <malloc.h>
  3.  
  4. #define INCL_SUB
  5. #include <os2.h>
  6.  
  7.  
  8. /*********************/
  9. /* globale Variablen */
  10. /*********************/
  11.  
  12. static int scr_lock;      /* Return Code von Screen-Lock */
  13.  
  14. static long *video_plane0;  /* savearea fuer videoplanes */
  15. static long *video_plane1;  /* used for SaveRedrawWait   */
  16. static long *video_plane2;
  17. static long *video_plane3;
  18.  
  19. /*******************/
  20. /*   EGA-Ports     */
  21. /*******************/
  22.  
  23. #define EGA_control       0x3ce        /* Grafik-Controller Control-Port */
  24. #define EGA_control_data  0x3cf        /* Grafik-Controller Daten-Port   */
  25.  
  26. #define EGA_read_map      0x04         /* Read-Map-Register im  */
  27.                                        /* Grafik-Controller     */
  28. #define EGA_mode          0x05         /* Mode-Register im      */
  29.                                        /* Grafik-Controller     */
  30. #define EGA_bit_mask      0x08         /* Bitmasken-Register im */
  31.                                        /* Grafik-Controller     */
  32.  
  33. #define EGA_sequence      0x3c4        /* Sequencer  Control-Port */
  34. #define EGA_sequence_data 0x3c5        /* Sequencer  Daten-Port   */
  35.  
  36. #define EGA_map_mask      0x02         /* Map-Mask-Register im */
  37.                                        /* Sequencer            */
  38.                                        
  39. extern int   EGA_base_adr;
  40. extern char *EGA_font_descr;
  41. /******************/
  42. /******************/
  43. setpxl(x_koord,y_koord,color)
  44.  
  45. int x_koord, y_koord, color;
  46.  
  47. {
  48.   char *p_pixel;
  49.   int   bit_maske;
  50.   int   dummy;
  51.  
  52.   /* Screen Lock holen */
  53.   
  54.   VioScrLock(LOCKIO_WAIT,&scr_lock,0);
  55.       
  56.   /* Pixeladresse berechnen */
  57.       
  58.   FP_SEG(p_pixel) = EGA_base_adr;
  59.   FP_OFF(p_pixel) = y_koord*80 + x_koord/8;
  60.       
  61.   /* Bit-Maske berechnen */
  62.       
  63.   bit_maske = 0x80 >>(x_koord % 8);
  64.       
  65.   /* Setze Bitmasken-Register */
  66.       
  67.   outp(EGA_control,EGA_bit_mask);
  68.   outp(EGA_control_data,bit_maske);
  69.       
  70.   /* folgende Befehlssequenz ist durch die Hardware bedingt */
  71.       
  72.   outp(EGA_control,EGA_mode);          /* setze Write-Mode 2 */
  73.   outp(EGA_control_data,2);
  74.  
  75.   dummy = *p_pixel;                    /* lade Planes in Latch-Register */
  76.   
  77.   *p_pixel = color;                    /* setze Pixel */
  78.       
  79.   /* Setze alle Register zurueck */
  80.       
  81.   outp(EGA_control,EGA_mode);          /* default Mode   */
  82.   outp(EGA_control_data,0);
  83.   
  84.   outp(EGA_control,EGA_bit_mask);      /* enable alle Bits   */
  85.   outp(EGA_control_data,0xff);
  86.   
  87.   /* Screen wieder freigeben */
  88.   
  89.   VioScrUnLock(0);
  90.       
  91. }
  92.  
  93. /***********/
  94.  
  95. write_char(x_pos,y_pos,c,color)
  96.  
  97. int x_pos,y_pos,c,color;
  98.  
  99. {
  100.   char *p_char;
  101.   char *p_char_descr;
  102.   int   i;
  103.   int   dummy;
  104.   
  105.   /* Screen Lock holen */
  106.   
  107.   VioScrLock(LOCKIO_WAIT,&scr_lock,0);
  108.       
  109.   /* Adresse der Fontbeschreibung berechnen */
  110.   
  111.   p_char_descr = EGA_font_descr + 16*c;  /* Fontsize 8*16 */
  112.   
  113.   /* Adresse des 1. Bytes berechnen */
  114.   
  115.   FP_SEG(p_char) = EGA_base_adr;
  116.   FP_OFF(p_char) = y_pos*80*16 +x_pos;   /* Fontsize 8*16 */
  117.   
  118.   /* setze alle Bytes */
  119.   
  120.   for (i = 0; i < 14; i++, p_char+=80, p_char_descr++)
  121.   {
  122.     dummy = *p_char;                    /* lade Planes in Latch Register */
  123.     *p_char = 0;                        /* setze Pixel auf Hintergrund   */
  124.     
  125.     outp(EGA_control,EGA_bit_mask);     /* erlaube die zu setzenden Pixel */
  126.     outp(EGA_control_data,*p_char_descr);
  127.     
  128.     outp(EGA_sequence,EGA_map_mask);    /* enable zu setzende Planes */
  129.     outp(EGA_sequence_data,color);
  130.     
  131.     *p_char = 0xff;                     /* setze Pixel */
  132.  
  133.     /* Setze Alle Register zurueck */
  134.       
  135.     outp(EGA_sequence,EGA_map_mask);     /* enable alle Planes */
  136.     outp(EGA_sequence_data,0xff);
  137.   
  138.     outp(EGA_control,EGA_bit_mask);      /* enable alle Bits   */
  139.     outp(EGA_control_data,0xff);
  140.   }
  141.   
  142.   /* Screen wieder freigeben */
  143.   
  144.   VioScrUnLock(0);
  145.       
  146. }
  147.  
  148. write_text(x_pos,y_pos,text,color)
  149.  
  150. int    x_pos,y_pos;
  151. char *text;
  152. int   color;
  153.  
  154. {
  155.   while (*text)
  156.   {
  157.     write_char(x_pos,y_pos,*text,color);
  158.     text++;
  159.     x_pos++;
  160.   }
  161. }
  162.  
  163. /******************/
  164.  
  165. save_redraw_thread()
  166.  
  167. {
  168.   int  save_redraw_cmd;  /* indicates, whether to save or to redraw screen */
  169.   
  170.   unsigned  count;
  171.   long     *buffer;
  172.   long      dummy;
  173.   
  174.   VIOMODEINFO  mode_info;   /* to save and restore video mode */
  175.   
  176.   unsigned  save_seg_adr;  /* Segmentadresse des Save-Buffers */
  177.  
  178.   /* allocate video plane buffers */
  179.   
  180.   DosAllocSeg(38400,&save_seg_adr,0);
  181.   FP_SEG(video_plane0) = save_seg_adr;
  182.   FP_OFF(video_plane0) = 0;
  183.   
  184.   DosAllocSeg(38400,&save_seg_adr,0);
  185.   FP_SEG(video_plane1) = save_seg_adr;
  186.   FP_OFF(video_plane1) = 0;
  187.   
  188.   DosAllocSeg(38400,&save_seg_adr,0);
  189.   FP_SEG(video_plane2) = save_seg_adr;
  190.   FP_OFF(video_plane2) = 0;
  191.   
  192.   DosAllocSeg(38400,&save_seg_adr,0);
  193.   FP_SEG(video_plane3) = save_seg_adr;
  194.   FP_OFF(video_plane3) = 0;
  195.   
  196.   while (1)              /* this thread continues forever */
  197.   {
  198.     VioSavRedrawWait(VSRWI_SAVEANDREDRAW,&save_redraw_cmd,0);
  199.  
  200.     if (save_redraw_cmd == VSRWN_SAVE)
  201.     {
  202.       /* save screen */
  203.       
  204.       mode_info.cb     = sizeof(VIOMODEINFO);
  205.       VioGetMode(&mode_info,0);
  206.   
  207.       FP_SEG(buffer) = EGA_base_adr;
  208.       
  209.       /* Video Plane 0 */
  210.       
  211.       outp(EGA_control,EGA_read_map);    /* enable Plane 0 */
  212.       outp(EGA_control_data,0);
  213.       
  214.       for (count = 0; count < 38400; count+=4)
  215.       {
  216.         FP_OFF(buffer) = count;
  217.         video_plane0[count>>2] = *buffer;
  218.       }
  219.       /* Video Plane 1 */
  220.       
  221.       outp(EGA_control,EGA_read_map);    /* enable Plane 1 */
  222.       outp(EGA_control_data,1);
  223.       
  224.       for (count = 0; count < 38400; count+=4)
  225.       {
  226.         FP_OFF(buffer) = count;
  227.         video_plane1[count>>2] = *buffer;
  228.       }
  229.       /* Video Plane 2 */
  230.       
  231.       outp(EGA_control,EGA_read_map);    /* enable Plane 2 */
  232.       outp(EGA_control_data,2);
  233.       
  234.       for (count = 0; count < 38400; count+=4)
  235.       {
  236.         FP_OFF(buffer) = count;
  237.         video_plane2[count>>2] = *buffer;
  238.       }
  239.       /* Video Plane 3 */
  240.       
  241.       outp(EGA_control,EGA_read_map);    /* enable Plane 3 */
  242.       outp(EGA_control_data,3);
  243.       
  244.       for (count = 0; count < 38400; count+=4)
  245.       {
  246.         FP_OFF(buffer) = count;
  247.         video_plane3[count>>2] = *buffer;
  248.       }
  249.       
  250.       /* Register ruecksetzen */
  251.       
  252.       outp(EGA_sequence,EGA_map_mask);    /* enable alle Planes */
  253.       outp(EGA_sequence_data,0xff);
  254.     }
  255.     else
  256.     {
  257.       PVIOPALSTATE palette_save;
  258.       PVIOPALSTATE palette_tmp;
  259.       
  260.       /* redraw screen */
  261.  
  262.       VioSetMode(&mode_info,0);
  263.  
  264.       /* do invisible screen redraw */
  265.       
  266.       palette_save = (PVIOPALSTATE) malloc(sizeof(VIOPALSTATE)+30);
  267.       palette_tmp  = (PVIOPALSTATE) malloc(sizeof(VIOPALSTATE)+30);
  268.       
  269.       palette_save->cb     = sizeof(VIOPALSTATE)+30;
  270.       palette_save->type   = 0;   /* get palette registers    */
  271.       palette_save->iFirst = 0;   /* starting with register 0 */
  272.       
  273.       VioGetState(palette_save,0);
  274.       
  275.       memcpy(palette_tmp,palette_save,palette_save->cb);
  276.       
  277.       for (count = 0; count < 16; count++)
  278.         palette_tmp->acolor[count] = 0;
  279.       
  280.       VioSetState(palette_tmp,0);
  281.       
  282.       /* now do redraw */
  283.       
  284.       FP_SEG(buffer) = EGA_base_adr;
  285.       
  286.       /* Video Plane 0 */
  287.       
  288.       outp(EGA_sequence,EGA_map_mask);    /* enable Plane 0 */
  289.       outp(EGA_sequence_data,1);
  290.       
  291.       for (count = 0; count < 38400; count+=4)
  292.       {
  293.         FP_OFF(buffer) = count;
  294.         dummy = *buffer;
  295.         *buffer = video_plane0[count>>2];
  296.       }
  297.       /* Video Plane 1 */
  298.       
  299.       outp(EGA_sequence,EGA_map_mask);    /* enable Plane 1 */
  300.       outp(EGA_sequence_data,2);
  301.       
  302.       for (count = 0; count < 38400; count+=4)
  303.       {
  304.         FP_OFF(buffer) = count;
  305.         dummy = *buffer;
  306.         *buffer = video_plane1[count>>2];
  307.       }
  308.       /* Video Plane 2 */
  309.       
  310.       outp(EGA_sequence,EGA_map_mask);    /* enable Plane 2 */
  311.       outp(EGA_sequence_data,4);
  312.       
  313.       for (count = 0; count < 38400; count+=4)
  314.       {
  315.         FP_OFF(buffer) = count;
  316.         dummy = *buffer;
  317.         *buffer = video_plane2[count>>2];
  318.       }
  319.       /* Video Plane 3 */
  320.       
  321.       outp(EGA_sequence,EGA_map_mask);    /* enable Plane 3 */
  322.       outp(EGA_sequence_data,8);
  323.       
  324.       for (count = 0; count < 38400; count+=4)
  325.       {
  326.         FP_OFF(buffer) = count;
  327.         dummy = *buffer;
  328.         *buffer = video_plane3[count>>2];
  329.       }
  330.       
  331.       /* Register ruecksetzen */
  332.       
  333.       outp(EGA_sequence,EGA_map_mask);    /* enable alle Planes */
  334.       outp(EGA_sequence_data,0xff);
  335.       
  336.       /* now make screen visible */
  337.       
  338.       VioSetState(palette_save,0);
  339.       
  340.     }
  341.   }
  342. }
  343.  
  344. /*****************/
  345.  
  346. mode_thread()
  347.  
  348. {
  349.   int          mode_cmd;
  350.   VIOMODEINFO  mode_info;
  351.  
  352.   while (1)                /* this thread continues forever */
  353.   {
  354.     VioModeWait(0,&mode_cmd,0);
  355.     
  356.     /* setting EGA-640*350 */
  357.  
  358.     mode_info.cb     = sizeof(VIOMODEINFO);
  359.     VioGetMode(&mode_info,0);
  360.   
  361.     mode_info.fbType = VGMT_GRAPHICS | VGMT_OTHER;
  362.     mode_info.color  = 4;
  363.     mode_info.col    = 80;
  364.     mode_info.row    = 25;
  365.     mode_info.hres   = 640;
  366.     mode_info.vres   = 350;
  367.            
  368.     VioSetMode(&mode_info,0);
  369.   }
  370. }
  371.