2020-01-31 23:18:11 -05:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
2020-07-06 21:26:08 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-01-31 23:18:11 -05:00
|
|
|
"image"
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
2020-02-01 21:06:22 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-06-19 02:19:27 -04:00
|
|
|
type Renderer struct {
|
2020-06-29 00:41:58 -04:00
|
|
|
renderCallback func(surface d2interface.Surface) error
|
2020-06-19 02:19:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) Update(screen *ebiten.Image) error {
|
2020-07-07 08:57:40 -04:00
|
|
|
err := r.renderCallback(createEbitenSurface(screen))
|
2020-06-19 02:19:27 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Renderer) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
|
|
|
return 800, 600
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
func CreateRenderer() (*Renderer, error) {
|
|
|
|
result := &Renderer{}
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
config := d2config.Get()
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-06-22 11:53:44 -04:00
|
|
|
ebiten.SetCursorMode(ebiten.CursorModeHidden)
|
2020-07-04 00:49:16 -04:00
|
|
|
ebiten.SetFullscreen(config.FullScreen())
|
|
|
|
ebiten.SetRunnableOnUnfocused(config.RunInBackground())
|
|
|
|
ebiten.SetVsyncEnabled(config.VsyncEnabled())
|
|
|
|
ebiten.SetMaxTPS(config.TicksPerSecond())
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
func (*Renderer) GetRendererName() string {
|
2020-01-31 23:18:11 -05:00
|
|
|
return "Ebiten"
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
func (*Renderer) SetWindowIcon(fileName string) {
|
2020-01-31 23:18:11 -05:00
|
|
|
_, iconImage, err := ebitenutil.NewImageFromFile(fileName, ebiten.FilterLinear)
|
|
|
|
if err == nil {
|
|
|
|
ebiten.SetWindowIcon([]image.Image{iconImage})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
func (r *Renderer) IsDrawingSkipped() bool {
|
2020-01-31 23:18:11 -05:00
|
|
|
return ebiten.IsDrawingSkipped()
|
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (r *Renderer) Run(f func(surface d2interface.Surface) error, width, height int, title string) error {
|
2020-06-19 02:19:27 -04:00
|
|
|
r.renderCallback = f
|
|
|
|
ebiten.SetWindowTitle(title)
|
|
|
|
ebiten.SetWindowResizable(true)
|
|
|
|
ebiten.SetWindowSize(width, height)
|
|
|
|
return ebiten.RunGame(r)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
func (r *Renderer) CreateSurface(surface d2interface.Surface) (d2interface.Surface, error) {
|
2020-07-07 08:57:40 -04:00
|
|
|
result := createEbitenSurface(
|
|
|
|
surface.(*ebitenSurface).image,
|
|
|
|
surfaceState{
|
2020-01-31 23:18:11 -05:00
|
|
|
filter: ebiten.FilterNearest,
|
|
|
|
mode: ebiten.CompositeModeSourceOver,
|
|
|
|
},
|
2020-07-07 08:57:40 -04:00
|
|
|
)
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
return result, nil
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:26:08 -04:00
|
|
|
func (r *Renderer) NewSurface(width, height int, filter d2enum.Filter) (d2interface.Surface, error) {
|
2020-01-31 23:18:11 -05:00
|
|
|
ebitenFilter := d2ToEbitenFilter(filter)
|
|
|
|
img, err := ebiten.NewImage(width, height, ebitenFilter)
|
|
|
|
if err != nil {
|
2020-02-09 14:12:04 -05:00
|
|
|
return nil, err
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
2020-07-07 08:57:40 -04:00
|
|
|
return createEbitenSurface(img), nil
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func (r *Renderer) IsFullScreen() bool {
|
|
|
|
return ebiten.IsFullscreen()
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func (r *Renderer) SetFullScreen(fullScreen bool) {
|
2020-01-31 23:18:11 -05:00
|
|
|
ebiten.SetFullscreen(fullScreen)
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func (r *Renderer) SetVSyncEnabled(vsync bool) {
|
2020-01-31 23:18:11 -05:00
|
|
|
ebiten.SetVsyncEnabled(vsync)
|
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func (r *Renderer) GetVSyncEnabled() bool {
|
|
|
|
return ebiten.IsVsyncEnabled()
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-09 14:12:04 -05:00
|
|
|
func (r *Renderer) GetCursorPos() (int, int) {
|
|
|
|
return ebiten.CursorPosition()
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-02-01 21:51:49 -05:00
|
|
|
func (r *Renderer) CurrentFPS() float64 {
|
2020-01-31 23:18:11 -05:00
|
|
|
return ebiten.CurrentFPS()
|
|
|
|
}
|