refactor main loop

This commit is contained in:
filifa 2024-09-04 21:31:52 -05:00
parent 6410b5ee6f
commit 9d16b2970c
1 changed files with 18 additions and 12 deletions

View File

@ -87,6 +87,23 @@ func handleEvent(event sdl.Event, m *milkDropWindow) bool {
return true
}
/*
* update handles events and renders new frames of the visualization. It
* returns a bool indicating whether the program should keep running and an
* error, if any.
*/
func update(m *milkDropWindow) (bool, error) {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
keepRunning := handleEvent(event, m)
if !keepRunning {
return false, nil
}
}
keepRunning, err := m.render()
return keepRunning, err
}
/*
* milkbucket sets up the program and starts a rendering loop.
*/
@ -119,18 +136,7 @@ func milkbucket(cmd *cobra.Command, args []string) {
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
running = handleEvent(event, m)
if !running {
break
}
}
if !running {
break
}
running, err = m.render()
running, err = update(m)
if err != nil {
panic(err)
}