make loading script a method of window

This commit is contained in:
filifa 2024-09-02 22:42:44 -05:00
parent 9e8aff813e
commit dbbbceaa49
1 changed files with 16 additions and 13 deletions

View File

@ -49,25 +49,35 @@ type script struct {
Times []float64 Times []float64
} }
func readScript(scriptPath string) (*script, error) { func (m *milkDropWindow) loadScript(scriptPath string) error {
if scriptPath == "" { if scriptPath == "" {
return nil, errors.New("no script given") return errors.New("no script given")
} }
f, err := os.Open(scriptPath) f, err := os.Open(scriptPath)
if err != nil { if err != nil {
return nil, err return err
} }
defer f.Close() defer f.Close()
data, err := io.ReadAll(f) data, err := io.ReadAll(f)
if err != nil { if err != nil {
return nil, err return err
} }
var s script var s script
err = json.Unmarshal(data, &s) err = json.Unmarshal(data, &s)
return &s, err if err != nil {
return err
}
m.preset = ring.New(len(s.Presets))
for _, preset := range s.Presets {
m.preset.Value = preset
m.preset = m.preset.Next()
}
return nil
} }
func (m *milkDropWindow) nextPreset() { func (m *milkDropWindow) nextPreset() {
@ -82,19 +92,12 @@ func (m *milkDropWindow) prevPreset() {
func newMilkDropWindow(width, height int32, scriptPath string) (*milkDropWindow, error) { func newMilkDropWindow(width, height int32, scriptPath string) (*milkDropWindow, error) {
var m milkDropWindow var m milkDropWindow
var err error
s, err := readScript(scriptPath) err := m.loadScript(scriptPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
m.preset = ring.New(len(s.Presets))
for _, preset := range s.Presets {
m.preset.Value = preset
m.preset = m.preset.Next()
}
m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE) m.window, err = sdl.CreateWindow("milkbucket", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, width, height, sdl.WINDOW_OPENGL|sdl.WINDOW_RESIZABLE)
if err != nil { if err != nil {
return &m, err return &m, err