home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / grafik / gif / tpgif / example2.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-03-09  |  14.5 KB  |  412 lines

  1. Program Example;
  2.   {Sample program for Turbo Pascal version 5.0 showing the use of SCR2GIFU.PAS
  3.   unit for screen compression to GIF graphics file which includes the LZW
  4.   routines in CMPRSS.INC.  To use the unit, simply include SCR2GIFU in
  5.   your USES statement, then call 'ScreenToGif(FileName:String):Integer' when you
  6.   are ready to send the screen to a GIF file.  The following is an elaborate
  7.   program allowing the selection of any of the numerous graphics modes
  8.   supported by TP 5.0, automatic selection of a file name 'SCR0000.GIF'
  9.   through 'SCR9999.GIF', display of a test screen, and saving to a GIF file.
  10.   For the graphics mode selection procedure to work properly, all the BGI
  11.   drivers, as either .BGI or .OBJ files, will have to be in the same directory
  12.   as this source code.  To compile this to an .EXE, all the .OBJ files will
  13.   have to be around (use TP's BINOBJ.EXE utility).   For details of the
  14.   SCR2GIFU unit usage, see the header of the SCR2GIFU.PAS file.
  15.  
  16.   Written by Rob Crockett [76167,1561] April 1992.  The include file
  17.   CMPRSS.INC written by Bob Berry [76555,167] 1988. }
  18.  
  19.  Uses CRT,
  20.       DOS,
  21.       GRAPH,
  22.       SCR2GIFU,
  23.       DRIVERS;  {DRIVERS.PAS,
  24.           uses EGAVGA.OBJ, CGA.OBJ, HERC.OBJ, ATT.OBJ,PC3270.OBJ, IBM8514.OBJ}
  25.  
  26.  Var
  27.    Debugger: Boolean;                 {used in detectgraph}
  28.    ForceMode: Integer;                {used in detectgraph}
  29.  
  30.  Function FileExists(FileName: String):Boolean;
  31.      Var
  32.        BinFile : File;
  33.      Begin
  34.        Assign(BinFile,FileName);
  35.        {$I-} Reset(BinFile); {$I+}
  36.        If IOResult=0
  37.          Then
  38.            Begin
  39.              Close(BinFile);
  40.              FileExists:=True;
  41.            End
  42.          Else FileExists:=False;
  43.      End; {of FileExists}
  44.  
  45.  Function GetGifFileName: String;
  46.    {Generates numbered GIF file names until a name is found that is
  47.     not in the current directory.  Named 'SCR0000.GIF' to 'SCR9999.GIF'.
  48.     Utilizes the 'FileExists' procedure.}
  49.  
  50.    Const
  51.      Preface = 'SCR';
  52.      Suffix = '.GIF';
  53.    Var
  54.      GifFileName,
  55.      FileNumberString: String;
  56.      FileNumber      : Integer;
  57.    Begin
  58.      FileNumber:=-1;
  59.      Repeat
  60.        FileNumber:=Succ(FileNumber);
  61.        Str(FileNumber,FileNumberString);
  62.        If (Length(FileNumberString)<4) then
  63.          Repeat
  64.            FileNumberString:='0'+FileNumberString;
  65.          Until (Length(FileNumberString)=4);
  66.        GifFileName:=Preface+FileNumberString+Suffix;
  67.      Until ((Not FileExists(GifFileName)) OR (FileNumber=9999));
  68.      GetGifFileName:=GifFileName;
  69.    End;
  70.  
  71.  
  72. Procedure DrawFigures;
  73.   {Draws a test graphics screen}
  74.  
  75.   Var
  76.     I,J,
  77.     XCenter,YCenter,
  78.     Width,Height      : Integer;
  79.     Radius            : Word;
  80.     XString,YString,
  81.     CString           : String[3];
  82.     InChar            : Char;
  83.   Begin
  84.     XCenter:=GetMaxX DIV 2;
  85.     YCenter:=GetMaxY DIV 2;
  86.     Radius:=GetMaxX DIV 6;
  87.     Height:=TextHeight('T');
  88.     Width:=TextWidth('000');
  89.  
  90.     PutPixel(XCenter,YCenter,LightMagenta);
  91.  
  92.  
  93.     If GetMaxColor<=15
  94.       Then
  95.          For I:=0 to GetMaxColor do
  96.            Begin
  97.              SetColor(I);
  98.              Str(I,CString);
  99.              OutTextXY(10,10+I*Height,CString);
  100.            End
  101.        Else
  102.          For I:=0 to 22 do       {25}
  103.            For J:=0 to 11 do
  104.              Begin
  105.                SetColor(I+J*23);
  106.                Str(I+J*23,CString);
  107.                OutTextXY(10+J*Width,10+I*Height,CString);
  108.             End;
  109.  
  110.     SetColor(Blue);
  111.     Circle(XCenter,YCenter,Radius);
  112.  
  113.     SetColor(Blue);
  114.     Rectangle(0,0,GetMaxX,GetMaxY);
  115.     Rectangle(100,100,115,115);
  116.  
  117.     For I:=1 to 1000 do
  118.       Begin
  119.         PutPixel((GetMaxX div 2)+Random((GetMaxX div 2)-1),
  120.               Random(GetMaxY-1),1+Random(GetMaxColor));
  121.       End;
  122.  
  123.     For I:=1 to 500 do
  124.       Begin
  125.         SetColor(Random(GetMaxColor));
  126.         Line((GetMaxX div 6)*4,(GetMaxY div 4),
  127.           Random(GetMaxX div 2)+(GetMaxX div 2),
  128.           Random(GetMaxY div 3)+1);
  129.       End;
  130.   End;
  131.  
  132. Procedure FillSpace(X1,Y1,X2,Y2:Integer;Ch:Char);
  133.   Var
  134.     FillString: String[80];
  135.     I: Integer;
  136.   Begin
  137.     FillString:='';
  138.     For I:=X1 to X2 do FillString:=FillString+Ch;
  139.     For I:=Y1 to Y2 do
  140.       Begin
  141.         GotoXY(X1,I);
  142.         Write(FillString);
  143.       End;
  144.    End;
  145.  
  146.  
  147. Procedure GetScreenType(Var ScreenDriver,ScreenMode:Integer);
  148.  
  149. Const
  150.    Cursor = '=>';
  151.    DriverNames: Array[1..10] of String[8] =
  152.      ('CGA     ','MCGA    ','EGA     ','EGA64   ',
  153.       'EGAMono ','IBM8514 ','Hercules','ATT400  ',
  154.       'VGA     ','PC3270  ');
  155.    ModeNames  : Array[1..10,0..5] of String[12]=
  156.     {CGA}     ((' 320x200   4',' 320x200   4',' 320x200   4',' 320x200   4',' 640x200   2','            '),
  157.     {MCGA}     (' 320x200   4',' 320x200   4',' 320x200   4',' 320x200   4',' 640x200   2',' 640x480   2'),
  158.     {EGA}      (' 640x200  16',' 640x350  16','            ','            ','            ','            '),
  159.     {EGA64}    (' 640x200  16',' 640x350   4','            ','            ','            ','            '),
  160.     {EGAMono}  ('            ','            ','            ',' 640x350   2','            ','            '),
  161.     {IBM8514}  (' 640x480 256','1024x480 256','            ','            ','            ','            '),
  162.     {Hercules} (' 720x348   2','            ','            ','            ','            ','            '),
  163.     {ATT400}   (' 320x200   4',' 320x200   4',' 320x200   4',' 320x200   4',' 640x200   2',' 640x400   2'),
  164.     {VGA}      (' 640x200  16',' 640x350  16',' 640x480  16','            ','            ','            '),
  165.     {PC3270}   (' 720x350   2','            ','            ','            ','            ','            '));
  166. Var             {procedure GetScreenType}
  167.  
  168.    GrDriver,GrMode,
  169.    Driver,Mode,
  170.    X1,Y1,X2,Y2,X3,Y3,X4,Y4,X5,Y5,
  171.    LoMode,HiMode,Error           :Integer;
  172.    InChar                        :Char;
  173.    Dum                           :String;
  174.  
  175.   Procedure WriteCursors(Color:Integer);   {within Procedure GetScreenType}
  176.     Begin
  177.       TextColor(Color);                {erase cursor}
  178.       GotoXY(X1-3,Y1+GrDriver);
  179.       Write(Cursor);
  180.       GotoXY(X2-3,Y2+GrMode+1);
  181.       Write(Cursor);
  182.     End;
  183.   Procedure WriteModes;                    {within Procedure GetScreenType}
  184.     Var
  185.       Mode: Integer;
  186.     Begin
  187.       FillSpace(X2,Y2,X2+Length(ModeNames[1,0])+4,Y2+6,' ');
  188.       TextColor(LightGray);
  189.       GetModeRange(GrDriver,LoMode,HiMode);
  190.       For Mode:=LoMode to HiMode do
  191.         Begin
  192.           GotoXY(X2,Mode+Y2+1);
  193.           Write(Mode:2,'. ',ModeNames[GrDriver,Mode]);
  194.         End;
  195.     End;
  196.  
  197. Begin                                 {Procedure GetScreenType}
  198.   ClrScr;
  199.   TextColor(Yellow);
  200.   GotoXY(30,2);
  201.   Write('Graphics Mode Menu');
  202.   TextColor(LightGray);
  203.   GotoXY(1,5);
  204.   Writeln('    Use up or down cursor keys to browse the graphics modes.  Press <ENTER>');
  205.   Writeln('to make a selection, or press <ESCAPE> to retain the default mode. ');
  206.   Writeln('The graphics mode first displayed is the default mode selected by TURBO.  ');
  207.   DetectGraph(GrDriver,GrMode);       {Turbo's best choice}
  208.  
  209.   X1:=15; Y1:=9;                      {initial list positions}
  210.   X2:=45; Y2:=9;                      {X1,Y1=Drivers; X2,Y2=Modes}
  211.   X3:=12; Y3:=23;                     {current selection}
  212.   X4:=12; Y4:=25;                     {forcemode}
  213.   X5:=12; Y5:=21;                     {black and white}
  214.  
  215.   TextColor(LightGray);
  216.   For Driver:=1 to 10 do
  217.     Begin
  218.       GotoXY(X1,Driver+Y1);
  219.       Write(Driver:2,'. ',DriverNames[Driver]);
  220.     End;
  221.  
  222.   TextColor(Yellow);
  223.   GetModeRange(GrDriver,LoMode,HiMode);
  224.   WriteModes;
  225.   WriteCursors(Yellow);
  226.   TextColor(Yellow);
  227.   GotoXY(X3,Y3);    Write('Graph Driver:');
  228.   GotoXY(X3,Y3+1);  Write('Graph Mode  :');
  229.   TextColor(White);
  230.   GotoXY(X3+15,Y3);  Write(DriverNames[GrDriver]);
  231.   GotoXY(X3+14,Y3+1);Write(ModeNames[GrDriver,GrMode],' colors');
  232.  
  233.   InChar:='.';
  234.   Repeat
  235.     If KeyPressed Then
  236.       Begin
  237.         InChar:=ReadKey;
  238.         If InChar = Chr(0) then
  239.           Begin
  240.             Case ReadKey of
  241.               'G': Begin End;                              {Home}
  242.               'I': Begin End;                              {Pg Up}
  243.               'H': Begin                                   {Up Arrow}
  244.                      WriteCursors(Black);
  245.                      If GrMode>LoMode                   {decrease mode}
  246.                        Then GrMode:=GrMode-1
  247.                        Else                           {decrease driver}
  248.                          Begin
  249.                            If GrDriver>1
  250.                              Then GrDriver:=GrDriver-1
  251.                              Else GrDriver:=10;
  252.                            GetModeRange(GrDriver,LoMode,HiMode);
  253.                            WriteModes;                {show new modes}
  254.                            GrMode:=HiMode;            {update gr/hi/lomode}
  255.                          End;
  256.                      WriteCursors(Yellow);
  257.                    End;
  258.               'K': Begin                                      {Left Arrow}
  259.                    End;
  260.               'M': Begin                                      {Right Arrow}
  261.                    End;
  262.               'O': Begin                                      {End}
  263.                    End;
  264.               'P': Begin                                      {Down Arrow}
  265.                      WriteCursors(Black);
  266.                      If GrMode<HiMode                 {increase mode}
  267.                        Then GrMode:=GrMode+1
  268.                        Else                           {increase driver}
  269.                          Begin
  270.                            If GrDriver<10
  271.                              Then GrDriver:=GrDriver+1
  272.                              Else GrDriver:=1;
  273.                            GetModeRange(GrDriver,LoMode,HiMode);
  274.                            WriteModes;                {show new modes}
  275.                            GrMode:=LoMode;            {update gr/hi/lomode}
  276.                          End;
  277.                      WriteCursors(Yellow);
  278.                    End;
  279.               'Q': Begin                                      {Pg Down}
  280.                    End;
  281.             End;   {of Case}
  282.             Sound(1000);Delay(3);Nosound;
  283.             TextColor(White);
  284.             GotoXY(X3+15,Y3);  Write(DriverNames[GrDriver]);
  285.             GotoXY(X3+14,Y3+1);Write(ModeNames[GrDriver,GrMode],' colors');
  286.           End  {of if InChar=chr(0)}
  287.         Else   {of if InChar=chr(0)}
  288.           Begin
  289.             Case InChar of
  290.               {'a'..'z': InChar:=UpCase(InChar); }
  291.                'Q','q': InChar:=Chr(27);          {escape}
  292.             End;   {of case upcase(InChar)}
  293.           End;    {of if inchar=chr(0) else}
  294.       End;    {of if keypressed}
  295.   Until (InChar=Chr(13)) or (InChar=Chr(27));      {return or escape}
  296.   If InChar=Chr(13) then                                {return}
  297.     Begin
  298.       ScreenDriver:=GrDriver;
  299.       ScreenMode:=GrMode;
  300.     End;         {of inchar=return}
  301.   If Debugger Then
  302.     Begin
  303.       TextColor(LightGreen);
  304.       GotoXY(X4,Y4);
  305.       Write('ForceMode(0): ');
  306.       Readln(Dum);
  307.       If (Length(Dum)<>0)
  308.         Then
  309.           Begin
  310.             Val(Dum,ForceMode,Error);
  311.             If (Error<>0) then ForceMode:=0;
  312.           End
  313.         Else ForceMode:=0;
  314.     End;
  315. End;             {of Procedure GetScreenType}
  316.  
  317.  
  318. Procedure SetMode(Mode : Byte);
  319.      {Uses EGA/VGA BIOS interupt service to directly access EGA/VGA
  320.       graphics modes not supported by virgin Turbo Pascal.  Then,
  321.       dispite being in the requested graphics mode, Turbo considers
  322.       you to be still in text mode}
  323.      Var
  324.        Regs: Registers;
  325.      Begin
  326.        With Regs do
  327.          Begin
  328.            AH := 0;
  329.            AL :=Mode;
  330.          End;
  331.        Intr($10, Regs);
  332.       { If Mode=0 then DirectVideo:=True Else DirectVideo:=False;}
  333.                 {allow writes to function correctly}
  334.      End;
  335.  
  336. Function InitializeGraphics: Integer;
  337.   {initializes all the graphics BGI drivers in one shot, as external
  338.    procedures in object files.  See 'Uses ... Drivers' at beginning of
  339.    program.  Returns accumulated error.}
  340.   Var
  341.     GraphError              : Integer;
  342.     Xaspect,Yaspect         :Word;
  343.   Begin
  344.     GraphError:=RegisterBGIdriver(@CGADriverProc)
  345.                +RegisterBGIdriver(@EGAVGADriverProc)
  346.                +RegisterBGIdriver(@HercDriverProc)
  347.                +RegisterBGIdriver(@ATTDriverProc)
  348.                +RegisterBGIdriver(@PC3270DriverProc)
  349.                +RegisterBGIdriver(@IBM8514DriverProc)
  350.              { +RegisterBGIfont(@GothicFontProc)   }
  351.              { +RegisterBGIfont(@SansSerifFontProc)}
  352.              { +RegisterBGIfont(@SmallFontProc)    }
  353.              { +RegisterBGIfont(@TriplexFontProc)  } ;
  354.     InitializeGraphics:=GraphError;
  355.   End;        {of InitializeGraphics}
  356.  
  357. Procedure StartTone;
  358.   Begin
  359.     Sound(1000);
  360.     Delay(50);
  361.     NoSound;
  362.   End;
  363.  
  364. Procedure OkTone;
  365.   Begin
  366.     Sound(900);
  367.     Delay(100);
  368.     Sound(1000);
  369.     Delay(100);
  370.     NoSound;
  371.   End;
  372.  
  373. Procedure NotOkTone;
  374.   Begin
  375.     Sound(50);
  376.     Delay(500);
  377.     NoSound;
  378.   End;
  379.  
  380.  
  381. {**********MAIN PROGRAM**************************************************}
  382.  
  383. Var
  384.   GifFileName: String;
  385.   GraphDriver,GraphMode,BGILoadError,
  386.   GifCompressError: Integer;
  387. Begin
  388.   Debugger:=False;                           {used by GetScreenType...}
  389.                                              {...set true to use forcemode}
  390.   ForceMode:=0;                              {used by getScreenType}
  391.   BGILoadError:=InitializeGraphics;          {identify graphics drivers}
  392.   GetScreenType(GraphDriver,GraphMode);      {select graphics mode}
  393.   If ForceMode<>0
  394.     Then SetMode(ForceMode)                  {set a graphics mode outside turbo}
  395.     Else InitGraph(GraphDriver,GraphMode,'');{turbo mode}
  396.  
  397.   {  GraphDriver:=Detect;                }   {All of the above can be skipped}
  398.   {  InitGraph(GraphDriver,GraphMode,'');}   {..by using these two lines.}
  399.  
  400.   DrawFigures;                               {draw some graphics}
  401.   GifFileName:=GetGifFileName;               {find name not already used}
  402.   SetColor(1);                               {set color for graphics write}
  403.   OutTextXY(2,2,GifFileName);                {write file name in corner}
  404.   StartTone;                                 {tone at start of save}
  405.   GifCompressError:=ScreenToGif(GifFileName);{save screen to GIF file}
  406.   CloseGraph;                                {close graphics and exit}
  407.   If GifCompressError=0                      {status tones}
  408.     Then OkTone
  409.     Else NotOkTone;
  410. End.
  411.  
  412.