use cobra
This commit is contained in:
120
cmd/root.go
Normal file
120
cmd/root.go
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
Copyright © 2024 filifa
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -I/home/nick/Downloads/include
|
||||
#cgo LDFLAGS: -L/home/nick/.local/lib -lprojectM-4
|
||||
#include "projectM-4/projectM.h"
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
var preset string
|
||||
|
||||
func handleWindowEvent(event *sdl.WindowEvent, m *milkDropWindow) {
|
||||
switch event.Event {
|
||||
case sdl.WINDOWEVENT_RESIZED:
|
||||
w, h := event.Data1, event.Data2
|
||||
C.projectm_set_window_size(m.handle, C.ulong(w), C.ulong(h))
|
||||
}
|
||||
}
|
||||
|
||||
func handleEvent(event sdl.Event, m *milkDropWindow) bool {
|
||||
switch event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
return false
|
||||
case *sdl.WindowEvent:
|
||||
event := event.(*sdl.WindowEvent)
|
||||
handleWindowEvent(event, m)
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func milkbucket(cmd *cobra.Command, args []string) {
|
||||
err := sdl.Init(sdl.INIT_VIDEO)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer sdl.Quit()
|
||||
|
||||
m, err := newMilkDropWindow(800, 600)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer m.destroy()
|
||||
|
||||
m.loadPreset(preset)
|
||||
|
||||
running := true
|
||||
for running {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
running = handleEvent(event, m)
|
||||
if !running {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !running {
|
||||
break
|
||||
}
|
||||
|
||||
running, err = m.render()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "milkbucket",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Run: milkbucket,
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Here you will define your flags and configuration settings.
|
||||
// Cobra supports persistent flags, which, if defined here,
|
||||
// will be global for your application.
|
||||
|
||||
// Cobra also supports local flags, which will only run
|
||||
// when this action is called directly.
|
||||
rootCmd.Flags().StringVarP(&preset, "preset", "p", "", "Preset file to use")
|
||||
}
|
||||
79
cmd/window.go
Normal file
79
cmd/window.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package cmd
|
||||
|
||||
/*
|
||||
#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(preset)
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user