Files
sml-projects/imperative-shell/event-loop.sml

62 lines
1.8 KiB
Standard ML
Raw Normal View History

2024-07-31 10:03:30 +01:00
structure EventLoop =
struct
open CML
open DrawMessage
2024-07-31 10:03:30 +01:00
2024-07-31 12:00:07 +01:00
local
fun loop (inputMailbox, drawMailbox, mouseX, mouseY, model) =
let
val inputMsg = Mailbox.recv inputMailbox
val (model, drawMsg, mouseX, mouseY) =
AppUpdate.update (model, mouseX, mouseY, inputMsg)
val _ = Mailbox.send (drawMailbox, drawMsg)
in
loop (inputMailbox, drawMailbox, mouseX, mouseY, model)
end
2024-07-31 12:00:07 +01:00
in
fun update (inputMailbox, drawMailbox) =
loop (inputMailbox, drawMailbox, 0, 0, AppType.initial)
end
2024-07-31 10:03:30 +01:00
fun draw
(drawMailbox, window, graphDrawObject, buttonDrawObject, buttonDrawLength) =
2024-07-31 10:03:30 +01:00
if not (Glfw.windowShouldClose window) then
case Mailbox.recvPoll drawMailbox of
NONE =>
let
val _ = Gles3.clearColor (1.0, 1.0, 1.0, 1.0)
val _ = Gles3.clear ()
2024-07-31 10:03:30 +01:00
val _ = AppDraw.drawGraphLines graphDrawObject
val _ = AppDraw.drawButton (buttonDrawObject, buttonDrawLength)
2024-07-31 10:03:30 +01:00
val _ = Glfw.pollEvents ()
val _ = Glfw.swapBuffers window
in
draw
( drawMailbox
, window
, graphDrawObject
, buttonDrawObject
, buttonDrawLength
)
end
| SOME drawMsg =>
(case drawMsg of
DRAW_BUTTON vec =>
let
val _ = AppDraw.uploadButtonVector (buttonDrawObject, vec)
val buttonDrawLength = Vector.length vec div 5
in
draw
( drawMailbox
, window
, graphDrawObject
, buttonDrawObject
, buttonDrawLength
)
end)
2024-07-31 10:03:30 +01:00
else
Glfw.terminate ()
end