1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-19 10:56:07 -05:00
OpenDiablo2/d2core/d2render/ebiten/ebiten_renderer.go
2020-02-09 14:12:04 -05:00

100 lines
2.3 KiB
Go

package ebiten
import (
"image"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2config"
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
)
type Renderer struct{}
func CreateRenderer() (*Renderer, error) {
result := &Renderer{}
config := d2config.Get()
ebiten.SetCursorVisible(false)
ebiten.SetFullscreen(config.FullScreen)
ebiten.SetRunnableInBackground(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 d2render.Surface) error, width, height int, title string) error {
config := d2config.Get()
return ebiten.Run(func(img *ebiten.Image) error {
err := f(&ebitenSurface{image: img})
if err != nil {
return err
}
return nil
}, width, height, config.Scale, title)
}
func (r *Renderer) CreateSurface(surface d2render.Surface) (d2render.Surface, error) {
result := &ebitenSurface{
image: surface.(*ebitenSurface).image,
stateCurrent: surfaceState{
filter: ebiten.FilterNearest,
mode: ebiten.CompositeModeSourceOver,
},
}
return result, nil
}
func (r *Renderer) NewSurface(width, height int, filter d2render.Filter) (d2render.Surface, error) {
ebitenFilter := d2ToEbitenFilter(filter)
img, err := ebiten.NewImage(width, height, ebitenFilter)
if err != nil {
return nil, err
}
result := &ebitenSurface{image: img}
return result, nil
}
func (r *Renderer) IsFullScreen() bool {
return ebiten.IsFullscreen()
}
func (r *Renderer) SetFullScreen(fullScreen bool) {
ebiten.SetFullscreen(fullScreen)
}
func (r *Renderer) SetVSyncEnabled(vsync bool) {
ebiten.SetVsyncEnabled(vsync)
}
func (r *Renderer) GetVSyncEnabled() bool {
return ebiten.IsVsyncEnabled()
}
func (r *Renderer) GetCursorPos() (int, int) {
return ebiten.CursorPosition()
}
func (r *Renderer) CurrentFPS() float64 {
return ebiten.CurrentFPS()
}