1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-10 06:16:27 -05:00
OpenDiablo2/d2core/d2render/ebiten/ebiten_renderer.go

112 lines
2.6 KiB
Go
Raw Normal View History

package ebiten
import (
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"image"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
)
2020-06-19 02:19:27 -04:00
type Renderer struct {
renderCallback func(surface d2interface.Surface) error
2020-06-19 02:19:27 -04:00
}
func (r *Renderer) Update(screen *ebiten.Image) error {
err := r.renderCallback(&ebitenSurface{image: screen})
if err != nil {
return err
}
return nil
}
func (r *Renderer) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
return 800, 600
}
func CreateRenderer() (*Renderer, error) {
result := &Renderer{}
2020-02-09 14:12:04 -05:00
config := d2config.Get()
ebiten.SetCursorMode(ebiten.CursorModeHidden)
ebiten.SetFullscreen(config.FullScreen())
ebiten.SetRunnableOnUnfocused(config.RunInBackground())
ebiten.SetVsyncEnabled(config.VsyncEnabled())
ebiten.SetMaxTPS(config.TicksPerSecond())
return result, nil
}
func (*Renderer) GetRendererName() string {
return "Ebiten"
}
func (*Renderer) SetWindowIcon(fileName string) {
_, iconImage, err := ebitenutil.NewImageFromFile(fileName, ebiten.FilterLinear)
if err == nil {
ebiten.SetWindowIcon([]image.Image{iconImage})
}
}
func (r *Renderer) IsDrawingSkipped() bool {
return ebiten.IsDrawingSkipped()
}
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)
}
func (r *Renderer) CreateSurface(surface d2interface.Surface) (d2interface.Surface, error) {
result := &ebitenSurface{
image: surface.(*ebitenSurface).image,
stateCurrent: surfaceState{
filter: ebiten.FilterNearest,
mode: ebiten.CompositeModeSourceOver,
},
}
2020-02-09 14:12:04 -05:00
return result, nil
}
func (r *Renderer) NewSurface(width, height int, filter d2enum.Filter) (d2interface.Surface, error) {
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-02-09 14:12:04 -05:00
result := &ebitenSurface{image: img}
return result, nil
}
2020-02-09 14:12:04 -05:00
func (r *Renderer) IsFullScreen() bool {
return ebiten.IsFullscreen()
}
2020-02-09 14:12:04 -05:00
func (r *Renderer) SetFullScreen(fullScreen bool) {
ebiten.SetFullscreen(fullScreen)
}
2020-02-09 14:12:04 -05:00
func (r *Renderer) SetVSyncEnabled(vsync bool) {
ebiten.SetVsyncEnabled(vsync)
}
2020-02-09 14:12:04 -05:00
func (r *Renderer) GetVSyncEnabled() bool {
return ebiten.IsVsyncEnabled()
}
2020-02-09 14:12:04 -05:00
func (r *Renderer) GetCursorPos() (int, int) {
return ebiten.CursorPosition()
}
func (r *Renderer) CurrentFPS() float64 {
return ebiten.CurrentFPS()
}