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-07-23 12:56:50 -04:00
|
|
|
"github.com/hajimehoshi/ebiten"
|
|
|
|
"github.com/hajimehoshi/ebiten/ebitenutil"
|
|
|
|
|
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-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2019-12-26 11:13:05 -05:00
|
|
|
)
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
const (
|
2020-09-12 16:25:09 -04:00
|
|
|
maxAlpha = 0xff
|
|
|
|
cacheLimit = 512
|
|
|
|
transparency25 = 0.25
|
|
|
|
transparency50 = 0.50
|
|
|
|
transparency75 = 0.75
|
2020-07-17 18:51:44 -04:00
|
|
|
)
|
2020-07-07 08:57:40 -04:00
|
|
|
|
|
|
|
type colorMCacheKey uint32
|
|
|
|
|
|
|
|
type colorMCacheEntry struct {
|
|
|
|
colorMatrix ebiten.ColorM
|
|
|
|
atime int64
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
type ebitenSurface struct {
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 17:31:45 -04:00
|
|
|
renderer *Renderer
|
2020-07-07 08:57:40 -04:00
|
|
|
stateStack []surfaceState
|
|
|
|
stateCurrent surfaceState
|
|
|
|
image *ebiten.Image
|
|
|
|
colorMCache map[colorMCacheKey]*colorMCacheEntry
|
|
|
|
monotonicClock int64
|
|
|
|
}
|
|
|
|
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 17:31:45 -04:00
|
|
|
func createEbitenSurface(r *Renderer, img *ebiten.Image, currentState ...surfaceState) *ebitenSurface {
|
2020-07-26 19:29:37 -04:00
|
|
|
state := surfaceState{
|
|
|
|
effect: d2enum.DrawEffectNone,
|
2020-08-05 22:04:36 -04:00
|
|
|
saturation: defaultSaturation,
|
|
|
|
brightness: defaultBrightness,
|
|
|
|
skewX: defaultSkewX,
|
|
|
|
skewY: defaultSkewY,
|
|
|
|
scaleX: defaultScaleX,
|
|
|
|
scaleY: defaultScaleY,
|
2020-07-26 19:29:37 -04:00
|
|
|
}
|
2020-07-07 08:57:40 -04:00
|
|
|
if len(currentState) > 0 {
|
|
|
|
state = currentState[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ebitenSurface{
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 17:31:45 -04:00
|
|
|
renderer: r,
|
2020-07-07 08:57:40 -04:00
|
|
|
image: img,
|
|
|
|
stateCurrent: state,
|
|
|
|
colorMCache: make(map[colorMCacheKey]*colorMCacheEntry),
|
|
|
|
}
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
Decouple asset manager from renderer (#730)
* improve AssetManager implementation
Notable changes are:
* removed the individual managers inside of d2asset, only one asset manager
* AssetManager now has caches for the types of files it loads
* created a type for TextDictionary (the txt file structs)
* fixed a file path bug in d2loader Source
* fixed a asset stream bug in d2loader Asset
* d2loader.Loader now needs a d2config.Config on creation (for resolving locale files)
* updated the mpq file in d2asset test data, added test case for "sub-directory"
* added a Data method to d2asset.Asset. The data is cached on first full read.
* renamed ArchiveDataStream to DataStream in d2interface
* moved palette utility func out of d2asset and into d2util
* bugfix for MacOS mpq loader issue
* lint fixes, added data caching to filesystem asset
* adding comment for mpq asset close
* Decouple d2asset from d2render
Notable changes in d2common:
* d2dcc.Load now fully decodes the dcc and stores the directions/frames in the dcc struct
* un-exported dcc.decodeDirection, it is only used in d2dcc
* removed font interface from d2interface, we only have one font implementation
* added `Renderer` method to d2interface.Surface, animations use this to bind to a renderer and create surfaces as they need
* added `BindRenderer` method to animation interface
Notable changes in d2common/d2asset:
* **d2asset.NewAssetManager only needs to be passed a d2config.Config**, it is decoupled from d2render
* exported Animation
* Animation implementation binds to the renderer to create surfaces only on the first time it is rendered
* font, dcc, dc6 initialization logic moved out of asset_manager.go
* for dc6 and dcc animations, the process of decoding and creating render surfaces has been broken into different methods
* the d2asset.Font struct now stores font table data for initialization purposes
Notable changes in d2core/d2render:
* Surfaces store a renderer reference, this allows animations to bind to the renderer and create a surface just-in-time
**These last changes should have been a separate PR, sorry.**
Notable changes in d2core/d2ui:
* ui.NewSprite now handles creating an animation internally, only needs image and palette path as arguments
Notable Changes in d2game:
Because of the change in d2ui, all instances of this code pattern...
```golang
animation, err := screen.asset.LoadAnimation(imgPath, palettePath)
sprite, err := screen.ui.NewSprite(animation)
```
... becomes this ...
```golang
sprite, err := screen.ui.NewSprite(imgPath, palettePath)
```
2020-09-14 17:31:45 -04:00
|
|
|
// Renderer returns the renderer
|
|
|
|
func (s *ebitenSurface) Renderer() d2interface.Renderer {
|
|
|
|
return s.renderer
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// PushTranslation pushes an x,y translation to the state stack
|
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-07-26 19:29:37 -04:00
|
|
|
// PushSkew pushes a skew to the state stack
|
|
|
|
func (s *ebitenSurface) PushSkew(skewX, skewY float64) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.skewX = skewX
|
|
|
|
s.stateCurrent.skewY = skewY
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushScale pushes a scale to the state stack
|
|
|
|
func (s *ebitenSurface) PushScale(scaleX, scaleY float64) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.scaleX = scaleX
|
|
|
|
s.stateCurrent.scaleY = scaleY
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// PushEffect pushes an effect to the state stack
|
2020-07-08 21:57:35 -04:00
|
|
|
func (s *ebitenSurface) PushEffect(effect d2enum.DrawEffect) {
|
2019-12-26 11:13:05 -05:00
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
2020-07-08 21:57:35 -04:00
|
|
|
s.stateCurrent.effect = effect
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// PushFilter pushes a filter to the state stack
|
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-07-17 18:51:44 -04:00
|
|
|
// PushColor pushes a color to the stat stack
|
|
|
|
func (s *ebitenSurface) PushColor(c color.Color) {
|
2019-12-28 16:46:08 -05:00
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
2020-07-17 18:51:44 -04:00
|
|
|
s.stateCurrent.color = c
|
2019-12-28 16:46:08 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// PushBrightness pushes a brightness value to the state stack
|
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-07-26 19:29:37 -04:00
|
|
|
// PushSaturation pushes a saturation value to the state stack
|
|
|
|
func (s *ebitenSurface) PushSaturation(saturation float64) {
|
|
|
|
s.stateStack = append(s.stateStack, s.stateCurrent)
|
|
|
|
s.stateCurrent.saturation = saturation
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// Pop pops a state off of the state stack
|
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-07-17 18:51:44 -04:00
|
|
|
// PopN pops n states off the the state stack
|
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-07-17 18:51:44 -04:00
|
|
|
// Render renders the given surface
|
2020-06-29 00:41:58 -04:00
|
|
|
func (s *ebitenSurface) Render(sfc d2interface.Surface) error {
|
2020-09-12 16:25:09 -04:00
|
|
|
opts := s.createDrawImageOptions()
|
2020-07-26 19:29:37 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
if s.stateCurrent.brightness != 1 || s.stateCurrent.saturation != 1 {
|
|
|
|
opts.ColorM.ChangeHSV(0, s.stateCurrent.saturation, s.stateCurrent.brightness)
|
2020-07-26 19:29:37 -04:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
s.handleStateEffect(opts)
|
2020-07-26 19:29:37 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
return s.image.DrawImage(sfc.(*ebitenSurface).image, opts)
|
|
|
|
}
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
// Renders the section of the surface, given the bounds
|
|
|
|
func (s *ebitenSurface) RenderSection(sfc d2interface.Surface, bound image.Rectangle) error {
|
|
|
|
opts := s.createDrawImageOptions()
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
if s.stateCurrent.brightness != 0 {
|
2020-07-26 19:29:37 -04:00
|
|
|
opts.ColorM.ChangeHSV(0, s.stateCurrent.saturation, s.stateCurrent.brightness)
|
2020-06-27 18:58:41 -04:00
|
|
|
}
|
2019-12-28 16:46:08 -05:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
s.handleStateEffect(opts)
|
2020-07-07 08:57:40 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
return s.image.DrawImage(sfc.(*ebitenSurface).image.SubImage(bound).(*ebiten.Image), opts)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
func (s *ebitenSurface) createDrawImageOptions() *ebiten.DrawImageOptions {
|
2020-07-08 21:57:35 -04:00
|
|
|
opts := &ebiten.DrawImageOptions{}
|
2020-07-26 19:29:37 -04:00
|
|
|
|
|
|
|
if s.stateCurrent.skewX != 0 || s.stateCurrent.skewY != 0 {
|
|
|
|
opts.GeoM.Skew(s.stateCurrent.skewX, s.stateCurrent.skewY)
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.stateCurrent.scaleX != 1.0 || s.stateCurrent.scaleY != 1.0 {
|
|
|
|
opts.GeoM.Scale(s.stateCurrent.scaleX, s.stateCurrent.scaleY)
|
|
|
|
}
|
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
opts.GeoM.Translate(float64(s.stateCurrent.x), float64(s.stateCurrent.y))
|
2020-07-26 19:29:37 -04:00
|
|
|
|
2020-06-30 12:43:13 -04:00
|
|
|
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-09-12 16:25:09 -04:00
|
|
|
return opts
|
|
|
|
}
|
2020-06-30 12:43:13 -04:00
|
|
|
|
2020-09-12 16:25:09 -04:00
|
|
|
func (s *ebitenSurface) handleStateEffect(opts *ebiten.DrawImageOptions) {
|
2020-07-08 21:57:35 -04:00
|
|
|
switch s.stateCurrent.effect {
|
|
|
|
case d2enum.DrawEffectPctTransparency25:
|
2020-09-12 16:25:09 -04:00
|
|
|
opts.ColorM.Translate(0, 0, 0, -transparency25)
|
2020-07-08 21:57:35 -04:00
|
|
|
case d2enum.DrawEffectPctTransparency50:
|
2020-09-12 16:25:09 -04:00
|
|
|
opts.ColorM.Translate(0, 0, 0, -transparency50)
|
2020-07-08 21:57:35 -04:00
|
|
|
case d2enum.DrawEffectPctTransparency75:
|
2020-09-12 16:25:09 -04:00
|
|
|
opts.ColorM.Translate(0, 0, 0, -transparency75)
|
2020-07-08 21:57:35 -04:00
|
|
|
case d2enum.DrawEffectModulate:
|
|
|
|
opts.CompositeMode = ebiten.CompositeModeLighter
|
2020-10-25 18:36:12 -04:00
|
|
|
// https://github.com/OpenDiablo2/OpenDiablo2/issues/822
|
2020-07-08 21:57:35 -04:00
|
|
|
case d2enum.DrawEffectBurn:
|
|
|
|
case d2enum.DrawEffectNormal:
|
|
|
|
case d2enum.DrawEffectMod2XTrans:
|
|
|
|
case d2enum.DrawEffectMod2X:
|
|
|
|
case d2enum.DrawEffectNone:
|
|
|
|
opts.CompositeMode = ebiten.CompositeModeSourceOver
|
|
|
|
}
|
2020-06-30 12:43:13 -04:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// DrawTextf renders the string to the surface with the given format string and a set of parameters
|
|
|
|
func (s *ebitenSurface) DrawTextf(format string, params ...interface{}) {
|
2020-08-04 22:23:15 -04:00
|
|
|
str := fmt.Sprintf(format, params...)
|
2020-09-08 15:58:35 -04:00
|
|
|
d2util.DebugPrinter.PrintAt(s.image, str, s.stateCurrent.x, s.stateCurrent.y)
|
2019-12-28 16:46:08 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// DrawLine draws a line
|
|
|
|
func (s *ebitenSurface) DrawLine(x, y int, fillColor 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),
|
2020-07-17 18:51:44 -04:00
|
|
|
fillColor,
|
2019-12-28 16:46:08 -05:00
|
|
|
)
|
2019-12-26 11:13:05 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// DrawRect draws a rectangle
|
|
|
|
func (s *ebitenSurface) DrawRect(width, height int, fillColor 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),
|
2020-07-17 18:51:44 -04:00
|
|
|
fillColor,
|
2019-12-26 11:13:05 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// Clear clears the entire surface, filling with the given color
|
|
|
|
func (s *ebitenSurface) Clear(fillColor color.Color) error {
|
|
|
|
return s.image.Fill(fillColor)
|
2019-12-28 16:46:08 -05:00
|
|
|
}
|
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// GetSize gets the size of the surface
|
|
|
|
func (s *ebitenSurface) GetSize() (x, y int) {
|
2019-12-26 11:13:05 -05:00
|
|
|
return s.image.Size()
|
|
|
|
}
|
2019-12-28 16:46:08 -05:00
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// GetDepth returns the depth of this surface in the stack
|
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
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// ReplacePixels replaces pixels in the surface with the given pixels
|
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
|
|
|
|
2020-07-17 18:51:44 -04:00
|
|
|
// Screenshot returns an *image.RGBA of the surface
|
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)
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
return emptyColorM
|
|
|
|
}
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
key := colorMCacheKey(cr | (cg << 8) | (cb << 16) | (ca << 24))
|
|
|
|
e, ok := s.colorMCache[key]
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
if ok {
|
|
|
|
e.atime = s.now()
|
|
|
|
return e.colorMatrix
|
|
|
|
}
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
if len(s.colorMCache) > cacheLimit {
|
|
|
|
oldest := int64(math.MaxInt64)
|
|
|
|
oldestKey := colorMCacheKey(0)
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
for key, c := range s.colorMCache {
|
|
|
|
if c.atime < oldest {
|
|
|
|
oldestKey = key
|
|
|
|
oldest = c.atime
|
|
|
|
}
|
|
|
|
}
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
delete(s.colorMCache, oldestKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
cm := ebiten.ColorM{}
|
|
|
|
rf := float64(cr) / float64(ca)
|
|
|
|
gf := float64(cg) / float64(ca)
|
|
|
|
bf := float64(cb) / float64(ca)
|
2020-07-17 18:51:44 -04:00
|
|
|
af := float64(ca) / maxAlpha
|
2020-07-07 08:57:40 -04:00
|
|
|
cm.Scale(rf, gf, bf, af)
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
e = &colorMCacheEntry{
|
|
|
|
colorMatrix: cm,
|
|
|
|
atime: s.now(),
|
|
|
|
}
|
2020-07-17 18:51:44 -04:00
|
|
|
|
2020-07-07 08:57:40 -04:00
|
|
|
s.colorMCache[key] = e
|
|
|
|
|
|
|
|
return e.colorMatrix
|
2020-02-22 23:59:45 -05:00
|
|
|
}
|