2020-01-31 23:18:11 -05:00
|
|
|
package ebiten
|
2019-12-26 11:13:05 -05:00
|
|
|
|
|
|
|
import (
|
2019-12-28 16:46:08 -05:00
|
|
|
"fmt"
|
2019-12-26 11:13:05 -05:00
|
|
|
"image/color"
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2render"
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2019-12-26 11:13:05 -05:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
)
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
type ebitenSurface struct {
|
2019-12-26 11:13:05 -05:00
|
|
|
stateStack []surfaceState
|
|
|
|
stateCurrent surfaceState
|
|
|
|
image *ebiten.Image
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) PushTranslation(x, y int) {
|
2019-12-26 11:13:05 -05:00
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.x += x
|
|
|
|
s.stateCurrent.y += y
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (s *ebitenSurface) PushCompositeMode(mode d2render.CompositeMode) {
|
2019-12-26 11:13:05 -05:00
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
2020-01-31 23:18:11 -05:00
|
|
|
s.stateCurrent.mode = d2ToEbitenCompositeMode(mode)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (s *ebitenSurface) PushFilter(filter d2render.Filter) {
|
2019-12-28 16:46:08 -05:00
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
2020-01-31 23:18:11 -05:00
|
|
|
s.stateCurrent.filter = d2ToEbitenFilter(filter)
|
2019-12-28 16:46:08 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) PushColor(color color.Color) {
|
2019-12-28 16:46:08 -05:00
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.color = color
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) Pop() {
|
2019-12-26 11:13:05 -05:00
|
|
|
count := len(s.stateStack)
|
|
|
|
if count == 0 {
|
|
|
|
panic("empty stack")
|
|
|
|
}
|
|
|
|
|
|
|
|
s.stateCurrent = s.stateStack[count-1]
|
|
|
|
s.stateStack = s.stateStack[:count-1]
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) PopN(n int) {
|
2019-12-28 16:46:08 -05:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
s.Pop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:28 -05:00
|
|
|
func (s *ebitenSurface) Render(sfc d2render.Surface) error {
|
2019-12-26 11:13:05 -05:00
|
|
|
opts := &ebiten.DrawImageOptions{CompositeMode: s.stateCurrent.mode}
|
|
|
|
opts.GeoM.Translate(float64(s.stateCurrent.x), float64(s.stateCurrent.y))
|
2019-12-28 16:46:08 -05:00
|
|
|
opts.Filter = s.stateCurrent.filter
|
|
|
|
if s.stateCurrent.color != nil {
|
2020-01-31 23:18:11 -05:00
|
|
|
opts.ColorM = ColorToColorM(s.stateCurrent.color)
|
2019-12-28 16:46:08 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
var img = sfc.(*ebitenSurface).image
|
|
|
|
return s.image.DrawImage(img, opts)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) DrawText(format string, params ...interface{}) {
|
2019-12-28 16:46:08 -05:00
|
|
|
ebitenutil.DebugPrintAt(s.image, fmt.Sprintf(format, params...), s.stateCurrent.x, s.stateCurrent.y)
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) DrawLine(x, y int, color color.Color) {
|
2019-12-28 16:46:08 -05:00
|
|
|
ebitenutil.DrawLine(
|
|
|
|
s.image,
|
|
|
|
float64(s.stateCurrent.x),
|
|
|
|
float64(s.stateCurrent.y),
|
|
|
|
float64(s.stateCurrent.x+x),
|
|
|
|
float64(s.stateCurrent.y+y),
|
|
|
|
color,
|
|
|
|
)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) DrawRect(width, height int, color color.Color) {
|
2019-12-26 11:13:05 -05:00
|
|
|
ebitenutil.DrawRect(
|
|
|
|
s.image,
|
|
|
|
float64(s.stateCurrent.x),
|
|
|
|
float64(s.stateCurrent.y),
|
|
|
|
float64(width),
|
|
|
|
float64(height),
|
|
|
|
color,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) Clear(color color.Color) error {
|
2019-12-28 16:46:08 -05:00
|
|
|
return s.image.Fill(color)
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) GetSize() (int, int) {
|
2019-12-26 11:13:05 -05:00
|
|
|
return s.image.Size()
|
|
|
|
}
|
2019-12-28 16:46:08 -05:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (s *ebitenSurface) GetDepth() int {
|
2019-12-28 16:46:08 -05:00
|
|
|
return len(s.stateStack)
|
|
|
|
}
|
2020-01-31 23:18:11 -05:00
|
|
|
|
|
|
|
func (s *ebitenSurface) ReplacePixels(pixels []byte) error {
|
|
|
|
return s.image.ReplacePixels(pixels)
|
|
|
|
}
|