1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-29 18:45:23 +00:00
OpenDiablo2/d2core/d2ui/sprite.go
lord 7e3aff557b
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

204 lines
5.1 KiB
Go

package d2ui
import (
"fmt"
"image"
"image/color"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
)
// Sprite is a positioned visual object.
type Sprite struct {
x int
y int
animation d2interface.Animation
}
const (
errNoAnimation = "no animation was specified"
)
// NewSprite creates a new Sprite
func (ui *UIManager) NewSprite(animationPath, palettePath string) (*Sprite, error) {
animation, err := ui.asset.LoadAnimation(animationPath, palettePath)
if animation == nil || err != nil {
return nil, fmt.Errorf(errNoAnimation)
}
err = animation.BindRenderer(ui.renderer)
if err != nil {
return nil, err
}
return &Sprite{animation: animation}, nil
}
// Render renders the sprite on the given surface
func (s *Sprite) Render(target d2interface.Surface) error {
_, frameHeight := s.animation.GetCurrentFrameSize()
target.PushTranslation(s.x, s.y-frameHeight)
defer target.Pop()
return s.animation.Render(target)
}
// RenderSection renders the section of the sprite enclosed by bounds
func (s *Sprite) RenderSection(sfc d2interface.Surface, bound image.Rectangle) error {
sfc.PushTranslation(s.x, s.y-bound.Dy())
defer sfc.Pop()
return s.animation.RenderSection(sfc, bound)
}
// RenderSegmented renders a sprite that is internally segmented as frames
func (s *Sprite) RenderSegmented(target d2interface.Surface, segmentsX, segmentsY, frameOffset int) error {
var currentY int
for y := 0; y < segmentsY; y++ {
var currentX, maxFrameHeight int
for x := 0; x < segmentsX; x++ {
idx := x + y*segmentsX + frameOffset*segmentsX*segmentsY
if err := s.animation.SetCurrentFrame(idx); err != nil {
return err
}
target.PushTranslation(s.x+currentX, s.y+currentY)
err := s.animation.Render(target)
target.Pop()
if err != nil {
return err
}
frameWidth, frameHeight := s.GetCurrentFrameSize()
maxFrameHeight = d2math.MaxInt(maxFrameHeight, frameHeight)
currentX += frameWidth
}
currentY += maxFrameHeight
}
return nil
}
// SetPosition places the sprite in 2D
func (s *Sprite) SetPosition(x, y int) {
s.x = x
s.y = y
}
// GetPosition retrieves the 2D position of the sprite
func (s *Sprite) GetPosition() (x, y int) {
return s.x, s.y
}
// GetFrameSize gets the Size(width, height) of a indexed frame.
func (s *Sprite) GetFrameSize(frameIndex int) (x, y int, err error) {
return s.animation.GetFrameSize(frameIndex)
}
// GetCurrentFrameSize gets the Size(width, height) of the current frame.
func (s *Sprite) GetCurrentFrameSize() (width, height int) {
return s.animation.GetCurrentFrameSize()
}
// GetFrameBounds gets maximum Size(width, height) of all frame.
func (s *Sprite) GetFrameBounds() (width, height int) {
return s.animation.GetFrameBounds()
}
// GetCurrentFrame gets index of current frame in animation
func (s *Sprite) GetCurrentFrame() int {
return s.animation.GetCurrentFrame()
}
// GetFrameCount gets number of frames in animation
func (s *Sprite) GetFrameCount() int {
return s.animation.GetFrameCount()
}
// IsOnFirstFrame gets if the animation on its first frame
func (s *Sprite) IsOnFirstFrame() bool {
return s.animation.IsOnFirstFrame()
}
// IsOnLastFrame gets if the animation on its last frame
func (s *Sprite) IsOnLastFrame() bool {
return s.animation.IsOnLastFrame()
}
// GetDirectionCount gets the number of animation direction
func (s *Sprite) GetDirectionCount() int {
return s.animation.GetDirectionCount()
}
// SetDirection places the animation in the direction of an animation
func (s *Sprite) SetDirection(directionIndex int) error {
return s.animation.SetDirection(directionIndex)
}
// GetDirection get the current animation direction
func (s *Sprite) GetDirection() int {
return s.animation.GetDirection()
}
// SetCurrentFrame sets animation at a specific frame
func (s *Sprite) SetCurrentFrame(frameIndex int) error {
return s.animation.SetCurrentFrame(frameIndex)
}
// Rewind sprite to beginning
func (s *Sprite) Rewind() {
_ = s.animation.SetCurrentFrame(0)
}
// PlayForward plays sprite forward
func (s *Sprite) PlayForward() {
s.animation.PlayForward()
}
// PlayBackward play sprites backward
func (s *Sprite) PlayBackward() {
s.animation.PlayBackward()
}
// Pause animation
func (s *Sprite) Pause() {
s.animation.Pause()
}
// SetPlayLoop sets whether to loop the animation
func (s *Sprite) SetPlayLoop(loop bool) {
s.animation.SetPlayLoop(loop)
}
// SetPlayLength sets the play length of the sprite animation
func (s *Sprite) SetPlayLength(playLength float64) {
s.animation.SetPlayLength(playLength)
}
// SetPlayLengthMs sets the play length of the sprite animation in milliseconds
func (s *Sprite) SetPlayLengthMs(playLengthMs int) {
s.animation.SetPlayLengthMs(playLengthMs)
}
// SetColorMod sets the color modifier
func (s *Sprite) SetColorMod(c color.Color) {
s.animation.SetColorMod(c)
}
// Advance advances the animation
func (s *Sprite) Advance(elapsed float64) error {
return s.animation.Advance(elapsed)
}
// SetEffect sets the draw effect type
func (s *Sprite) SetEffect(e d2enum.DrawEffect) {
s.animation.SetEffect(e)
}