Checkpoint
This commit is contained in:
125 files changed
+59376
No files matched your search
@@ -0,0 +1,127 @@
|
||||
package sdl
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
|
||||
"git.maze.io/go/harmon/input"
|
||||
)
|
||||
|
||||
var (
|
||||
initOnce sync.Once
|
||||
initTime time.Time
|
||||
quitOnce sync.Once
|
||||
)
|
||||
|
||||
type Device struct {
|
||||
window *sdl.Window
|
||||
surface *sdl.Surface
|
||||
keyboardInput *keyboardInput
|
||||
}
|
||||
|
||||
func New() (*Device, error) {
|
||||
var err error
|
||||
initOnce.Do(func() {
|
||||
err = sdl.Init(sdl.INIT_EVERYTHING)
|
||||
if err == nil {
|
||||
initTime = time.Now()
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var (
|
||||
b int32 = 32
|
||||
w int32 = 256
|
||||
h int32 = 32
|
||||
)
|
||||
|
||||
window, err := sdl.CreateWindow("Bragi OLED emulator",
|
||||
sdl.WINDOWPOS_UNDEFINED,
|
||||
sdl.WINDOWPOS_UNDEFINED,
|
||||
w+2*b,
|
||||
h+2*b,
|
||||
sdl.WINDOW_SHOWN)
|
||||
if err != nil {
|
||||
sdl.Quit()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
surface, err := window.GetSurface()
|
||||
if err != nil {
|
||||
_ = window.Destroy()
|
||||
sdl.Quit()
|
||||
return nil, err
|
||||
}
|
||||
surface.FillRect(nil, 0)
|
||||
|
||||
surface.FillRect(&sdl.Rect{0, 0, b + b + w, b + b + h}, sdl.MapRGBA(surface.Format, 128, 64, 0, 255))
|
||||
surface.FillRect(&sdl.Rect{b, b, w, h}, sdl.MapRGBA(surface.Format, 255, 128, 0, 255))
|
||||
window.UpdateSurface()
|
||||
|
||||
d := &Device{
|
||||
window: window,
|
||||
surface: surface,
|
||||
keyboardInput: newKeyboardInput(),
|
||||
}
|
||||
|
||||
input.Register(func() ([]input.Source, error) {
|
||||
return []input.Source{
|
||||
d.keyboardInput,
|
||||
}, nil
|
||||
})
|
||||
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *Device) Close() error {
|
||||
err := d.window.Destroy()
|
||||
quitOnce.Do(sdl.Quit)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Device) Run() error {
|
||||
for running := true; running; {
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch event := event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
running = false
|
||||
break
|
||||
|
||||
case *sdl.KeyboardEvent:
|
||||
d.handleKeyboardEvent(event)
|
||||
|
||||
case *sdl.MouseMotionEvent:
|
||||
//select {
|
||||
//case d.mouseMotionEvents <- event:
|
||||
//default:
|
||||
//}
|
||||
|
||||
default:
|
||||
log.Printf("SDL event: %T", event)
|
||||
}
|
||||
}
|
||||
}
|
||||
return d.Close()
|
||||
}
|
||||
|
||||
func (d *Device) handleKeyboardEvent(event *sdl.KeyboardEvent) {
|
||||
/*
|
||||
var kind input.EventType
|
||||
switch event.Type {
|
||||
case sdl.KEYDOWN:
|
||||
kind = input.KeyPress
|
||||
case sdl.KEYUP:
|
||||
kind = input.KeyRelease
|
||||
}
|
||||
d.keyboardInput.events <- input.Event{
|
||||
Type: kind,
|
||||
Time: initTime.Add(time.Duration(event.Timestamp) * time.Second),
|
||||
Code: int(event.Keysym.Sym),
|
||||
}
|
||||
*/
|
||||
}
|
||||
Reference in new issue
Block a user