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"
|
2020-02-22 23:59:45 -05:00
|
|
|
"image"
|
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-06-27 18:58:41 -04:00
|
|
|
func (s *ebitenSurface) PushBrightness(brightness float64) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.brightness = brightness
|
|
|
|
}
|
|
|
|
|
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-06-27 18:58:41 -04:00
|
|
|
if s.stateCurrent.brightness != 0 {
|
|
|
|
opts.ColorM.ChangeHSV(0, 1, s.stateCurrent.brightness)
|
|
|
|
}
|
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)
|
|
|
|
}
|
2020-02-22 23:59:45 -05:00
|
|
|
|
|
|
|
func (s *ebitenSurface) Screenshot() *image.RGBA {
|
|
|
|
width, height := s.GetSize()
|
|
|
|
bounds := image.Rectangle{image.Point{0, 0}, image.Point{width, height}}
|
|
|
|
image := image.NewRGBA(bounds)
|
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width; x++ {
|
|
|
|
image.Set(x, y, s.image.At(x, y))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|