Luffy
Luffy

Reputation: 677

F# Windows Forms game loop

I want to make game loop in F# but i have problem with that.

let rec gameLoop (gamePanel:Panel) =  async {
    (*redraw gemaPanel *)
    lock gamePanel ( fun() -> 
      let graphics = gamePanel.CreateGraphics();
      let rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
      graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
    ) 
    return! gameLoop (gamePanel:Panel)
    }

let startGame (p:Panel) (p1:Panel) = hide p; Async.StartAsTask(gameLoop p1);()

If i add async this loop wont execute. If i will make this loop sync it will run forever.

What i want, to have loop which will be working all time in second thread and and will redraw game every 30ms and handle keyevents.

Upvotes: 1

Views: 398

Answers (1)

Ganesh Sittampalam
Ganesh Sittampalam

Reputation: 29110

You need to use Async.RunSynchronously in startGame to actually kick off the async:

let startGame (p:Panel) (p1:Panel) = hide p; Async.RunSynchronously (gameLoop p1)

Upvotes: 4

Related Questions