sedyh / mizu

Entity Component System framework for Ebitengine
Other
86 stars 4 forks source link

How can I use a custom Layout method? #8

Open xtrn143 opened 1 year ago

xtrn143 commented 1 year ago

In mizu,struct world implements the method "Layout" from ebiten.Game interface,But I want overwrite the method,or my ebiten app draw text with chinese would be fuzzy。

if I use ebiten directly,use the code below,the text display very clear,

func (g Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
    s := ebiten.DeviceScaleFactor()
    return outsideWidth * int(s), outsideHeight * int(s)
}

But if I use mizu, I didnt overwrite the method named Layout, So How do I do?

sedyh commented 1 year ago

Hello, since the mizu's world implements the ebiten.Game interface, you can just insert it into your ebiten.Game implementation by connecting its methods. The screen borders that it inserts into Bounds() will also change.

Here is a complete example:

package main

import (
    "log"

    "github.com/sedyh/mizu/pkg/engine"

    "github.com/hajimehoshi/ebiten/v2"
)

func main() {
    ebiten.SetVsyncEnabled(false)
    ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
    if err := ebiten.RunGame(NewGame(engine.NewGame(NewSetup()))); err != nil {
        log.Fatal(err)
    }
}

type Setup struct{}

func NewSetup() *Setup {
    return &Setup{}
}

func (s *Setup) Setup(w engine.World) {
    // Register some systems here.
        // w.Bounds() will get a modified Layout() now.
}

type Game struct {
    e ebiten.Game
}

func NewGame(e ebiten.Game) *Game {
    return &Game{e}
}

func (g *Game) Update() error {
    return g.e.Update()
}

func (g *Game) Draw(screen *ebiten.Image) {
    g.e.Draw(screen)
}

func (g *Game) Layout(w, h int) (int, int) {
    // Modify your layout here.
    g.e.Layout(w, h)
    return w, h
}
sedyh commented 1 year ago

Perhaps in the future, I can provide a separate callback for changing the layout, but for now this option will work.