home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MAGAZINE / MISC / ITPAUG90.ZIP / GRPIX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1990-06-20  |  9.9 KB  |  436 lines

  1. PROGRAM GrPix;
  2.  
  3. USES Dos,GRAPH, Crt;
  4.  
  5. {* This program demonstrates some Turbo Pascal
  6.  * graphics pixel manipulation functions.
  7.  *
  8.  * It requires the TP BGI drivers in the current
  9.  * directory or an environment variable, BGI,
  10.  * that points to it, e.g.:
  11.  *
  12.  * BGI=D:\TP\BGI
  13.  *
  14.  *}
  15.  
  16. CONST
  17.   MaxLogoRows = 9;
  18.   MaxLogoCols = 42;
  19. TYPE
  20.   BitMapType = ARRAY[1..MaxLogoRows] OF
  21.                               String[MaxLogoCols];
  22. CONST
  23.   CobbLogoPixelMap : BitMapType =
  24.     ('00FFFF000F000FFFF000F00FFFFF000F00FFFFF000',
  25.      '0FFFFFF00F00FFFFFF00F00FF00FF00F00FF00FF00',
  26.      '0FF00FF00F00FF00FF00F00FF00FF00F00FF00FF00',
  27.      '0FF000000F00FF00FF00F00FFFFFF00F00FFFFFF00',
  28.      '0FF000000F00FF00FF00F00FFFFF000F00FFFFF000',
  29.      '0FF000000F00FF00FF00F00FF00FF00F00FF00FF00',
  30.      '0FF00FF00F00FF00FF00F00FF00FF00F00FF00FF00',
  31.      '0FFFFFF00F00FFFFFF00F00FFFFFF00F00FFFFFF00',
  32.      '00FFFF000F000FFFF000F00FFFFF000F00FFFFF000');
  33.  
  34. CONST
  35.   BLACKRECTSTARTCOL = 100;
  36.   BLACKRECTSTARTROW = 100;
  37.   BLACKRECTENDCOL   = 109;
  38.   BLACKRECTENDROW   = 210; 
  39.  
  40.   BLUERECTSTARTCOL  = (BLACKRECTSTARTCOL-2);
  41.   BLUERECTSTARTROW  = (BLACKRECTSTARTROW-2);
  42.   BLUERECTENDCOL    = (BLACKRECTENDCOL + 2 + 33);
  43.   BLUERECTENDROW    = (BLACKRECTENDROW+2); 
  44.  
  45.   WHITERECTSTARTCOL = BLACKRECTSTARTCOL;
  46.   WHITERECTSTARTROW = BLACKRECTSTARTROW;
  47.   WHITERECTENDCOL   = (BLACKRECTENDCOL + 33);
  48.   WHITERECTENDROW   = BLACKRECTENDROW;
  49.  
  50. VAR
  51.   MaxX, MaxY, MaxColor: Integer;
  52.   MaxPix,QuarterPix   : Longint;
  53.  
  54. FUNCTION BlackRectOffset(N: Integer): Integer;
  55. BEGIN
  56.   BlackRectOffset := N * 11
  57. END;
  58.  
  59.     { displays error #, message and exits  }
  60.  
  61. PROCEDURE ErrorExit(ErrCode: Integer;
  62.                     Message: String);
  63. BEGIN
  64.   WriteLn('Error ',ErrCode:3,' returned from ',
  65.                                       Message);
  66.   Halt
  67. END;
  68.  
  69.     { graphics compatible message function     }
  70.  
  71. PROCEDURE GrMessage(Message: String);
  72. BEGIN
  73.   Bar(0,GetMaxY-(TextHeight(Message)*2),
  74.       TextWidth(Message),
  75.       GetMaxY-(TextHeight(Message)));
  76.  
  77.   { write message                        }
  78.  
  79.   OutTextXY(0,GetMaxY-(TextHeight(Message)*2),
  80.             Message);
  81. END;
  82.  
  83. PROCEDURE Pause;
  84. VAR
  85.   TempStr : String;
  86.   A       : Char;
  87. BEGIN
  88.   TempStr := 'Press any key to continue...';
  89.   GrMessage(TempStr);
  90.   WHILE NOT KeyPressed DO; A:= ReadKey;
  91.   GrMessage('                            ')
  92. END;
  93.  
  94.     { randomly draws randomly colored pixels   }
  95.  
  96. PROCEDURE DrawRandomlyColoredPixels;
  97. VAR
  98.   Color, X, Y: Word;
  99.   L          : Longint;
  100.  
  101. BEGIN
  102.  
  103.   { for 1/4 of the pixels on screen    }
  104.  
  105.   FOR L := 1 TO QuarterPix DO BEGIN
  106.  
  107.     { get coordinates and color        }
  108.  
  109.     X := Random(MaxX + 1);
  110.     Y := Random(MaxY + 1);
  111.     Color := Random(MaxColor + 1);
  112.  
  113.     { write pixel                      }
  114.  
  115.     PutPixel(X,Y,Color);
  116.     IF KeyPressed THEN Exit
  117.     END
  118. END;
  119.  
  120.     { fills screen with each color             }
  121.  
  122. PROCEDURE FillScreenWithColoredPixels;
  123. VAR
  124.   Color, X, Y : Word;
  125.   M, N        : Longint;
  126.  
  127. BEGIN
  128.   N := MaxPix DIV MaxColor;
  129.  
  130.         { for each color available     }
  131.  
  132.   X := 0;
  133.   Y := 0;
  134.  
  135.   FOR Color := 1 TO MaxColor DO BEGIN
  136.     FOR M := 1 TO N DO BEGIN
  137.               { write the pixel          }
  138.       PutPixel(X,Y,Color);
  139.       Inc(X);    { next column          }
  140.       { if last column, reset, next row  }
  141.       IF (X > MaxX) THEN BEGIN
  142.         X := 0;
  143.         Inc(Y)    {temp}
  144.         END;
  145.             { if on last line, break           }
  146.       IF (Y > MaxY) THEN Exit;
  147.       IF KeyPressed THEN Exit;
  148.       END;
  149.     IF KeyPressed THEN Exit;
  150.     END;
  151. END;
  152.  
  153.     { draws line from coordinates              }
  154.  
  155. PROCEDURE DrawColoredLine(Fx, Fy, Tx, Ty: Word);
  156.  
  157. VAR
  158.   Pixel,I,J,K,L:  Word;
  159.   Up           :  Boolean;
  160.  
  161. BEGIN
  162.  
  163.   Up := FALSE;
  164.  
  165.         { if drawing -> \ or / }
  166.  
  167.   IF Tx >= Fx THEN BEGIN
  168.     I := Fx;
  169.     K := Tx;
  170.     J := Fy;
  171.     L := Ty;
  172.     IF Ty < Fy THEN Up := TRUE  { drawing /    }
  173.     END
  174.   ELSE BEGIN        { else drawing <- \ or /   }
  175.     I := Tx;
  176.     K := Fx;
  177.     J := Ty;
  178.     L := Fy;
  179.     IF Ty > Fy THEN Up := TRUE   { drawing /    }
  180.     END;
  181.  
  182.   WHILE TRUE DO BEGIN
  183.  
  184.             { get the pixel    }
  185.  
  186.     Pixel := GetPixel(I,J);
  187.  
  188.             { adjust color     }
  189.  
  190.     IF Pixel > 7 THEN
  191.       Dec(Pixel,6)
  192.     ELSE
  193.       Inc(Pixel,7);
  194.  
  195.            { write pixel      }
  196.  
  197.     PutPixel(I,J,Pixel);
  198.  
  199.     { increment (decrement) if needed }
  200.  
  201.     IF J <> L THEN
  202.       IF Up THEN
  203.         Dec(J)
  204.       ELSE
  205.         Inc(J);
  206.     IF I <> K THEN
  207.       Inc(I);
  208.  
  209.     { when to and from are equal   }
  210.  
  211.     IF (J = L) AND (I = K) THEN Exit
  212.     END
  213. END;
  214.  
  215.     { draws lines from center pixel            }
  216.  
  217. PROCEDURE DrawLinesFromCenter;
  218. VAR
  219.   CenterX, CenterY:  Integer;
  220. BEGIN
  221.     CenterX := MaxX DIV 2; CenterY := MaxY DIV 2;
  222.  
  223.     DrawColoredLine(centerx-100,centery-100,
  224.             CenterX,CenterY);
  225.     DrawColoredLine(CenterX,centery-100,
  226.             CenterX,CenterY);
  227.     DrawColoredLine(CenterX+100,centery-100,
  228.             CenterX,CenterY);
  229.     DrawColoredLine(centerx-100,CenterY,
  230.             CenterX,CenterY);
  231.     DrawColoredLine(CenterX+100,CenterY,
  232.             CenterX,CenterY);
  233.     DrawColoredLine(centerx-100,CenterY+100,
  234.             CenterX,CenterY);
  235.     DrawColoredLine(CenterX,CenterY+100,
  236.             CenterX,CenterY);
  237.     DrawColoredLine(CenterX+100,CenterY+100,
  238.             CenterX,CenterY);
  239. END;
  240.  
  241.     { concentric squares from center pixel     }
  242.  
  243. PROCEDURE ExplodingFilledSquare(radius,Color:
  244.                                           Word);
  245. VAR
  246.   CenterX, CenterY, I, J: Word;
  247.  
  248. BEGIN
  249.  
  250.   CenterX := MaxX DIV 2; CenterY := MaxY DIV 2;
  251.  
  252.         { write center pixel   }
  253.  
  254.   PutPixel(CenterX,CenterY,Color);
  255.  
  256.         { for each square      }
  257.  
  258.   FOR I := 0 TO Radius-1 DO BEGIN
  259.             { write top line       }
  260.     FOR J := centerx-i TO CenterX+i-1 DO
  261.       PutPixel(J,centery-i,Color);
  262.             { write sides          }
  263.     FOR J := centery-i TO CenterY+i-1 DO BEGIN
  264.       PutPixel(centerx-i,J,Color);
  265.       PutPixel(CenterX+I,J,Color)
  266.       END;
  267.            { write bottom         }
  268.     FOR J := centerx-i TO CenterX+I DO
  269.       PutPixel(J,CenterY+I,Color);
  270.     IF KeyPressed THEN Exit
  271.     END
  272. END;
  273.  
  274.     { randomly fills display with pixels       }
  275.  
  276. PROCEDURE decay(numpix: Longint; Color: Word);
  277. VAR
  278.   L: Longint;
  279.   X, Y: Word;
  280. BEGIN
  281.   FOR L := 1 TO numpix DO BEGIN
  282.           { get coordinates          }
  283.     X := Random(MaxX+1);
  284.     Y := Random(MaxY+1);
  285.           { write 4 pixels of color  }
  286.     PutPixel(X,Y,Color);
  287.     PutPixel(X+1,Y,Color);
  288.     PutPixel(X,Y+1,Color);
  289.     PutPixel(X+1,Y+1,Color);
  290.     IF KeyPressed THEN Exit
  291.     END
  292. END;
  293.  
  294.  
  295.     { draws filled rectangle in coordinates    }
  296.  
  297. PROCEDURE DrawFilledRectangle(startcol,
  298.         startrow, endcol, endrow, Color: Word);
  299. VAR
  300.   I : Word;
  301. BEGIN
  302.   WHILE (startrow <= endrow) DO BEGIN
  303.     FOR I := startcol TO endcol DO
  304.       PutPixel(I,startrow,Color);
  305.     Inc(startrow)
  306.     END
  307. END;
  308.  
  309.     { draws pixelmap at coordinates            }
  310.  
  311.  
  312. PROCEDURE DrawPixelMap(startcol, startrow: Word;
  313.                        BitMap: BitMapType);
  314.  
  315. FUNCTION AtoH(C: Char): Word;
  316. BEGIN
  317.   IF C > '9' THEN
  318.     AtoH := Ord(C) - Ord('A') + 10
  319.   ELSE
  320.     AtoH := Ord(C) - Ord('0');
  321. END;
  322.  
  323. VAR
  324.   I, J: Word;
  325.   P   : String;
  326.  
  327. BEGIN
  328.         { for each pointer in the array    }
  329.  
  330.   FOR I := 1 TO MaxLogoRows DO BEGIN
  331.     P := BitMap[I];
  332.             { for each byte in the string      }
  333.     FOR J := 1 TO MaxLogoCols DO
  334.             { display the converted pixel      }
  335.       PutPixel(startcol+J,startrow+I,AtoH(P[J]))
  336.     END
  337. END;
  338.  
  339. VAR
  340.   GraphDriver,GraphMode : Integer;
  341.   BgiPath               : String;
  342.   ErrMsg                : String[80];
  343.  
  344. BEGIN
  345.   { get BGI path if one }
  346.  
  347.   BgiPath := GetEnv('BGI');
  348.  
  349.   { initialize graphics System}
  350.  
  351.   GraphDriver := Detect;
  352.   InitGraph(GraphDriver,GraphMode,BgiPath);
  353.  
  354.   IF(GraphDriver < 0) THEN BEGIN
  355.     ErrMsg := 'InitGraph (' +
  356.                GraphErrorMsg(GraphDriver) + ')';
  357.     ErrorExit(GraphDriver,ErrMsg);
  358.     END;
  359.  
  360.   { initialize random number generator }
  361.  
  362.   Randomize;
  363.  
  364.   { initialize global variables          }
  365.  
  366.   MaxX := GetMaxX;
  367.   MaxY := GetMaxY;
  368.   MaxColor := GetMaxColor;
  369.   MaxPix := Longint(MaxX) * Longint(MaxY);
  370.  
  371.   QuarterPix := MaxPix DIV 4;
  372.  
  373.   SetColor(BLACK);
  374.  
  375.   DrawRandomlyColoredPixels;
  376.   Pause;
  377.  
  378.   ClearDevice;
  379.   FillScreenWithColoredPixels;
  380.   Pause;
  381.  
  382.   DrawLinesFromCenter;
  383.   Pause;
  384.  
  385.   ExplodingFilledSquare(50,BLACK);
  386.   Pause;
  387.   ExplodingFilledSquare(50,CYAN);
  388.   Pause;
  389.  
  390.   decay((MaxPix DIV 12),BLACK);
  391.   Pause;
  392.  
  393.  
  394.       { draw COBB Group Logo:                }
  395.       { first, the blue background           }
  396.  
  397.   DrawFilledRectangle(BLUERECTSTARTCOL,
  398.           BLUERECTSTARTROW,BLUERECTENDCOL,
  399.           BLUERECTENDROW,BLUE);
  400.  
  401.       { next, the white logo background      }
  402.   DrawFilledRectangle(WHITERECTSTARTCOL,
  403.           WHITERECTSTARTROW,WHITERECTENDCOL,
  404.           WHITERECTENDROW,WHITE);
  405.  
  406.       { then, each black column              }
  407.  
  408.   DrawFilledRectangle(BLACKRECTSTARTCOL+
  409.           BlackRectOffset(0),BLACKRECTSTARTROW,
  410.           BLACKRECTENDCOL+BlackRectOffset(0),
  411.           BLACKRECTENDROW,BLACK);
  412.  
  413.   DrawFilledRectangle(BLACKRECTSTARTCOL+
  414.           BlackRectOffset(1),BLACKRECTSTARTROW,
  415.           BLACKRECTENDCOL+BlackRectOffset(1),
  416.           BLACKRECTENDROW,BLACK);
  417.   DrawFilledRectangle(BLACKRECTSTARTCOL+
  418.           BlackRectOffset(2),BLACKRECTSTARTROW,
  419.           BLACKRECTENDCOL+BlackRectOffset(2),
  420.           BLACKRECTENDROW,BLACK);
  421.  
  422.   DrawFilledRectangle(BLACKRECTSTARTCOL+
  423.           BlackRectOffset(3),BLACKRECTSTARTROW,
  424.           BLACKRECTENDCOL+BlackRectOffset(3),
  425.           BLACKRECTENDROW,BLACK);
  426.  
  427.       { finally, the COBB pixelmap           }
  428.  
  429.   DrawPixelMap(100,103,CobbLogoPixelMap);
  430.  
  431.   Pause;
  432.  
  433.       { switch back to text mode, cleanup}
  434.   CloseGraph;
  435. END.
  436.