home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Book Demos in Pascal / PixelPlotTest / PixelPlotTest.p < prev    next >
Encoding:
Text File  |  1995-04-06  |  3.8 KB  |  154 lines  |  [TEXT/MWPS]

  1. program PixelPlotTest;
  2.  
  3. {$IFC UNDEFINED THINK_PASCAL}
  4.     uses
  5.     Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  6.     OSUtils, ToolUtils, OSEvents;
  7. {$ENDC}
  8.  
  9.     const
  10.         kSizeH = 300;
  11.         kSizeV = 40;
  12.     var
  13.         w: WindowPtr;
  14.         c, saveColor: RGBColor;
  15.         startTime, endTime: LongInt;
  16.         h, v, i: Integer;
  17.         r: Rect;
  18.         rowBytes: Longint;
  19.         pixAddr: Ptr;
  20.         baseAddr: Longint;
  21.         rowStartTable: array[0..kSizeV] of Longint;
  22.         mmuMode: SignedByte;
  23.  
  24.     procedure InitToolbox;
  25.     begin
  26. {$IFC UNDEFINED THINK_PASCAL}
  27.         InitGraf(@qd.thePort);
  28.         InitFonts;
  29.         FlushEvents(everyEvent, 0);
  30.         InitWindows;
  31.         InitMenus;
  32.         TEInit;
  33.         InitDialogs(nil);
  34. {$ENDC}
  35.         InitCursor;
  36.     end; (*InitToolbox*)
  37.  
  38. begin
  39.     InitToolbox;
  40.  
  41.     SetRect(r, 100, 100, 400, 301);
  42.     w := NewCWindow(nil, r, 'Pixel Plotting Speed Test', true, 2, WindowPtr(-1), false, 0);
  43.     SetPort(w);
  44.  
  45.     GetForeColor(saveColor);
  46.  
  47.     c.red := $ffff;
  48.     c.green := 0;
  49.     c.blue := $ffff;
  50.     startTime := TickCount;
  51.     for h := 0 to kSizeH do
  52.         for v := 0 to kSizeV do
  53.             SetCPixel(h, v, c);
  54.     endTime := TickCount;
  55.  
  56.     MoveTo(10, kSizeV + 20);
  57.     DrawString(stringof('SetCPixel test: ', (endTime - startTime) : 1, ' ticks.'));
  58.  
  59.     c.red := $ffff;
  60.     c.green := $ffff;
  61.     c.blue := 0;
  62.     RGBForeColor(c);
  63.     startTime := TickCount;
  64.     for h := 0 to kSizeH do
  65.         for v := 0 to kSizeV do
  66.             begin
  67.                 RGBForeColor(c);
  68.                 MoveTo(h, v);
  69.                 Line(0, 0);
  70.             end;
  71.     endTime := TickCount;
  72.  
  73.     RGBForeColor(saveColor);
  74.     MoveTo(10, kSizeV + 40);
  75.     DrawString(stringof('MoveTo/LineTo test: ', (endTime - startTime) : 1, ' ticks.'));
  76.  
  77.     c.red := 0;
  78.     c.green := $ffff;
  79.     c.blue := $ffff;
  80.     RGBForeColor(c);
  81.     startTime := TickCount;
  82.     for h := 0 to kSizeH do
  83.         for v := 0 to kSizeV do
  84.             begin
  85.                 RGBForeColor(c);
  86.                 SetRect(r, h, v, h + 1, v + 1);
  87.                 PaintRect(r);
  88.             end;
  89.     endTime := TickCount;
  90.  
  91.     RGBForeColor(saveColor);
  92.     MoveTo(10, kSizeV + 60);
  93.     DrawString(stringof('SetRect/PaintRect test: ', (endTime - startTime) : 1, ' ticks.'));
  94.  
  95.     if GetMainDevice^^.gdPMap^^.pixelSize <> 8 then
  96.         begin
  97.             MoveTo(10, kSizeV + 80);
  98.             DrawString('Direct plotting is only supported in 256 colors/grays.');
  99.         end
  100.     else
  101.         begin
  102.  
  103.             mmuMode := true32b;
  104.  
  105. {CWindowPtr(w)^.portPixMap^^.bounds.top;}
  106. {CWindowPtr(w)^.portPixMap^^.bounds.left;}
  107.             rowBytes := BitAnd(GetMainDevice^^.gdPMap^^.rowBytes, $3fff);
  108.             SetRect(r, 0, 0, kSizeH, kSizeV);
  109.             ShieldCursor(r, CWindowPtr(w)^.portPixMap^^.bounds.topLeft);
  110.             baseAddr := Longint(GetMainDevice^^.gdPMap^^.baseAddr);
  111.  
  112.             startTime := TickCount;
  113.             SwapMMUMode(mmuMode);
  114.             for i := 1 to 10 do
  115.                 for h := 0 to kSizeH do
  116.                     for v := 0 to kSizeV do
  117.                         begin
  118.                             pixAddr := Ptr(baseAddr + rowBytes * (v + 100) + h + 100);
  119.                             pixAddr^ := $70 + i; {Some pixel value. We can get this through the color table.}
  120.                         end;
  121.             SwapMMUMode(mmuMode);
  122.             endTime := TickCount;
  123.  
  124.             ShowCursor;
  125.             MoveTo(10, kSizeV + 80);
  126.             DrawString(stringof('Direct plotting test: ', (endTime - startTime) : 1, ' ticks/10 times.'));
  127.  
  128. {Optimize by precalculating as much as possible:}
  129.             for v := 0 to kSizeV do
  130.                 rowStartTable[v] := baseAddr + rowBytes * (v + 100) + 100;
  131.             ShieldCursor(r, CWindowPtr(w)^.portPixMap^^.bounds.topLeft);        {Protect us from damaging the cursor}
  132.             startTime := TickCount;
  133.             SwapMMUMode(mmuMode);                                                {We must be in 32-bit mode}
  134.             for i := 1 to 10 do
  135.                 for h := 0 to kSizeH do
  136.                     for v := 0 to kSizeV do
  137.                         begin
  138.                             pixAddr := Ptr(rowStartTable[v] + h);
  139.                             pixAddr^ := $60 + i; {Some pixel value. We can get this through the color table.}
  140.                         end;
  141.             SwapMMUMode(mmuMode);                                                {Back to previous addressing mode}
  142.             endTime := TickCount;
  143.             ShowCursor;                                                                {Balances ShieldCursor}
  144.             MoveTo(10, kSizeV + 100);
  145.             DrawString(stringof('Opt. direct plotting test: ', (endTime - startTime) : 1, ' ticks/10 times.'));
  146.  
  147.         end;
  148.  
  149.     MoveTo(10, kSizeV + 120);
  150.     DrawString('Click mouse to exit.');
  151.     while not Button do
  152.         ;
  153.  
  154. end.