gg/src/gx/main.go

77 lines
1.1 KiB
Go
Raw Normal View History

2023-02-17 07:04:29 +03:00
package gx
import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/surdeus/godat/src/sparsex"
"fmt"
2023-02-17 07:04:29 +03:00
)
// The type represents order of drawing.
type Layer int
2023-02-17 07:04:29 +03:00
type WindowConfig struct {
Title string
Width, Height int
}
type Engine struct {
wcfg *WindowConfig
layers *sparsex.Sparse[Layer, *[]Behaver]
behavers []Behaver
2023-02-17 07:04:29 +03:00
}
type engine Engine
func New(
cfg *WindowConfig,
) *Engine {
return &Engine{
wcfg: cfg,
layers: sparsex.New[
Layer,
*[]Behaver,
](true),
}
}
func (e *Engine) Add(l Layer, b Behaver) {
g, ok := e.layers.Get(l)
if !ok {
e.layers.Set(
l,
&[]Behaver{b},
)
} else {
set := append(*g, b)
*g = set
2023-02-17 07:04:29 +03:00
}
e.behavers = append(e.behavers, b)
2023-02-17 07:04:29 +03:00
}
func (e *engine) Update() error {
eng := (*Engine)(e)
for _, v := range eng.behavers {
v.Update(eng)
fmt.Println(v)
}
2023-02-17 07:04:29 +03:00
return nil
}
2023-02-17 07:04:29 +03:00
func (e *engine) Draw(s *ebiten.Image) {
}
func (e *engine) Layout(ow, oh int) (int, int) {
return e.wcfg.Width, e.wcfg.Height
}
func (e *Engine) Run() error {
ebiten.SetWindowTitle(e.wcfg.Title)
ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height)
return ebiten.RunGame((*engine)(e))
}