Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let drawGameBoard () =
- let bmp = new System.Drawing.Bitmap(fst GUI.BoardSize, snd GUI.BoardSize)
- let data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat)
- let adr x y = NativePtr.add<byte> (NativePtr.ofNativeInt data.Scan0) ((y*data.Stride) + (x*4))
- for cellX in { 0 .. fst GUI.CellCount - 1 } do
- for cellY in { 0 .. snd GUI.CellCount - 1 } do
- let color = if gameBoard.[cellX, cellY] = 1 then 0uy else 255uy
- for x in { 0 .. GUI.CellSize - 1 } do
- for y in { 0 .. GUI.CellSize - 1 } do
- let addr = adr (cellX * GUI.CellSize + x) (cellY * GUI.CellSize + y)
- NativePtr.set addr 3 255uy
- NativePtr.set addr 2 color
- NativePtr.set addr 1 color
- NativePtr.write addr color
- bmp.UnlockBits(data)
- GUI.gamePicture.Image <- bmp
- let mutable mouseDown = false
- let mutable downFill = 1
- // Handle clicks
- GUI.btnStep.Click.Add(fun _ -> if not !running then progressGame (); drawGameBoard ())
- GUI.btnRun.Click.Add(fun _ -> (GUI.btnRun.Text <- if !running then "Run" else "Stop"); running := not !running;)
- 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())
- GUI.gamePicture.MouseDown.Add(fun e -> mouseDown <- true; if not !running then downFill <- clickGameBoard e.Location.X e.Location.Y)
- GUI.gamePicture.MouseUp.Add(fun _ -> mouseDown <- false)
- GUI.gamePicture.MouseMove.Add(fun e -> if mouseDown && not !running then
- if e.Location.X >= 0 && e.Location.Y >= 0 && e.Location.X <= fst GUI.BoardSize && e.Location.Y <= snd GUI.BoardSize
- && gameBoard.[e.Location.X/GUI.CellSize % fst GUI.CellCount, e.Location.Y/GUI.CellSize % snd GUI.CellCount] <> downFill then
- let timer = new Stopwatch()
- timer.Start()
- ignore (clickGameBoard e.Location.X e.Location.Y)
- timer.Stop()
- printfn "%d ms" timer.ElapsedMilliseconds)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement