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-07-07 08:57:40 -04:00
|
|
|
"math"
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-06-30 09:58:53 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
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-07-07 08:57:40 -04:00
|
|
|
const cacheLimit = 512
|
|
|
|
|
|
|
|
type colorMCacheKey uint32
|
|
|
|
|
|
|
|
type colorMCacheEntry struct {
|
|
|
|
colorMatrix ebiten.ColorM
|
|
|
|
atime int64
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
type ebitenSurface struct {
|
2020-07-07 08:57:40 -04:00
|
|
|
stateStack []surfaceState
|
|
|
|
stateCurrent surfaceState
|
|
|
|
image *ebiten.Image
|
|
|
|
colorMCache map[colorMCacheKey]*colorMCacheEntry
|
|
|
|
monotonicClock int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func createEbitenSurface(img *ebiten.Image, currentState ...surfaceState) *ebitenSurface {
|
|
|
|
state := surfaceState{}
|
|
|
|
if len(currentState) > 0 {
|
|
|
|
state = currentState[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ebitenSurface{
|
|
|
|
image: img,
|
|
|
|
stateCurrent: state,
|
|
|
|
colorMCache: make(map[colorMCacheKey]*colorMCacheEntry),
|
|
|
|
}
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
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-06-30 09:58:53 -04:00
|
|
|
func (s *ebitenSurface) PushCompositeMode(mode d2enum.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-07-06 21:26:08 -04:00
|
|
|
func (s *ebitenSurface) PushFilter(filter d2enum.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-06-29 00:41:58 -04:00
|
|
|
func (s *ebitenSurface) Render(sfc d2interface.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
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2019-12-28 16:46:08 -05:00
|
|
|
if s.stateCurrent.color != nil {
|
2020-07-07 08:57:40 -04:00
|
|
|
opts.ColorM = s.colorToColorM(s.stateCurrent.color)
|
2019-12-28 16:46:08 -05:00
|
|
|
}
|
2020-07-07 08:57:40 -04: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
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
return s.image.DrawImage(img, opts)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
// Renders the section of the animation frame enclosed by bounds
|
|
|
|
func (s *ebitenSurface) RenderSection(sfc d2interface.Surface, bound image.Rectangle) error {
|
|
|
|
opts := &ebiten.DrawImageOptions{CompositeMode: s.stateCurrent.mode}
|
|
|
|
opts.GeoM.Translate(float64(s.stateCurrent.x), float64(s.stateCurrent.y))
|
|
|
|
opts.Filter = s.stateCurrent.filter
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
if s.stateCurrent.color != nil {
|
2020-07-07 08:57:40 -04:00
|
|
|
opts.ColorM = s.colorToColorM(s.stateCurrent.color)
|
2020-06-30 12:43:13 -04:00
|
|
|
}
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
if s.stateCurrent.brightness != 0 {
|
|
|
|
opts.ColorM.ChangeHSV(0, 1, s.stateCurrent.brightness)
|
|
|
|
}
|
|
|
|
|
|
|
|
var img = sfc.(*ebitenSurface).image
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
return s.image.DrawImage(img.SubImage(bound).(*ebiten.Image), opts)
|
|
|
|
}
|
|
|
|
|
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()
|
2020-07-07 08:57:40 -04:00
|
|
|
bounds := image.Rectangle{Min: image.Point{X: 0, Y: 0}, Max: image.Point{X: width, Y: height}}
|
|
|
|
rgba := image.NewRGBA(bounds)
|
2020-02-22 23:59:45 -05:00
|
|
|
|
|
|
|
for y := 0; y < height; y++ {
|
|
|
|
for x := 0; x < width; x++ {
|
2020-07-07 08:57:40 -04:00
|
|
|
rgba.Set(x, y, s.image.At(x, y))
|
2020-02-22 23:59:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
return rgba
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ebitenSurface) now() int64 {
|
|
|
|
s.monotonicClock++
|
|
|
|
return s.monotonicClock
|
|
|
|
}
|
|
|
|
|
|
|
|
// colorToColorM converts a normal color to a color matrix
|
|
|
|
func (s *ebitenSurface) colorToColorM(clr color.Color) ebiten.ColorM {
|
|
|
|
// RGBA() is in [0 - 0xffff]. Adjust them in [0 - 0xff].
|
|
|
|
cr, cg, cb, ca := clr.RGBA()
|
|
|
|
cr >>= 8
|
|
|
|
cg >>= 8
|
|
|
|
cb >>= 8
|
|
|
|
ca >>= 8
|
|
|
|
|
|
|
|
if ca == 0 {
|
|
|
|
emptyColorM := ebiten.ColorM{}
|
|
|
|
emptyColorM.Scale(0, 0, 0, 0)
|
|
|
|
return emptyColorM
|
|
|
|
}
|
|
|
|
key := colorMCacheKey(cr | (cg << 8) | (cb << 16) | (ca << 24))
|
|
|
|
e, ok := s.colorMCache[key]
|
|
|
|
if ok {
|
|
|
|
e.atime = s.now()
|
|
|
|
return e.colorMatrix
|
|
|
|
}
|
|
|
|
if len(s.colorMCache) > cacheLimit {
|
|
|
|
oldest := int64(math.MaxInt64)
|
|
|
|
oldestKey := colorMCacheKey(0)
|
|
|
|
for key, c := range s.colorMCache {
|
|
|
|
if c.atime < oldest {
|
|
|
|
oldestKey = key
|
|
|
|
oldest = c.atime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(s.colorMCache, oldestKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
cm := ebiten.ColorM{}
|
|
|
|
rf := float64(cr) / float64(ca)
|
|
|
|
gf := float64(cg) / float64(ca)
|
|
|
|
bf := float64(cb) / float64(ca)
|
|
|
|
af := float64(ca) / 0xff
|
|
|
|
cm.Scale(rf, gf, bf, af)
|
|
|
|
e = &colorMCacheEntry{
|
|
|
|
colorMatrix: cm,
|
|
|
|
atime: s.now(),
|
|
|
|
}
|
|
|
|
s.colorMCache[key] = e
|
|
|
|
|
|
|
|
return e.colorMatrix
|
2020-02-22 23:59:45 -05:00
|
|
|
}
|