Advertisement
Guest User

Untitled

a guest
May 1st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.47 KB | None | 0 0
  1.     let drawGameBoard () =
  2.         let bmp = new System.Drawing.Bitmap(fst GUI.BoardSize, snd GUI.BoardSize)
  3.         let data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
  4.         let adr x y = NativePtr.add<byte> (NativePtr.ofNativeInt data.Scan0) ((y*data.Stride) + (x*4))
  5.         for cellX in { 0 .. fst GUI.CellCount - 1 } do
  6.             for cellY in { 0 .. snd GUI.CellCount - 1 } do
  7.                 let color = if gameBoard.[cellX, cellY] = 1 then 0uy else 255uy
  8.                 for x in { 0 .. GUI.CellSize - 1 } do
  9.                     for y in { 0 .. GUI.CellSize - 1 } do
  10.                         let addr = adr (cellX * GUI.CellSize + x) (cellY * GUI.CellSize + y)
  11.                         NativePtr.set addr 3 255uy
  12.                         NativePtr.set addr 2 color
  13.                         NativePtr.set addr 1 color
  14.                         NativePtr.write addr color
  15.         bmp.UnlockBits(data)
  16.         GUI.gamePicture.Image <- bmp
  17.  
  18.     let mutable mouseDown = false
  19.     let mutable downFill = 1
  20.  
  21.     // Handle clicks
  22.     GUI.btnStep.Click.Add(fun _ -> if not !running then progressGame (); drawGameBoard ())
  23.     GUI.btnRun.Click.Add(fun _ -> (GUI.btnRun.Text <- if !running then "Run" else "Stop"); running := not !running;)
  24.     GUI.btnClear.Click.Add(fun _ -> running := false; (for x in { 0 .. fst GUI.CellCount - 1 } do for y in { 0 .. snd GUI.CellCount - 1 } do gameBoard.[x,y] <- 0); drawGameBoard())
  25.     GUI.gamePicture.MouseDown.Add(fun e -> mouseDown <- true; if not !running then downFill <- clickGameBoard e.Location.X e.Location.Y)
  26.     GUI.gamePicture.MouseUp.Add(fun _ -> mouseDown <- false)
  27.     GUI.gamePicture.MouseMove.Add(fun e -> if mouseDown && not !running then
  28.                                             if e.Location.X >= 0 && e.Location.Y >= 0 && e.Location.X <= fst GUI.BoardSize && e.Location.Y <= snd GUI.BoardSize
  29.                                                 && gameBoard.[e.Location.X/GUI.CellSize % fst GUI.CellCount, e.Location.Y/GUI.CellSize % snd GUI.CellCount] <> downFill then
  30.                                                     let timer = new Stopwatch()
  31.                                                     timer.Start()
  32.                                                     ignore (clickGameBoard e.Location.X e.Location.Y)
  33.                                                     timer.Stop()
  34.                                                     printfn "%d ms" timer.ElapsedMilliseconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement