This commit is contained in:
Andrey Parhomenko 2023-02-18 01:51:40 +05:00
parent 5fc2dd92dd
commit be9c3dc619
6 changed files with 136 additions and 13 deletions

View file

@ -2,18 +2,37 @@ package main
import ( import (
"github.com/surdeus/gox/src/gx" "github.com/surdeus/gox/src/gx"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"bytes"
"log"
) )
type Player struct { type Player struct {
*gx.Object *gx.Sprite
} }
var (
playerImg *gx.Image
)
func NewPlayer() *Player { func NewPlayer() *Player {
return &Player{ return &Player{
Object: &gx.Object{}, Sprite: &gx.Sprite{
Object: &gx.Object{
T: gx.Transform {
P: gx.Vector{1, 1},
S: gx.Vector{1, 1},
},
},
Image: playerImg,
},
} }
} }
func (p *Player) Update(e *gx.Engine) {
p.Sprite.Object.T.P.Y += 0.1
}
func main() { func main() {
e := gx.New(&gx.WindowConfig{ e := gx.New(&gx.WindowConfig{
Title: "Test title", Title: "Test title",
@ -21,6 +40,13 @@ func main() {
Height: 320, Height: 320,
}) })
var err error
playerImg, err = gx.LoadImage(bytes.NewReader(images.Runner_png))
if err != nil {
log.Fatal(err)
}
e.Add(0, NewPlayer()) e.Add(0, NewPlayer())
e.Run() e.Run()
} }

30
src/gx/img.go Normal file
View file

@ -0,0 +1,30 @@
package gx
import (
"github.com/hajimehoshi/ebiten/v2"
"image"
_ "image/png"
"io"
)
type Image = ebiten.Image
type Rectangle = image.Rectangle
type Point = image.Point
// The interface describes anything that can be
// drawn. It will be drew corresponding to
// the layers order.
type Drawer interface {
Draw(*Engine, *Image)
}
func LoadImage(input io.Reader) (*Image, error) {
img, _, err := image.Decode(input)
if err != nil {
return nil, err
}
ret := ebiten.NewImageFromImage(img)
return ret, nil
}

View file

@ -3,7 +3,8 @@ package gx
import ( import (
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/surdeus/godat/src/sparsex" "github.com/surdeus/godat/src/sparsex"
"fmt" //"fmt"
"time"
) )
// The type represents order of drawing. // The type represents order of drawing.
@ -12,12 +13,15 @@ type Layer int
type WindowConfig struct { type WindowConfig struct {
Title string Title string
Width, Height int Width, Height int
FullScreen bool
} }
type Engine struct { type Engine struct {
wcfg *WindowConfig wcfg *WindowConfig
layers *sparsex.Sparse[Layer, *[]Behaver] layers *sparsex.Sparse[Layer, *[]Drawer]
behavers []Behaver behavers []Behaver
lastTime time.Time
dt Float
} }
type engine Engine type engine Engine
@ -29,44 +33,75 @@ func New(
wcfg: cfg, wcfg: cfg,
layers: sparsex.New[ layers: sparsex.New[
Layer, Layer,
*[]Behaver, *[]Drawer,
](true), ](true),
} }
} }
func (e *Engine) Add(l Layer, b Behaver) { // Add new object considering what
// interfaces it implements.
func (e *Engine) Add(l Layer, b any) {
beh, ok := b.(Behaver)
if ok {
e.AddBehaver(beh)
}
drw, ok := b.(Drawer)
if ok {
e.AddDrawer(l, drw)
}
}
func (e *Engine) AddDrawer(l Layer, d Drawer) {
g, ok := e.layers.Get(l) g, ok := e.layers.Get(l)
if !ok { if !ok {
e.layers.Set( e.layers.Set(
l, l,
&[]Behaver{b}, &[]Drawer{d},
) )
} else { } else {
set := append(*g, b) set := append(*g, d)
*g = set *g = set
} }
}
func (e *Engine) AddBehaver(b Behaver) {
e.behavers = append(e.behavers, b) e.behavers = append(e.behavers, b)
} }
func (e *engine) Update() error { func (e *engine) Update() error {
eng := (*Engine)(e) eng := (*Engine)(e)
e.dt = time.Since(e.lastTime).Seconds()
for _, v := range eng.behavers { for _, v := range eng.behavers {
v.Update(eng) v.Update(eng)
fmt.Println(v) //fmt.Println(v)
} }
e.lastTime = time.Now()
return nil return nil
} }
func (e *engine) Draw(s *ebiten.Image) { func (e *engine) Draw(i *ebiten.Image) {
eng := (*Engine)(e)
for p := range e.layers.Vals() {
for _, d := range *p.V {
d.Draw(eng, i)
}
}
} }
func (e *engine) Layout(ow, oh int) (int, int) { func (e *engine) Layout(ow, oh int) (int, int) {
return e.wcfg.Width, e.wcfg.Height return e.wcfg.Width, e.wcfg.Height
} }
// Return the delta time duration value.
func (e *Engine) DT() Float {
return e.dt
}
func (e *Engine) Run() error { func (e *Engine) Run() error {
ebiten.SetWindowTitle(e.wcfg.Title) ebiten.SetWindowTitle(e.wcfg.Title)
ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height) ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height)

View file

@ -3,7 +3,6 @@ package gx
type Behaver interface { type Behaver interface {
Start(*Engine) Start(*Engine)
Update(*Engine) Update(*Engine)
GetObject() *Object
} }
// The object type represents // The object type represents
@ -13,7 +12,6 @@ type Object struct {
T Transform T Transform
} }
// The functions that
func (o *Object) Start(e *Engine) {} func (o *Object) Start(e *Engine) {}
func (o *Object) Update(e *Engine) {} func (o *Object) Update(e *Engine) {}
func (o *Object) GetObject() *Object { func (o *Object) GetObject() *Object {

View file

@ -1,4 +1,21 @@
package gx package gx
import (
"github.com/hajimehoshi/ebiten/v2"
)
type Sprite struct { type Sprite struct {
*Object
*Image
} }
func (s *Sprite) Draw(
e *Engine,
i *Image,
) {
op := &ebiten.DrawImageOptions{}
op.GeoM = s.Object.T.Matrix()
i.DrawImage(s.Image, op)
}

View file

@ -1,6 +1,11 @@
package gx package gx
type Float float64 import (
"github.com/hajimehoshi/ebiten/v2"
)
type Matrix = ebiten.GeoM
type Float = float64
type Vector struct { type Vector struct {
X, Y Float X, Y Float
@ -18,3 +23,15 @@ func T() Transform {
return ret return ret
} }
// Returns the GeoM with corresponding
// to the transfrom transformation
func (t Transform)Matrix() Matrix {
g := &Matrix{}
g.Scale(t.S.X, t.S.Y)
g.Rotate(t.R)
g.Translate(t.P.X, t.P.Y)
return *g
}