home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / GDIDEMO.ZIP / ARTY.CPP next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  9.9 KB  |  353 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. // Arty demo window object
  4.  
  5. #include <owl.h>
  6. #include <stdlib.h>
  7. #include "demobase.h"
  8. #include "arty.h"           // class definition for TArtyWindow
  9. #include "artypriv.h"       // internal component classes of TArtyWindow
  10.  
  11.  
  12. //---------- TList ----------------------------------------------
  13.  
  14. /* Initialize the list-of-lines object */
  15. TList::TList( int Max )
  16. {
  17.   MaxLines = min( Max, MaxLineCount );
  18.   CurrentLine = 1;
  19.   Xmax = 0;
  20.   Ymax = 0;
  21.   ColorDuration = MaxColorDuration;
  22.   IncrementCount = 0;
  23.   MaxDelta = 10;
  24.   PenColor = RGB(random(256), random(256), random(256));
  25. };
  26.  
  27. /* Keep X within range, and reverse Delta if necessary to do so */
  28. void TList::AdjustX( int &X, int &DeltaX )
  29. {
  30.   int TestX = X + DeltaX;
  31.   if ((TestX < 1) || (TestX > Xmax)) {
  32.     TestX = X;
  33.     DeltaX = -DeltaX;
  34.   };
  35.   X = TestX;
  36. };
  37.  
  38. /* Keep Y within range, and reverse Delta if necessary to do so */
  39. void TList::AdjustY( int &Y, int &DeltaY )
  40. {
  41.   int TestY = Y + DeltaY;
  42.   if ((TestY < 1) || (TestY > Ymax)) {
  43.     TestY = Y;
  44.     DeltaY = -DeltaY;
  45.   };
  46.   Y = TestY;
  47. };
  48.  
  49. /* Clear the array of lines */
  50. void TList::ResetLines()
  51. {
  52.   int StartX, StartY, I;
  53.  
  54.   StartX = Xmax / 2;
  55.   StartY = Ymax / 2;
  56.   for( I = 0; I < MaxLines; I++) {
  57.     Line[I].LX1 = StartX;
  58.     Line[I].LX2 = StartX;
  59.     Line[I].LY1 = StartY;
  60.     Line[I].LY2 = StartY;
  61.     Line[I].Color = 0;
  62.   };
  63.   X1 = StartX;
  64.   X2 = StartX;
  65.   Y1 = StartY;
  66.   Y2 = StartY;
  67. };
  68.  
  69. /* Scale the old line coordinates to the new Xmax and Ymax coordinates.
  70.   The new Xmax and new Ymax are passed in as parameters so we can
  71.   calculate the scaling ratios. */
  72. void TList::ScaleTo( int NewXmax, int NewYmax )
  73. {
  74.   int I;
  75.   float RatioX, RatioY;
  76.  
  77.   if ((!Xmax) || (!Ymax)) { /* at startup, Xmax and Ymax are zero */
  78.     Xmax = NewXmax;
  79.     Ymax = NewYmax;
  80.     ResetLines();
  81.   } else {
  82.     RatioX = NewXmax / Xmax;
  83.     RatioY = NewYmax / Ymax;
  84.     X1 = X1 * RatioX;
  85.     X2 = X2 * RatioX;
  86.     Y1 = Y1 * RatioY;
  87.     Y2 = Y2 * RatioY;
  88.     for( I = 0; I < MaxLines; I++) {
  89.       Line[I].LX1 = Line[I].LX1 * RatioX;
  90.       Line[I].LX2 = Line[I].LX2 * RatioX;
  91.       Line[I].LY1 = Line[I].LY1 * RatioY;
  92.       Line[I].LY2 = Line[I].LY2 * RatioY;
  93.     };
  94.   };
  95.   Xmax = NewXmax;
  96.   Ymax = NewYmax;
  97. };
  98.  
  99. /* The low-level Draw method of the object. */
  100. void TList::Draw( HDC DC, int a1, int b1, int a2, int b2, long lPenColor )
  101. {
  102.   HPEN OldPen;
  103.  
  104.   OldPen = (HPEN)SelectObject(DC, CreatePen(PS_SOLID, 1, lPenColor));
  105.   MoveTo(DC, a1, b1);
  106.   LineTo(DC, a2, b2);
  107.   DeleteObject(SelectObject(DC, OldPen));
  108. };
  109.  
  110. /* The high-level Draw method of the object. */
  111. void TList::DrawLine( HDC DC, int Index )
  112. {
  113.   Draw(DC, Line[Index].LX1,
  114.            Line[Index].LY1,
  115.            Line[Index].LX2,
  116.            Line[Index].LY2,
  117.            Line[Index].Color);
  118. };
  119.  
  120. /* The high-level draw which erases a line. */
  121. void TList::EraseLine( HDC DC, int Index )
  122. {
  123.   Draw(DC, Line[Index].LX1,
  124.            Line[Index].LY1,
  125.            Line[Index].LX2,
  126.            Line[Index].LY2,
  127.            RGB(0,0,0));
  128. };
  129.  
  130. /* Redraw all the lines in the array. */
  131. void TList::Redraw( HDC DC )
  132. {
  133.   for( int I = 0; I < MaxLines; I++ )
  134.     DrawLine(DC, I);
  135. };
  136.  
  137. /* Reset the color counter and pick a random color. */
  138. void TList::SelectNewColor()
  139. {
  140.   ColorDuration = MaxColorDuration;
  141.   PenColor = RGB(random(256), random(256), random(256));
  142. };
  143.  
  144. /* Pick random directional deltas and reset the delta counter. */
  145. void TList::SelectNewDeltaValues()
  146. {
  147.   DeltaX1 = random(MaxDelta)-(MaxDelta / 2);
  148.   DeltaX2 = random(MaxDelta)-(MaxDelta / 2);
  149.   DeltaY1 = random(MaxDelta)-(MaxDelta / 2);
  150.   DeltaY2 = random(MaxDelta)-(MaxDelta / 2);
  151.   IncrementCount = 2 * (1 + random(10));
  152. };
  153.  
  154. /* Process the movement of one line. */
  155. void TList::LineTick( HDC DC )
  156. {
  157.   EraseLine(DC, CurrentLine);
  158.   if (ColorDuration < 0)   SelectNewColor();
  159.   if (!IncrementCount)     SelectNewDeltaValues();
  160.   AdjustX(X1,DeltaX1);  AdjustX(X2,DeltaX2);
  161.   AdjustY(Y1,DeltaY1);  AdjustY(Y2,DeltaY2);
  162.  
  163.   Line[CurrentLine].LX1 = X1;
  164.   Line[CurrentLine].LX2 = X2;
  165.   Line[CurrentLine].LY1 = Y1;
  166.   Line[CurrentLine].LY2 = Y2;
  167.   Line[CurrentLine].Color = PenColor;
  168.  
  169.   DrawLine(DC, CurrentLine);
  170.   CurrentLine++;
  171.   if (CurrentLine >= MaxLines)  CurrentLine = 1;
  172.   ColorDuration--;
  173.   IncrementCount--;
  174. };
  175.  
  176.  
  177. //------------ TQuadList ----------------------------------------
  178.  
  179. /* Draw the line and 3 reflections of it. */
  180. void TQuadList::DrawLine( HDC DC, int Index )
  181. {
  182.   Draw(DC, Line[Index].LX1,
  183.            Line[Index].LY1,
  184.            Line[Index].LX2,
  185.            Line[Index].LY2,
  186.            Line[Index].Color);
  187.   Draw(DC, Xmax - Line[Index].LX1,
  188.            Line[Index].LY1,
  189.            Xmax - Line[Index].LX2,
  190.            Line[Index].LY2,
  191.            Line[Index].Color);
  192.   Draw(DC, Line[Index].LX1,
  193.            Ymax - Line[Index].LY1,
  194.            Line[Index].LX2,
  195.            Ymax - Line[Index].LY2,
  196.            Line[Index].Color);
  197.   Draw(DC, Xmax - Line[Index].LX1,
  198.            Ymax - Line[Index].LY1,
  199.            Xmax - Line[Index].LX2,
  200.            Ymax - Line[Index].LY2,
  201.            Line[Index].Color);
  202. };
  203.  
  204. /* Erase the line and 3 reflections of it. */
  205. void TQuadList::EraseLine( HDC DC, int Index )
  206. {
  207.   Draw(DC, Line[Index].LX1,
  208.            Line[Index].LY1,
  209.            Line[Index].LX2,
  210.            Line[Index].LY2,
  211.            RGB(0,0,0));
  212.   Draw(DC, Xmax - Line[Index].LX1,
  213.            Line[Index].LY1,
  214.            Xmax - Line[Index].LX2,
  215.            Line[Index].LY2,
  216.            RGB(0,0,0));
  217.   Draw(DC, Line[Index].LX1,
  218.            Ymax - Line[Index].LY1,
  219.            Line[Index].LX2,
  220.            Ymax - Line[Index].LY2,
  221.            RGB(0,0,0));
  222.   Draw(DC, Xmax - Line[Index].LX1,
  223.            Ymax - Line[Index].LY1,
  224.            Xmax - Line[Index].LX2,
  225.            Ymax - Line[Index].LY2,
  226.            RGB(0,0,0));
  227. };
  228.  
  229. //----------- TArtyWindow ------------------------------------------
  230.  
  231. TArtyWindow::TArtyWindow( PTWindowsObject AParent, LPSTR ATitle ) :
  232.                 TBaseDemoWindow( AParent, ATitle )
  233. {
  234.   StaticControl = new TStatic(this, 100,
  235.     "Press Left Button to pause, Right Button to Clear",10,10,10,10,0);
  236.   Iconized = False;
  237.   TextHeight = 20;
  238.   Paused = FALSE;
  239.  
  240.   /* Initialize two line list objects:
  241.       BigLineList is the 4-reflection artwork that is displayed in
  242.       a full sized window.  Mouse clicks will pause or clear
  243.       the display, and the line list will be scaled to the
  244.       new window coordinates when the window is resized.
  245.  
  246.       IconicLineList is a smaller list implementing a single-line
  247.       quark to display in the iconized window region.  Since
  248.       mouse clicks are not sent to iconized windows, the icon
  249.       cannot be paused or cleared, and since there is only one
  250.       icon window size, scaling the lines to new coordinates
  251.       has no visual effect.
  252.  
  253.     The List pointer will be toggled between the two line list
  254.     objects: when the window is iconized, List will point to the
  255.     IconicLineList object.  When the window is restored to full
  256.     size, List will be made to point to the BigLineList object.
  257.     This is so the window routines don't have to know which kind
  258.     of list they're dealing with.  Keyword: polymorphism.   */
  259.  
  260.   BigLineList = new TQuadList(MaxLineCount);
  261.   IconicLineList = new TList(MaxIconicLineCount);
  262.   List = BigLineList;
  263. };
  264.  
  265. /* Dispose of the objects that this window object created.  There's
  266.   no need to dispose the List pointer, since it will only point to
  267.   one of these two objects which are being disposed by their
  268.   primary pointers */
  269. TArtyWindow::~TArtyWindow()
  270. {
  271.   delete BigLineList;
  272.   delete IconicLineList;
  273. };
  274.  
  275. void TArtyWindow::GetWindowClass( WNDCLASS& WndClass )
  276. {
  277.   TBaseDemoWindow::GetWindowClass( WndClass );
  278.   WndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  279.   WndClass.hIcon = 0;  // we'll paint our own icon when minimized, thank you.
  280. };
  281.  
  282. /* When the window is resized, scale the line list to fit the new
  283.   window extent, or switch between full size and iconized window
  284.   states.  */
  285. void TArtyWindow::WMSize( TMessage& Message )
  286. {
  287.   int NewXmax, NewYmax;
  288.  
  289.   TBaseDemoWindow::WMSize(Message);
  290.  
  291.   /* Force Windows to repaint the entire window region */
  292.   InvalidateRect(HWindow, NULL, TRUE);
  293.  
  294.   NewXmax = Message.LP.Lo;
  295.   NewYmax = Message.LP.Hi;
  296.   if (IsIconic(HWindow)) {
  297.     if (!Iconized) {
  298.       Iconized = TRUE;
  299.       List = IconicLineList;
  300.     }
  301.   } else {
  302.     if (Iconized) {
  303.       Iconized = FALSE;
  304.       List = BigLineList;
  305.     };
  306.     NewYmax -= TextHeight;  /* allow room for the text at the bottom */
  307.   };
  308.  
  309.   List->ScaleTo(NewXmax, NewYmax);  /* scale the lines in the list */
  310.   if (StaticControl)
  311.     MoveWindow(StaticControl->HWindow, 0, NewYmax, NewXmax, TextHeight, True);
  312. };
  313.  
  314. /* Toggle the window's Paused status.  Since the window will
  315.   not receive mouse clicks when iconized, this will not pause the
  316.   iconized lines display.  */
  317. void TArtyWindow::WMLButtonDown( TMessage& )
  318. {
  319.   Paused = !Paused;
  320. };
  321.  
  322. /* Clear the line list when the user presses the right mouse
  323.   button.  Same comments as above on iconized windows.  */
  324. void TArtyWindow::WMRButtonDown( TMessage& )
  325. {
  326.   InvalidateRect(HWindow, NULL, TRUE);
  327.   List->ResetLines();
  328. };
  329.  
  330. /* When the window is resized, or some other window blots out part
  331.   of our client area, redraw the entire line list.  The PaintDC
  332.   is fetched before Paint is called and is released for us after
  333.   Paint is finished. */
  334. void TArtyWindow::Paint( HDC PaintDC, PAINTSTRUCT& PaintInfo)
  335. {
  336.   TBaseDemoWindow::Paint(PaintDC, PaintInfo);
  337.   List->Redraw(PaintDC);
  338. };
  339.  
  340. /* Fetch a device context, pass it to the line list object, then
  341.   release the device context back to Windows.  */
  342. void TArtyWindow::TimerTick()
  343. {
  344.   HDC DC;
  345.  
  346.   if (!Paused) {
  347.     DC = GetDC(HWindow);
  348.     List->LineTick(DC);
  349.     ReleaseDC(HWindow, DC);
  350.   }
  351. };
  352.  
  353.