Advertisement
mixster

mixster

Jan 2nd, 2009
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.80 KB | None | 0 0
  1. program Meh;
  2. type
  3.   TTile = record
  4.     t: TPoint;
  5.     o: Boolean;
  6.   end;
  7.   TBoard = record
  8.     t: array of array of TTile;
  9.     h, w: Integer;
  10.   end;
  11.   TPiece = record
  12.     t: array of Integer;
  13.     h: Integer;
  14.   end;
  15.   TPieceSet = record
  16.     p: array of TPiece;
  17.     s: TPoint;
  18.     h: Integer;
  19.   end;
  20.  
  21. var
  22.   frmMain: TForm;
  23.   imgBoard: TImage;
  24.   board: TBoard;
  25.   pieceset: TPieceset;
  26.  
  27. procedure Write(v: TVariantArray);
  28. var
  29.   i: Integer;
  30.   s: string;
  31. begin
  32.   if False then
  33.     exit;
  34.   if High(v) < 0 then
  35.     exit;
  36.   for i := 0 to High(v) do
  37.     s := s + v[i];
  38.   Writeln(s);
  39. end;
  40.  
  41. procedure SetupBoard(var brd: TBoard; w, h: Integer);
  42. var
  43.   x, y: Integer;
  44. begin
  45.   brd.w := w - 1;
  46.   brd.h := h - 1;
  47.   SetLength(brd.t, h);
  48.   for y := 0 to brd.h do
  49.   begin
  50.     SetLength(brd.t[y], w);
  51.     for x := 0 to brd.w do
  52.     begin
  53.       brd.t[y][x].t.x := 0;
  54.       brd.t[y][x].t.y := 0;
  55.       brd.t[y][x].o := False;
  56.     end;
  57.   end;
  58. end;
  59.  
  60. procedure RandomBoard(var brd: TBoard; pce: TPieceset);
  61. var
  62.   i, ii: Integer;
  63. begin
  64.   for i := 0 to brd.h do
  65.     for ii := 0 to brd.w do
  66.       with brd.t[i][ii].t do
  67.       begin
  68.         y := Random(pce.h + 1);
  69.         x := Random(pce.p[y].h + 1);
  70.       end;
  71. end;
  72.  
  73. procedure SetupPieceset(var pce: TPieceSet; bmp, tw, th: Integer);
  74. var
  75.   l, wl, hl, m, x, y, tb, ii, i: Integer;
  76.   c, ts: TCanvas;
  77.   p: TPointArray;
  78. begin
  79.   l := 20;
  80.   pce.s.x := Round(Sin(Radians(30)) * l);
  81.   pce.s.y := Round(Cos(Radians(30)) * l);
  82.   wl := (2 * pce.s.x) + l;
  83.   hl := 2 * pce.s.y;
  84.   m := BitmapFromString(wl, hl, '');
  85.   FastDrawClear(m, clBlack);
  86.   c := GetBitmapCanvas(m);
  87.   c.Pen.Color := RGBtoColor(1, 1, 1);
  88.   c.Brush.Color := clWhite;
  89.   with pce.s do
  90.     c.Polygon([Point(0, y), Point(x, y + y), Point(x + l, y + y), Point(x + l + x, y), Point(x + l, 0), Point(x, 0)]);
  91.   SetTransparentColor(m, clWhite);
  92.   GetBitmapSize(bmp, x, y);
  93.   x := x / tw;
  94.   y := y / th;
  95.   tb := BitmapFromString(x, y, '');
  96.   FastDrawClear(tb, 0);
  97.   SetTargetBitmap(bmp);
  98.   ts := GetBitmapCanvas(bmp);
  99.   pce.h := th - 1;
  100.   SetLength(pce.p, th);
  101.   for ii := 0 to pce.h do
  102.     with pce.p[ii] do
  103.     begin
  104.       h := -1;
  105.       for i := 0 to tw - 1 do
  106.       begin
  107.         SetLength(p, 0);
  108.         FindColorsTolerance(p, clBlack, x * i, y * ii, (x * i) + wl, (y * ii) + hl, 0);
  109.         if Length(p) >= ((wl + 1) * (hl + 1)) then
  110.           Break;
  111.         Inc(h);
  112.         SetLength(t, h + 1);
  113.         t[h] := BitmapFromString(wl, hl, '');
  114.         SafeCopyCanvas(ts, GetBitmapCanvas(t[h]), x * i, y * ii, x * i + wl, y * ii + hl, 0, 0, wl, hl);
  115.         FastDrawTransparent(0, 0, m, t[h]);
  116.         SetTransparentColor(t[h], clBlack);
  117.       end;
  118.     end;
  119.   FreeBitmap(m);
  120.   ResetDc;
  121. end;
  122.  
  123. procedure DrawBoard(var img: TImage; brd: TBoard; pce: TPieceSet);
  124. var
  125.   t, i, ii, w, h, l, o: Integer;
  126. begin
  127.   t := BitmapFromString(img.Width, img.Height, '');
  128.   FastDrawClear(t, clBlack);
  129.   GetBitmapSize(pce.p[0].t[0], w, h);
  130.   l := w - (pce.s.x * 2);
  131.   w := w + l;
  132.   h := h / 2;
  133.   Write([l, ',', w, ',', h]);
  134.   for ii := 0 to brd.h do
  135.   begin
  136.     if ii mod 2 = 0 then
  137.       o := 0
  138.     else
  139.       o := pce.s.x + l;
  140.     for i := 0 to brd.w do
  141.     begin
  142.       if o > 0 then
  143.         if i = brd.w then
  144.           Break;
  145.       with brd.t[ii][i].t do
  146.       begin
  147.         if (y > pce.h) then
  148.           Continue;
  149.         if (x > pce.p[y].h) then
  150.           Continue;
  151.         FastDrawTransparent((i * w) + o, (ii * h), pce.p[y].t[x], t);
  152.       end;
  153.     end;
  154.   end;
  155.  
  156.   SafeDrawBitmap(t, img.Canvas, 0, 0);
  157. end;
  158.  
  159. procedure SetupForm;
  160. var
  161.   i, a: Integer;
  162.   c: TCanvas;
  163. begin
  164.   frmMain := CreateForm;
  165.   with frmMain do
  166.   begin
  167.     ClientWidth := 600;
  168.     ClientHeight := 500;
  169.     Position:= poScreenCenter;
  170.     Caption := 'Strategy Game by mixster';
  171.   end;
  172.  
  173.   imgBoard := TImage.Create(frmMain);
  174.   with imgBoard do
  175.   begin
  176.     Parent := frmMain;
  177.     Width := 500;
  178.     Height := 400
  179.     Left := 50;
  180.     Top := 50;
  181.     DrawBoard(imgBoard, board, pieceset);
  182.  
  183.   end;
  184.  
  185.   frmMain.ShowModal;
  186. end;
  187.  
  188. procedure LaunchForm;
  189. var
  190.   v: TVariantArray;
  191. begin
  192.   ThreadSafeCall('SetupForm', v);
  193. end;
  194.  
  195. var
  196.   t, i, ii: Integer;
  197. begin
  198.   Write(['Begin']);
  199.  
  200.   t := BitmapFromString(200, 400, '');
  201.   for i := 0 to 7 do
  202.     for ii := 0 to 3 do
  203.     begin
  204.       if (ii > 0) then
  205.         if Random(3) = 0 then
  206.           Break;
  207.  
  208.       GetBitmapCanvas(t).Brush.Color := Random(clWhite);
  209.       GetBitmapCanvas(t).Pen.Color := GetBitmapCanvas(t).Brush.Color;
  210.       GetBitmapCanvas(t).Rectangle(ii * 50, i * 50, ii * 50 + 50, i * 50 + 50);
  211.     end;
  212.   DisplayDebugImgWindow(200, 400);
  213.   SafeDrawBitmap(t, GetDebugCanvas, 0, 0);
  214.   SetupPieceset(pieceset, t, 4, 8);
  215.   SetupBoard(board, 8, 21);
  216.   RandomBoard(board, pieceset);
  217.   LaunchForm;
  218.   Write(['End']);
  219. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement