refactor window creation

This commit is contained in:
filifa 2024-08-24 19:38:27 -05:00
parent 01e888ad25
commit 4a9d73ef04
1 changed files with 48 additions and 24 deletions

72
main.go
View File

@ -12,6 +12,7 @@ import "C"
import (
"encoding/binary"
"errors"
"io"
"os"
"unsafe"
@ -19,6 +20,48 @@ import (
const bufSize uint = 2048
type milkDropWindow struct {
window *sdl.Window
context sdl.GLContext
handle C.projectm_handle
}
func newMilkDropWindow(width, height int32) (*milkDropWindow, error) {
var m milkDropWindow
var err error
m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE)
if err != nil {
return &m, err
}
m.context, err = m.window.GLCreateContext()
if err != nil {
return &m, err
}
m.handle = C.projectm_create()
if m.handle == nil {
return &m, errors.New("error creating projectM instance")
}
C.projectm_set_window_size(m.handle, C.ulong(width), C.ulong(height))
return &m, nil
}
func (m *milkDropWindow) destroy() {
C.projectm_destroy(m.handle)
sdl.GLDeleteContext(m.context)
m.window.Destroy()
}
func (m *milkDropWindow) loadPreset(preset string) {
cPreset := C.CString("/usr/share/projectM/presets/presets_stock/Zylot - Wisps.milk")
defer C.free(unsafe.Pointer(cPreset))
C.projectm_load_preset_file(m.handle, cPreset, false)
}
func render(window *sdl.Window, handle C.projectm_handle) (bool, error) {
var audioData [bufSize]int16
@ -58,43 +101,24 @@ func handleEvent(event sdl.Event, handle C.projectm_handle) bool {
}
func main() {
windowWidth := 800
windowHeight := 600
err := sdl.Init(sdl.INIT_VIDEO)
if err != nil {
panic(err)
}
defer sdl.Quit()
window, err := sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, int32(windowWidth), int32(windowHeight), sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE)
m, err := newMilkDropWindow(800, 600)
if err != nil {
panic(err)
}
defer window.Destroy()
defer m.destroy()
context, err := window.GLCreateContext()
if err != nil {
panic(err)
}
defer sdl.GLDeleteContext(context)
handle := C.projectm_create()
if handle == nil {
panic("nil")
}
defer C.projectm_destroy(handle)
C.projectm_set_window_size(handle, C.ulong(windowWidth), C.ulong(windowHeight))
preset := C.CString("/usr/share/projectM/presets/presets_stock/Zylot - Wisps.milk")
defer C.free(unsafe.Pointer(preset))
C.projectm_load_preset_file(handle, preset, false)
m.loadPreset("/usr/share/projectM/presets/presets_stock/Zylot - Wisps.milk")
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
running = handleEvent(event, handle)
running = handleEvent(event, m.handle)
if !running {
break
}
@ -104,7 +128,7 @@ func main() {
break
}
running, err = render(window, handle)
running, err = render(m.window, m.handle)
if err != nil {
panic(err)
}