1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-04 17:27:16 -05:00
OpenDiablo2/d2core/d2render/ebiten/ebiten_renderer.go
Haashi 5e1725dd7d
385/lintissues (#391)
* camera offset for ui panels :

added maprenderer viewport to be aligned left or right

calling alignement on keyPress in game_controls

* check if already aligned

* fix bugs

-forgot to assign alignement
-defaultScreenRect instead of screenRect because of issue mentionned in original comment

* remove config.json and replace go.mod line

* removing duplicate import of d2common

replacing all dh to d2common

* remove useless breaks from switch statement

* better range when value unused + prettying import

* item_affix rewrite

using return values instead of pointer references in arguments

* ebiten deprecated calls

* small fixes
2020-06-22 11:53:44 -04:00

110 lines
2.5 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 {
renderCallback func(surface d2render.Surface) error
}
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{}
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 d2render.Surface) error, width, height int, title string) error {
r.renderCallback = f
ebiten.SetWindowTitle(title)
ebiten.SetWindowResizable(true)
ebiten.SetWindowSize(width, height)
return ebiten.RunGame(r)
}
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()
}