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"
)
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() {
windowWidth := 800
windowHeight := 600
@ -51,38 +87,22 @@ func main() {
defer C.free(unsafe.Pointer(preset))
C.projectm_load_preset_file(handle, preset, false)
const bufSize uint = 2048
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
running = false
running = handleEvent(event, handle)
if !running {
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
err = binary.Read(os.Stdin, binary.LittleEndian, &audioData)
if err == io.ErrUnexpectedEOF {
running = false
if !running {
break
} else if err != nil {
}
running, err = render(window, handle)
if err != nil {
panic(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()
}
}