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

  1. program HexGrid;
  2.  
  3. {$IFC UNDEFINED THINK_PASCAL}
  4. uses Types, QuickDraw, Fonts, Events, Packages, Menus, Dialogs, Windows,{}
  5.     OSUtils, ToolUtils;
  6. {$ENDC}
  7.  
  8.  
  9.     const
  10. (*Size of the array*)
  11.         kArraySizeH = 10;
  12.         kArraySizeV = 8;
  13.  
  14.     var
  15. (* Pictures*)
  16.         hexTile: PicHandle;
  17.  
  18.     const
  19. (* Define constants that match our hexes *)
  20.         kHorizontalSpacing = 32;
  21.         kVerticalSpacing = 26;
  22.  
  23. (* Draw a tile *)
  24.  
  25.     procedure DrawHexTile (h: Integer; v: Integer);
  26.         var
  27.             tileRectangle: Rect;
  28. (* Use the picture frame *)
  29.     begin
  30.         tileRectangle := hexTile^^.picFrame;
  31. (* Move it to 0,0 *)
  32.         OffsetRect(tileRectangle, -tileRectangle.left, -tileRectangle.top);
  33. (* Offset to the proper position *)
  34. (* For every other line, offset a bit extra *)
  35.         if BitAnd(v, 1) = 0 then
  36.             OffsetRect(tileRectangle, kHorizontalSpacing * h, kVerticalSpacing * v)
  37.         else
  38.             OffsetRect(tileRectangle, kHorizontalSpacing * h + kHorizontalSpacing div 2, kVerticalSpacing * v);
  39. (* Draw it *)
  40.         DrawPicture(hexTile, tileRectangle);
  41.     end; (*DrawHexTile*)
  42.  
  43.  
  44. (* Standard inits *)
  45.  
  46.     procedure InitToolbox;
  47.     begin
  48. {$IFC UNDEFINED THINK_PASCAL}
  49.         InitGraf(@qd.thePort);
  50.         InitFonts;
  51.         FlushEvents(everyEvent, 0);
  52.         InitWindows;
  53.         InitMenus;
  54.         TEInit;
  55.         InitDialogs(nil);
  56. {$ENDC}
  57.         InitCursor;
  58.     end;
  59.  
  60. (****************** Main program ******************)
  61.  
  62.     var
  63.         myWindow: WindowPtr;
  64.         windowRectangle: Rect;
  65.         h, v: Integer;
  66.  
  67. begin
  68.     InitToolbox;
  69.  
  70. (*Set up the window*)
  71.     SetRect(windowRectangle, 50, 50, 50 + kArraySizeH * kHorizontalSpacing + kHorizontalSpacing div 2, 50 + kArraySizeV * kVerticalSpacing + kVerticalSpacing div 3);
  72.     myWindow := NewCWindow(nil, windowRectangle, 'Hex grid demo', true, 0, WindowPtr(-1), false, 0);
  73.     SetPort(myWindow);
  74.  
  75. (*Load the picture*)
  76.     hexTile := GetPicture(128);            (*PICT resource #128.*)
  77.  
  78. (*Draw all tiles!*)
  79.     for h := 0 to kArraySizeH - 1 do
  80.         for v := 0 to kArraySizeV - 1 do
  81.             DrawHexTile(h, v);
  82.  
  83.     while not Button do
  84.         ;
  85. end. (*TextGrid*)