refactor main loop

This commit is contained in:
filifa 2024-08-24 19:13:02 -05:00
parent 1d81506ef6
commit 85c55e5a21
1 changed files with 43 additions and 23 deletions

66
main.go
View File

@ -17,6 +17,42 @@ import (
"unsafe" "unsafe"
) )
const bufSize uint = 2048
func render(window *sdl.Window, handle C.projectm_handle) (bool, error) {
var audioData [bufSize]int16
err := binary.Read(os.Stdin, binary.LittleEndian, &audioData)
if err == io.ErrUnexpectedEOF {
return false, nil
} else if err != nil {
return true, err
}
C.projectm_pcm_add_int16(handle, (*C.int16_t)(&audioData[0]), C.uint(bufSize), C.PROJECTM_STEREO)
C.projectm_opengl_render_frame(handle)
window.GLSwap()
return true, nil
}
func handleEvent(event sdl.Event, handle C.projectm_handle) bool {
switch event.(type) {
case *sdl.QuitEvent:
return false
case *sdl.WindowEvent:
event := event.(*sdl.WindowEvent)
switch event.Event {
case sdl.WINDOWEVENT_RESIZED:
w, h := event.Data1, event.Data2
C.projectm_set_window_size(handle, C.ulong(w), C.ulong(h))
}
}
return true
}
func main() { func main() {
windowWidth := 800 windowWidth := 800
windowHeight := 600 windowHeight := 600
@ -51,38 +87,22 @@ func main() {
defer C.free(unsafe.Pointer(preset)) defer C.free(unsafe.Pointer(preset))
C.projectm_load_preset_file(handle, preset, false) C.projectm_load_preset_file(handle, preset, false)
const bufSize uint = 2048
running := true running := true
for running { for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) { running = handleEvent(event, handle)
case *sdl.QuitEvent: if !running {
running = false
break break
case *sdl.WindowEvent:
event := event.(*sdl.WindowEvent)
switch event.Event {
case sdl.WINDOWEVENT_RESIZED:
w, h := event.Data1, event.Data2
C.projectm_set_window_size(handle, C.ulong(w), C.ulong(h))
}
} }
} }
var audioData [bufSize]int16 if !running {
err = binary.Read(os.Stdin, binary.LittleEndian, &audioData)
if err == io.ErrUnexpectedEOF {
running = false
break break
} else if err != nil {
panic(err)
} }
C.projectm_pcm_add_int16(handle, (*C.int16_t)(&audioData[0]), C.uint(bufSize), C.PROJECTM_STEREO) running, err = render(window, handle)
if err != nil {
C.projectm_opengl_render_frame(handle) panic(err)
window.GLSwap() }
} }
} }