move window stuff to own file

This commit is contained in:
filifa 2024-08-24 19:53:39 -05:00
parent 622e85a4f6
commit 4aaf8e6638
2 changed files with 80 additions and 70 deletions

71
main.go
View File

@ -1,85 +1,16 @@
package main
import "github.com/veandco/go-sdl2/sdl"
/*
#cgo CFLAGS: -I/home/nick/Downloads/include
#cgo LDFLAGS: -L/home/nick/.local/lib -lprojectM-4
#include "projectM-4/projectM.h"
#include <stdlib.h>
*/
import "C"
import (
"encoding/binary"
"errors"
"io"
"os"
"unsafe"
"github.com/veandco/go-sdl2/sdl"
)
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 (m *milkDropWindow) render() (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(m.handle, (*C.int16_t)(&audioData[0]), C.uint(bufSize), C.PROJECTM_STEREO)
C.projectm_opengl_render_frame(m.handle)
m.window.GLSwap()
return true, nil
}
func handleWindowEvent(event *sdl.WindowEvent, handle C.projectm_handle) {
switch event.Event {
case sdl.WINDOWEVENT_RESIZED:

79
window.go Normal file
View File

@ -0,0 +1,79 @@
package main
/*
#include "projectM-4/projectM.h"
#include <stdlib.h>
*/
import "C"
import (
"encoding/binary"
"errors"
"io"
"os"
"unsafe"
"github.com/veandco/go-sdl2/sdl"
)
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 (m *milkDropWindow) render() (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(m.handle, (*C.int16_t)(&audioData[0]), C.uint(bufSize), C.PROJECTM_STEREO)
C.projectm_opengl_render_frame(m.handle)
m.window.GLSwap()
return true, nil
}