ebiten: convert view-obj
This commit is contained in:
@@ -3,8 +3,7 @@ package ui
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -13,35 +12,50 @@ var (
|
||||
)
|
||||
|
||||
type Window struct {
|
||||
PixelWindow *pixelgl.Window
|
||||
ButtonEventHandlers map[pixelgl.Button][]func()
|
||||
Title string
|
||||
KeyEventHandlers map[ebiten.Key][]func()
|
||||
|
||||
// User-provided update actions
|
||||
updateFn func() error
|
||||
drawFn func(*ebiten.Image) error
|
||||
}
|
||||
|
||||
// WARE! 0,0 is the *bottom left* of the window
|
||||
// 0,0 is the *top left* of the window
|
||||
//
|
||||
// To invert things so 0,0 is the *top left*, apply the InvertY` matrix
|
||||
// ebiten assumes a single window, so only call this once...
|
||||
func NewWindow(title string) (*Window, error) {
|
||||
cfg := pixelgl.WindowConfig{
|
||||
Title: title,
|
||||
Bounds: pixel.R(0, 0, float64(*winX), float64(*winY)),
|
||||
VSync: true,
|
||||
Resizable: true,
|
||||
}
|
||||
|
||||
pixelWindow, err := pixelgl.NewWindow(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Window{
|
||||
PixelWindow: pixelWindow,
|
||||
Title: title,
|
||||
KeyEventHandlers: make(map[ebiten.Key][]func()),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// TODO: a stop or other cancellation mechanism
|
||||
func (w *Window) Run(f func()) {
|
||||
for !w.PixelWindow.Closed() {
|
||||
f()
|
||||
w.PixelWindow.Update()
|
||||
}
|
||||
func (w *Window) OnKeyUp(key ebiten.Key, f func()) {
|
||||
|
||||
}
|
||||
|
||||
func (w *Window) run(screen *ebiten.Image) error {
|
||||
if err := w.updateFn(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Process keys
|
||||
|
||||
if !ebiten.IsDrawingSkipped() {
|
||||
if err := w.drawFn(screen); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: a stop or other cancellation mechanism
|
||||
//
|
||||
// Note that this must be called on the main OS thread
|
||||
func (w *Window) Run(updateFn func() error, drawFn func(*ebiten.Image) error) error {
|
||||
w.updateFn = updateFn
|
||||
w.drawFn = drawFn
|
||||
|
||||
return ebiten.Run(w.run, *winX, *winY, 1, w.Title)
|
||||
}
|
||||
|
Reference in New Issue
Block a user